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

9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. namespace Shadowsocks.Model
  9. {
  10. [Serializable]
  11. public class StatisticsStrategyConfiguration
  12. {
  13. private static Logger logger = LogManager.GetCurrentClassLogger();
  14. public static readonly string ID = "com.shadowsocks.strategy.statistics";
  15. public bool StatisticsEnabled { get; set; } = false;
  16. public bool ByHourOfDay { get; set; } = true;
  17. public bool Ping { get; set; }
  18. public int ChoiceKeptMinutes { get; set; } = 10;
  19. public int DataCollectionMinutes { get; set; } = 10;
  20. public int RepeatTimesNum { get; set; } = 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 = JsonConvert.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. logger.LogUsefulException(e);
  39. return new StatisticsStrategyConfiguration();
  40. }
  41. }
  42. public static void Save(StatisticsStrategyConfiguration configuration)
  43. {
  44. try
  45. {
  46. var content = JsonConvert.SerializeObject(configuration, Formatting.Indented);
  47. File.WriteAllText(ConfigFile, content);
  48. }
  49. catch (Exception e)
  50. {
  51. logger.LogUsefulException(e);
  52. }
  53. }
  54. public Dictionary<string, float> Calculations;
  55. public StatisticsStrategyConfiguration()
  56. {
  57. var properties = typeof(StatisticsRecord).GetFields(BindingFlags.Instance | BindingFlags.Public);
  58. Calculations = properties.ToDictionary(p => p.Name, _ => (float)0);
  59. }
  60. }
  61. }