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.

StatisticsRecord.cs 3.6 kB

9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Shadowsocks.Model
  6. {
  7. // Simple processed records for a short period of time
  8. public class StatisticsRecord
  9. {
  10. public DateTime Timestamp { get; set; } = DateTime.Now;
  11. public string ServerIdentifier { get; set; }
  12. // in ping-only records, these fields would be null
  13. public int? AverageLatency;
  14. public int? MinLatency;
  15. public int? MaxLatency;
  16. private bool EmptyLatencyData => (AverageLatency == null) && (MinLatency == null) && (MaxLatency == null);
  17. public int? AverageInboundSpeed;
  18. public int? MinInboundSpeed;
  19. public int? MaxInboundSpeed;
  20. private bool EmptyInboundSpeedData
  21. => (AverageInboundSpeed == null) && (MinInboundSpeed == null) && (MaxInboundSpeed == null);
  22. public int? AverageOutboundSpeed;
  23. public int? MinOutboundSpeed;
  24. public int? MaxOutboundSpeed;
  25. private bool EmptyOutboundSpeedData
  26. => (AverageOutboundSpeed == null) && (MinOutboundSpeed == null) && (MaxOutboundSpeed == null);
  27. // if user disabled ping test, response would be null
  28. public int? AverageResponse;
  29. public int? MinResponse;
  30. public int? MaxResponse;
  31. public float? PackageLoss;
  32. private bool EmptyResponseData
  33. => (AverageResponse == null) && (MinResponse == null) && (MaxResponse == null) && (PackageLoss == null);
  34. public bool IsEmptyData() {
  35. return EmptyInboundSpeedData && EmptyOutboundSpeedData && EmptyResponseData && EmptyLatencyData;
  36. }
  37. public StatisticsRecord()
  38. {
  39. }
  40. public StatisticsRecord(string identifier, ICollection<int> inboundSpeedRecords, ICollection<int> outboundSpeedRecords, ICollection<int> latencyRecords)
  41. {
  42. ServerIdentifier = identifier;
  43. if (inboundSpeedRecords != null && inboundSpeedRecords.Any())
  44. {
  45. AverageInboundSpeed = (int) inboundSpeedRecords.Average();
  46. MinInboundSpeed = inboundSpeedRecords.Min();
  47. MaxInboundSpeed = inboundSpeedRecords.Max();
  48. }
  49. if (outboundSpeedRecords != null && outboundSpeedRecords.Any())
  50. {
  51. AverageOutboundSpeed = (int) outboundSpeedRecords.Average();
  52. MinOutboundSpeed = outboundSpeedRecords.Min();
  53. MaxOutboundSpeed = outboundSpeedRecords.Max();
  54. }
  55. if (latencyRecords != null && latencyRecords.Any())
  56. {
  57. AverageLatency = (int) latencyRecords.Average();
  58. MinLatency = latencyRecords.Min();
  59. MaxLatency = latencyRecords.Max();
  60. }
  61. }
  62. public StatisticsRecord(string identifier, ICollection<int?> responseRecords)
  63. {
  64. ServerIdentifier = identifier;
  65. SetResponse(responseRecords);
  66. }
  67. public void SetResponse(ICollection<int?> responseRecords)
  68. {
  69. if (responseRecords == null) return;
  70. var records = responseRecords.Where(response => response != null).Select(response => response.Value).ToList();
  71. if (!records.Any()) return;
  72. AverageResponse = (int?) records.Average();
  73. MinResponse = records.Min();
  74. MaxResponse = records.Max();
  75. PackageLoss = responseRecords.Count(response => response != null)/(float) responseRecords.Count;
  76. }
  77. }
  78. }