You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatisticsStrategyConfiguration.cs 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Shadowsocks.Controller;
  7. using Shadowsocks.Controller.Strategy;
  8. using SimpleJson;
  9. using Newtonsoft.Json;
  10. namespace Shadowsocks.Model
  11. {
  12. [Serializable]
  13. public class StatisticsStrategyConfiguration
  14. {
  15. public static readonly string ID = "com.shadowsocks.strategy.statistics";
  16. private bool _statisticsEnabled;
  17. private bool _byIsp;
  18. private bool _byHourOfDay;
  19. private int _choiceKeptMinutes;
  20. private int _dataCollectionMinutes;
  21. private int _repeatTimesNum;
  22. private const string ConfigFile = "statistics-config.json";
  23. public static StatisticsStrategyConfiguration Load()
  24. {
  25. try
  26. {
  27. var content = File.ReadAllText(ConfigFile);
  28. var configuration = JsonConvert.DeserializeObject<StatisticsStrategyConfiguration>(content);
  29. return configuration;
  30. }
  31. catch (FileNotFoundException e)
  32. {
  33. return new StatisticsStrategyConfiguration();
  34. }
  35. catch (Exception e)
  36. {
  37. Logging.LogUsefulException(e);
  38. return new StatisticsStrategyConfiguration();
  39. }
  40. }
  41. public static void Save(StatisticsStrategyConfiguration configuration)
  42. {
  43. try
  44. {
  45. var content = JsonConvert.SerializeObject(configuration, Formatting.Indented);
  46. File.WriteAllText(ConfigFile, content);
  47. }
  48. catch (Exception e)
  49. {
  50. Logging.LogUsefulException(e);
  51. }
  52. }
  53. public Dictionary<string, float> Calculations;
  54. public StatisticsStrategyConfiguration()
  55. {
  56. var statisticsStrategy = typeof (StatisticsStrategy);
  57. var statisticsData = statisticsStrategy.GetNestedType("StatisticsData");
  58. var properties = statisticsData.GetFields(BindingFlags.Instance | BindingFlags.Public);
  59. Calculations = properties.ToDictionary(p => p.Name, _ => (float) 0);
  60. }
  61. public bool StatisticsEnabled
  62. {
  63. get { return _statisticsEnabled; }
  64. set { _statisticsEnabled = value; }
  65. }
  66. public bool ByIsp
  67. {
  68. get { return _byIsp; }
  69. set { _byIsp = value; }
  70. }
  71. public bool ByHourOfDay
  72. {
  73. get { return _byHourOfDay; }
  74. set { _byHourOfDay = value; }
  75. }
  76. public int ChoiceKeptMinutes
  77. {
  78. get { return _choiceKeptMinutes; }
  79. set { _choiceKeptMinutes = value; }
  80. }
  81. public int DataCollectionMinutes
  82. {
  83. get { return _dataCollectionMinutes; }
  84. set { _dataCollectionMinutes = value; }
  85. }
  86. public int RepeatTimesNum
  87. {
  88. get { return _repeatTimesNum; }
  89. set { _repeatTimesNum = value; }
  90. }
  91. }
  92. }