public Task AddReportAsync(InstanceAnomalyReport report) { if (report != null) { return(_repository.AddReportAsync(report)); } return(Task.CompletedTask); }
public static InstanceAnomalyReport GetAnomalyReport(Guid instanceId) { var parameters = new List <string> { "CPU", "RAM", "DISC" }; var random = new Random(); var groups = new List <AzureMLAnomalyGroup>(3); var dateTime = DateTime.UtcNow; var processes = parameters.Count; for (var i = 0; i < processes; i++) { var warningsCount = random.Next(10, 29); groups.Add(new AzureMLAnomalyGroup() { Name = parameters[i], Warnings = new List <AzureMLAnomaly>(warningsCount), Anomalies = new List <AzureMLAnomaly>((warningsCount / 2) + 1), Total = random.Next(10, 50) }); } foreach (var g in groups) { for (var i = 0; i < g.Warnings.Capacity; i++) { var anomaly = new AzureMLAnomaly { Time = dateTime.AddMinutes(i * 2), Data = random.Next(50, 100) }; g.Warnings.Add(anomaly); if (i % 2 == 0) { g.Anomalies.Add(anomaly); } } } var report = new InstanceAnomalyReport { Id = Guid.NewGuid(), Date = DateTime.UtcNow.AddHours(1), ClientId = Guid.NewGuid(), AnomalyGroups = groups, CollectedDataTypeOfReport = CollectedDataType.AggregationForHour }; return(report); }
public Task AddReportAsync(InstanceAnomalyReport report) { return(_context.AnomalyReportsCollection.InsertOneAsync(report)); }
private Task SendMLReport(IEnumerable <CollectedDataDto> data, CollectedDataType reportType) { var results = (from d in data group d by d.ClientId into g select new { ClientId = g.Key, CollectedDatas = g.ToList() }).ToList(); var tasks = results.Select( async r => { try { var result = await _anomalyDetector.AnalyzeData(r.CollectedDatas); result.CollectedDataTypeOfReport = reportType; var report = new InstanceAnomalyReport { Id = Guid.NewGuid(), ClientId = r.ClientId, Date = result.Date, AnomalyGroups = result.AnomalyGroups, CollectedDataTypeOfReport = result.CollectedDataTypeOfReport }; await _reportsRepository.AddReportAsync(report); var reportMessage = new InstanceAnomalyReportMessage { AnomalyReportId = report.Id, InstanceId = r.ClientId }; Debug.WriteLine($"Generated id of report {report.Id}"); await _serviceBusProvider.SendAnomalyReportMessage(reportMessage); } catch (Exception ex) { _logger.LogError(ex, $"Unhandled error occurred while analyzing Anomalies of instance with id: {r?.ClientId}"); // TODO: Maybe send notification to user about that error that analyzing Anomalies of instance with id was unsuccessful } }); return(Task.WhenAll(tasks)); //var res = Parallel.ForEach( // results, // async obj => // { // try // { // var result = await _anomalyDetector.AnalyzeData(obj.CollectedDatas); // result.CollectedDataTypeOfReport = reportType; // var report = new InstanceAnomalyReport // { // Id = Guid.NewGuid(), // ClientId = obj.ClientId, // Date = result.Date, // AnomalyGroups = result.AnomalyGroups, // CollectedDataTypeOfReport = // result.CollectedDataTypeOfReport // }; // await _reportsRepository.AddReportAsync(report); // var reportMessage = // new InstanceAnomalyReportMessage { AnomalyReportId = report.Id, InstanceId = obj.ClientId }; // Debug.WriteLine($"Generated id of report {report.Id}"); // await _serviceBusProvider.SendAnomalyReportMessage(reportMessage); // } // catch (Exception ex) // { // _logger.LogError(ex, $"Unhandled error occurred while analyzing Anomalies of instance with id: {obj?.ClientId}"); // // TODO: Maybe send notification to user about that error that analyzing Anomalies of instance with id was unsuccessful // } // }); }