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

9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Shadowsocks.Controller;
  8. namespace Shadowsocks.Model
  9. {
  10. [Serializable]
  11. public class StatisticsStrategyConfiguration
  12. {
  13. public static readonly string ID = "com.shadowsocks.strategy.statistics";
  14. public bool StatisticsEnabled { get; set; } = false;
  15. public bool ByHourOfDay { get; set; } = true;
  16. public bool Ping { get; set; }
  17. public int ChoiceKeptMinutes { get; set; } = 10;
  18. public int DataCollectionMinutes { get; set; } = 10;
  19. public int RepeatTimesNum { get; set; } = 4;
  20. private const string ConfigFile = "statistics-config.json";
  21. public static StatisticsStrategyConfiguration Load()
  22. {
  23. try
  24. {
  25. var content = File.ReadAllText(ConfigFile);
  26. var configuration = JsonConvert.DeserializeObject<StatisticsStrategyConfiguration>(content);
  27. return configuration;
  28. }
  29. catch (FileNotFoundException)
  30. {
  31. var configuration = new StatisticsStrategyConfiguration();
  32. Save(configuration);
  33. return configuration;
  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 properties = typeof(StatisticsRecord).GetFields(BindingFlags.Instance | BindingFlags.Public);
  57. Calculations = properties.ToDictionary(p => p.Name, _ => (float)0);
  58. }
  59. }
  60. }