public void ItGetsSimulationStatistics() { // Arrange SimulationStatisticsModel expectedStatistics = new SimulationStatisticsModel { ActiveDevices = 15, TotalMessagesSent = 300, FailedDeviceConnections = 6, FailedDevicePropertiesUpdates = 8, FailedMessages = 10 }; this.simulationStatisticsStorage .Setup(x => x.GetAllAsync()) .ReturnsAsync(this.storageRecords); // Act var result = this.target.GetSimulationStatisticsAsync(SIM_ID).CompleteOrTimeout(); // Assert Assert.Equal(expectedStatistics.ActiveDevices, result.Result.ActiveDevices); Assert.Equal(expectedStatistics.TotalMessagesSent, result.Result.TotalMessagesSent); Assert.Equal(expectedStatistics.FailedDeviceConnections, result.Result.FailedDeviceConnections); Assert.Equal(expectedStatistics.FailedDevicePropertiesUpdates, result.Result.FailedDevicePropertiesUpdates); Assert.Equal(expectedStatistics.FailedMessages, result.Result.FailedMessages); }
public async Task <SimulationStatisticsModel> GetSimulationStatisticsAsync(string simulationId) { if (string.IsNullOrEmpty(simulationId)) { this.log.Error("Simulation Id cannot be null or empty"); throw new InvalidInputException("Simulation Id cannot be null or empty"); } SimulationStatisticsModel statistics = new SimulationStatisticsModel(); try { var simulationRecords = (await this.simulationStatisticsStorage.GetAllAsync()) .Select(p => JsonConvert.DeserializeObject <SimulationStatisticsRecord>(p.Data)) .Where(i => i.SimulationId == simulationId) .ToList(); foreach (var record in simulationRecords) { statistics.ActiveDevices += record.Statistics.ActiveDevices; statistics.TotalMessagesSent += record.Statistics.TotalMessagesSent; statistics.FailedDeviceConnections += record.Statistics.FailedDeviceConnections; statistics.FailedDevicePropertiesUpdates += record.Statistics.FailedDevicePropertiesUpdates; statistics.FailedMessages += record.Statistics.FailedMessages; } } catch (Exception e) { this.log.Error("Error on getting statistics records", e); } return(statistics); }
public void ItIgnoresAverageCountForExpiredNode() { // Arrange SimulationStatisticsModel expectedStatistics = new SimulationStatisticsModel { ActiveDevices = 10, TotalMessagesSent = 300, FailedDeviceConnections = 6, FailedDevicePropertiesUpdates = 8, FailedMessages = 10 }; this.simulationStatisticsStorage .Setup(x => x.GetAllAsync()) .ReturnsAsync(this.storageRecords); this.clusterNodes .Setup(x => x.GetSortedIdListAsync()) .ReturnsAsync(new SortedSet <string> { NODE_IDS[1] }); // Act var result = this.target.GetSimulationStatisticsAsync(SIM_ID).CompleteOrTimeout(); // Assert Assert.Equal(expectedStatistics.ActiveDevices, result.Result.ActiveDevices); Assert.Equal(expectedStatistics.TotalMessagesSent, result.Result.TotalMessagesSent); Assert.Equal(expectedStatistics.FailedDeviceConnections, result.Result.FailedDeviceConnections); Assert.Equal(expectedStatistics.FailedDevicePropertiesUpdates, result.Result.FailedDevicePropertiesUpdates); Assert.Equal(expectedStatistics.FailedMessages, result.Result.FailedMessages); }
public void ItSetsSimulationStatisticsFromServiceModel() { // Arrange var simulation = this.GetSimulationModel(); var statistics = new SimulationStatisticsModel { ActiveDevices = 10, TotalMessagesSent = 100, FailedDeviceConnections = 1, FailedDevicePropertiesUpdates = 2, FailedMessages = 3 }; simulation.Statistics = statistics; var now = DateTimeOffset.UtcNow; simulation.ActualStartTime = now.AddSeconds(-60); simulation.StoppedTime = now; simulation.Enabled = false; // Avg messages = 100/60 (TotalMessagesSent / stoppedTime - startTime) var expectedAvgMessages = 1.67; // Act var result = Model.FromServiceModel(simulation); // Assert Assert.IsType <Model>(result); Assert.Equal(simulation.Id, result.Id); Assert.NotNull(result.Statistics); Assert.Equal(statistics.ActiveDevices, result.Statistics.ActiveDevices); Assert.Equal(statistics.TotalMessagesSent, result.Statistics.TotalMessagesSent); Assert.Equal(statistics.FailedDeviceConnections, result.Statistics.FailedDeviceConnections); Assert.Equal(statistics.FailedDevicePropertiesUpdates, result.Statistics.FailedDevicePropertiesUpdates); Assert.Equal(statistics.FailedMessages, result.Statistics.FailedMessages); Assert.Equal(expectedAvgMessages, result.Statistics.AverageMessagesPerSecond); }
public async Task CreateOrUpdateAsync(string simulationId, SimulationStatisticsModel statistics) { var nodeId = this.clusterNodes.GetCurrentNodeId(); var statisticsRecordId = this.GetStatisticsRecordId(simulationId, nodeId); var statisticsRecord = new SimulationStatisticsRecord { NodeId = nodeId, SimulationId = simulationId, Statistics = statistics }; var statisticsStorageRecord = new StorageRecord { Id = statisticsRecordId, Data = JsonConvert.SerializeObject(statisticsRecord) }; try { this.log.Debug("Creating statistics record", () => new { statisticsStorageRecord }); await this.simulationStatisticsStorage.CreateAsync(statisticsStorageRecord); } catch (Exception e) { this.log.Error("Error on saving statistics records", e); } }
public async Task SaveStatisticsAsync() { try { var prefix = this.GetDictKey(string.Empty); var telemetryActors = this.deviceTelemetryActors.Where(a => a.Key.StartsWith(prefix)).ToList(); var connectionActors = this.deviceConnectionActors.Where(a => a.Key.StartsWith(prefix)).ToList(); var propertiesActors = this.devicePropertiesActors.Where(a => a.Key.StartsWith(prefix)).ToList(); var stateActors = this.deviceStateActors.Where(a => a.Key.StartsWith(prefix)).ToList(); var simulationModel = new SimulationStatisticsModel { ActiveDevices = stateActors.Count(a => a.Value.IsDeviceActive), TotalMessagesSent = telemetryActors.Sum(a => a.Value.TotalMessagesCount), FailedMessages = telemetryActors.Sum(a => a.Value.FailedMessagesCount), FailedDeviceConnections = connectionActors.Sum(a => a.Value.FailedDeviceConnectionsCount), FailedDevicePropertiesUpdates = propertiesActors.Sum(a => a.Value.FailedTwinUpdatesCount), }; await this.simulationStatistics.CreateOrUpdateAsync(this.simulation.Id, simulationModel); } catch (Exception e) { // Log and do not rethrow this.log.Error("Error saving simulation statistics", () => new { this.simulation.Id, e }); } }
/// <summary> /// Deletes statistics records for a simulation. /// </summary> public async Task DeleteSimulationStatisticsAsync(string simulationId) { if (string.IsNullOrEmpty(simulationId)) { this.log.Error("Simulation Id cannot be null or empty"); throw new InvalidInputException("Simulation Id cannot be null or empty"); } SimulationStatisticsModel statistics = new SimulationStatisticsModel(); try { var statisticsRecordsIds = (await this.simulationStatisticsStorage.GetAllAsync()) .Select(r => r.Id) .Where(i => i.StartsWith(simulationId)) .ToList(); if (statisticsRecordsIds.Count > 0) { this.log.Debug("Deleting statistics records", () => new { statisticsRecordsIds }); await this.simulationStatisticsStorage.DeleteMultiAsync(statisticsRecordsIds); } else { this.log.Debug("No records to delete."); } } catch (Exception e) { this.log.Error("Error on getting statistics records", e); } }
/// <summary> /// Updates statistics record for a given simulation. /// Note: exceptions are caught upstream /// </summary> public async Task UpdateAsync(string simulationId, SimulationStatisticsModel statistics) { var nodeId = this.clusterNodes.GetCurrentNodeId(); var statisticsRecordId = this.GetStatisticsRecordId(simulationId, nodeId); var statisticsStorageRecord = this.GetStorageRecord(simulationId, statistics); this.log.Debug("Updating statistics record", () => new { statisticsStorageRecord }); var record = await this.simulationStatisticsStorage.GetAsync(statisticsRecordId); await this.simulationStatisticsStorage.UpsertAsync(statisticsStorageRecord, record.ETag); }
/// <summary> /// Updates statistics record for a given simulation. /// Note: exceptions are caught upstream /// </summary> public async Task UpdateAsync(string simulationId, SimulationStatisticsModel statistics) { var nodeId = this.clusterNodes.GetCurrentNodeId(); var recordId = this.BuildRecordId(simulationId, nodeId); this.log.Debug("Fetch record to have latest ETag and overwrite the existing record", () => new { recordId }); IDataRecord existingRecord = await this.simulationStatisticsStorage.GetAsync(recordId); this.log.Debug("Updating statistics record", () => new { recordId }); IDataRecord newRecord = this.BuildStorageRecord(simulationId, statistics); await this.simulationStatisticsStorage.UpsertAsync(newRecord, existingRecord.GetETag()); }
private IDataRecord BuildStorageRecord(string simulationId, SimulationStatisticsModel statistics) { var nodeId = this.clusterNodes.GetCurrentNodeId(); var statisticsRecordId = this.BuildRecordId(simulationId, nodeId); var statisticsRecord = new SimulationStatisticsRecord { NodeId = nodeId, SimulationId = simulationId, Statistics = statistics }; return(this.simulationStatisticsStorage.BuildRecord( statisticsRecordId, JsonConvert.SerializeObject(statisticsRecord))); }
void ItCalculatesStatsAndInvokesCreateOrUpdateStatistics() { // Arrange var expectedTotalMessageCount = 200; var expectedFailedMessagesCount = 2; var expectedFailedDeviceConnectionsCount = 6; var expectedFailedTwinUpdatesCount = 10; var statisticsModel = new SimulationStatisticsModel { TotalMessagesSent = expectedTotalMessageCount, FailedMessages = expectedFailedMessagesCount, FailedDeviceConnections = expectedFailedDeviceConnectionsCount, FailedDevicePropertiesUpdates = expectedFailedTwinUpdatesCount }; var mockDeviceConnectionActor = new Mock <IDeviceConnectionActor>(); var mockDevicePropertiesActor = new Mock <IDevicePropertiesActor>(); var mockDeviceTelemetryActor = new Mock <IDeviceTelemetryActor>(); for (int i = 0; i < 2; i++) { var deviceName = SIM_ID + "_" + i; mockDeviceTelemetryActor.Setup(x => x.TotalMessagesCount).Returns(100); mockDeviceTelemetryActor.Setup(x => x.FailedMessagesCount).Returns(1); this.deviceTelemetryActors.TryAdd(deviceName, mockDeviceTelemetryActor.Object); mockDeviceConnectionActor.Setup(x => x.FailedDeviceConnectionsCount).Returns(3); this.mockDeviceContext.TryAdd(deviceName, mockDeviceConnectionActor.Object); mockDevicePropertiesActor.Setup(x => x.FailedTwinUpdatesCount).Returns(5); this.devicePropertiesActors.TryAdd(deviceName, mockDevicePropertiesActor.Object); } // Act this.target.SaveStatisticsAsync().CompleteOrTimeout(); // Assert this.mockSimulationStatistics.Verify(x => x.CreateOrUpdateAsync(SIM_ID, It.Is <SimulationStatisticsModel>( a => a.ActiveDevices == statisticsModel.ActiveDevices && a.TotalMessagesSent == statisticsModel.TotalMessagesSent && a.FailedMessages == statisticsModel.FailedMessages && a.FailedDeviceConnections == statisticsModel.FailedDeviceConnections && a.FailedDevicePropertiesUpdates == statisticsModel.FailedDevicePropertiesUpdates))); }
private StorageRecord GetStorageRecord(string simulationId, SimulationStatisticsModel statistics) { var nodeId = this.clusterNodes.GetCurrentNodeId(); var statisticsRecordId = this.GetStatisticsRecordId(simulationId, nodeId); var statisticsRecord = new SimulationStatisticsRecord { NodeId = nodeId, SimulationId = simulationId, Statistics = statistics }; return(new StorageRecord { Id = statisticsRecordId, Data = JsonConvert.SerializeObject(statisticsRecord) }); }
/// <summary> /// Creates or updates statistics record for a given simulation. /// Note: exceptions are caught upstream /// </summary> public async Task CreateOrUpdateAsync(string simulationId, SimulationStatisticsModel statistics) { var nodeId = this.clusterNodes.GetCurrentNodeId(); string statisticsRecordId = this.BuildRecordId(simulationId, nodeId); IDataRecord statisticsStorageRecord = this.BuildStorageRecord(simulationId, statistics); if (await this.simulationStatisticsStorage.ExistsAsync(statisticsRecordId)) { this.log.Debug("Updating statistics record", () => new { statisticsStorageRecord }); var record = await this.simulationStatisticsStorage.GetAsync(statisticsRecordId); await this.simulationStatisticsStorage.UpsertAsync(statisticsStorageRecord, record.GetETag()); } else { this.log.Debug("Creating statistics record", () => new { statisticsStorageRecord }); await this.simulationStatisticsStorage.CreateAsync(statisticsStorageRecord); } }
public void ItUpdatesSimulationStatistics() { // Arrange var statisticsRecordId = $"{SIM_ID}__{NODE_IDS[0]}"; SimulationStatisticsModel inputStatistics = new SimulationStatisticsModel { ActiveDevices = 5, TotalMessagesSent = 300, FailedDeviceConnections = 6, FailedDevicePropertiesUpdates = 8, FailedMessages = 10 }; SimulationStatisticsRecord expectedStatistics = new SimulationStatisticsRecord { SimulationId = SIM_ID, NodeId = NODE_IDS[0], Statistics = inputStatistics }; StorageRecord storageRecord = new StorageRecord { Id = statisticsRecordId, Data = JsonConvert.SerializeObject(expectedStatistics), }; this.clusterNodes.Setup(x => x.GetCurrentNodeId()).Returns(NODE_IDS[0]); this.simulationStatisticsStorage.Setup(x => x.ExistsAsync(statisticsRecordId)).ReturnsAsync(true); this.simulationStatisticsStorage.Setup(x => x.GetAsync(statisticsRecordId)).ReturnsAsync(storageRecord); // Act var result = this.target.CreateOrUpdateAsync(SIM_ID, inputStatistics).CompleteOrTimeout(); // Assert this.simulationStatisticsStorage.Verify(x => x.GetAsync(It.Is <string>( a => a == statisticsRecordId))); this.simulationStatisticsStorage.Verify(x => x.UpsertAsync(It.Is <StorageRecord>( a => a.Id == storageRecord.Id && a.Data == storageRecord.Data), It.IsAny <string>())); }
public async Task SaveStatisticsAsync() { try { var telemetryActors = this.deviceTelemetryActors ?? this.deviceTelemetryActors.Where(a => a.Key.StartsWith(this.simulation.Id)); var simulationModel = new SimulationStatisticsModel { TotalMessagesSent = telemetryActors != null?telemetryActors.Sum(a => a.Value.TotalMessagesCount) : 0, FailedMessages = telemetryActors != null?telemetryActors.Sum(a => a.Value.FailedMessagesCount) : 0, FailedDeviceConnections = this.deviceConnectionActors != null?this.deviceConnectionActors.Where(a => a.Key.StartsWith(this.simulation.Id)).Sum(a => a.Value.FailedDeviceConnectionsCount) : 0, FailedDevicePropertiesUpdates = this.devicePropertiesActors != null?this.devicePropertiesActors.Where(a => a.Key.StartsWith(this.simulation.Id)).Sum(a => a.Value.FailedTwinUpdatesCount) : 0, }; await this.simulationStatistics.CreateOrUpdateAsync(this.simulation.Id, simulationModel); } catch (Exception e) { this.log.Error("Error saving simulation statistics", () => new { this.simulation.Id, e }); } }
public void ItCreatesSimulationStatistics() { // Arrange var statisticsRecordId = $"{SIM_ID}__{NODE_IDS[0]}"; SimulationStatisticsModel statistics = new SimulationStatisticsModel { ActiveDevices = 5, TotalMessagesSent = 300, FailedDeviceConnections = 6, FailedDevicePropertiesUpdates = 8, FailedMessages = 10 }; SimulationStatisticsRecord statisticsRecord = new SimulationStatisticsRecord { NodeId = NODE_IDS[0], SimulationId = SIM_ID, Statistics = statistics }; IDataRecord storageRecord = new DataRecord { Id = statisticsRecordId, Data = JsonConvert.SerializeObject(statisticsRecord) }; this.clusterNodes.Setup(x => x.GetCurrentNodeId()).Returns(NODE_IDS[0]); this.simulationStatisticsStorage.Setup(x => x.ExistsAsync(NODE_IDS[0])).ReturnsAsync(false); // Act this.target.CreateOrUpdateAsync(SIM_ID, statistics).CompleteOrTimeout(); // Assert this.simulationStatisticsStorage.Verify(x => x.CreateAsync(It.Is <IDataRecord>( a => a.GetId() == storageRecord.GetId() && a.GetData() == storageRecord.GetData()))); }
void ItCalculatesStatsAndInvokesCreateOrUpdateStatistics() { // Arrange var expectedTotalMessageCount = 200; var expectedFailedMessagesCount = 2; var expectedFailedDeviceConnectionsCount = 6; var expectedFailedTwinUpdatesCount = 10; var expectedActiveDevicesCount = 2; var statisticsModel = new SimulationStatisticsModel { ActiveDevices = expectedActiveDevicesCount, TotalMessagesSent = expectedTotalMessageCount, FailedMessages = expectedFailedMessagesCount, FailedDeviceConnections = expectedFailedDeviceConnectionsCount, FailedDevicePropertiesUpdates = expectedFailedTwinUpdatesCount }; // Add data for expected simulation for (int i = 0; i < 2; i++) { var deviceName = SIM_ID + ACTOR_PREFIX_SEPARATOR + i; var mockDeviceTelemetryActor = new Mock <IDeviceTelemetryActor>(); mockDeviceTelemetryActor.Setup(x => x.TotalMessagesCount).Returns(100); mockDeviceTelemetryActor.Setup(x => x.FailedMessagesCount).Returns(1); this.deviceTelemetryActors.TryAdd(deviceName, mockDeviceTelemetryActor.Object); var mockDeviceConnectionActor = new Mock <IDeviceConnectionActor>(); mockDeviceConnectionActor.Setup(x => x.FailedDeviceConnectionsCount).Returns(3); this.mockDeviceContext.TryAdd(deviceName, mockDeviceConnectionActor.Object); var mockDevicePropertiesActor = new Mock <IDevicePropertiesActor>(); mockDevicePropertiesActor.Setup(x => x.FailedTwinUpdatesCount).Returns(5); this.devicePropertiesActors.TryAdd(deviceName, mockDevicePropertiesActor.Object); var mockDeviceStateActor = new Mock <IDeviceStateActor>(); mockDeviceStateActor.Setup(x => x.IsDeviceActive).Returns(true); this.deviceStateActors.TryAdd(deviceName, mockDeviceStateActor.Object); } // Add data for additional simulations for (int i = 2; i < 5; i++) { var deviceName = SIM_ID + i + ACTOR_PREFIX_SEPARATOR + i; var mockDeviceTelemetryActor = new Mock <IDeviceTelemetryActor>(); mockDeviceTelemetryActor.Setup(x => x.TotalMessagesCount).Returns(100 * i); mockDeviceTelemetryActor.Setup(x => x.FailedMessagesCount).Returns(1 * i); this.deviceTelemetryActors.TryAdd(deviceName, mockDeviceTelemetryActor.Object); var mockDeviceConnectionActor = new Mock <IDeviceConnectionActor>(); mockDeviceConnectionActor.Setup(x => x.FailedDeviceConnectionsCount).Returns(3 * i); this.mockDeviceContext.TryAdd(deviceName, mockDeviceConnectionActor.Object); var mockDevicePropertiesActor = new Mock <IDevicePropertiesActor>(); mockDevicePropertiesActor.Setup(x => x.FailedTwinUpdatesCount).Returns(5 * i); this.devicePropertiesActors.TryAdd(deviceName, mockDevicePropertiesActor.Object); var mockDeviceStateActor = new Mock <IDeviceStateActor>(); mockDeviceStateActor.Setup(x => x.IsDeviceActive).Returns(true); this.deviceStateActors.TryAdd(deviceName, mockDeviceStateActor.Object); } // Act this.target.SaveStatisticsAsync().CompleteOrTimeout(); // Assert create or update is called with expected simulation id and statistics this.mockSimulationStatistics.Verify(x => x.CreateOrUpdateAsync(SIM_ID, It.Is <SimulationStatisticsModel>( a => a.ActiveDevices == statisticsModel.ActiveDevices && a.TotalMessagesSent == statisticsModel.TotalMessagesSent && a.FailedMessages == statisticsModel.FailedMessages && a.FailedDeviceConnections == statisticsModel.FailedDeviceConnections && a.FailedDevicePropertiesUpdates == statisticsModel.FailedDevicePropertiesUpdates)), Times.Once); }