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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. var inbound = inboundSpeedRecords?.Where(s => s > 0).ToList();
  44. if (inbound != null && inbound.Any())
  45. {
  46. AverageInboundSpeed = (int) inbound.Average();
  47. MinInboundSpeed = inbound.Min();
  48. MaxInboundSpeed = inbound.Max();
  49. }
  50. var outbound = outboundSpeedRecords?.Where(s => s > 0).ToList();
  51. if (outbound!= null && outbound.Any())
  52. {
  53. AverageOutboundSpeed = (int) outbound.Average();
  54. MinOutboundSpeed = outbound.Min();
  55. MaxOutboundSpeed = outbound.Max();
  56. }
  57. var latency = latencyRecords?.Where(s => s > 0).ToList();
  58. if (latency!= null && latency.Any())
  59. {
  60. AverageLatency = (int) latency.Average();
  61. MinLatency = latency.Min();
  62. MaxLatency = latency.Max();
  63. }
  64. }
  65. public StatisticsRecord(string identifier, ICollection<int?> responseRecords)
  66. {
  67. ServerIdentifier = identifier;
  68. SetResponse(responseRecords);
  69. }
  70. public void SetResponse(ICollection<int?> responseRecords)
  71. {
  72. if (responseRecords == null) return;
  73. var records = responseRecords.Where(response => response != null).Select(response => response.Value).ToList();
  74. if (!records.Any()) return;
  75. AverageResponse = (int?) records.Average();
  76. MinResponse = records.Min();
  77. MaxResponse = records.Max();
  78. PackageLoss = responseRecords.Count(response => response != null)/(float) responseRecords.Count;
  79. }
  80. }
  81. }