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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = true;
  17. private bool _byIsp = false;
  18. private bool _byHourOfDay = false;
  19. private int _choiceKeptMinutes = 10;
  20. private int _dataCollectionMinutes = 10;
  21. private int _repeatTimesNum = 4;
  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. var configuration = new StatisticsStrategyConfiguration();
  34. Save(configuration);
  35. return configuration;
  36. }
  37. catch (Exception e)
  38. {
  39. Logging.LogUsefulException(e);
  40. return new StatisticsStrategyConfiguration();
  41. }
  42. }
  43. public static void Save(StatisticsStrategyConfiguration configuration)
  44. {
  45. try
  46. {
  47. var content = JsonConvert.SerializeObject(configuration, Formatting.Indented);
  48. File.WriteAllText(ConfigFile, content);
  49. }
  50. catch (Exception e)
  51. {
  52. Logging.LogUsefulException(e);
  53. }
  54. }
  55. public Dictionary<string, float> Calculations;
  56. public StatisticsStrategyConfiguration()
  57. {
  58. var availabilityStatisticsType = typeof (AvailabilityStatistics);
  59. var statisticsData = availabilityStatisticsType.GetNestedType("StatisticsData");
  60. var properties = statisticsData.GetFields(BindingFlags.Instance | BindingFlags.Public);
  61. Calculations = properties.ToDictionary(p => p.Name, _ => (float) 0);
  62. }
  63. public bool StatisticsEnabled
  64. {
  65. get { return _statisticsEnabled; }
  66. set { _statisticsEnabled = value; }
  67. }
  68. public bool ByIsp
  69. {
  70. get { return _byIsp; }
  71. set { _byIsp = value; }
  72. }
  73. public bool ByHourOfDay
  74. {
  75. get { return _byHourOfDay; }
  76. set { _byHourOfDay = value; }
  77. }
  78. public int ChoiceKeptMinutes
  79. {
  80. get { return _choiceKeptMinutes; }
  81. set { _choiceKeptMinutes = value; }
  82. }
  83. public int DataCollectionMinutes
  84. {
  85. get { return _dataCollectionMinutes; }
  86. set { _dataCollectionMinutes = value; }
  87. }
  88. public int RepeatTimesNum
  89. {
  90. get { return _repeatTimesNum; }
  91. set { _repeatTimesNum = value; }
  92. }
  93. }
  94. }