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.2 kB

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