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 2.3 kB

10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Newtonsoft.Json;
  7. using NLog;
  8. using Shadowsocks.Controller;
  9. namespace Shadowsocks.Model
  10. {
  11. [Serializable]
  12. public class StatisticsStrategyConfiguration
  13. {
  14. private static Logger logger = LogManager.GetCurrentClassLogger();
  15. public static readonly string ID = "com.shadowsocks.strategy.statistics";
  16. public bool StatisticsEnabled { get; set; } = false;
  17. public bool ByHourOfDay { get; set; } = true;
  18. public bool Ping { get; set; }
  19. public int ChoiceKeptMinutes { get; set; } = 10;
  20. public int DataCollectionMinutes { get; set; } = 10;
  21. public int RepeatTimesNum { get; set; } = 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)
  32. {
  33. var configuration = new StatisticsStrategyConfiguration();
  34. Save(configuration);
  35. return configuration;
  36. }
  37. catch (Exception e)
  38. {
  39. logger.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. logger.LogUsefulException(e);
  53. }
  54. }
  55. public Dictionary<string, float> Calculations;
  56. public StatisticsStrategyConfiguration()
  57. {
  58. var properties = typeof(StatisticsRecord).GetFields(BindingFlags.Instance | BindingFlags.Public);
  59. Calculations = properties.ToDictionary(p => p.Name, _ => (float)0);
  60. }
  61. }
  62. }