Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Delete connected concentrators metric #1535

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
<MudItem xs="12" sm="6" md="3">
<DashboardMetricCounter Title="Concentrators" Counter="portalMetric.ConcentratorCount" Icon="@Icons.Material.Filled.WifiTethering" Color="Color.Info" />
</MudItem>
<MudItem xs="12" sm="6" md="3">
<DashboardMetricCounter Title="Connected Concentrators" Counter="portalMetric.ConnectedConcentratorCount" Icon="@Icons.Material.Filled.WifiTethering" Color="Color.Success" />
</MudItem>
</MudGrid>

@code {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Services
namespace AzureIoTHub.Portal.Server.Jobs
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Shared.Constants;
Expand All @@ -11,15 +11,14 @@ namespace AzureIoTHub.Portal.Server.Services
using Shared.Models.v1._0;

[DisallowConcurrentExecution]
public class ConcentratorMetricExporterService : IJob
public class ConcentratorMetricExporterJob : IJob
{
private readonly ILogger<ConcentratorMetricExporterService> logger;
private readonly ILogger<ConcentratorMetricExporterJob> logger;
private readonly PortalMetric portalMetric;

private readonly Counter concentratorCounter = Metrics.CreateCounter(MetricName.ConcentratorCount, "Concentrators count");
private readonly Counter connectedConcentratorCounter = Metrics.CreateCounter(MetricName.ConnectedConcentratorCount, "Connected concentrators count");

public ConcentratorMetricExporterService(ILogger<ConcentratorMetricExporterService> logger, PortalMetric portalMetric)
public ConcentratorMetricExporterJob(ILogger<ConcentratorMetricExporterJob> logger, PortalMetric portalMetric)
{
this.logger = logger;
this.portalMetric = portalMetric;
Expand All @@ -30,7 +29,6 @@ public Task Execute(IJobExecutionContext context)
this.logger.LogInformation("Start exporting concentrators metrics");

this.concentratorCounter.IncTo(this.portalMetric.ConcentratorCount);
this.connectedConcentratorCounter.IncTo(this.portalMetric.ConnectedConcentratorCount);

this.logger.LogInformation("End exporting concentrators metrics");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Services
namespace AzureIoTHub.Portal.Server.Jobs
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Server.Services;
using Microsoft.Extensions.Logging;
using Quartz;
using Shared.Models.v1._0;

[DisallowConcurrentExecution]
public class ConcentratorMetricLoaderService : IJob
public class ConcentratorMetricLoaderJob : IJob
{
private readonly ILogger<ConcentratorMetricLoaderService> logger;
private readonly ILogger<ConcentratorMetricLoaderJob> logger;
private readonly PortalMetric portalMetric;
private readonly IExternalDeviceService externalDeviceService;

public ConcentratorMetricLoaderService(ILogger<ConcentratorMetricLoaderService> logger, PortalMetric portalMetric, IExternalDeviceService externalDeviceService)
public ConcentratorMetricLoaderJob(ILogger<ConcentratorMetricLoaderJob> logger, PortalMetric portalMetric, IExternalDeviceService externalDeviceService)
{
this.logger = logger;
this.portalMetric = portalMetric;
Expand All @@ -28,7 +29,6 @@ public async Task Execute(IJobExecutionContext context)
this.logger.LogInformation("Start loading concentrators metrics");

await LoadConcentratorsCountMetric();
await LoadConnectedConcentratorsCountMetric();

this.logger.LogInformation("End loading concentrators metrics");
}
Expand All @@ -44,17 +44,5 @@ private async Task LoadConcentratorsCountMetric()
this.logger.LogError($"Unable to load concentrators count metric: {e.Detail}", e);
}
}

private async Task LoadConnectedConcentratorsCountMetric()
{
try
{
this.portalMetric.ConnectedConcentratorCount = await this.externalDeviceService.GetConnectedConcentratorsCount();
}
catch (InternalServerErrorException e)
{
this.logger.LogError($"Unable to load connected concentrators count metric: {e.Detail}", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Services
namespace AzureIoTHub.Portal.Server.Jobs
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Shared.Constants;
Expand All @@ -11,15 +11,15 @@ namespace AzureIoTHub.Portal.Server.Services
using Shared.Models.v1._0;

[DisallowConcurrentExecution]
public class DeviceMetricExporterService : IJob
public class DeviceMetricExporterJob : IJob
{
private readonly ILogger<DeviceMetricExporterService> logger;
private readonly ILogger<DeviceMetricExporterJob> logger;
private readonly PortalMetric portalMetric;

private readonly Counter deviceCounter = Metrics.CreateCounter(MetricName.DeviceCount, "Devices count");
private readonly Counter connectedDeviceCounter = Metrics.CreateCounter(MetricName.ConnectedDeviceCount, "Connected devices count");

public DeviceMetricExporterService(ILogger<DeviceMetricExporterService> logger, PortalMetric portalMetric)
public DeviceMetricExporterJob(ILogger<DeviceMetricExporterJob> logger, PortalMetric portalMetric)
{
this.logger = logger;
this.portalMetric = portalMetric;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Services
namespace AzureIoTHub.Portal.Server.Jobs
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Server.Services;
using Microsoft.Extensions.Logging;
using Quartz;
using Shared.Models.v1._0;

[DisallowConcurrentExecution]
public class DeviceMetricLoaderService : IJob
public class DeviceMetricLoaderJob : IJob
{
private readonly ILogger<DeviceMetricLoaderService> logger;
private readonly ILogger<DeviceMetricLoaderJob> logger;
private readonly PortalMetric portalMetric;
private readonly IExternalDeviceService externalDeviceService;

public DeviceMetricLoaderService(ILogger<DeviceMetricLoaderService> logger, PortalMetric portalMetric, IExternalDeviceService externalDeviceService)
public DeviceMetricLoaderJob(ILogger<DeviceMetricLoaderJob> logger, PortalMetric portalMetric, IExternalDeviceService externalDeviceService)
{
this.logger = logger;
this.portalMetric = portalMetric;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Services
namespace AzureIoTHub.Portal.Server.Jobs
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Shared.Constants;
Expand All @@ -11,16 +11,16 @@ namespace AzureIoTHub.Portal.Server.Services
using Shared.Models.v1._0;

[DisallowConcurrentExecution]
public class EdgeDeviceMetricExporterService : IJob
public class EdgeDeviceMetricExporterJob : IJob
{
private readonly ILogger<EdgeDeviceMetricExporterService> logger;
private readonly ILogger<EdgeDeviceMetricExporterJob> logger;
private readonly PortalMetric portalMetric;

private readonly Counter edgeDeviceCounter = Metrics.CreateCounter(MetricName.EdgeDeviceCount, "Edge devices count");
private readonly Counter connectedEdgeDeviceCounter = Metrics.CreateCounter(MetricName.ConnectedEdgeDeviceCount, "Connected edge devices count");
private readonly Counter failedDeploymentCount = Metrics.CreateCounter(MetricName.FailedDeploymentCount, "Failed deployments count");

public EdgeDeviceMetricExporterService(ILogger<EdgeDeviceMetricExporterService> logger, PortalMetric portalMetric)
public EdgeDeviceMetricExporterJob(ILogger<EdgeDeviceMetricExporterJob> logger, PortalMetric portalMetric)
{
this.logger = logger;
this.portalMetric = portalMetric;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Services
namespace AzureIoTHub.Portal.Server.Jobs
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Server.Services;
using Microsoft.Extensions.Logging;
using Quartz;
using Shared.Models.v1._0;

[DisallowConcurrentExecution]
public class EdgeDeviceMetricLoaderService : IJob
public class EdgeDeviceMetricLoaderJob : IJob
{
private readonly ILogger<EdgeDeviceMetricLoaderService> logger;
private readonly ILogger<EdgeDeviceMetricLoaderJob> logger;
private readonly PortalMetric portalMetric;
private readonly IExternalDeviceService externalDeviceService;
private readonly IConfigService configService;

public EdgeDeviceMetricLoaderService(ILogger<EdgeDeviceMetricLoaderService> logger, PortalMetric portalMetric, IExternalDeviceService externalDeviceService, IConfigService configService)
public EdgeDeviceMetricLoaderJob(ILogger<EdgeDeviceMetricLoaderJob> logger, PortalMetric portalMetric, IExternalDeviceService externalDeviceService, IConfigService configService)
{
this.logger = logger;
this.portalMetric = portalMetric;
Expand Down
16 changes: 0 additions & 16 deletions src/AzureIoTHub.Portal.Server/Services/ExternalDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,6 @@ public async Task<int> GetConcentratorsCount()
}
}

public async Task<int> GetConnectedConcentratorsCount()
{
try
{
var count = await this.registryManager
.CreateQuery("SELECT COUNT() as totalNumber FROM devices WHERE devices.capabilities.iotEdge = false AND devices.tags.deviceType = 'LoRa Concentrator' AND connectionState = 'Connected'")
.GetNextAsJsonAsync();

return !JObject.Parse(count.Single()).TryGetValue("totalNumber", out var result) ? 0 : result.Value<int>();
}
catch (RequestFailedException e)
{
throw new InternalServerErrorException("Unable to get connected LoRaWAN concentrators count", e);
}
}

public async Task<EnrollmentCredentials> GetEnrollmentCredentials(string deviceId)
{
var device = await GetDeviceTwin(deviceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ Task<PaginationResult<Twin>> GetAllEdgeDevice(

Task<int> GetConcentratorsCount();

Task<int> GetConnectedConcentratorsCount();

Task<EnrollmentCredentials> GetEnrollmentCredentials(string deviceId);

Task<EnrollmentCredentials> GetEdgeDeviceCredentials(string edgeDeviceId);
Expand Down
8 changes: 4 additions & 4 deletions src/AzureIoTHub.Portal.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AzureIoTHub.Portal.Server
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using AzureIoTHub.Portal.Domain.Options;
using AzureIoTHub.Portal.Server.Jobs;
using Domain;
using Domain.Exceptions;
using Domain.Repositories;
Expand All @@ -22,7 +23,6 @@ namespace AzureIoTHub.Portal.Server
using Infrastructure;
using Infrastructure.Repositories;
using Infrastructure.Seeds;
using Jobs;
using Managers;
using Mappers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -362,9 +362,9 @@ Specify the authorization token got from your IDP as a header.
});
});

q.AddMetricsService<DeviceMetricExporterService, DeviceMetricLoaderService>(configuration);
q.AddMetricsService<EdgeDeviceMetricExporterService, EdgeDeviceMetricLoaderService>(configuration);
q.AddMetricsService<ConcentratorMetricExporterService, ConcentratorMetricLoaderService>(configuration);
q.AddMetricsService<DeviceMetricExporterJob, DeviceMetricLoaderJob>(configuration);
q.AddMetricsService<EdgeDeviceMetricExporterJob, EdgeDeviceMetricLoaderJob>(configuration);
q.AddMetricsService<ConcentratorMetricExporterJob, ConcentratorMetricLoaderJob>(configuration);

_ = q.AddJob<SyncDevicesJob>(j => j.WithIdentity(nameof(SyncDevicesJob)))
.AddTrigger(t => t
Expand Down
2 changes: 0 additions & 2 deletions src/AzureIoTHub.Portal.Shared/Models/v1.0/PortalMetric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ public class PortalMetric
public int FailedDeploymentCount { get; set; }

public int ConcentratorCount { get; set; }

public int ConnectedConcentratorCount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public void DashboardMetricShouldRenderCorrectly()
EdgeDeviceCount = 3,
ConnectedEdgeDeviceCount = 4,
FailedDeploymentCount = 5,
ConcentratorCount = 6,
ConnectedConcentratorCount = 7
ConcentratorCount = 6
};

_ = this.mockDashboardMetricsClientService.Setup(c => c.GetPortalMetrics())
Expand All @@ -54,9 +53,9 @@ public void DashboardMetricShouldRenderCorrectly()
var cut = RenderComponent<DashboardMetrics>();

// Assert
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(6));

cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-title").TextContent.Should().Be("Devices"));
cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-value").TextContent.Should().Be(portalMetric.DeviceCount.ToString(CultureInfo.InvariantCulture)));
Expand All @@ -75,18 +74,17 @@ public void OnRefreshDashboardEventDashboardMetricShouldRefreshMetrics()
EdgeDeviceCount = 3,
ConnectedEdgeDeviceCount = 4,
FailedDeploymentCount = 5,
ConcentratorCount = 6,
ConnectedConcentratorCount = 7
ConcentratorCount = 6
};

_ = this.mockDashboardMetricsClientService.Setup(c => c.GetPortalMetrics())
.ReturnsAsync(portalMetric);

var cut = RenderComponent<DashboardMetrics>();

cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(6));

cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-title").TextContent.Should().Be("Devices"));
cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-value").TextContent.Should().Be(portalMetric.DeviceCount.ToString(CultureInfo.InvariantCulture)));
Expand All @@ -95,9 +93,9 @@ public void OnRefreshDashboardEventDashboardMetricShouldRefreshMetrics()
TestContext?.Services.GetRequiredService<IDashboardLayoutService>().RefreshDashboard();

// Assert
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(6));

cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-title").TextContent.Should().Be("Devices"));
cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-value").TextContent.Should().Be(portalMetric.DeviceCount.ToString(CultureInfo.InvariantCulture)));
Expand All @@ -117,9 +115,9 @@ public void DashboardMetricShouldProcessProblemDetailsExceptionWhenIssueOccurs()
var cut = RenderComponent<DashboardMetrics>();

// Assert
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(7));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-icon").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-title").Count.Should().Be(6));
cut.WaitForAssertion(() => cut.FindAll("#dashboard-metric-counter-value").Count.Should().Be(6));

cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-title").TextContent.Should().Be("Devices"));
cut.WaitForAssertion(() => cut.Find("#dashboard-metric-counter-value").TextContent.Should().Be("0"));
Expand Down
Loading