private static SimulationStatistics GetSimulationStatistics(Simulation simulation) { var statistics = SimulationStatistics.FromServiceModel(simulation.Statistics); // AverageMessagesPerSecond calculation needs ActualStartTime to be set. // ActualStartTime will be set once partitioning and device creation is done, upto that point it can be null. if (statistics != null && simulation.ActualStartTime.HasValue) { DateTimeOffset now = DateTimeOffset.UtcNow; var actualStartTime = simulation.ActualStartTime.Value; double durationInSeconds = 0; if (simulation.IsActiveNow) { // If the simulation is active, calculate duration from start till now. durationInSeconds = now.Subtract(actualStartTime).TotalSeconds; } else if (simulation.StoppedTime.HasValue) { // If simulation is stopped, calculate duration from start till stop time. durationInSeconds = simulation.StoppedTime.Value.Subtract(actualStartTime).TotalSeconds; } statistics.AverageMessagesPerSecond = durationInSeconds > 0 ? Math.Round((double)statistics.TotalMessagesSent / durationInSeconds, 2) : 0; } return(statistics); }
private static SimulationStatistics GetSimulationStatistics(Simulation simulation) { var statistics = SimulationStatistics.FromServiceModel(simulation.Statistics); if (statistics != null) { DateTimeOffset now = DateTimeOffset.UtcNow; TimeSpan duration = simulation.IsActiveNow ? now.Subtract(simulation.ActualStartTime.Value) : simulation.StoppedTime.Value.Subtract(simulation.ActualStartTime.Value); statistics.AverageMessagesPerSecond = (double)statistics.TotalMessagesSent / duration.TotalSeconds; } return(statistics); }
// Default constructor used by web service requests public SimulationApiModel() { this.Id = string.Empty; this.Name = string.Empty; this.Description = string.Empty; // When unspecified, a simulation is enabled this.Enabled = true; this.Running = false; this.IotHubs = new List <SimulationIotHub>(); this.StartTime = null; this.EndTime = null; this.StoppedTime = null; this.DeviceModels = new List <SimulationDeviceModelRef>(); this.Statistics = new SimulationStatistics(); }
// Map service model to API model public static SimulationApiModel FromServiceModel( Simulation value, IServicesConfig servicesConfig, IDeploymentConfig deploymentConfig, IIotHubConnectionStringManager connectionStringManager, ISimulationRunner simulationRunner, IRateLimiting rateReporter) { if (value == null) { return(null); } var result = new SimulationApiModel { ETag = value.ETag, Id = value.Id, Name = value.Name, Description = value.Description, Enabled = value.Enabled, Running = value.ShouldBeRunning, StartTime = value.StartTime.ToString(), EndTime = value.EndTime.ToString(), StoppedTime = value.StoppedTime.ToString(), IotHubs = new List <SimulationIotHub>() }; foreach (var iotHubConnectionString in value.IotHubConnectionStrings) { var iotHub = new SimulationIotHub { ConnectionString = iotHubConnectionString }; result.IotHubs.Add(iotHub); } // Ignore the date if the simulation doesn't have a start time if (value.StartTime.HasValue && !value.StartTime.Value.Equals(DateTimeOffset.MinValue)) { result.StartTime = value.StartTime?.ToString(DATE_FORMAT); } // Ignore the date if the simulation doesn't have an end time if (value.EndTime.HasValue && !value.EndTime.Value.Equals(DateTimeOffset.MaxValue)) { result.EndTime = value.EndTime?.ToString(DATE_FORMAT); } // Ignore the date if the simulation doesn't have an end time if (value.StoppedTime.HasValue && !value.StoppedTime.Value.Equals(DateTimeOffset.MaxValue)) { result.StoppedTime = value.StoppedTime?.ToString(DATE_FORMAT); } result.DeviceModels = SimulationDeviceModelRef.FromServiceModel(value.DeviceModels); result.Statistics = SimulationStatistics.FromServiceModel(value.Statistics); result.created = value.Created; result.modified = value.Modified; result.AppendHubPropertiesAndStatistics(servicesConfig, deploymentConfig, connectionStringManager, simulationRunner, rateReporter); return(result); }