Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

145 lignes
5.5 KiB

  1. // Copyright (c) .NET Core Community. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using DotNetCore.CAP.Dashboard.Resources;
  7. using DotNetCore.CAP.Internal;
  8. using Microsoft.Extensions.DependencyInjection;
  9. namespace DotNetCore.CAP.Dashboard
  10. {
  11. public static class DashboardMetrics
  12. {
  13. private static readonly Dictionary<string, DashboardMetric> Metrics = new Dictionary<string, DashboardMetric>();
  14. public static readonly DashboardMetric ServerCount = new DashboardMetric(
  15. "servers:count",
  16. "Metrics_Servers",
  17. page => new Metric(page.Statistics.Servers.ToString("N0"))
  18. {
  19. Style = page.Statistics.Servers == 0 ? MetricStyle.Warning : MetricStyle.Default,
  20. Highlighted = page.Statistics.Servers == 0,
  21. Title = page.Statistics.Servers == 0
  22. ? "No active servers found. Jobs will not be processed."
  23. : null
  24. });
  25. public static readonly DashboardMetric SubscriberCount = new DashboardMetric(
  26. "retries:count",
  27. "Metrics_Retries",
  28. page =>
  29. {
  30. long retryCount;
  31. var methodCache = page.RequestServices.GetService<MethodMatcherCache>();
  32. retryCount = methodCache.GetCandidatesMethodsOfGroupNameGrouped().Sum(x => x.Value.Count);
  33. return new Metric(retryCount.ToString("N0"))
  34. {
  35. Style = retryCount > 0 ? MetricStyle.Default : MetricStyle.Warning
  36. };
  37. });
  38. //----------------------------------------------------
  39. public static readonly DashboardMetric PublishedFailedCountOrNull = new DashboardMetric(
  40. "published_failed:count-or-null",
  41. "Metrics_FailedJobs",
  42. page => page.Statistics.PublishedFailed > 0
  43. ? new Metric(page.Statistics.PublishedFailed.ToString("N0"))
  44. {
  45. Style = MetricStyle.Danger,
  46. Highlighted = true,
  47. Title = string.Format(Strings.Metrics_FailedCountOrNull, page.Statistics.PublishedFailed)
  48. }
  49. : null);
  50. public static readonly DashboardMetric ReceivedFailedCountOrNull = new DashboardMetric(
  51. "received_failed:count-or-null",
  52. "Metrics_FailedJobs",
  53. page => page.Statistics.ReceivedFailed > 0
  54. ? new Metric(page.Statistics.ReceivedFailed.ToString("N0"))
  55. {
  56. Style = MetricStyle.Danger,
  57. Highlighted = true,
  58. Title = string.Format(Strings.Metrics_FailedCountOrNull, page.Statistics.ReceivedFailed)
  59. }
  60. : null);
  61. //----------------------------------------------------
  62. public static readonly DashboardMetric PublishedSucceededCount = new DashboardMetric(
  63. "published_succeeded:count",
  64. "Metrics_SucceededJobs",
  65. page => new Metric(page.Statistics.PublishedSucceeded.ToString("N0"))
  66. {
  67. IntValue = page.Statistics.PublishedSucceeded
  68. });
  69. public static readonly DashboardMetric ReceivedSucceededCount = new DashboardMetric(
  70. "received_succeeded:count",
  71. "Metrics_SucceededJobs",
  72. page => new Metric(page.Statistics.ReceivedSucceeded.ToString("N0"))
  73. {
  74. IntValue = page.Statistics.ReceivedSucceeded
  75. });
  76. //----------------------------------------------------
  77. public static readonly DashboardMetric PublishedFailedCount = new DashboardMetric(
  78. "published_failed:count",
  79. "Metrics_FailedJobs",
  80. page => new Metric(page.Statistics.PublishedFailed.ToString("N0"))
  81. {
  82. IntValue = page.Statistics.PublishedFailed,
  83. Style = page.Statistics.PublishedFailed > 0 ? MetricStyle.Danger : MetricStyle.Default,
  84. Highlighted = page.Statistics.PublishedFailed > 0
  85. });
  86. public static readonly DashboardMetric ReceivedFailedCount = new DashboardMetric(
  87. "received_failed:count",
  88. "Metrics_FailedJobs",
  89. page => new Metric(page.Statistics.ReceivedFailed.ToString("N0"))
  90. {
  91. IntValue = page.Statistics.ReceivedFailed,
  92. Style = page.Statistics.ReceivedFailed > 0 ? MetricStyle.Danger : MetricStyle.Default,
  93. Highlighted = page.Statistics.ReceivedFailed > 0
  94. });
  95. static DashboardMetrics()
  96. {
  97. AddMetric(ServerCount);
  98. AddMetric(SubscriberCount);
  99. AddMetric(PublishedFailedCountOrNull);
  100. AddMetric(ReceivedFailedCountOrNull);
  101. AddMetric(PublishedSucceededCount);
  102. AddMetric(ReceivedSucceededCount);
  103. AddMetric(PublishedFailedCount);
  104. AddMetric(ReceivedFailedCount);
  105. }
  106. public static void AddMetric(DashboardMetric metric)
  107. {
  108. if (metric == null)
  109. {
  110. throw new ArgumentNullException(nameof(metric));
  111. }
  112. lock (Metrics)
  113. {
  114. Metrics[metric.Name] = metric;
  115. }
  116. }
  117. public static IEnumerable<DashboardMetric> GetMetrics()
  118. {
  119. lock (Metrics)
  120. {
  121. return Metrics.Values.ToList();
  122. }
  123. }
  124. }
  125. }