/// <summary> /// Handles the Delete event of the gMetricValues control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gMetricValues_Delete(object sender, RowEventArgs e) { var rockContext = new RockContext(); var metricValueService = new MetricValueService(rockContext); var metricValuePartitionService = new MetricValuePartitionService(rockContext); var metricValue = metricValueService.Get(e.RowKeyId); if (metricValue != null) { string errorMessage; if (!metricValueService.CanDelete(metricValue, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } rockContext.WrapTransaction(() => { metricValuePartitionService.DeleteRange(metricValue.MetricValuePartitions); metricValueService.Delete(metricValue); rockContext.SaveChanges(); }); } BindGrid(); }
/// <summary> /// Handles the Delete event of the gMetricValues control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gMetricValues_Delete( object sender, RowEventArgs e ) { var rockContext = new RockContext(); var metricValueService = new MetricValueService( rockContext ); var metricValuePartitionService = new MetricValuePartitionService( rockContext ); var metricValue = metricValueService.Get( e.RowKeyId ); if ( metricValue != null ) { string errorMessage; if ( !metricValueService.CanDelete( metricValue, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } rockContext.WrapTransaction( () => { metricValuePartitionService.DeleteRange( metricValue.MetricValuePartitions ); metricValueService.Delete( metricValue ); rockContext.SaveChanges(); } ); } BindGrid(); }
/// <summary> /// Saves the <see cref="MetricValue"/>s and updates the <see cref="IJobExecutionContext"/>'s Result property. /// </summary> /// <param name="context">The <see cref="IJobExecutionContext"/>.</param> /// <exception cref="Exception">Unable to find the "Hosting Metrics" Category ID.</exception> private void SaveMetricValues(IJobExecutionContext context) { var rockContext = new RockContext(); rockContext.Database.CommandTimeout = _commandTimeout; var hostingMetricsCategoryId = CategoryCache.GetId(SystemGuid.Category.METRIC_HOSTING_METRICS.AsGuid()); if (!hostingMetricsCategoryId.HasValue) { throw new Exception(@"Unable to find the ""Hosting Metrics"" Category ID."); } var metricIdsQuery = new MetricCategoryService(rockContext).Queryable() .Where(mc => mc.CategoryId == hostingMetricsCategoryId) .Select(mc => mc.MetricId); // Get all of the Metrics tied to the "Hosting Metrics" Category var metrics = new MetricService(rockContext).Queryable("MetricPartitions") .Where(m => metricIdsQuery.Contains(m.Id)) .ToList(); var metricValues = new List <MetricValue>(); foreach (var metric in metrics) { // Attempt to add any PerformanceCounter MetricValues TryAddPerformanceCounterMetricValue(metric, metricValues); } /* * 2020-04-22 - JPH * * The Metrics being collected by the first revision of this Job each have a single, default MetricPartition. * If we add Metrics to this Job in the future that are more complicated, we'll want to revisit the below logic. * */ var metricValueService = new MetricValueService(rockContext); var metricValuePartitionService = new MetricValuePartitionService(rockContext); foreach (var metricValue in metricValues) { foreach (var metricPartition in metricValue.Metric.MetricPartitions) { metricValue.MetricValuePartitions.Add(new MetricValuePartition { MetricPartitionId = metricPartition.Id }); } metricValueService.Add(metricValue); } rockContext.SaveChanges(); /* * 2020-05-19 - SK * Removing the old metrics based on Maximum Metric to Retain value * */ if (_maximumMetricsToRetain.HasValue) { foreach (var metric in metrics) { var deleteMetricValuesQry = metricValueService .Queryable() .AsNoTracking() .Where(a => a.MetricId == metric.Id) .OrderByDescending(a => a.MetricValueDateTime) .Skip(_maximumMetricsToRetain.Value); if (deleteMetricValuesQry.Any()) { var metricValuePartitionQry = metricValuePartitionService.Queryable().AsNoTracking().Where(a => deleteMetricValuesQry.Any(u => u.Id == a.MetricValueId)); rockContext.BulkDelete(metricValuePartitionQry); rockContext.BulkDelete(deleteMetricValuesQry); } } } context.Result = $"Calculated a total of {metricValues.Count} metric values for {metrics.Count} metrics"; }