///GENMHASH:5BA999F4378FAE6FDDFE418C8C58C8FA:C0C35E00AF4E17F141675A2C05C7067B
 public SqlDatabaseMetricValueImpl(MetricValue innerObject) : base(innerObject)
 {
 }
Esempio n. 2
0
        // Gets the named metric by calling the provided query on each table that overlaps the given time range
        // Note: Does not populate Metric fields unrelated to query (i.e. "display name", resourceUri, and properties)
        private static async Task <Metric> GetMetricByNameAsync(string name, TableQuery query, MetricFilter filter, MetricLocation location, string invocationId)
        {
            Metric metric = new Metric()
            {
                Name = new LocalizableString()
                {
                    Value = name
                },
                StartTime    = filter.StartTime,
                EndTime      = filter.EndTime,
                TimeGrain    = filter.TimeGrain,
                MetricValues = new List <MetricValue>()
            };

            var instanceMetrics = new List <MetricValue>();
            var globalMetrics   = new List <DynamicTableEntity>();

            // The GetEnititesAsync function provides one task that will call all the queries in parallel
            var entities = await GetEntitiesAsync(GetNdayTables(filter, location), query, invocationId).ConfigureAwait(false);

            // Iterate over the instances to do conversion and aggregation when needed
            foreach (var entity in entities)
            {
                // Skip aggregated entities
                if (!IsInstanceMetric(entity.RowKey))
                {
                    // We ignore the aggergated metrics if there are instance metrics.
                    if (instanceMetrics.Count == 0)
                    {
                        globalMetrics.Add(entity);
                    }

                    continue;
                }

                MetricValue lastMetricValue = instanceMetrics.LastOrDefault();
                if (lastMetricValue == null)
                {
                    instanceMetrics.Add(ResolveMetricEntity(entity));
                }
                else
                {
                    if (lastMetricValue.Timestamp.Ticks == GetTimestampFromIndexMetricNameTimestamp(entity))
                    {
                        Aggregate(lastMetricValue, entity);
                    }
                    else
                    {
                        instanceMetrics.Add(ResolveMetricEntity(entity));
                    }
                }
            }

            if (instanceMetrics.Count > 0)
            {
                foreach (var metricValue in instanceMetrics)
                {
                    metricValue.Average = metricValue.Total / metricValue.Count;
                }

                metric.MetricValues = instanceMetrics;
            }
            else
            {
                metric.MetricValues = globalMetrics.Select(me => ResolveMetricEntity(me)).ToList();
            }

            return(metric);
        }
Esempio n. 3
0
        /// <summary>
        /// Loads the family data.
        /// </summary>
        /// <param name="csvData">The CSV data.</param>
        private int LoadMetrics(CSVInstance csvData)
        {
            // Required variables
            var lookupContext      = new RockContext();
            var metricService      = new MetricService(lookupContext);
            var categoryService    = new CategoryService(lookupContext);
            var metricSourceTypes  = DefinedTypeCache.Read(new Guid(Rock.SystemGuid.DefinedType.METRIC_SOURCE_TYPE)).DefinedValues;
            var metricManualSource = metricSourceTypes.FirstOrDefault(m => m.Guid == new Guid(Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_MANUAL));

            var metricEntityTypeId = EntityTypeCache.Read <Rock.Model.MetricCategory>(false, lookupContext).Id;
            var campusEntityTypeId = EntityTypeCache.Read <Rock.Model.Campus>(false, lookupContext).Id;

            var campuses         = CampusCache.All();
            var allMetrics       = metricService.Queryable().AsNoTracking().ToList();
            var metricCategories = categoryService.Queryable().AsNoTracking()
                                   .Where(c => c.EntityType.Guid == new Guid(Rock.SystemGuid.EntityType.METRICCATEGORY)).ToList();

            var defaultMetricCategory = metricCategories.FirstOrDefault(c => c.Name == "Metrics");

            if (defaultMetricCategory == null)
            {
                defaultMetricCategory              = new Category();
                defaultMetricCategory.Name         = "Metrics";
                defaultMetricCategory.IsSystem     = false;
                defaultMetricCategory.EntityTypeId = metricEntityTypeId;
                defaultMetricCategory.EntityTypeQualifierColumn = string.Empty;
                defaultMetricCategory.EntityTypeQualifierValue  = string.Empty;

                lookupContext.Categories.Add(defaultMetricCategory);
                lookupContext.SaveChanges();

                metricCategories.Add(defaultMetricCategory);
            }

            var metricValues = new List <MetricValue>();

            Metric currentMetric = null;
            int    completed     = 0;

            ReportProgress(0, string.Format("Starting metrics import ({0:N0} already exist).", 0));

            string[] row;
            // Uses a look-ahead enumerator: this call will move to the next record immediately
            while ((row = csvData.Database.FirstOrDefault()) != null)
            {
                string metricCampus   = row[MetricCampus];
                string metricName     = row[MetricName];
                string metricCategory = row[MetricCategory];

                if (!string.IsNullOrEmpty(metricName))
                {
                    decimal? value            = row[MetricValue].AsDecimalOrNull();
                    DateTime?valueDate        = row[MetricService].AsDateTime();
                    var      metricCategoryId = defaultMetricCategory.Id;

                    // create the category if it doesn't exist
                    Category newMetricCategory = null;
                    if (!string.IsNullOrEmpty(metricCategory))
                    {
                        newMetricCategory = metricCategories.FirstOrDefault(c => c.Name == metricCategory);
                        if (newMetricCategory == null)
                        {
                            newMetricCategory              = new Category();
                            newMetricCategory.Name         = metricCategory;
                            newMetricCategory.IsSystem     = false;
                            newMetricCategory.EntityTypeId = metricEntityTypeId;
                            newMetricCategory.EntityTypeQualifierColumn = string.Empty;
                            newMetricCategory.EntityTypeQualifierValue  = string.Empty;

                            lookupContext.Categories.Add(newMetricCategory);
                            lookupContext.SaveChanges();

                            metricCategories.Add(newMetricCategory);
                        }

                        metricCategoryId = newMetricCategory.Id;
                    }

                    if (valueDate.HasValue)
                    {
                        var timeFrame = ( DateTime )valueDate;
                        if (timeFrame.TimeOfDay.TotalSeconds > 0)
                        {
                            metricName = string.Format("{0} {1}", timeFrame.ToString("HH:mm"), metricName);
                        }
                    }

                    // create metric if it doesn't exist
                    currentMetric = allMetrics.FirstOrDefault(m => m.Title == metricName && m.MetricCategories.Any(c => c.CategoryId == metricCategoryId));
                    if (currentMetric == null)
                    {
                        currentMetric                        = new Metric();
                        currentMetric.Title                  = metricName;
                        currentMetric.IsSystem               = false;
                        currentMetric.IsCumulative           = false;
                        currentMetric.SourceSql              = string.Empty;
                        currentMetric.Subtitle               = string.Empty;
                        currentMetric.Description            = string.Empty;
                        currentMetric.IconCssClass           = string.Empty;
                        currentMetric.EntityTypeId           = campusEntityTypeId;
                        currentMetric.SourceValueTypeId      = metricManualSource.Id;
                        currentMetric.CreatedByPersonAliasId = ImportPersonAliasId;
                        currentMetric.CreatedDateTime        = ImportDateTime;
                        currentMetric.MetricCategories.Add(new MetricCategory {
                            CategoryId = metricCategoryId
                        });

                        lookupContext.Metrics.Add(currentMetric);
                        lookupContext.SaveChanges();

                        allMetrics.Add(currentMetric);
                    }

                    var campusId = campuses.Where(c => c.Name == metricCampus || c.ShortCode == metricCampus)
                                   .Select(c => ( int? )c.Id).FirstOrDefault();

                    // create values for this metric
                    var metricValue = new MetricValue();
                    metricValue.MetricValueType        = MetricValueType.Measure;
                    metricValue.CreatedByPersonAliasId = ImportPersonAliasId;
                    metricValue.CreatedDateTime        = ImportDateTime;
                    metricValue.MetricValueDateTime    = valueDate;
                    metricValue.MetricId = currentMetric.Id;
                    metricValue.EntityId = campusId;
                    metricValue.Note     = string.Empty;
                    metricValue.XValue   = string.Empty;
                    metricValue.YValue   = value;
                    metricValues.Add(metricValue);

                    completed++;
                    if (completed % (ReportingNumber * 10) < 1)
                    {
                        ReportProgress(0, string.Format("{0:N0} metrics imported.", completed));
                    }
                    else if (completed % ReportingNumber < 1)
                    {
                        SaveMetrics(metricValues);
                        ReportPartialProgress();

                        // Reset lookup context
                        lookupContext = new RockContext();
                        metricValues.Clear();
                    }
                }
            }

            // Check to see if any rows didn't get saved to the database
            if (metricValues.Any())
            {
                SaveMetrics(metricValues);
            }

            ReportProgress(0, string.Format("Finished metrics import: {0:N0} metrics added or updated.", completed));
            return(completed);
        }
Esempio n. 4
0
        /// <summary>
        /// Handles the Click event of the btnSaveValue control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSaveValue_Click( object sender, EventArgs e )
        {
            using ( new Rock.Data.UnitOfWorkScope() )
            {
                int metricValueId = hfMetricValueId.ValueAsInt();
                var metricValueService = new MetricValueService();
                MetricValue metricValue;

                if ( metricValueId == 0 )
                {
                    metricValue = new MetricValue();
                    metricValue.IsSystem = false;
                    metricValue.MetricId = hfMetricId.ValueAsInt();
                    metricValueService.Add( metricValue, CurrentPersonId );
                }
                else
                {
                    metricValue = metricValueService.Get( metricValueId );
                }

                metricValue.Value = tbValue.Text;
                metricValue.Description = tbValueDescription.Text;
                metricValue.xValue = tbXValue.Text;
                metricValue.Label = tbLabel.Text;
                metricValue.isDateBased = cbIsDateBased.Checked;
                metricValue.MetricId = Int32.Parse(ddlMetricFilter.SelectedValue);

                if ( metricValue.IsValid )
                {
                    metricValueService.Save( metricValue, CurrentPersonId );
                    BindGrid();
                    hfMetricValueId.Value = string.Empty;
                    mdValueDialog.Hide();
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Loads the family data.
        /// </summary>
        /// <param name="csvData">The CSV data.</param>
        private int LoadMetrics(CSVInstance csvData)
        {
            // Required variables
            var lookupContext         = new RockContext();
            var metricService         = new MetricService(lookupContext);
            var metricCategoryService = new MetricCategoryService(lookupContext);
            var categoryService       = new CategoryService(lookupContext);
            var metricSourceTypes     = DefinedTypeCache.Get(new Guid(Rock.SystemGuid.DefinedType.METRIC_SOURCE_TYPE)).DefinedValues;
            var metricManualSource    = metricSourceTypes.FirstOrDefault(m => m.Guid == new Guid(Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_MANUAL));

            var scheduleService = new ScheduleService(lookupContext);
            var scheduleMetrics = scheduleService.Queryable().AsNoTracking()
                                  .Where(s => s.Category.Guid == new Guid(Rock.SystemGuid.Category.SCHEDULE_SERVICE_TIMES)).ToList();
            var scheduleCategoryId = categoryService.Queryable().AsNoTracking()
                                     .Where(c => c.Guid == new Guid(Rock.SystemGuid.Category.SCHEDULE_SERVICE_TIMES)).FirstOrDefault().Id;

            var metricEntityTypeId   = EntityTypeCache.Get <MetricCategory>(false, lookupContext).Id;
            var campusEntityTypeId   = EntityTypeCache.Get <Campus>(false, lookupContext).Id;
            var scheduleEntityTypeId = EntityTypeCache.Get <Schedule>(false, lookupContext).Id;

            var allMetrics       = metricService.Queryable().AsNoTracking().ToList();
            var metricCategories = categoryService.Queryable().AsNoTracking()
                                   .Where(c => c.EntityType.Guid == new Guid(Rock.SystemGuid.EntityType.METRICCATEGORY)).ToList();

            var defaultMetricCategory = metricCategories.FirstOrDefault(c => c.Name == "Metrics");

            if (defaultMetricCategory == null)
            {
                defaultMetricCategory              = new Category();
                defaultMetricCategory.Name         = "Metrics";
                defaultMetricCategory.IsSystem     = false;
                defaultMetricCategory.EntityTypeId = metricEntityTypeId;
                defaultMetricCategory.EntityTypeQualifierColumn = string.Empty;
                defaultMetricCategory.EntityTypeQualifierValue  = string.Empty;
                defaultMetricCategory.IconCssClass = string.Empty;
                defaultMetricCategory.Description  = string.Empty;

                lookupContext.Categories.Add(defaultMetricCategory);
                lookupContext.SaveChanges();

                metricCategories.Add(defaultMetricCategory);
            }

            var metricValues = new List <MetricValue>();

            Metric currentMetric = null;
            int    completed     = 0;

            ReportProgress(0, string.Format("Starting metrics import ({0:N0} already exist).", 0));

            string[] row;
            // Uses a look-ahead enumerator: this call will move to the next record immediately
            while ((row = csvData.Database.FirstOrDefault()) != null)
            {
                string metricCampus         = row[MetricCampus];
                string metricName           = row[MetricName];
                string metricCategoryString = row[MetricCategory];
                string metricNote           = row[MetricNote];

                if (!string.IsNullOrEmpty(metricName))
                {
                    decimal? value            = row[MetricValue].AsDecimalOrNull();
                    DateTime?valueDate        = row[MetricService].AsDateTime();
                    var      metricCategoryId = defaultMetricCategory.Id;

                    // create the category if it doesn't exist
                    Category newMetricCategory = null;
                    if (!string.IsNullOrEmpty(metricCategoryString))
                    {
                        newMetricCategory = metricCategories.FirstOrDefault(c => c.Name == metricCategoryString);
                        if (newMetricCategory == null)
                        {
                            newMetricCategory              = new Category();
                            newMetricCategory.Name         = metricCategoryString;
                            newMetricCategory.IsSystem     = false;
                            newMetricCategory.EntityTypeId = metricEntityTypeId;
                            newMetricCategory.EntityTypeQualifierColumn = string.Empty;
                            newMetricCategory.EntityTypeQualifierValue  = string.Empty;
                            newMetricCategory.IconCssClass = string.Empty;
                            newMetricCategory.Description  = string.Empty;

                            lookupContext.Categories.Add(newMetricCategory);
                            lookupContext.SaveChanges();

                            metricCategories.Add(newMetricCategory);
                        }

                        metricCategoryId = newMetricCategory.Id;
                    }

                    // create metric if it doesn't exist
                    currentMetric = allMetrics.FirstOrDefault(m => m.Title == metricName && m.MetricCategories.Any(c => c.CategoryId == metricCategoryId));
                    if (currentMetric == null)
                    {
                        currentMetric                        = new Metric();
                        currentMetric.Title                  = metricName;
                        currentMetric.IsSystem               = false;
                        currentMetric.IsCumulative           = false;
                        currentMetric.SourceSql              = string.Empty;
                        currentMetric.Subtitle               = string.Empty;
                        currentMetric.Description            = string.Empty;
                        currentMetric.IconCssClass           = string.Empty;
                        currentMetric.SourceValueTypeId      = metricManualSource.Id;
                        currentMetric.CreatedByPersonAliasId = ImportPersonAliasId;
                        currentMetric.CreatedDateTime        = ImportDateTime;
                        currentMetric.ForeignKey             = string.Format("Metric imported {0}", ImportDateTime);

                        currentMetric.MetricPartitions = new List <MetricPartition>();
                        currentMetric.MetricPartitions.Add(new MetricPartition {
                            Label = "Campus", EntityTypeId = campusEntityTypeId, Metric = currentMetric
                        });
                        currentMetric.MetricPartitions.Add(new MetricPartition {
                            Label = "Service", EntityTypeId = scheduleEntityTypeId, Metric = currentMetric
                        });

                        metricService.Add(currentMetric);
                        lookupContext.SaveChanges();

                        if (currentMetric.MetricCategories == null || !currentMetric.MetricCategories.Any(a => a.CategoryId == metricCategoryId))
                        {
                            metricCategoryService.Add(new MetricCategory {
                                CategoryId = metricCategoryId, MetricId = currentMetric.Id
                            });
                            lookupContext.SaveChanges();
                        }

                        allMetrics.Add(currentMetric);
                    }

                    // create values for this metric
                    var metricValue = new MetricValue();
                    metricValue.MetricValueType        = MetricValueType.Measure;
                    metricValue.CreatedByPersonAliasId = ImportPersonAliasId;
                    metricValue.CreatedDateTime        = ImportDateTime;
                    metricValue.MetricValueDateTime    = valueDate;
                    metricValue.MetricId   = currentMetric.Id;
                    metricValue.Note       = string.Empty;
                    metricValue.XValue     = string.Empty;
                    metricValue.YValue     = value;
                    metricValue.ForeignKey = string.Format("Metric Value imported {0}", ImportDateTime);
                    metricValue.Note       = metricNote;

                    if (valueDate.HasValue)
                    {
                        var metricPartitionScheduleId = currentMetric.MetricPartitions.FirstOrDefault(p => p.Label == "Service").Id;

                        var date         = ( DateTime )valueDate;
                        var scheduleName = date.DayOfWeek.ToString();

                        if (date.TimeOfDay.TotalSeconds > 0)
                        {
                            scheduleName = scheduleName + string.Format(" {0}", date.ToString("hh:mm")) + string.Format("{0}", date.ToString("tt").ToLower());
                        }

                        if (!scheduleMetrics.Any(s => s.Name == scheduleName))
                        {
                            Schedule newSchedule = new Schedule();
                            newSchedule.Name                   = scheduleName;
                            newSchedule.CategoryId             = scheduleCategoryId;
                            newSchedule.CreatedByPersonAliasId = ImportPersonAliasId;
                            newSchedule.CreatedDateTime        = ImportDateTime;
                            newSchedule.ForeignKey             = string.Format("Metric Schedule imported {0}", ImportDateTime);

                            scheduleMetrics.Add(newSchedule);
                            lookupContext.Schedules.Add(newSchedule);
                            lookupContext.SaveChanges();
                        }

                        var scheduleId = scheduleMetrics.FirstOrDefault(s => s.Name == scheduleName).Id;

                        metricValue.MetricValuePartitions.Add(new MetricValuePartition {
                            MetricPartitionId = metricPartitionScheduleId, EntityId = scheduleId
                        });
                    }

                    if (!string.IsNullOrWhiteSpace(metricCampus))
                    {
                        var campus = CampusList.Where(c => c.Name.Equals(metricCampus, StringComparison.InvariantCultureIgnoreCase) ||
                                                      c.ShortCode.Equals(metricCampus, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

                        if (campus == null)
                        {
                            var newCampus = new Campus();
                            newCampus.IsSystem  = false;
                            newCampus.Name      = metricCampus;
                            newCampus.ShortCode = metricCampus.RemoveWhitespace();
                            newCampus.IsActive  = true;
                            lookupContext.Campuses.Add(newCampus);
                            lookupContext.SaveChanges(DisableAuditing);
                            CampusList.Add(newCampus);
                            campus = newCampus;
                        }

                        if (campus != null)
                        {
                            var metricPartitionCampusId = currentMetric.MetricPartitions.FirstOrDefault(p => p.Label == "Campus").Id;

                            metricValue.MetricValuePartitions.Add(new MetricValuePartition {
                                MetricPartitionId = metricPartitionCampusId, EntityId = campus.Id
                            });
                        }
                    }

                    metricValues.Add(metricValue);

                    completed++;
                    if (completed % (ReportingNumber * 10) < 1)
                    {
                        ReportProgress(0, string.Format("{0:N0} metrics imported.", completed));
                    }

                    if (completed % ReportingNumber < 1)
                    {
                        SaveMetrics(metricValues);
                        ReportPartialProgress();

                        metricValues.Clear();
                    }
                }
            }

            // Check to see if any rows didn't get saved to the database
            if (metricValues.Any())
            {
                SaveMetrics(metricValues);
            }

            ReportProgress(0, string.Format("Finished metrics import: {0:N0} metrics added or updated.", completed));
            return(completed);
        }
        private List <MetricValue> readMetricValuesIntoEntity(BIQEntityBase entity, JobTarget jobTarget, string metricName, AppDRESTMetric appDRESTMetric, int timerangeDuration)
        {
            List <MetricValue> metricValues = new List <MetricValue>(appDRESTMetric.metricValues.Count);

            foreach (AppDRESTMetricValue appDRESTMetricValue in appDRESTMetric.metricValues)
            {
                // Populate metrics into the list for output into CSV
                MetricValue metricValue = new MetricValue();

                metricValue.Controller      = jobTarget.Controller;
                metricValue.ApplicationID   = jobTarget.ApplicationID;
                metricValue.ApplicationName = jobTarget.Application;

                if (entity != null)
                {
                    if (entity is BIQMetric)
                    {
                        BIQMetric metric = (BIQMetric)entity;

                        try { metricValue.EntityID = Convert.ToInt64(metric.MetricID); } catch { }
                        metricValue.EntityName = metric.MetricName;
                        metricValue.EntityType = metric.DataSource;
                    }
                    else if (entity is BIQBusinessJourney)
                    {
                        BIQBusinessJourney businessJourney = (BIQBusinessJourney)entity;

                        // The JourneyID is a hash of some sorts. Let's turn it into unique value
                        metricValue.EntityID   = businessJourney.JourneyID.GetHashCode();
                        metricValue.EntityName = businessJourney.JourneyName;
                        metricValue.EntityType = businessJourney.KeyField;
                    }
                }

                metricValue.EventTimeStampUtc = UnixTimeHelper.ConvertFromUnixTimestamp(appDRESTMetricValue.startTimeInMillis);
                metricValue.EventTimeStamp    = metricValue.EventTimeStampUtc.ToLocalTime();
                metricValue.EventTime         = metricValue.EventTimeStamp;

                metricValue.MetricName = metricName;
                metricValue.MetricID   = appDRESTMetric.metricId;
                switch (appDRESTMetric.frequency)
                {
                case "SIXTY_MIN":
                {
                    metricValue.MetricResolution = 60;
                    break;
                }

                case "TEN_MIN":
                {
                    metricValue.MetricResolution = 10;
                    break;
                }

                case "ONE_MIN":
                {
                    metricValue.MetricResolution = 1;
                    break;
                }

                default:
                {
                    metricValue.MetricResolution = 1;
                    break;
                }
                }

                metricValue.Count       = appDRESTMetricValue.count;
                metricValue.Min         = appDRESTMetricValue.min;
                metricValue.Max         = appDRESTMetricValue.max;
                metricValue.Occurrences = appDRESTMetricValue.occurrences;
                metricValue.Sum         = appDRESTMetricValue.sum;
                metricValue.Value       = appDRESTMetricValue.value;

                metricValues.Add(metricValue);
            }

            return(metricValues);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates the dynamic controls.
        /// </summary>
        /// <param name="metricValue">The metric value.</param>
        /// <param name="setValues">if set to <c>true</c> [set values].</param>
        /// <param name="readOnly">if set to <c>true</c> [read only].</param>
        private void CreateDynamicControls( MetricValue metricValue, bool setValues, bool readOnly )
        {
            if ( metricValue != null )
            {
                foreach ( var metricPartition in metricValue.Metric.MetricPartitions )
                {
                    if ( metricPartition.EntityTypeId.HasValue )
                    {
                        var entityTypeCache = EntityTypeCache.Read( metricPartition.EntityTypeId.Value );
                        if ( entityTypeCache != null && entityTypeCache.SingleValueFieldType != null )
                        {
                            var fieldType = entityTypeCache.SingleValueFieldType;

                            Dictionary<string, Rock.Field.ConfigurationValue> configurationValues;
                            if ( fieldType.Field is IEntityQualifierFieldType )
                            {
                                configurationValues = ( fieldType.Field as IEntityQualifierFieldType ).GetConfigurationValuesFromEntityQualifier( metricPartition.EntityTypeQualifierColumn, metricPartition.EntityTypeQualifierValue );
                            }
                            else
                            {
                                configurationValues = new Dictionary<string, ConfigurationValue>();
                            }

                            var entityTypeEditControl = fieldType.Field.EditControl( configurationValues, string.Format( "metricPartition{0}_entityTypeEditControl", metricPartition.Id ) );
                            var panelCol4 = new Panel { CssClass = "col-md-4" };
                            if ( entityTypeEditControl != null && entityTypeEditControl is IRockControl )
                            {
                                panelCol4.Controls.Add( entityTypeEditControl );
                                phMetricValuePartitions.Controls.Add( panelCol4 );

                                var entityTypeRockControl = ( entityTypeEditControl as IRockControl );
                                entityTypeRockControl.Label = metricPartition.Label;
                                if ( entityTypeEditControl is WebControl )
                                {
                                    ( entityTypeEditControl as WebControl ).Enabled = !readOnly;
                                }

                                if ( setValues && metricValue.MetricValuePartitions != null )
                                {
                                    var metricValuePartition = metricValue.MetricValuePartitions.FirstOrDefault( a => a.MetricPartitionId == metricPartition.Id );
                                    if ( metricValuePartition != null )
                                    {
                                        if ( fieldType.Field is IEntityFieldType )
                                        {
                                            ( fieldType.Field as IEntityFieldType ).SetEditValueFromEntityId( entityTypeEditControl, new Dictionary<string, ConfigurationValue>(), metricValuePartition.EntityId );
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var errorControl = new LiteralControl();
                                errorControl.Text = string.Format( "<span class='label label-danger'>Unable to create Partition control for {0}. Verify that the metric partition settings are set correctly</span>", metricPartition.Label );
                                phMetricValuePartitions.Controls.Add( errorControl );
                            }
                        }
                    }
                }
            }
        }
 private static HostMetricValue ToHostMetricValue(string hostId, long time, MetricGroup metricGroup, MetricValue metricValue)
 {
     return(new HostMetricValue()
     {
         hostId = hostId,
         time = time,
         name = metricGroup.name + (string.IsNullOrEmpty(metricValue.name) ? "" : "." + metricValue.name.NoamalizeName()),
         value = metricValue.value
     });
 }
        /// <summary>
        /// Creates the dynamic controls.
        /// </summary>
        /// <param name="metricValue">The metric value.</param>
        /// <param name="setValues">if set to <c>true</c> [set values].</param>
        /// <param name="readOnly">if set to <c>true</c> [read only].</param>
        private void CreateDynamicControls(MetricValue metricValue, bool setValues, bool readOnly)
        {
            if (metricValue != null)
            {
                foreach (var metricPartition in metricValue.Metric.MetricPartitions)
                {
                    if (metricPartition.EntityTypeId.HasValue)
                    {
                        var entityTypeCache = EntityTypeCache.Get(metricPartition.EntityTypeId.Value);
                        if (entityTypeCache != null && entityTypeCache.SingleValueFieldType != null)
                        {
                            var fieldType = entityTypeCache.SingleValueFieldType;

                            Dictionary <string, Rock.Field.ConfigurationValue> configurationValues;
                            if (fieldType.Field is IEntityQualifierFieldType)
                            {
                                configurationValues = (fieldType.Field as IEntityQualifierFieldType).GetConfigurationValuesFromEntityQualifier(metricPartition.EntityTypeQualifierColumn, metricPartition.EntityTypeQualifierValue);
                            }
                            else
                            {
                                configurationValues = new Dictionary <string, ConfigurationValue>();
                            }

                            var entityTypeEditControl = fieldType.Field.EditControl(configurationValues, string.Format("metricPartition{0}_entityTypeEditControl", metricPartition.Id));
                            var panelCol4             = new Panel {
                                CssClass = "col-md-4"
                            };
                            if (entityTypeEditControl != null && entityTypeEditControl is IRockControl)
                            {
                                panelCol4.Controls.Add(entityTypeEditControl);
                                phMetricValuePartitions.Controls.Add(panelCol4);

                                var entityTypeRockControl = (entityTypeEditControl as IRockControl);
                                entityTypeRockControl.Label    = metricPartition.Label;
                                entityTypeRockControl.Required = metricPartition.IsRequired;
                                if (entityTypeEditControl is WebControl)
                                {
                                    (entityTypeEditControl as WebControl).Enabled = !readOnly;
                                }

                                if (setValues && metricValue.MetricValuePartitions != null)
                                {
                                    var metricValuePartition = metricValue.MetricValuePartitions.FirstOrDefault(a => a.MetricPartitionId == metricPartition.Id);
                                    if (metricValuePartition != null)
                                    {
                                        if (fieldType.Field is IEntityFieldType)
                                        {
                                            (fieldType.Field as IEntityFieldType).SetEditValueFromEntityId(entityTypeEditControl, new Dictionary <string, ConfigurationValue>(), metricValuePartition.EntityId);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var errorControl = new LiteralControl();
                                errorControl.Text = string.Format("<span class='label label-danger'>Unable to create Partition control for {0}. Verify that the metric partition settings are set correctly</span>", metricPartition.Label);
                                phMetricValuePartitions.Controls.Add(errorControl);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 10
0
 /// <summary>
 /// Gets the loaded configuration for the current metric instance.
 /// </summary>
 /// <param name="metric"></param>
 /// <returns></returns>
 private static MetricConfiguration GetMetricConfiguration(MetricValue metric)
 {
     return(Configuration.First(configuration => configuration.Class.Equals(metric.Component) && configuration.Name.Equals(metric.Name)));
 }
 private static IEnumerable <HostMetricValue> ToMetricValues(string hostId, long time, MetricGroup metricGroup, MetricValue metricValue)
 {
     return(metricGroup.metric.Select(x => ToHostMetricValue(hostId, time, metricGroup, x)));
 }
Esempio n. 12
0
 /// <summary>
 /// Save the specified metric with the givenn end datetime.
 /// </summary>
 /// <param name="metricValue"></param>
 /// <param name="end"></param>
 private static void SaveMetric(MetricValue metricValue, DateTime end)
 {
     Save(GetCurrentMetric(metricValue, end));
 }
Esempio n. 13
0
 /// <summary>
 /// Gets the current end for the specified metric.
 /// </summary>
 /// <param name="metricValue"></param>
 /// <param name="configuration"></param>
 /// <returns></returns>
 private static DateTime GetEnd(MetricValue metricValue, MetricConfiguration configuration)
 {
     return(metricValue.LastInstance.Subtract(metricValue.FirstInstance) >= configuration.Interval.TimeOfDay ? metricValue.LastInstance : DateTime.UtcNow);
 }
Esempio n. 14
0
        /// <summary>
        /// Loads the family data.
        /// </summary>
        /// <param name="csvData">The CSV data.</param>
        private int LoadMetrics( CSVInstance csvData )
        {
            // Required variables
            var lookupContext = new RockContext();
            var metricService = new MetricService( lookupContext );
            var categoryService = new CategoryService( lookupContext );
            var metricSourceTypes = DefinedTypeCache.Read( new Guid( Rock.SystemGuid.DefinedType.METRIC_SOURCE_TYPE ) ).DefinedValues;
            var metricManualSource = metricSourceTypes.FirstOrDefault( m => m.Guid == new Guid( Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_MANUAL ) );

            var metricEntityTypeId = EntityTypeCache.Read<Rock.Model.MetricCategory>( false, lookupContext ).Id;
            var campusEntityTypeId = EntityTypeCache.Read<Rock.Model.Campus>( false, lookupContext ).Id;

            var campuses = CampusCache.All();
            var allMetrics = metricService.Queryable().AsNoTracking().ToList();
            var metricCategories = categoryService.Queryable().AsNoTracking()
                .Where( c => c.EntityType.Guid == new Guid( Rock.SystemGuid.EntityType.METRICCATEGORY ) ).ToList();

            var defaultMetricCategory = metricCategories.FirstOrDefault( c => c.Name == "Metrics" );

            if ( defaultMetricCategory == null )
            {
                defaultMetricCategory = new Category();
                defaultMetricCategory.Name = "Metrics";
                defaultMetricCategory.IsSystem = false;
                defaultMetricCategory.EntityTypeId = metricEntityTypeId;
                defaultMetricCategory.EntityTypeQualifierColumn = string.Empty;
                defaultMetricCategory.EntityTypeQualifierValue = string.Empty;

                lookupContext.Categories.Add( defaultMetricCategory );
                lookupContext.SaveChanges();

                metricCategories.Add( defaultMetricCategory );
            }

            var metricValues = new List<MetricValue>();

            Metric currentMetric = null;
            int completed = 0;

            ReportProgress( 0, string.Format( "Starting metrics import ({0:N0} already exist).", 0 ) );

            string[] row;
            // Uses a look-ahead enumerator: this call will move to the next record immediately
            while ( (row = csvData.Database.FirstOrDefault()) != null )
            {
                string metricCampus = row[MetricCampus];
                string metricName = row[MetricName];
                string metricCategory = row[MetricCategory];

                if ( !string.IsNullOrEmpty( metricName ) )
                {
                    decimal? value = row[MetricValue].AsDecimalOrNull();
                    DateTime? valueDate = row[MetricService].AsDateTime();
                    var metricCategoryId = defaultMetricCategory.Id;

                    // create the category if it doesn't exist
                    Category newMetricCategory = null;
                    if ( !string.IsNullOrEmpty( metricCategory ) )
                    {
                        newMetricCategory = metricCategories.FirstOrDefault( c => c.Name == metricCategory );
                        if ( newMetricCategory == null )
                        {
                            newMetricCategory = new Category();
                            newMetricCategory.Name = metricCategory;
                            newMetricCategory.IsSystem = false;
                            newMetricCategory.EntityTypeId = metricEntityTypeId;
                            newMetricCategory.EntityTypeQualifierColumn = string.Empty;
                            newMetricCategory.EntityTypeQualifierValue = string.Empty;

                            lookupContext.Categories.Add( newMetricCategory );
                            lookupContext.SaveChanges();

                            metricCategories.Add( newMetricCategory );
                        }

                        metricCategoryId = newMetricCategory.Id;
                    }

                    if ( valueDate.HasValue )
                    {
                        var timeFrame = ( DateTime )valueDate;
                        if ( timeFrame.TimeOfDay.TotalSeconds > 0 )
                        {
                            metricName = string.Format( "{0} {1}", timeFrame.ToString( "HH:mm" ), metricName );
                        }
                    }

                    // create metric if it doesn't exist
                    currentMetric = allMetrics.FirstOrDefault( m => m.Title == metricName && m.MetricCategories.Any( c => c.CategoryId == metricCategoryId ) );
                    if ( currentMetric == null )
                    {
                        currentMetric = new Metric();
                        currentMetric.Title = metricName;
                        currentMetric.IsSystem = false;
                        currentMetric.IsCumulative = false;
                        currentMetric.SourceSql = string.Empty;
                        currentMetric.Subtitle = string.Empty;
                        currentMetric.Description = string.Empty;
                        currentMetric.IconCssClass = string.Empty;
                        currentMetric.EntityTypeId = campusEntityTypeId;
                        currentMetric.SourceValueTypeId = metricManualSource.Id;
                        currentMetric.CreatedByPersonAliasId = ImportPersonAliasId;
                        currentMetric.CreatedDateTime = ImportDateTime;
                        currentMetric.MetricCategories.Add( new MetricCategory { CategoryId = metricCategoryId } );

                        lookupContext.Metrics.Add( currentMetric );
                        lookupContext.SaveChanges();

                        allMetrics.Add( currentMetric );
                    }

                    var campusId = campuses.Where( c => c.Name == metricCampus || c.ShortCode == metricCampus )
                        .Select( c => ( int? )c.Id ).FirstOrDefault();

                    // create values for this metric
                    var metricValue = new MetricValue();
                    metricValue.MetricValueType = MetricValueType.Measure;
                    metricValue.CreatedByPersonAliasId = ImportPersonAliasId;
                    metricValue.CreatedDateTime = ImportDateTime;
                    metricValue.MetricValueDateTime = valueDate;
                    metricValue.MetricId = currentMetric.Id;
                    metricValue.EntityId = campusId;
                    metricValue.Note = string.Empty;
                    metricValue.XValue = string.Empty;
                    metricValue.YValue = value;
                    metricValues.Add( metricValue );

                    completed++;
                    if ( completed % (ReportingNumber * 10) < 1 )
                    {
                        ReportProgress( 0, string.Format( "{0:N0} metrics imported.", completed ) );
                    }
                    else if ( completed % ReportingNumber < 1 )
                    {
                        SaveMetrics( metricValues );
                        ReportPartialProgress();

                        // Reset lookup context
                        lookupContext = new RockContext();
                        metricValues.Clear();
                    }
                }
            }

            // Check to see if any rows didn't get saved to the database
            if ( metricValues.Any() )
            {
                SaveMetrics( metricValues );
            }

            ReportProgress( 0, string.Format( "Finished metrics import: {0:N0} metrics added or updated.", completed ) );
            return completed;
        }
Esempio n. 15
0
        public void SaveMetric(DateTime dt, int metric, Decimal value, int campus, string notes, int type)
        {
            int metricValueId = 0;
            RockContext rockContext = new RockContext();
            MetricValue metricValue;
            MetricValueService metricValueService = new MetricValueService(rockContext);

            //Does this metric already exist?
            var existingMetric = metricValueService
                .Queryable(
                    ).FirstOrDefault(a => a.MetricId == metric && a.MetricValueDateTime == dt && a.EntityId == campus);

            if (existingMetric != null)
            {
                metricValueId = existingMetric.Id;
            }

            if (metricValueId == 0)
            {
                metricValue = new MetricValue();
                metricValueService.Add(metricValue);
                metricValue.MetricId = metric;
                metricValue.Metric = metricValue.Metric ?? new MetricService(rockContext).Get(metricValue.MetricId);
            }
            else
            {
                metricValue = metricValueService.Get(metricValueId);
            }

            metricValue.MetricValueType = (MetricValueType)type;
            metricValue.XValue = null;
            metricValue.YValue = value;
            metricValue.Note = notes;
            metricValue.MetricValueDateTime = dt;
            metricValue.EntityId = campus;

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            MetricValue        metricValue;
            var                rockContext        = new RockContext();
            MetricValueService metricValueService = new MetricValueService(rockContext);

            int metricValueId = int.Parse(hfMetricValueId.Value);

            if (metricValueId == 0)
            {
                metricValue = new MetricValue();
                metricValueService.Add(metricValue);
                metricValue.MetricId = hfMetricId.ValueAsInt();
                metricValue.Metric   = metricValue.Metric ?? new MetricService(rockContext).Get(metricValue.MetricId);
                metricValue.MetricValuePartitions = new List <MetricValuePartition>();
            }
            else
            {
                metricValue = metricValueService.Get(metricValueId);
            }

            metricValue.MetricValueType     = ddlMetricValueType.SelectedValueAsEnum <MetricValueType>();
            metricValue.XValue              = tbXValue.Text;
            metricValue.YValue              = tbYValue.Text.AsDecimalOrNull();
            metricValue.Note                = tbNote.Text;
            metricValue.MetricValueDateTime = dpMetricValueDateTime.SelectedDate;

            // Get EntityId from EntityType UI controls
            foreach (var metricPartition in metricValue.Metric.MetricPartitions)
            {
                var     metricPartitionEntityType = EntityTypeCache.Get(metricPartition.EntityTypeId ?? 0);
                var     controlId             = string.Format("metricPartition{0}_entityTypeEditControl", metricPartition.Id);
                Control entityTypeEditControl = phMetricValuePartitions.FindControl(controlId);
                var     metricValuePartition  = metricValue.MetricValuePartitions.FirstOrDefault(a => a.MetricPartitionId == metricPartition.Id);
                if (metricValuePartition == null)
                {
                    metricValuePartition = new MetricValuePartition();
                    metricValuePartition.MetricPartitionId = metricPartition.Id;
                    metricValue.MetricValuePartitions.Add(metricValuePartition);
                }

                if (metricPartitionEntityType != null && metricPartitionEntityType.SingleValueFieldType != null && metricPartitionEntityType.SingleValueFieldType.Field is IEntityFieldType)
                {
                    metricValuePartition.EntityId = (metricPartitionEntityType.SingleValueFieldType.Field as IEntityFieldType).GetEditValueAsEntityId(entityTypeEditControl, new Dictionary <string, ConfigurationValue>());
                }
                else
                {
                    metricValuePartition.EntityId = null;
                }

                if (metricPartition.IsRequired && metricPartitionEntityType != null && !metricValuePartition.EntityId.HasValue)
                {
                    nbValueRequired.Text        = string.Format("A value for {0} is required", metricPartition.Label ?? metricPartitionEntityType.FriendlyName);
                    nbValueRequired.Dismissable = true;
                    nbValueRequired.Visible     = true;
                    return;
                }
            }


            if (!metricValue.IsValid)
            {
                // Controls will render the error messages
                return;
            }

            rockContext.SaveChanges();

            var qryParams = new Dictionary <string, string>();

            qryParams.Add("MetricId", hfMetricId.Value);
            qryParams.Add("MetricCategoryId", hfMetricCategoryId.Value);
            qryParams.Add("ExpandedIds", PageParameter("ExpandedIds"));
            NavigateToParentPage(qryParams);
        }
Esempio n. 17
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int campusEntityTypeId   = EntityTypeCache.Get(typeof(Rock.Model.Campus)).Id;
            int scheduleEntityTypeId = EntityTypeCache.Get(typeof(Rock.Model.Schedule)).Id;

            int?campusId = bddlCampus.SelectedValueAsInt();
            //int? scheduleId = bddlService.SelectedValueAsInt();
            DateTime?weekend = bddlWeekend.SelectedValue.AsDateTime();

            if (campusId.HasValue && weekend.HasValue)
            {
                using (var rockContext = new RockContext())
                {
                    var metricService      = new MetricService(rockContext);
                    var metricValueService = new MetricValueService(rockContext);

                    foreach (RepeaterItem category in rptrMetricCategory.Items)
                    {
                        var rptrMetric = category.FindControl("rptrMetric") as Repeater;
                        foreach (RepeaterItem item in rptrMetric.Items)
                        {
                            var hfMetricIId   = item.FindControl("hfMetricId") as HiddenField;
                            var nbMetricValue = item.FindControl("nbMetricValue") as NumberBox;

                            if (hfMetricIId != null && nbMetricValue != null && !string.IsNullOrEmpty(nbMetricValue.Text))
                            {
                                int metricId = hfMetricIId.ValueAsInt();
                                var metric   = new MetricService(rockContext).Get(metricId);

                                if (metric != null)
                                {
                                    int campusPartitionId = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId).Select(p => p.Id).FirstOrDefault();

                                    var metricValue = metricValueService
                                                      .Queryable()
                                                      .Where(v =>
                                                             v.MetricId == metric.Id &&
                                                             v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value &&
                                                             v.MetricValuePartitions.Count == 1 &&
                                                             v.MetricValuePartitions.Any(p => p.MetricPartitionId == campusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value))
                                                      .FirstOrDefault();

                                    if (metricValue == null)
                                    {
                                        metricValue = new MetricValue();
                                        metricValue.MetricValueType     = MetricValueType.Measure;
                                        metricValue.MetricId            = metric.Id;
                                        metricValue.MetricValueDateTime = weekend.Value;
                                        metricValueService.Add(metricValue);

                                        var campusValuePartition = new MetricValuePartition();
                                        campusValuePartition.MetricPartitionId = campusPartitionId;
                                        campusValuePartition.EntityId          = campusId.Value;
                                        metricValue.MetricValuePartitions.Add(campusValuePartition);
                                    }

                                    metricValue.YValue = nbMetricValue.Text.AsDecimalOrNull();
                                    metricValue.Note   = tbNote.Text;
                                }
                            }
                        }
                    }

                    foreach (RepeaterItem service in rptrService.Items)
                    {
                        var hfScheduleId      = service.FindControl("hfScheduleId") as HiddenField;
                        int scheduleId        = hfScheduleId.ValueAsInt();
                        var rptrServiceMetric = service.FindControl("pnlwService").FindControl("rptrServiceMetric") as Repeater;

                        foreach (RepeaterItem item in rptrServiceMetric.Items)
                        {
                            var hfMetricIId   = item.FindControl("hfMetricId") as HiddenField;
                            var nbMetricValue = item.FindControl("nbMetricValue") as NumberBox;

                            if (hfMetricIId != null && nbMetricValue != null && !string.IsNullOrEmpty(nbMetricValue.Text))
                            {
                                int metricId = hfMetricIId.ValueAsInt();
                                var metric   = new MetricService(rockContext).Get(metricId);

                                if (metric != null)
                                {
                                    int campusPartitionId   = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId).Select(p => p.Id).FirstOrDefault();
                                    int schedulePartitionId = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId).Select(p => p.Id).FirstOrDefault();

                                    var metricValue = metricValueService
                                                      .Queryable()
                                                      .Where(v =>
                                                             v.MetricId == metric.Id &&
                                                             v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value &&
                                                             v.MetricValuePartitions.Count == 2 &&
                                                             v.MetricValuePartitions.Any(p => p.MetricPartitionId == campusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value) &&
                                                             v.MetricValuePartitions.Any(p => p.MetricPartitionId == schedulePartitionId && p.EntityId.HasValue && p.EntityId.Value == scheduleId))
                                                      .FirstOrDefault();

                                    if (metricValue == null)
                                    {
                                        metricValue = new MetricValue();
                                        metricValue.MetricValueType     = MetricValueType.Measure;
                                        metricValue.MetricId            = metric.Id;
                                        metricValue.MetricValueDateTime = weekend.Value;
                                        metricValueService.Add(metricValue);

                                        var campusValuePartition = new MetricValuePartition();
                                        campusValuePartition.MetricPartitionId = campusPartitionId;
                                        campusValuePartition.EntityId          = campusId.Value;
                                        metricValue.MetricValuePartitions.Add(campusValuePartition);

                                        var scheduleValuePartition = new MetricValuePartition();
                                        scheduleValuePartition.MetricPartitionId = schedulePartitionId;
                                        scheduleValuePartition.EntityId          = scheduleId;
                                        metricValue.MetricValuePartitions.Add(scheduleValuePartition);
                                    }

                                    metricValue.YValue = nbMetricValue.Text.AsDecimalOrNull();
                                    metricValue.Note   = tbNote.Text;
                                }
                            }
                        }
                    }

                    rockContext.SaveChanges();
                }

                nbMetricsSaved.Text = string.Format("Your metrics for {0} at the {1} Campus have been saved.",
                                                    bddlWeekend.SelectedItem.Text, bddlCampus.SelectedItem.Text);
                nbMetricsSaved.Visible = true;

                BindMetrics();
            }
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="metricValueId">The metric value identifier.</param>
        /// <param name="metricId">The metric identifier.</param>
        public void ShowDetail(int metricValueId, int?metricId)
        {
            pnlDetails.Visible = true;

            // Load depending on Add(0) or Edit
            MetricValue metricValue = null;

            if (!metricValueId.Equals(0))
            {
                metricValue       = new MetricValueService(new RockContext()).Get(metricValueId);
                lActionTitle.Text = ActionTitle.Edit(MetricValue.FriendlyTypeName).FormatAsHtmlTitle();
                pdAuditDetails.SetEntity(metricValue, ResolveRockUrl("~"));
            }

            if (metricValue == null && metricId.HasValue)
            {
                metricValue = new MetricValue {
                    Id = 0, MetricId = metricId.Value
                };
                metricValue.Metric = metricValue.Metric ?? new MetricService(new RockContext()).Get(metricValue.MetricId);
                lActionTitle.Text  = ActionTitle.Add(MetricValue.FriendlyTypeName).FormatAsHtmlTitle();
                // hide the panel drawer that show created and last modified dates
                pdAuditDetails.Visible = false;
            }

            hfMetricValueId.Value = metricValue.Id.ToString();

            LoadDropDowns();

            ddlMetricValueType.SelectedValue = metricValue.MetricValueType.ConvertToInt().ToString();
            tbXValue.Text    = metricValue.XValue;
            tbYValue.Text    = metricValue.YValue.ToString();
            hfMetricId.Value = metricValue.MetricId.ToString();
            tbNote.Text      = metricValue.Note;
            dpMetricValueDateTime.SelectedDate = metricValue.MetricValueDateTime;

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;

            bool canEdit = UserCanEdit;

            if (!canEdit && metricId.HasValue && metricId.Value > 0)
            {
                var metric = new MetricService(new RockContext()).Get(metricId.Value);
                if (metric != null && metric.IsAuthorized(Authorization.EDIT, CurrentPerson))
                {
                    canEdit = true;
                }
            }

            if (!canEdit)
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(MetricValue.FriendlyTypeName);
            }

            if (readOnly)
            {
                lActionTitle.Text = ActionTitle.View(MetricValue.FriendlyTypeName);
                btnCancel.Text    = "Close";
            }

            CreateDynamicControls(metricValue, true, readOnly);

            ddlMetricValueType.Enabled    = !readOnly;
            tbXValue.ReadOnly             = readOnly;
            tbYValue.ReadOnly             = readOnly;
            tbNote.ReadOnly               = readOnly;
            dpMetricValueDateTime.Enabled = !readOnly;

            btnSave.Visible = !readOnly;
        }
Esempio n. 19
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            MetricValue metricValue;
            var rockContext = new RockContext();
            MetricValueService metricValueService = new MetricValueService( rockContext );

            int metricValueId = int.Parse( hfMetricValueId.Value );

            if ( metricValueId == 0 )
            {
                metricValue = new MetricValue();
                metricValueService.Add( metricValue );
                metricValue.MetricId = hfMetricId.ValueAsInt();
                metricValue.Metric = metricValue.Metric ?? new MetricService( rockContext ).Get( metricValue.MetricId );
                metricValue.MetricValuePartitions = new List<MetricValuePartition>();
            }
            else
            {
                metricValue = metricValueService.Get( metricValueId );
            }

            metricValue.MetricValueType = ddlMetricValueType.SelectedValueAsEnum<MetricValueType>();
            metricValue.XValue = tbXValue.Text;
            metricValue.YValue = tbYValue.Text.AsDecimalOrNull();
            metricValue.Note = tbNote.Text;
            metricValue.MetricValueDateTime = dpMetricValueDateTime.SelectedDate;

            // Get EntityId from EntityType UI controls
            foreach ( var metricPartition in metricValue.Metric.MetricPartitions )
            {
                var metricPartitionEntityType = EntityTypeCache.Read( metricPartition.EntityTypeId ?? 0 );
                var controlId = string.Format( "metricPartition{0}_entityTypeEditControl", metricPartition.Id );
                Control entityTypeEditControl = phMetricValuePartitions.FindControl( controlId );
                var metricValuePartition = metricValue.MetricValuePartitions.FirstOrDefault( a => a.MetricPartitionId == metricPartition.Id );
                if ( metricValuePartition == null )
                {
                    metricValuePartition = new MetricValuePartition();
                    metricValuePartition.MetricPartitionId = metricPartition.Id;
                    metricValue.MetricValuePartitions.Add( metricValuePartition );
                }

                if ( metricPartitionEntityType != null && metricPartitionEntityType.SingleValueFieldType != null && metricPartitionEntityType.SingleValueFieldType.Field is IEntityFieldType )
                {
                    metricValuePartition.EntityId = ( metricPartitionEntityType.SingleValueFieldType.Field as IEntityFieldType ).GetEditValueAsEntityId( entityTypeEditControl, new Dictionary<string, ConfigurationValue>() );
                }
                else
                {
                    metricValuePartition.EntityId = null;
                }

                if ( metricPartition.IsRequired && metricPartitionEntityType != null && !metricValuePartition.EntityId.HasValue )
                {
                    nbValueRequired.Text = string.Format( "A value for {0} is required", metricPartition.Label ?? metricPartitionEntityType.FriendlyName );
                    nbValueRequired.Dismissable = true;
                    nbValueRequired.Visible = true;
                    return;
                }
            }

            if ( !metricValue.IsValid )
            {
                // Controls will render the error messages
                return;
            }

            rockContext.SaveChanges();

            var qryParams = new Dictionary<string, string>();
            qryParams.Add( "MetricId", hfMetricId.Value );
            qryParams.Add( "MetricCategoryId", hfMetricCategoryId.Value );
            qryParams.Add( "ExpandedIds", PageParameter( "ExpandedIds" ) );
            NavigateToParentPage( qryParams );
        }
Esempio n. 20
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            var rockContext        = new RockContext();
            var metricService      = new MetricService(rockContext);
            var metricValueService = new MetricValueService(rockContext);

            var metricSourceValueTypeDataviewGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_DATAVIEW.AsGuid();
            var metricSourceValueTypeSqlGuid      = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_SQL.AsGuid();

            var metricsQry = metricService.Queryable("Schedule").Where(
                a => a.ScheduleId.HasValue &&
                a.SourceValueTypeId.HasValue &&
                (a.SourceValueType.Guid == metricSourceValueTypeDataviewGuid || a.SourceValueType.Guid == metricSourceValueTypeSqlGuid));

            var metricsList = metricsQry.OrderBy(a => a.Title).ThenBy(a => a.Subtitle).ToList();

            var metricExceptions = new List <Exception>();

            foreach (var metric in metricsList)
            {
                try
                {
                    var lastRunDateTime = metric.LastRunDateTime ?? metric.CreatedDateTime ?? metric.ModifiedDateTime;
                    if (lastRunDateTime.HasValue)
                    {
                        var currentDateTime = RockDateTime.Now;
                        // get all the schedule times that were supposed to run since that last time it was scheduled to run
                        var scheduledDateTimesToProcess = metric.Schedule.GetScheduledStartTimes(lastRunDateTime.Value, currentDateTime).Where(a => a > lastRunDateTime.Value).ToList();
                        foreach (var scheduleDateTime in scheduledDateTimesToProcess)
                        {
                            Dictionary <int, decimal> resultValues = new Dictionary <int, decimal>();
                            if (metric.SourceValueType.Guid == metricSourceValueTypeDataviewGuid)
                            {
                                // get the metric value from the DataView
                                if (metric.DataView != null)
                                {
                                    var errorMessages = new List <string>();
                                    var qry           = metric.DataView.GetQuery(null, null, out errorMessages);
                                    if (metric.EntityTypeId.HasValue)
                                    {
                                        throw new NotImplementedException("Partitioned Metrics using DataViews is not supported.");
                                    }
                                    else
                                    {
                                        resultValues.Add(0, qry.Count());
                                    }
                                }
                            }
                            else if (metric.SourceValueType.Guid == metricSourceValueTypeSqlGuid)
                            {
                                // calculate the metricValue assuming that the SQL returns one row with one numeric field
                                if (!string.IsNullOrWhiteSpace(metric.SourceSql))
                                {
                                    string formattedSql = metric.SourceSql.ResolveMergeFields(metric.GetMergeObjects(scheduleDateTime));
                                    var    tableResult  = DbService.GetDataTable(formattedSql, System.Data.CommandType.Text, null);
                                    foreach (var row in tableResult.Rows.OfType <System.Data.DataRow>())
                                    {
                                        int     entityId = 0;
                                        decimal countValue;
                                        if (tableResult.Columns.Count >= 2)
                                        {
                                            if (tableResult.Columns.Contains("EntityId"))
                                            {
                                                entityId = Convert.ToInt32(row["EntityId"]);
                                            }
                                            else
                                            {
                                                // assume SQL is in the form "SELECT Count(*), EntityId FROM ..."
                                                entityId = Convert.ToInt32(row[1]);
                                            }
                                        }

                                        if (tableResult.Columns.Contains("Value"))
                                        {
                                            countValue = Convert.ToDecimal(row["Value"]);
                                        }
                                        else
                                        {
                                            // assume SQL is in the form "SELECT Count(*), EntityId FROM ..."
                                            countValue = Convert.ToDecimal(row[0]);
                                        }

                                        resultValues.Add(entityId, countValue);
                                    }
                                }
                            }

                            metric.LastRunDateTime = scheduleDateTime;

                            if (resultValues.Any())
                            {
                                foreach (var resultValue in resultValues)
                                {
                                    var metricValue = new MetricValue();
                                    metricValue.MetricId            = metric.Id;
                                    metricValue.MetricValueDateTime = scheduleDateTime;
                                    metricValue.MetricValueType     = MetricValueType.Measure;
                                    metricValue.YValue   = resultValue.Value;
                                    metricValue.EntityId = resultValue.Key > 0 ? resultValue.Key : (int?)null;

                                    metricValueService.Add(metricValue);
                                }
                            }

                            rockContext.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    metricExceptions.Add(new Exception(string.Format("Exception when calculating metric for ", metric), ex));
                }
            }

            if (metricExceptions.Any())
            {
                throw new AggregateException("One or more metric calculations failed ", metricExceptions);
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            MetricValue metricValue;
            var rockContext = new RockContext();
            MetricValueService metricValueService = new MetricValueService( rockContext );

            int metricValueId = int.Parse( hfMetricValueId.Value );

            if ( metricValueId == 0 )
            {
                metricValue = new MetricValue();
                metricValueService.Add( metricValue );
                metricValue.MetricId = hfMetricId.ValueAsInt();
                metricValue.Metric = metricValue.Metric ?? new MetricService( rockContext ).Get( metricValue.MetricId );
            }
            else
            {
                metricValue = metricValueService.Get( metricValueId );
            }

            metricValue.MetricValueType = ddlMetricValueType.SelectedValueAsEnum<MetricValueType>();
            metricValue.XValue = tbXValue.Text;
            metricValue.YValue = tbYValue.Text.AsDecimalOrNull();
            metricValue.Note = tbNote.Text;
            metricValue.MetricValueDateTime = dpMetricValueDateTime.SelectedDate;

            // Get EntityId from EntityType UI controls
            var metricEntityType = EntityTypeCache.Read( metricValue.Metric.EntityTypeId ?? 0 );
            Control entityTypeEditControl = phEntityTypeEntityIdValue.FindControl( "entityTypeEditControl" );
            if ( metricEntityType != null && metricEntityType.SingleValueFieldType != null && metricEntityType.SingleValueFieldType.Field is IEntityFieldType )
            {
                metricValue.EntityId = ( metricEntityType.SingleValueFieldType.Field as IEntityFieldType ).GetEditValueAsEntityId( entityTypeEditControl, new Dictionary<string, ConfigurationValue>() );
            }
            else
            {
                metricValue.EntityId = null;
            }

            if ( !metricValue.IsValid )
            {
                // Controls will render the error messages
                return;
            }

            rockContext.SaveChanges();

            var qryParams = new Dictionary<string, string>();
            qryParams.Add( "MetricId", hfMetricId.Value );
            qryParams.Add( "MetricCategoryId", hfMetricCategoryId.Value );
            qryParams.Add( "ExpandedIds", PageParameter( "ExpandedIds" ) );
            NavigateToParentPage( qryParams );
        }
Esempio n. 22
0
 public bool CollectMetric(string metricKey, MetricValue value)
 {
     throw new NotImplementedException();
 }
Esempio n. 23
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            int campusEntityTypeId = EntityTypeCache.Read( typeof( Rock.Model.Campus ) ).Id;
            int scheduleEntityTypeId = EntityTypeCache.Read( typeof( Rock.Model.Schedule ) ).Id;

            int? campusId = bddlCampus.SelectedValueAsInt();
            int? scheduleId = bddlService.SelectedValueAsInt();
            DateTime? weekend = bddlWeekend.SelectedValue.AsDateTime();

            if ( campusId.HasValue && scheduleId.HasValue && weekend.HasValue )
            {
                using ( var rockContext = new RockContext() )
                {
                    var metricService = new MetricService( rockContext );
                    var metricValueService = new MetricValueService( rockContext );

                    foreach ( RepeaterItem item in rptrMetric.Items )
                    {
                        var hfMetricIId = item.FindControl( "hfMetricId" ) as HiddenField;
                        var nbMetricValue = item.FindControl( "nbMetricValue" ) as NumberBox;

                        if ( hfMetricIId != null && nbMetricValue != null  )
                        {
                            int metricId = hfMetricIId.ValueAsInt();
                            var metric = new MetricService( rockContext ).Get( metricId );

                            if ( metric != null )
                            {
                                int campusPartitionId = metric.MetricPartitions.Where( p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId ).Select( p => p.Id ).FirstOrDefault();
                                int schedulePartitionId = metric.MetricPartitions.Where( p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId ).Select( p => p.Id ).FirstOrDefault();

                                var metricValue = metricValueService
                                    .Queryable()
                                    .Where( v =>
                                        v.MetricId == metric.Id &&
                                        v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value &&
                                        v.MetricValuePartitions.Count == 2 &&
                                        v.MetricValuePartitions.Any( p => p.MetricPartitionId == campusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value ) &&
                                        v.MetricValuePartitions.Any( p => p.MetricPartitionId == schedulePartitionId && p.EntityId.HasValue && p.EntityId.Value == scheduleId.Value ) )
                                    .FirstOrDefault();

                                if ( metricValue == null )
                                {
                                    metricValue = new MetricValue();
                                    metricValue.MetricValueType = MetricValueType.Measure;
                                    metricValue.MetricId = metric.Id;
                                    metricValue.MetricValueDateTime = weekend.Value;
                                    metricValueService.Add( metricValue );

                                    var campusValuePartition = new MetricValuePartition();
                                    campusValuePartition.MetricPartitionId = campusPartitionId;
                                    campusValuePartition.EntityId = campusId.Value;
                                    metricValue.MetricValuePartitions.Add( campusValuePartition );

                                    var scheduleValuePartition = new MetricValuePartition();
                                    scheduleValuePartition.MetricPartitionId = schedulePartitionId;
                                    scheduleValuePartition.EntityId = scheduleId.Value;
                                    metricValue.MetricValuePartitions.Add( scheduleValuePartition );
                                }

                                metricValue.YValue = nbMetricValue.Text.AsDecimalOrNull();
                                metricValue.Note = tbNote.Text;
                            }
                        }
                    }

                    rockContext.SaveChanges();
                }

                nbMetricsSaved.Text = string.Format( "Your metrics for the {0} service on {1} at the {2} Campus have been saved.",
                    bddlService.SelectedItem.Text, bddlWeekend.SelectedItem.Text, bddlCampus.SelectedItem.Text );
                nbMetricsSaved.Visible = true;

                BindMetrics();

            }
        }
Esempio n. 24
0
 public Task <bool> CollectMetricAsync(string metricKey, MetricValue value)
 {
     throw new NotImplementedException();
 }
Esempio n. 25
0
        // Generates queries for all metrics by timestamp (timestamp-name rowKey format) and filters the results to the requested metrics (if any)
        // Note: Does not populate Metric fields unrelated to query (i.e. "display name", resourceUri, and properties)
        private static async Task <MetricCollection> GetMetricsByTimestampAsync(MetricFilter filter, MetricLocation location, string invocationId)
        {
            // Find all the tables that fall partially or fully within the timerange
            IEnumerable <CloudTable> tables = GetNdayTables(filter, location);

            // Generate a query for the partition key and time range
            TableQuery query = GenerateMetricTimestampQuery(location.PartitionKey, filter.StartTime, filter.EndTime);

            // Get all the entities for the query
            IEnumerable <DynamicTableEntity> entities = await GetEntitiesAsync(tables, query, invocationId).ConfigureAwait(false);

            ICollection <string> dimensionFilterNames = null;

            if (filter.DimensionFilters != null)
            {
                dimensionFilterNames = new HashSet <string>(filter.DimensionFilters.Select(df => ShoeboxHelper.TrimAndEscapeKey(df.Name)));
            }

            var metricWraps = new Dictionary <string, MetricWrap>();
            var metrics     = new List <Metric>();

            // Iterate over the instances to do conversion and aggregation when needed.
            foreach (var entity in entities)
            {
                string encodedName = GetMetricNameFromRowKeyByTimestampByMetricName(entity.RowKey);

                // When there is filter, skip entities not included in the filter.
                if (dimensionFilterNames != null && !dimensionFilterNames.Contains(encodedName, StringComparer.OrdinalIgnoreCase))
                {
                    continue;
                }

                MetricWrap metricWrap;
                if (!metricWraps.TryGetValue(encodedName, out metricWrap))
                {
                    metricWrap = new MetricWrap
                    {
                        Metric = new Metric()
                        {
                            Name = new LocalizableString()
                            {
                                Value = encodedName
                            },
                            StartTime    = filter.StartTime,
                            EndTime      = filter.EndTime,
                            TimeGrain    = filter.TimeGrain,
                            MetricValues = new List <MetricValue>()
                        },
                        InstanceMetrics = new List <MetricValue>(),
                        GlobalMetrics   = new List <DynamicTableEntity>()
                    };

                    metricWraps[encodedName] = metricWrap;
                    metrics.Add(metricWrap.Metric);
                }

                // Skip aggregated entities
                if (!IsInstanceMetric(entity.RowKey))
                {
                    // We ignore the aggergated metrics if there are instance metrics.
                    if (metricWrap.InstanceMetrics.Count == 0)
                    {
                        metricWrap.GlobalMetrics.Add(entity);
                    }

                    continue;
                }

                MetricValue lastMetricValue = metricWrap.InstanceMetrics.LastOrDefault();
                if (lastMetricValue == null)
                {
                    metricWrap.InstanceMetrics.Add(ResolveMetricEntity(entity));
                }
                else
                {
                    if (lastMetricValue.Timestamp.Ticks == GetTimestampFromIndexTimestampMetricName(entity))
                    {
                        Aggregate(lastMetricValue, entity);
                    }
                    else
                    {
                        metricWrap.InstanceMetrics.Add(ResolveMetricEntity(entity));
                    }
                }
            }

            foreach (var metricWrap in metricWraps.Values)
            {
                // Decide whether to return the aggregation of the instance metrics on the fly or the final value in the storage account
                // If there are instance metrics, the aggregation on the fly is used.
                Metric metric = metricWrap.Metric;
                metric.Name.Value = FindMetricName(metric.Name.Value, dimensionFilterNames);
                if (metricWrap.InstanceMetrics.Count > 0)
                {
                    foreach (var metricValue in metricWrap.InstanceMetrics)
                    {
                        metricValue.Average = metricValue.Total / metricValue.Count;
                    }

                    metric.MetricValues = metricWrap.InstanceMetrics;
                }
                else
                {
                    metric.MetricValues = metricWrap.GlobalMetrics.Select(me => ResolveMetricEntity(me)).ToList();
                }
            }

            return(new MetricCollection()
            {
                Value = metrics
            });
        }
Esempio n. 26
0
 public void create(MetricValue value)
 {
     values.Add(value);
 }
Esempio n. 27
0
        // Converts a TableEntity to a MetricValue object
        // TODO: this needs to be made more robust to handle types properly and do conversions as needed
        private static MetricValue ResolveMetricEntity(DynamicTableEntity entity)
        {
            Dictionary <string, string> otherProperties = new Dictionary <string, string>();
            MetricValue metricValue = new MetricValue();

            EntityProperty timestamp = entity["TIMESTAMP"];

            switch (timestamp.PropertyType)
            {
            case EdmType.DateTime:
                metricValue.Timestamp = timestamp.DateTime ?? entity.Timestamp.UtcDateTime;
                break;

            case EdmType.String:
                DateTime value;
                if (DateTime.TryParse(timestamp.StringValue, out value))
                {
                    metricValue.Timestamp = value;
                }

                break;

            default:
                metricValue.Timestamp = entity.Timestamp.UtcDateTime;
                break;
            }

            foreach (string key in entity.Properties.Keys)
            {
                switch (key)
                {
                case "Average":
                    metricValue.Average = entity[key].DoubleValue;
                    break;

                case "Minimum":
                    metricValue.Minimum = entity[key].DoubleValue;
                    break;

                case "Maximum":
                    metricValue.Maximum = entity[key].DoubleValue;
                    break;

                case "Total":
                    metricValue.Total = entity[key].DoubleValue;
                    break;

                case "Count":
                    metricValue.Count = entity[key].PropertyType == EdmType.Int64 ? entity[key].Int64Value : entity[key].Int32Value;
                    break;

                case "Last":
                    metricValue.Last = entity[key].DoubleValue;
                    break;

                default:
                    // if it is a string then store it in the properties
                    if (entity[key].PropertyType == EdmType.String)
                    {
                        otherProperties.Add(key, entity[key].StringValue);
                    }

                    break;
                }
            }

            metricValue.Properties = otherProperties;
            return(metricValue);
        }
Esempio n. 28
0
 public void remove(MetricValue value)
 {
     values.Remove(value);
 }
Esempio n. 29
0
        /// <summary>
        /// The List Metric Value operation lists the metric value sets for the
        /// resource metrics.
        /// </summary>
        /// <param name='resourceId'>
        /// The id of the resource.
        /// </param>
        /// <param name='metricNames'>
        /// The names of the metrics.
        /// </param>
        /// <param name='metricNamespace'>
        /// The namespace of the metrics.
        /// </param>
        /// <param name='timeGrain'>
        /// The time grain of the metrics.
        /// </param>
        /// <param name='startTime'>
        /// The start time (in UTC) of the metrics.
        /// </param>
        /// <param name='endTime'>
        /// The end time (in UTC) of the metrics.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Metric values operation response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Monitoring.Metrics.Models.MetricValueListResponse> ListAsync(string resourceId, IList <string> metricNames, string metricNamespace, TimeSpan timeGrain, DateTime startTime, DateTime endTime, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceId == null)
            {
                throw new ArgumentNullException("resourceId");
            }
            if (metricNames == null)
            {
                throw new ArgumentNullException("metricNames");
            }
            if (metricNames.Count <= 0)
            {
                throw new ArgumentException("metricNames cannot be empty.");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceId", resourceId);
                tracingParameters.Add("metricNames", metricNames);
                tracingParameters.Add("metricNamespace", metricNamespace);
                tracingParameters.Add("timeGrain", timeGrain);
                tracingParameters.Add("startTime", startTime);
                tracingParameters.Add("endTime", endTime);
                Tracing.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = new Uri(this.Client.BaseUri, "/").AbsoluteUri + this.Client.Credentials.SubscriptionId + "/services/monitoring/metricvalues/query?";

            url = url + "&resourceId=" + Uri.EscapeUriString(resourceId);
            if (metricNamespace != null)
            {
                url = url + "&namespace=" + Uri.EscapeUriString(metricNamespace);
            }
            url = url + "&names=" + Uri.EscapeUriString(string.Join(",", metricNames));
            url = url + "&timeGrain=" + Uri.EscapeUriString(TypeConversion.To8601String(timeGrain));
            url = url + "&startTime=" + Uri.EscapeUriString(string.Format(CultureInfo.InvariantCulture, "{0:O}", startTime.ToUniversalTime()));
            url = url + "&endTime=" + Uri.EscapeUriString(string.Format(CultureInfo.InvariantCulture, "{0:O}", endTime.ToUniversalTime()));

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2013-10-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Json);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MetricValueListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new MetricValueListResponse();
                    JToken responseDoc = JToken.Parse(responseContent);

                    if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                    {
                        MetricValueSetCollection metricValueSetCollectionInstance = new MetricValueSetCollection();
                        result.MetricValueSetCollection = metricValueSetCollectionInstance;

                        JToken valueArray = responseDoc["Value"];
                        if (valueArray != null && valueArray.Type != JTokenType.Null)
                        {
                            foreach (JToken valueValue in (JArray)valueArray)
                            {
                                MetricValueSet metricValueSetInstance = new MetricValueSet();
                                metricValueSetCollectionInstance.Value.Add(metricValueSetInstance);

                                JToken nameValue = valueValue["Name"];
                                if (nameValue != null && nameValue.Type != JTokenType.Null)
                                {
                                    string nameInstance = (string)nameValue;
                                    metricValueSetInstance.Name = nameInstance;
                                }

                                JToken namespaceValue = valueValue["Namespace"];
                                if (namespaceValue != null && namespaceValue.Type != JTokenType.Null)
                                {
                                    string namespaceInstance = (string)namespaceValue;
                                    metricValueSetInstance.Namespace = namespaceInstance;
                                }

                                JToken displayNameValue = valueValue["DisplayName"];
                                if (displayNameValue != null && displayNameValue.Type != JTokenType.Null)
                                {
                                    string displayNameInstance = (string)displayNameValue;
                                    metricValueSetInstance.DisplayName = displayNameInstance;
                                }

                                JToken unitValue = valueValue["Unit"];
                                if (unitValue != null && unitValue.Type != JTokenType.Null)
                                {
                                    string unitInstance = (string)unitValue;
                                    metricValueSetInstance.Unit = unitInstance;
                                }

                                JToken primaryAggregationValue = valueValue["PrimaryAggregation"];
                                if (primaryAggregationValue != null && primaryAggregationValue.Type != JTokenType.Null)
                                {
                                    string primaryAggregationInstance = (string)primaryAggregationValue;
                                    metricValueSetInstance.PrimaryAggregation = primaryAggregationInstance;
                                }

                                JToken timeGrainValue = valueValue["TimeGrain"];
                                if (timeGrainValue != null && timeGrainValue.Type != JTokenType.Null)
                                {
                                    TimeSpan timeGrainInstance = TypeConversion.From8601TimeSpan((string)timeGrainValue);
                                    metricValueSetInstance.TimeGrain = timeGrainInstance;
                                }

                                JToken startTimeValue = valueValue["StartTime"];
                                if (startTimeValue != null && startTimeValue.Type != JTokenType.Null)
                                {
                                    DateTime startTimeInstance = (DateTime)startTimeValue;
                                    metricValueSetInstance.StartTime = startTimeInstance;
                                }

                                JToken endTimeValue = valueValue["EndTime"];
                                if (endTimeValue != null && endTimeValue.Type != JTokenType.Null)
                                {
                                    DateTime endTimeInstance = (DateTime)endTimeValue;
                                    metricValueSetInstance.EndTime = endTimeInstance;
                                }

                                JToken metricValuesArray = valueValue["MetricValues"];
                                if (metricValuesArray != null && metricValuesArray.Type != JTokenType.Null)
                                {
                                    foreach (JToken metricValuesValue in (JArray)metricValuesArray)
                                    {
                                        MetricValue metricValueInstance = new MetricValue();
                                        metricValueSetInstance.MetricValues.Add(metricValueInstance);

                                        JToken timestampValue = metricValuesValue["Timestamp"];
                                        if (timestampValue != null && timestampValue.Type != JTokenType.Null)
                                        {
                                            DateTime timestampInstance = (DateTime)timestampValue;
                                            metricValueInstance.Timestamp = timestampInstance;
                                        }

                                        JToken averageValue = metricValuesValue["Average"];
                                        if (averageValue != null && averageValue.Type != JTokenType.Null)
                                        {
                                            double averageInstance = (double)averageValue;
                                            metricValueInstance.Average = averageInstance;
                                        }

                                        JToken minimumValue = metricValuesValue["Minimum"];
                                        if (minimumValue != null && minimumValue.Type != JTokenType.Null)
                                        {
                                            double minimumInstance = (double)minimumValue;
                                            metricValueInstance.Minimum = minimumInstance;
                                        }

                                        JToken maximumValue = metricValuesValue["Maximum"];
                                        if (maximumValue != null && maximumValue.Type != JTokenType.Null)
                                        {
                                            double maximumInstance = (double)maximumValue;
                                            metricValueInstance.Maximum = maximumInstance;
                                        }

                                        JToken totalValue = metricValuesValue["Total"];
                                        if (totalValue != null && totalValue.Type != JTokenType.Null)
                                        {
                                            double totalInstance = (double)totalValue;
                                            metricValueInstance.Total = totalInstance;
                                        }

                                        JToken annotationValue = metricValuesValue["Annotation"];
                                        if (annotationValue != null && annotationValue.Type != JTokenType.Null)
                                        {
                                            string annotationInstance = (string)annotationValue;
                                            metricValueInstance.Annotation = annotationInstance;
                                        }

                                        JToken countValue = metricValuesValue["Count"];
                                        if (countValue != null && countValue.Type != JTokenType.Null)
                                        {
                                            int countInstance = (int)countValue;
                                            metricValueInstance.Count = countInstance;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int groupEntityTypeId  = EntityTypeCache.Get(typeof(Rock.Model.Group)).Id;
            int campusEntityTypeId = EntityTypeCache.Get(typeof(Rock.Model.Campus)).Id;

            int?     groupId = gpSelectGroup.SelectedValueAsInt();
            DateTime?dateVal = dpMetricValueDateTime.SelectedDate;

            if (groupId.HasValue && dateVal.HasValue)
            {
                using (var rockContext = new RockContext())
                {
                    var metricService      = new MetricService(rockContext);
                    var metricValueService = new MetricValueService(rockContext);

                    var group    = new GroupService(rockContext).Queryable().FirstOrDefault(g => g.Id == groupId);
                    int?campusId = group.CampusId;

                    foreach (RepeaterItem item in rptrMetric.Items)
                    {
                        var hfMetricIId   = item.FindControl("hfMetricId") as HiddenField;
                        var nbMetricValue = item.FindControl("nbMetricValue") as NumberBox;

                        if (hfMetricIId != null && nbMetricValue != null)
                        {
                            int metricId = hfMetricIId.ValueAsInt();
                            var metric   = new MetricService(rockContext).Get(metricId);

                            if (metric != null)
                            {
                                int groupPartitionId  = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == groupEntityTypeId).Select(p => p.Id).FirstOrDefault();
                                int campusPartitionId = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId).Select(p => p.Id).FirstOrDefault();

                                var metricValue = metricValueService
                                                  .Queryable()
                                                  .Where(v =>
                                                         v.MetricId == metric.Id &&
                                                         v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == dateVal.Value &&
                                                         (
                                                             (
                                                                 v.MetricValuePartitions.Count == 2 &&
                                                                 v.MetricValuePartitions.Any(p => p.MetricPartitionId == campusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value) &&
                                                                 v.MetricValuePartitions.Any(p => p.MetricPartitionId == groupPartitionId && p.EntityId.HasValue && p.EntityId.Value == groupId.Value)
                                                             ) ||
                                                             (
                                                                 v.MetricValuePartitions.Count == 1 &&
                                                                 (
                                                                     v.MetricValuePartitions.Any(p => p.MetricPartitionId == groupPartitionId && p.EntityId.HasValue && p.EntityId.Value == groupId.Value)
                                                                 )
                                                             )
                                                         )
                                                         )
                                                  .FirstOrDefault();

                                if (metricValue == null)
                                {
                                    metricValue = new MetricValue();
                                    metricValue.MetricValueType     = MetricValueType.Measure;
                                    metricValue.MetricId            = metric.Id;
                                    metricValue.MetricValueDateTime = dateVal.Value;
                                    metricValueService.Add(metricValue);

                                    if (groupPartitionId > 0)
                                    {
                                        var groupValuePartition = new MetricValuePartition();
                                        groupValuePartition.MetricPartitionId = groupPartitionId;
                                        groupValuePartition.EntityId          = groupId.Value;
                                        metricValue.MetricValuePartitions.Add(groupValuePartition);
                                    }
                                    if (campusPartitionId > 0 && campusId.HasValue)
                                    {
                                        var campusValuePartition = new MetricValuePartition();
                                        campusValuePartition.MetricPartitionId = campusPartitionId;
                                        campusValuePartition.EntityId          = campusId.Value;
                                        metricValue.MetricValuePartitions.Add(campusValuePartition);
                                    }
                                }

                                metricValue.YValue = nbMetricValue.Text.AsDecimalOrNull();
                                metricValue.Note   = tbNote.Text;
                            }
                        }
                    }

                    rockContext.SaveChanges();
                }

                nbMetricsSaved.Text    = string.Format("The metrics for '{0}' on {1} have been saved.", gpSelectGroup.ItemName, dpMetricValueDateTime.SelectedDate.ToString());
                nbMetricsSaved.Visible = true;

                BindMetrics();
            }
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="metricValueId">The metric value identifier.</param>
        /// <param name="metricId">The metric identifier.</param>
        public void ShowDetail(int metricValueId, int?metricId)
        {
            pnlDetails.Visible = true;

            // Load depending on Add(0) or Edit
            MetricValue metricValue = null;

            if (!metricValueId.Equals(0))
            {
                metricValue       = new MetricValueService(new RockContext()).Get(metricValueId);
                lActionTitle.Text = ActionTitle.Edit(MetricValue.FriendlyTypeName).FormatAsHtmlTitle();
            }

            if (metricValue == null && metricId.HasValue)
            {
                metricValue = new MetricValue {
                    Id = 0, MetricId = metricId.Value
                };
                metricValue.Metric = metricValue.Metric ?? new MetricService(new RockContext()).Get(metricValue.MetricId);
                lActionTitle.Text  = ActionTitle.Add(MetricValue.FriendlyTypeName).FormatAsHtmlTitle();
            }

            hfMetricValueId.Value = metricValue.Id.ToString();

            LoadDropDowns();

            ddlMetricValueType.SelectedValue = metricValue.MetricValueType.ConvertToInt().ToString();
            tbXValue.Text    = metricValue.XValue;
            tbYValue.Text    = metricValue.YValue.ToString();
            hfMetricId.Value = metricValue.MetricId.ToString();
            tbNote.Text      = metricValue.Note;
            dpMetricValueDateTime.SelectedDate = metricValue.MetricValueDateTime;

            var metricEntityType = EntityTypeCache.Read(metricValue.Metric.EntityTypeId ?? 0);

            // Setup EntityType UI controls
            Control entityTypeEditControl = null;

            if (metricEntityType != null && metricEntityType.SingleValueFieldType != null)
            {
                hfSingleValueFieldTypeId.Value = metricEntityType.SingleValueFieldType.Id.ToString();
                FieldTypeCache fieldType = FieldTypeCache.Read(hfSingleValueFieldTypeId.Value.AsInteger());
                entityTypeEditControl = fieldType.Field.EditControl(new Dictionary <string, Rock.Field.ConfigurationValue>(), "entityTypeEditControl");

                if (entityTypeEditControl is IRockControl)
                {
                    (entityTypeEditControl as IRockControl).Label = fieldType.Name;
                }

                phEntityTypeEntityIdValue.Controls.Add(entityTypeEditControl);
                IEntityFieldType entityFieldType = metricEntityType.SingleValueFieldType.Field as IEntityFieldType;
                if (entityFieldType != null)
                {
                    entityFieldType.SetEditValueFromEntityId(entityTypeEditControl, new Dictionary <string, ConfigurationValue>(), metricValue.EntityId);
                }
            }

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;

            bool canEdit = UserCanEdit;

            if (!canEdit && metricId.HasValue && metricId.Value > 0)
            {
                var metric = new MetricService(new RockContext()).Get(metricId.Value);
                if (metric != null && metric.IsAuthorized(Authorization.EDIT, CurrentPerson))
                {
                    canEdit = true;
                }
            }

            if (!canEdit)
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(MetricValue.FriendlyTypeName);
            }

            if (readOnly)
            {
                lActionTitle.Text = ActionTitle.View(MetricValue.FriendlyTypeName);
                btnCancel.Text    = "Close";
            }

            ddlMetricValueType.Enabled    = !readOnly;
            tbXValue.ReadOnly             = readOnly;
            tbYValue.ReadOnly             = readOnly;
            tbNote.ReadOnly               = readOnly;
            dpMetricValueDateTime.Enabled = !readOnly;
            if (entityTypeEditControl is WebControl)
            {
                (entityTypeEditControl as WebControl).Enabled = !readOnly;
            }

            btnSave.Visible = !readOnly;
        }
        public IList <CollectableValue> Read()
        {
            // Only collect events and event metric every nth interval
            if (_intervalCounter++ % _intervalMultiplier != 0)
            {
                return(new List <CollectableValue>());
            }

            IList <CollectableValue> collectableValues = new List <CollectableValue>();
            long totalEvents    = 0;
            long collectionTime = (long)(Util.toEpoch(DateTime.UtcNow));

            List <long> recordIds = new List <long>();

            foreach (EventQuery eventQuery in _events)
            {
                List <EventRecord> records = GetEventRecords(eventQuery.minLevel, eventQuery.maxLevel, eventQuery.log, eventQuery.source);

                // Filter the events - event ID must be in target range, description must match regex and we mustn't have already read this event record ID in another query
                Regex filterExp = new Regex(eventQuery.filterExp, RegexOptions.None);
                List <EventRecord> filteredRecords = records.FindAll(delegate(EventRecord record)
                {
                    return(!recordIds.Contains((long)record.RecordId) && record.Id >= eventQuery.minEventId && record.Id <= eventQuery.maxEventId && filterExp.IsMatch(record.FormatDescription()));
                });

                // Add these record IDs to dedupe list so we don't capture them again in a later query
                filteredRecords.ForEach(delegate(EventRecord record) { recordIds.Add((long)record.RecordId); });

                if (filteredRecords.Count <= eventQuery.maxPerCycle)
                {
                    foreach (EventRecord record in filteredRecords)
                    {
                        // Timestamp from record is machine time, not GMT
                        long       timestamp = (long)(Util.toEpoch(record.TimeCreated.Value.ToUniversalTime()));
                        long       id        = (long)record.RecordId;
                        string     message   = record.FormatDescription();
                        EventValue newevent  = new EventValue(_hostName, timestamp, record.Level.Value, eventQuery.title, message, id, record.ProviderName);
                        collectableValues.Add(newevent);
                        totalEvents++;
                    }
                }
                else
                {
                    // Too many events - summarise by counting events by application, level and code
                    Dictionary <string, int> detailMap = new Dictionary <string, int>();
                    int minLevel = 999; // used to get the most severe event in the period for the summary level

                    string source = null;
                    filteredRecords.ForEach(delegate(EventRecord record)
                    {
                        string key = string.Format("{0} in {1} ({2})", record.LevelDisplayName, record.ProviderName, record.Id);

                        if (record.Level.Value < minLevel)
                        {
                            minLevel = record.Level.Value;
                        }

                        if (detailMap.ContainsKey(key))
                        {
                            detailMap[key] = detailMap[key] + 1;
                        }
                        else
                        {
                            detailMap.Add(key, 1);
                        }

                        if (source == null)
                        {
                            source = record.ProviderName;
                        }
                        else if (!source.Equals(record.ProviderName))
                        {
                            source = "Various";
                        }
                    });

                    List <KeyValuePair <string, int> > detailList = new List <KeyValuePair <string, int> >();
                    foreach (string key in detailMap.Keys)
                    {
                        detailList.Add(new KeyValuePair <string, int>(key, detailMap[key]));
                    }
                    detailList.Sort(delegate(KeyValuePair <string, int> pair1, KeyValuePair <string, int> pair2) { return(-pair1.Value.CompareTo(pair2.Value)); });

                    string[] messageLines = new string[detailList.Count];

                    int ix = 0;
                    foreach (KeyValuePair <string, int> pair in detailList)
                    {
                        messageLines[ix++] = pair.Value + " x " + pair.Key;
                    }
                    string     title    = string.Format("{0} ({1} events)", eventQuery.title, filteredRecords.Count);
                    EventValue newevent = new EventValue(_hostName, collectionTime, minLevel, title, String.Join(", ", messageLines), 0, source);
                    collectableValues.Add(newevent);
                    totalEvents += filteredRecords.Count;
                }
            }

            // Add event count metric
            MetricValue eventCountMetric = new MetricValue
            {
                HostName           = _hostName,
                PluginName         = "windows_events",
                PluginInstanceName = "",
                TypeName           = "count",
                TypeInstanceName   = "event_count",
                Values             = new double[] { totalEvents },
                FriendlyNames      = new string[] { "Windows Event Count" },
                Timestamp          = collectionTime
            };

            collectableValues.Add(eventCountMetric);

            return(collectableValues);
        }
Esempio n. 33
0
        /// <summary>
        /// The List Metric operation lists the metric value sets for the
        /// resource metrics.
        /// </summary>
        /// <param name='resourceUri'>
        /// Required. The resource identifier of the target resource to get
        /// metrics for.
        /// </param>
        /// <param name='filterString'>
        /// Optional. An OData $filter expression that supports querying by the
        /// name, startTime, endTime and timeGrain of the metric value sets.
        /// For example, "(name.value eq 'Percentage CPU') and startTime eq
        /// 2014-07-02T01:00Z and endTime eq 2014-08-21T01:00:00Z and
        /// timeGrain eq duration'PT1H'". In the expression, startTime,
        /// endTime and timeGrain are required. Name is optional.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Metric values operation response.
        /// </returns>
        public async Task <MetricListResponse> GetMetricsInternalAsync(string resourceUri, string filterString, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceUri", resourceUri);
                tracingParameters.Add("filterString", filterString);
                TracingAdapter.Enter(invocationId, this, "GetMetricsAsync", tracingParameters);
            }

            // Construct URL
            string url = "/" + Uri.EscapeDataString(resourceUri) + "/metrics?";

            url = url + "api-version=2014-04-01";
            if (filterString != null)
            {
                url = url + "&$filter=" + Uri.EscapeDataString(filterString);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-04-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MetricListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new MetricListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            MetricCollection metricCollectionInstance = new MetricCollection();
                            result.MetricCollection = metricCollectionInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Metric metricInstance = new Metric();
                                    metricCollectionInstance.Value.Add(metricInstance);

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString nameInstance = new LocalizableString();
                                        metricInstance.Name = nameInstance;

                                        JToken valueValue2 = nameValue["value"];
                                        if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue2);
                                            nameInstance.Value = valueInstance;
                                        }

                                        JToken localizedValueValue = nameValue["localizedValue"];
                                        if (localizedValueValue != null && localizedValueValue.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance = ((string)localizedValueValue);
                                            nameInstance.LocalizedValue = localizedValueInstance;
                                        }
                                    }

                                    JToken unitValue = valueValue["unit"];
                                    if (unitValue != null && unitValue.Type != JTokenType.Null)
                                    {
                                        Unit unitInstance = ((Unit)Enum.Parse(typeof(Unit), ((string)unitValue), true));
                                        metricInstance.Unit = unitInstance;
                                    }

                                    JToken timeGrainValue = valueValue["timeGrain"];
                                    if (timeGrainValue != null && timeGrainValue.Type != JTokenType.Null)
                                    {
                                        TimeSpan timeGrainInstance = XmlConvert.ToTimeSpan(((string)timeGrainValue));
                                        metricInstance.TimeGrain = timeGrainInstance;
                                    }

                                    JToken startTimeValue = valueValue["startTime"];
                                    if (startTimeValue != null && startTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTime startTimeInstance = ((DateTime)startTimeValue);
                                        metricInstance.StartTime = startTimeInstance;
                                    }

                                    JToken endTimeValue = valueValue["endTime"];
                                    if (endTimeValue != null && endTimeValue.Type != JTokenType.Null)
                                    {
                                        DateTime endTimeInstance = ((DateTime)endTimeValue);
                                        metricInstance.EndTime = endTimeInstance;
                                    }

                                    JToken metricValuesArray = valueValue["metricValues"];
                                    if (metricValuesArray != null && metricValuesArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken metricValuesValue in ((JArray)metricValuesArray))
                                        {
                                            MetricValue metricValueInstance = new MetricValue();
                                            metricInstance.MetricValues.Add(metricValueInstance);

                                            JToken timestampValue = metricValuesValue["timestamp"];
                                            if (timestampValue != null && timestampValue.Type != JTokenType.Null)
                                            {
                                                DateTime timestampInstance = ((DateTime)timestampValue);
                                                metricValueInstance.Timestamp = timestampInstance;
                                            }

                                            JToken averageValue = metricValuesValue["average"];
                                            if (averageValue != null && averageValue.Type != JTokenType.Null)
                                            {
                                                double averageInstance = ((double)averageValue);
                                                metricValueInstance.Average = averageInstance;
                                            }

                                            JToken minimumValue = metricValuesValue["minimum"];
                                            if (minimumValue != null && minimumValue.Type != JTokenType.Null)
                                            {
                                                double minimumInstance = ((double)minimumValue);
                                                metricValueInstance.Minimum = minimumInstance;
                                            }

                                            JToken maximumValue = metricValuesValue["maximum"];
                                            if (maximumValue != null && maximumValue.Type != JTokenType.Null)
                                            {
                                                double maximumInstance = ((double)maximumValue);
                                                metricValueInstance.Maximum = maximumInstance;
                                            }

                                            JToken totalValue = metricValuesValue["total"];
                                            if (totalValue != null && totalValue.Type != JTokenType.Null)
                                            {
                                                double totalInstance = ((double)totalValue);
                                                metricValueInstance.Total = totalInstance;
                                            }

                                            JToken countValue = metricValuesValue["count"];
                                            if (countValue != null && countValue.Type != JTokenType.Null)
                                            {
                                                long countInstance = ((long)countValue);
                                                metricValueInstance.Count = countInstance;
                                            }

                                            JToken lastValue = metricValuesValue["last"];
                                            if (lastValue != null && lastValue.Type != JTokenType.Null)
                                            {
                                                double lastInstance = ((double)lastValue);
                                                metricValueInstance.Last = lastInstance;
                                            }

                                            JToken propertiesSequenceElement = ((JToken)metricValuesValue["properties"]);
                                            if (propertiesSequenceElement != null && propertiesSequenceElement.Type != JTokenType.Null)
                                            {
                                                foreach (JProperty property in propertiesSequenceElement)
                                                {
                                                    string propertiesKey   = ((string)property.Name);
                                                    string propertiesValue = ((string)property.Value);
                                                    metricValueInstance.Properties.Add(propertiesKey, propertiesValue);
                                                }
                                            }
                                        }
                                    }

                                    JToken resourceIdValue = valueValue["resourceId"];
                                    if (resourceIdValue != null && resourceIdValue.Type != JTokenType.Null)
                                    {
                                        string resourceIdInstance = ((string)resourceIdValue);
                                        metricInstance.ResourceId = resourceIdInstance;
                                    }

                                    JToken propertiesSequenceElement2 = ((JToken)valueValue["properties"]);
                                    if (propertiesSequenceElement2 != null && propertiesSequenceElement2.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property2 in propertiesSequenceElement2)
                                        {
                                            string propertiesKey2   = ((string)property2.Name);
                                            string propertiesValue2 = ((string)property2.Value);
                                            metricInstance.Properties.Add(propertiesKey2, propertiesValue2);
                                        }
                                    }

                                    JToken dimensionNameValue = valueValue["dimensionName"];
                                    if (dimensionNameValue != null && dimensionNameValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString dimensionNameInstance = new LocalizableString();
                                        metricInstance.DimensionName = dimensionNameInstance;

                                        JToken valueValue3 = dimensionNameValue["value"];
                                        if (valueValue3 != null && valueValue3.Type != JTokenType.Null)
                                        {
                                            string valueInstance2 = ((string)valueValue3);
                                            dimensionNameInstance.Value = valueInstance2;
                                        }

                                        JToken localizedValueValue2 = dimensionNameValue["localizedValue"];
                                        if (localizedValueValue2 != null && localizedValueValue2.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance2 = ((string)localizedValueValue2);
                                            dimensionNameInstance.LocalizedValue = localizedValueInstance2;
                                        }
                                    }

                                    JToken dimensionValueValue = valueValue["dimensionValue"];
                                    if (dimensionValueValue != null && dimensionValueValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString dimensionValueInstance = new LocalizableString();
                                        metricInstance.DimensionValue = dimensionValueInstance;

                                        JToken valueValue4 = dimensionValueValue["value"];
                                        if (valueValue4 != null && valueValue4.Type != JTokenType.Null)
                                        {
                                            string valueInstance3 = ((string)valueValue4);
                                            dimensionValueInstance.Value = valueInstance3;
                                        }

                                        JToken localizedValueValue3 = dimensionValueValue["localizedValue"];
                                        if (localizedValueValue3 != null && localizedValueValue3.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance3 = ((string)localizedValueValue3);
                                            dimensionValueInstance.LocalizedValue = localizedValueInstance3;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Esempio n. 34
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            var metricSourceValueTypeDataviewGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_DATAVIEW.AsGuid();
            var metricSourceValueTypeSqlGuid      = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_SQL.AsGuid();
            var metricSourceValueTypeLavaGuid     = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_LAVA.AsGuid();

            Guid[] calculatedSourceTypes = new Guid[] {
                metricSourceValueTypeDataviewGuid,
                metricSourceValueTypeSqlGuid,
                metricSourceValueTypeLavaGuid
            };

            var metricsQry = new MetricService(new RockContext()).Queryable().AsNoTracking().Where(
                a => a.ScheduleId.HasValue &&
                a.SourceValueTypeId.HasValue &&
                calculatedSourceTypes.Contains(a.SourceValueType.Guid));

            var metricIdList = metricsQry.OrderBy(a => a.Title).ThenBy(a => a.Subtitle).Select(a => a.Id).ToList();

            var    metricExceptions       = new List <Exception>();
            int    metricsCalculated      = 0;
            int    metricValuesCalculated = 0;
            Metric metric = null;

            foreach (var metricId in metricIdList)
            {
                try
                {
                    using (var rockContextForMetricEntity = new RockContext())
                    {
                        var metricService = new MetricService(rockContextForMetricEntity);

                        metric = metricService.Get(metricId);
                        var lastRunDateTime = metric.LastRunDateTime ?? metric.CreatedDateTime ?? metric.ModifiedDateTime;
                        if (lastRunDateTime.HasValue)
                        {
                            var currentDateTime = RockDateTime.Now;

                            // get all the schedule times that were supposed to run since that last time it was scheduled to run
                            var scheduledDateTimesToProcess = metric.Schedule.GetScheduledStartTimes(lastRunDateTime.Value, currentDateTime).Where(a => a > lastRunDateTime.Value).ToList();
                            foreach (var scheduleDateTime in scheduledDateTimesToProcess)
                            {
                                using (var rockContextForMetricValues = new RockContext())
                                {
                                    var metricPartitions                     = new MetricPartitionService(rockContextForMetricValues).Queryable().Where(a => a.MetricId == metric.Id).ToList();
                                    var metricValueService                   = new MetricValueService(rockContextForMetricValues);
                                    List <ResultValue> resultValues          = new List <ResultValue>();
                                    bool getMetricValueDateTimeFromResultSet = false;
                                    if (metric.SourceValueType.Guid == metricSourceValueTypeDataviewGuid)
                                    {
                                        // get the metric value from the DataView
                                        if (metric.DataView != null)
                                        {
                                            var errorMessages = new List <string>();
                                            var qry           = metric.DataView.GetQuery(null, null, out errorMessages);
                                            if (metricPartitions.Count > 1 || metricPartitions.First().EntityTypeId.HasValue)
                                            {
                                                throw new NotImplementedException("Partitioned Metrics using DataViews is not supported.");
                                            }
                                            else
                                            {
                                                var resultValue = new ResultValue();
                                                resultValue.Value               = Convert.ToDecimal(qry.Count());
                                                resultValue.Partitions          = new List <ResultValuePartition>();
                                                resultValue.MetricValueDateTime = scheduleDateTime;
                                                resultValues.Add(resultValue);
                                            }
                                        }
                                    }
                                    else if (metric.SourceValueType.Guid == metricSourceValueTypeSqlGuid)
                                    {
                                        //// calculate the metricValue using the results from the SQL
                                        //// assume SQL is in one of the following forms:
                                        //// -- "SELECT Count(*) FROM ..."
                                        //// -- "SELECT Count(*), [MetricValueDateTime] FROM ..."
                                        //// -- "SELECT Count(*), Partition0EntityId, Partition1EntityId, Partition2EntityId,.. FROM ..."
                                        //// -- "SELECT Count(*), [MetricValueDateTime], Partition0EntityId, Partition1EntityId, Partition2EntityId,.. FROM ..."
                                        if (!string.IsNullOrWhiteSpace(metric.SourceSql))
                                        {
                                            string formattedSql = metric.SourceSql.ResolveMergeFields(metric.GetMergeObjects(scheduleDateTime));
                                            var    tableResult  = DbService.GetDataTable(formattedSql, System.Data.CommandType.Text, null);

                                            if (tableResult.Columns.Count >= 2 && tableResult.Columns[1].ColumnName == "MetricValueDateTime")
                                            {
                                                getMetricValueDateTimeFromResultSet = true;
                                            }

                                            foreach (var row in tableResult.Rows.OfType <System.Data.DataRow>())
                                            {
                                                var resultValue = new ResultValue();

                                                resultValue.Value = Convert.ToDecimal(row[0]);
                                                if (getMetricValueDateTimeFromResultSet)
                                                {
                                                    resultValue.MetricValueDateTime = Convert.ToDateTime(row[1]);
                                                }
                                                else
                                                {
                                                    resultValue.MetricValueDateTime = scheduleDateTime;
                                                }

                                                resultValue.Partitions = new List <ResultValuePartition>();
                                                int partitionPosition    = 0;
                                                int partitionFieldIndex  = getMetricValueDateTimeFromResultSet ? 2 : 1;
                                                int partitionColumnCount = tableResult.Columns.Count - 1;
                                                while (partitionFieldIndex <= partitionColumnCount)
                                                {
                                                    resultValue.Partitions.Add(new ResultValuePartition
                                                    {
                                                        PartitionPosition = partitionPosition,
                                                        EntityId          = row[partitionFieldIndex] as int?
                                                    });

                                                    partitionPosition++;
                                                    partitionFieldIndex++;
                                                }

                                                resultValues.Add(resultValue);
                                            }
                                        }
                                    }
                                    else if (metric.SourceValueType.Guid == metricSourceValueTypeLavaGuid)
                                    {
                                        //// calculate the metricValue using the results from Lava
                                        //// assume Lava Output is in one of the following forms:
                                        //// A single Count
                                        //// 42
                                        //// A List of Count, MetricValueDateTime
                                        //// 42, 1/1/2017
                                        //// 40, 1/2/2017
                                        //// 49, 1/3/2017
                                        //// A List of Count, Partition0EntityId, Partition1EntityId, Partition2EntityId
                                        //// 42, 201, 450, 654
                                        //// 42, 202, 450, 654
                                        //// 42, 203, 450, 654
                                        //// A List of Count, MetricValueDateTime,  Partition0EntityId, Partition1EntityId, Partition2EntityId
                                        //// 42, 1/1/2017, 201, 450, 654
                                        //// 42, 1/2/2017, 202, 450, 654
                                        //// 42, 1/3/2017, 203, 450, 654
                                        if (!string.IsNullOrWhiteSpace(metric.SourceLava))
                                        {
                                            var           mergeObjects = metric.GetMergeObjects(scheduleDateTime);
                                            string        lavaResult   = metric.SourceLava.ResolveMergeFields(mergeObjects, enabledLavaCommands: "All", throwExceptionOnErrors: true);
                                            List <string> resultLines  = lavaResult.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                                                         .Select(a => a.Trim()).Where(a => !string.IsNullOrEmpty(a)).ToList();
                                            List <string[]> resultList = resultLines.Select(a => a.SplitDelimitedValues()).ToList();

                                            if (resultList.Any())
                                            {
                                                if (resultList[0].Length >= 2)
                                                {
                                                    // if the value of the data in the 2nd column is a Date, assume that is is the MetricValueDateTime
                                                    if (resultList[0][1].AsDateTime().HasValue)
                                                    {
                                                        getMetricValueDateTimeFromResultSet = true;
                                                    }
                                                }
                                            }

                                            foreach (var row in resultList)
                                            {
                                                var resultValue = new ResultValue();

                                                resultValue.Value = row[0].AsDecimal();
                                                if (getMetricValueDateTimeFromResultSet)
                                                {
                                                    resultValue.MetricValueDateTime = row[1].AsDateTime() ?? scheduleDateTime;
                                                }
                                                else
                                                {
                                                    resultValue.MetricValueDateTime = scheduleDateTime;
                                                }

                                                resultValue.Partitions = new List <ResultValuePartition>();
                                                int partitionPosition    = 0;
                                                int partitionFieldIndex  = getMetricValueDateTimeFromResultSet ? 2 : 1;
                                                int partitionColumnCount = row.Length - 1;
                                                while (partitionFieldIndex <= partitionColumnCount)
                                                {
                                                    resultValue.Partitions.Add(new ResultValuePartition
                                                    {
                                                        PartitionPosition = partitionPosition,
                                                        EntityId          = row[partitionFieldIndex].AsIntegerOrNull()
                                                    });

                                                    partitionPosition++;
                                                    partitionFieldIndex++;
                                                }

                                                resultValues.Add(resultValue);
                                            }
                                        }
                                    }

                                    metric.LastRunDateTime = scheduleDateTime;
                                    metricsCalculated++;
                                    metricValuesCalculated += resultValues.Count();

                                    if (resultValues.Any())
                                    {
                                        List <MetricValue> metricValuesToAdd = new List <MetricValue>();
                                        foreach (var resultValue in resultValues)
                                        {
                                            var metricValue = new MetricValue();
                                            metricValue.MetricId            = metric.Id;
                                            metricValue.MetricValueDateTime = resultValue.MetricValueDateTime;
                                            metricValue.MetricValueType     = MetricValueType.Measure;
                                            metricValue.YValue                = resultValue.Value;
                                            metricValue.CreatedDateTime       = RockDateTime.Now;
                                            metricValue.ModifiedDateTime      = RockDateTime.Now;
                                            metricValue.MetricValuePartitions = new List <MetricValuePartition>();
                                            var metricPartitionsByPosition = metricPartitions.OrderBy(a => a.Order).ToList();

                                            if (!resultValue.Partitions.Any() && metricPartitionsByPosition.Count() == 1 && !metricPartitionsByPosition[0].EntityTypeId.HasValue)
                                            {
                                                // a metric with just the default partition (not partitioned by Entity)
                                                var metricPartition      = metricPartitionsByPosition[0];
                                                var metricValuePartition = new MetricValuePartition();
                                                metricValuePartition.MetricPartition   = metricPartition;
                                                metricValuePartition.MetricPartitionId = metricPartition.Id;
                                                metricValuePartition.MetricValue       = metricValue;
                                                metricValuePartition.EntityId          = null;
                                                metricValue.MetricValuePartitions.Add(metricValuePartition);
                                            }
                                            else
                                            {
                                                foreach (var partitionResult in resultValue.Partitions)
                                                {
                                                    if (metricPartitionsByPosition.Count > partitionResult.PartitionPosition)
                                                    {
                                                        var metricPartition      = metricPartitionsByPosition[partitionResult.PartitionPosition];
                                                        var metricValuePartition = new MetricValuePartition();
                                                        metricValuePartition.MetricPartition   = metricPartition;
                                                        metricValuePartition.MetricPartitionId = metricPartition.Id;
                                                        metricValuePartition.MetricValue       = metricValue;
                                                        metricValuePartition.EntityId          = partitionResult.EntityId;
                                                        metricValue.MetricValuePartitions.Add(metricValuePartition);
                                                    }
                                                }
                                            }

                                            if (metricValue.MetricValuePartitions == null || !metricValue.MetricValuePartitions.Any())
                                            {
                                                // shouldn't happen, but just in case
                                                throw new Exception("MetricValue requires at least one Partition Value");
                                            }
                                            else
                                            {
                                                metricValuesToAdd.Add(metricValue);
                                            }
                                        }

                                        // if a single metricValueDateTime was specified, delete any existing metric values for this date and refresh with the current results
                                        var dbTransaction = rockContextForMetricValues.Database.BeginTransaction();
                                        if (getMetricValueDateTimeFromResultSet)
                                        {
                                            var metricValueDateTimes = metricValuesToAdd.Select(a => a.MetricValueDateTime).Distinct().ToList();
                                            foreach (var metricValueDateTime in metricValueDateTimes)
                                            {
                                                bool alreadyHasMetricValues = metricValueService.Queryable().Where(a => a.MetricId == metric.Id && a.MetricValueDateTime == metricValueDateTime).Any();
                                                if (alreadyHasMetricValues)
                                                {
                                                    // use direct SQL to clean up any existing metric values
                                                    rockContextForMetricValues.Database.ExecuteSqlCommand(@"
                                                        DELETE
                                                        FROM MetricValuePartition
                                                        WHERE MetricValueId IN (
                                                            SELECT Id
                                                            FROM MetricValue
                                                            WHERE MetricId = @metricId
                                                            AND MetricValueDateTime = @metricValueDateTime
                                                        )
                                                    ", new SqlParameter("@metricId", metric.Id), new SqlParameter("@metricValueDateTime", metricValueDateTime));

                                                    rockContextForMetricValues.Database.ExecuteSqlCommand(@"
                                                        DELETE
                                                        FROM MetricValue
                                                        WHERE MetricId = @metricId
                                                        AND MetricValueDateTime = @metricValueDateTime
                                                    ", new SqlParameter("@metricId", metric.Id), new SqlParameter("@metricValueDateTime", metricValueDateTime));
                                                }
                                            }
                                        }

                                        metricValueService.AddRange(metricValuesToAdd);

                                        // disable savechanges PrePostProcessing since there could be hundreds or thousands of metric values getting inserted/updated
                                        rockContextForMetricValues.SaveChanges(true);
                                        dbTransaction.Commit();
                                    }

                                    rockContextForMetricEntity.SaveChanges();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    metricExceptions.Add(new Exception(string.Format("Exception when calculating metric for {0} ", metric), ex));
                }
            }

            context.Result = string.Format("Calculated a total of {0} metric values for {1} metrics", metricValuesCalculated, metricsCalculated);

            if (metricExceptions.Any())
            {
                throw new AggregateException("One or more metric calculations failed ", metricExceptions);
            }
        }
Esempio n. 35
0
        private static double InterpretMetricValue(AggregationType metricAggregation, MetricValue relevantMetricValue)
        {
            switch (metricAggregation)
            {
            case AggregationType.Average:
                return(relevantMetricValue.Average ?? -1);

            case AggregationType.Count:
                return(relevantMetricValue.Count ?? -1);

            case AggregationType.Maximum:
                return(relevantMetricValue.Maximum ?? -1);

            case AggregationType.Minimum:
                return(relevantMetricValue.Minimum ?? -1);

            case AggregationType.Total:
                return(relevantMetricValue.Total ?? -1);

            case AggregationType.None:
                return(0);

            default:
                throw new Exception($"Unable to determine the metrics value for aggregator '{metricAggregation}'");
            }
        }
Esempio n. 36
0
        private static void AddUsageObjectIfAvailable(CloudServiceInstanceId instance, MetricValueSet metric, MetricValue value, double?metricValue, string valueName, List <UsageObject> usageObjects)
        {
            if (metricValue == null)
            {
                return;
            }
            var usageObject = new UsageObject {
                GraphiteCounterName = instance.BuildGraphiteCounterName(metric.Name, metric.Unit, valueName).ToString(),
                Timestamp           = value.Timestamp.ToString("o"),
                Value = metricValue.Value
            };

            usageObjects.Add(usageObject);
        }
Esempio n. 37
0
        protected void CalulateMetrics(int CampusId)
        {
            // specify which attribute key we want to work with
            var attributeKeyCompositeUseGlobalValue = "CompositeUseGlobalValue";
            var attributeKeyCompositeGoalMultiplier = "CompositeGoalMultiplier";

            var attributeValueService = new AttributeValueService(rockContext);

            // specify NULL as the EntityId since this is a GlobalAttribute
            var compositeUseGlobalValue = attributeValueService.GetGlobalAttributeValue(attributeKeyCompositeUseGlobalValue);
            var compositeGoalMultiplier = attributeValueService.GetGlobalAttributeValue(attributeKeyCompositeGoalMultiplier);

            if (bool.Parse(compositeUseGlobalValue.ToString()) == true)
            {
                UseGlobalAttributeGoal = true;
                GoalMultiplier = Convert.ToDouble(compositeGoalMultiplier.ToString());
            }

            //Set Last Sunday Date
            DateTime now = DateTime.Now;
            DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
            SundayDate = dt.ToShortDateString();
            SundayDateSQLFormatted = dt.Date.ToString("yyyy-MM-dd");

            DateTime lastTuesday = DateTime.Now.AddDays(-1);
            while (lastTuesday.DayOfWeek != DayOfWeek.Wednesday)
                lastTuesday = lastTuesday.AddDays(-1);

            DateTime lastWednesday = DateTime.Now.AddDays(-1);
            while (lastWednesday.DayOfWeek != DayOfWeek.Wednesday)
                lastWednesday = lastWednesday.AddDays(-1);

            FinancialStartDate = lastTuesday.AddDays(-7).ToString("yyyy-MM-dd");
            FinancialEndDate = lastWednesday.ToString("yyyy-MM-dd");

            FinancialStartDateLastWeek = lastTuesday.AddDays(-14).ToString("yyyy-MM-dd");
            FinancialEndDateLastWeek = lastWednesday.AddDays(-7).ToString("yyyy-MM-dd");

            sMonth = DateTime.Now.Month.ToString();
            sYear = DateTime.Now.Year.ToString();
            int iMonth = Int32.Parse(sMonth);
            int iYear = Int32.Parse(sYear);

            int lastDayOfCurrentMonth = DateTime.DaysInMonth(iYear, iMonth);
            int lastDayOfLastMonth = DateTime.DaysInMonth(iYear, iMonth - 1);

            DateTime fiscalYearStartDate = new DateTime(iYear, 9, 1);
            DateTime fiscalYearEndDate = new DateTime(iYear + 1, 8, 31);
            DateTime lastFiscalYearStartDate = new DateTime(iYear - 1, 9, 1);
            DateTime lastFiscalYearEndDate = new DateTime(iYear, 8, 31);

            DateTime start2020 = new DateTime(2020, 1, 1);
            DateTime end2020 = new DateTime(2020, 12, 31);

            if (iMonth < 9)
            {
                fiscalYearStartDate = fiscalYearStartDate.AddYears(-1);
                fiscalYearEndDate = fiscalYearEndDate.AddYears(-1);
                lastFiscalYearStartDate = lastFiscalYearStartDate.AddYears(-1);
                lastFiscalYearEndDate = lastFiscalYearEndDate.AddYears(-1);
            }

            DateTime periodEndDate = new DateTime(iYear, iMonth - 1, lastDayOfLastMonth);
            DateTime lastPeriodEndDate = new DateTime(iYear - 1, iMonth - 1, lastDayOfLastMonth);

            FiscalYearStartDate = fiscalYearStartDate.ToShortDateString();
            FiscalYearEndDate = fiscalYearEndDate.ToShortDateString();
            PeriodStartDate = fiscalYearStartDate.ToShortDateString();
            PeriodEndDate = periodEndDate.ToShortDateString();
            LastPeriodStartDate = lastFiscalYearStartDate.ToShortDateString();
            LastPeriodEndDate = lastPeriodEndDate.ToShortDateString();

            Last8WkStartDate = now.AddDays(-57).ToShortDateString();
            Last8WkEndDate = now.ToShortDateString();
            Last8WkStartDateLy = now.AddDays(-57).AddYears(-1).ToShortDateString();
            Last8WkEndDateLy = now.AddYears(-1).ToShortDateString();

            Last6WkStartDate = now.AddDays(-43).ToShortDateString();
            Last6WkEndDate = now.ToShortDateString();
            Last6WkStartDateLy = now.AddDays(-43).AddYears(-1).ToShortDateString();
            Last6WkEndDateLy = now.AddYears(-1).ToShortDateString();

            DateTime last6WkStartDate = now.AddDays(-43);
            DateTime last6WkEndDate = now;
            DateTime last6WkStartDateLy = now.AddDays(-43).AddYears(-1);
            DateTime last6WkEndDateLy = now.AddYears(-1);

            switch (iMonth)
            {
                case 09:
                    CurrentMonthInFiscalYear = 1;
                    GoalOffsetMultiplier = .083;
                    SecondaryGoalOffsetMultiplier = .89;
                    break;
                case 10:
                    CurrentMonthInFiscalYear = 2;
                    GoalOffsetMultiplier = .167;
                    SecondaryGoalOffsetMultiplier = .90;
                    break;
                case 11:
                    CurrentMonthInFiscalYear = 3;
                    GoalOffsetMultiplier = .25;
                    SecondaryGoalOffsetMultiplier = .91;
                    break;
                case 12:
                    CurrentMonthInFiscalYear = 4;
                    GoalOffsetMultiplier = .333;
                    SecondaryGoalOffsetMultiplier = .92;
                    break;
                case 01:
                    CurrentMonthInFiscalYear = 5;
                    GoalOffsetMultiplier = .417;
                    SecondaryGoalOffsetMultiplier = .93;
                    break;
                case 02:
                    CurrentMonthInFiscalYear = 6;
                    GoalOffsetMultiplier = .5;
                    SecondaryGoalOffsetMultiplier = .94;
                    break;
                case 03:
                    CurrentMonthInFiscalYear = 7;
                    GoalOffsetMultiplier = .583;
                    SecondaryGoalOffsetMultiplier = .95;
                    break;
                case 04:
                    CurrentMonthInFiscalYear = 8;
                    GoalOffsetMultiplier = .667;
                    SecondaryGoalOffsetMultiplier = .96;
                    break;
                case 05:
                    CurrentMonthInFiscalYear = 9;
                    GoalOffsetMultiplier = .75;
                    SecondaryGoalOffsetMultiplier = .97;
                    break;
                case 06:
                    CurrentMonthInFiscalYear = 10;
                    GoalOffsetMultiplier = .883;
                    SecondaryGoalOffsetMultiplier = .98;
                    break;
                case 07:
                    CurrentMonthInFiscalYear = 11;
                    GoalOffsetMultiplier = .917;
                    SecondaryGoalOffsetMultiplier = .99;
                    break;
                case 08:
                    CurrentMonthInFiscalYear = 12;
                    GoalOffsetMultiplier = 1;
                    SecondaryGoalOffsetMultiplier = 1;
                    break;
            }

            //-------Attendance-------

            //Auditorium
            iAttendanceAud = Get6WkAttendanceAuditorium(CampusId, last6WkStartDate, last6WkEndDate);
            iAttendanceAudLastYear = Get6WkAttendanceAuditorium(CampusId, last6WkStartDateLy, last6WkEndDateLy);
            iAttendanceAudGoal2020 = GetMetrics(2, CampusId, start2020, end2020, 1);

            //Kids
            iAttendanceKids = Get6WkAttendanceKids(CampusId, last6WkStartDate, last6WkEndDate);
            iAttendanceKidsLastYear = Get6WkAttendanceKids(CampusId, last6WkStartDateLy, last6WkEndDateLy);
            iAttendanceChildGoal2020 = GetMetrics(3, CampusId, start2020, end2020, 1);

            //Students
            iAttendanceStudents = Get6WkAttendanceStudents(CampusId, last6WkStartDate, last6WkEndDate);
            iAttendanceStudentsLastYear = Get6WkAttendanceStudents(CampusId, last6WkStartDateLy, last6WkEndDateLy);
            iAttendanceStudentGoal2020 = GetMetrics(5, CampusId, start2020, end2020, 1);

            //HighSchool
            iAttendanceHighSchool = Get6WkAttendanceHighSchool(CampusId, last6WkStartDate, last6WkEndDate);
            iAttendanceHighSchoolLastYear = Get6WkAttendanceHighSchool(CampusId, last6WkStartDateLy, last6WkEndDateLy);
            iAttendanceStudentGoal2020 = GetMetrics(5, CampusId, start2020, end2020, 1);

            if (UseGlobalAttributeGoal == true)
            {
                iAttendanceAudGoalCurrent = Convert.ToInt32((double?)iAttendanceAudLastYear * GoalMultiplier);
                iAttendanceChildGoalCurrent = Convert.ToInt32((double?)iAttendanceKidsLastYear * GoalMultiplier);
                iAttendanceStudentGoalCurrent = Convert.ToInt32((double?)iAttendanceStudentsLastYear * GoalMultiplier);
            }
            else
            {
                iAttendanceAudGoalCurrent = GetMetrics(2, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iAttendanceChildGoalCurrent = GetMetrics(3, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iAttendanceStudentGoalCurrent = GetMetrics(5, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
            }

            //All
            iAttendanceAll = Get6WkAttendanceAll(CampusId, last6WkStartDate, last6WkEndDate);
            iAttendanceAllLastYear = Get6WkAttendanceAll(CampusId, last6WkStartDateLy, last6WkEndDateLy);
            iAttendanceAllGoalCurrent = iAttendanceAudGoalCurrent + iAttendanceChildGoalCurrent + iAttendanceStudentGoalCurrent;
            iAttendanceAllGoal2020 = iAttendanceAudGoal2020 + iAttendanceChildGoal2020 + iAttendanceStudentGoal2020;

            //Calculate attendance goal progress
            iAttendanceAudGoalProgress = (double?)iAttendanceAud / ((double?)iAttendanceAudGoalCurrent * SecondaryGoalOffsetMultiplier) * 100;
            iAttendanceChildGoalProgress = (double?)iAttendanceKids / ((double?)iAttendanceChildGoalCurrent * SecondaryGoalOffsetMultiplier) * 100;
            iAttendanceStudentGoalProgress = (double?)iAttendanceStudents / ((double?)iAttendanceStudentGoalCurrent * SecondaryGoalOffsetMultiplier) * 100;
            iAttendanceHighSchoolGoalProgress = (double?)iAttendanceHighSchool / ((double?)iAttendanceHighSchoolGoalCurrent * SecondaryGoalOffsetMultiplier) * 100;
            iAttendanceAllGoalProgress = (double?)iAttendanceAll / ((double?)iAttendanceAllGoalCurrent * SecondaryGoalOffsetMultiplier) * 100;

            //-------Baptisms-------

            //YTD
            iBaptisms = GetMetrics(11, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iBaptismsLastYear = GetMetrics(11, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iBaptismsGoalCurrent = GetMetrics(11, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iBaptismsGoal2020 = GetMetrics(11, CampusId, start2020, end2020, 1);

            //8Wk
            iBaptisms8Wk = GetMetrics(11, CampusId, now.AddDays(-57), now, 0);
            iBaptisms8WkLy = GetMetrics(11, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);
            iBaptisms8WkProgress = (((double)iBaptisms8Wk - (double)iBaptisms8WkLy) / (double)iBaptisms8WkLy) * 100;

            //-------Partners-------

            //YTD
            iPartners = GetMetricsLatest(20, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iPartnersLastYear = GetMetricsLatest(20, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iPartnersGoalCurrent = GetMetricsLatest(20, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iPartnersGoal2020 = GetMetricsLatest(20, CampusId, start2020, end2020, 1);

            //-------Commitments-------

            //YTD
            iCommitments = GetMetrics(12, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iCommitmentsLastYear = GetMetrics(12, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iCommitmentsGoalCurrent = GetMetrics(12, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iCommitmentsGoal2020 = GetMetrics(12, CampusId, start2020, end2020, 1);

            //8Wk
            iCommitments8Wk = GetMetrics(12, CampusId, now.AddDays(-57), now, 0);
            iCommitments8WkLy = GetMetrics(12, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);

            //-------Recommitments-------

            //YTD
            iRecommitments = GetMetrics(13, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iRecommitmentsLastYear = GetMetrics(13, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iRecommitmentsGoalCurrent = GetMetrics(13, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iRecommitmentsGoal2020 = GetMetrics(13, CampusId, start2020, end2020, 1);

            //8Wk
            iRecommitments8Wk = GetMetrics(13, CampusId, now.AddDays(-57), now, 0);
            iRecommitments8WkLy = GetMetrics(13, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);

            //-------Volunteers-------

            //YTD
            iVolunteers = GetMetricsLatest(16, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iVolunteersLastYear = GetMetricsLatest(16, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iVolunteersGoalCurrent = GetMetricsLatest(16, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iVolunteersGoal2020 = GetMetricsLatest(16, CampusId, start2020, end2020, 1);

            //-------SmallGroupParticipants-------

            //YTD
            iSmallGroupParticipants = GetMetricsLatest(17, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iSmallGroupParticipantsLastYear = GetMetricsLatest(17, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iSmallGroupParticipantsGoalCurrent = GetMetricsLatest(17, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iSmallGroupParticipantsGoal2020 = GetMetricsLatest(17, CampusId, start2020, end2020, 1);

            //-------SmallGroupLeaders-------

            //YTD
            iSmallGroupLeaders = GetMetricsLatest(18, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iSmallGroupLeadersLastYear = GetMetricsLatest(18, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iSmallGroupLeadersGoalCurrent = GetMetricsLatest(18, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iSmallGroupLeadersGoal2020 = GetMetricsLatest(18, CampusId, start2020, end2020, 1);

            //-------SmallGroups-------

            //YTD
            iSmallGroups = GetMetricsLatest(34, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iSmallGroupsLastYear = GetMetricsLatest(34, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iSmallGroupsGoalCurrent = GetMetricsLatest(34, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iSmallGroupsGoal2020 = GetMetricsLatest(34, CampusId, start2020, end2020, 1);

            //-------NewtoNewPointe-------

            //YTD
            iNewtoNewPointe = GetMetrics(21, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iNewtoNewPointeLastYear = GetMetrics(21, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iNewtoNewPointeGoalCurrent = GetMetrics(21, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iNewtoNewPointeGoal2020 = GetMetrics(21, CampusId, start2020, end2020, 1);

            //8Wk
            iNewtoNewPointe8Wk = GetMetrics(21, CampusId, now.AddDays(-57), now, 0);
            iNewtoNewPointe8WkLy = GetMetrics(21, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);
            dNewtoNewPointe8WkProgress = (((double)iNewtoNewPointe8Wk - (double)iNewtoNewPointe8WkLy) / (double)iNewtoNewPointe8WkLy) * 100;

            //-------DiscoverGroups-------

            //YTD
            iDiscoverGroups = GetMetrics(22, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iDiscoverGroupsLastYear = GetMetrics(22, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iDiscoverGroupsGoalCurrent = GetMetrics(22, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iDiscoverGroupsGoal2020 = GetMetrics(2, CampusId, start2020, end2020, 1);

            //8Wk
            iDiscoverGroups8Wk = GetMetrics(22, CampusId, now.AddDays(-57), now, 0);
            iDiscoverGroups8WkLy = GetMetrics(22, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);
            dDiscoverGroups8WkProgress = (((double)iDiscoverGroups8Wk - (double)iDiscoverGroups8WkLy) / (double)iDiscoverGroups8WkLy) * 100;

            //-------CampusGroups-------

            //YTD
            iCampusGroups = GetMetrics(24, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iCampusGroupsLastYear = GetMetrics(24, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iCampusGroupsGoalCurrent = GetMetrics(24, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iCampusGroupsGoal2020 = GetMetrics(24, CampusId, start2020, end2020, 1);

            //8Wk
            iCampusGroups8Wk = GetMetrics(24, CampusId, now.AddDays(-57), now, 0);
            iCampusGroups8WkLy = GetMetrics(24, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);
            dCampusGroups8WkProgress = (((double)iCampusGroups8Wk - (double)iCampusGroups8WkLy) / (double)iCampusGroups8WkLy) * 100;

            //-------NewHere-------

            //YTD
            iNewHere = GetMetrics(14, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //LastYTD
            iNewHereLastYear = GetMetrics(14, CampusId, lastFiscalYearStartDate, lastPeriodEndDate, 0);

            //Current Goal
            iNewHereGoalCurrent = GetMetrics(14, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);

            //2020 Goal
            iNewHereGoal2020 = GetMetrics(14, CampusId, start2020, end2020, 1);

            //8Wk
            iNewHere8Wk = GetMetrics(14, CampusId, now.AddDays(-57), now, 0);
            iNewHere8WkLy = GetMetrics(14, CampusId, now.AddDays(-57).AddYears(-1), now.AddYears(-1), 0);
            dNewHere8WkProgress = (((double)iNewHere8Wk - (double)iNewHere8WkLy) / (double)iNewHere8WkLy) * 100;

            //-------Inactive Followups-------

            //Total
            iInactiveFollowup = GetMetrics(35, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //Completed
            iInactiveFollowupComplete = GetMetrics(38, CampusId, fiscalYearStartDate, periodEndDate, 0);

            //Inconplete
            iInactiveFollowupIncomplete = iInactiveFollowup - iInactiveFollowupComplete;

            //Progress
            iInactiveFollowupProgress = (double)iInactiveFollowupComplete / (double)iInactiveFollowup * 100;

            //-------Finances-------

            expenses = .99;
            giving = .95;

            //TODO: need to make a method to get these as a double (not an int)

            //Get Goals based on Global Attribute Multiplier or values stored in metrics table

            if (UseGlobalAttributeGoal == true)
            {
                iBaptismsGoalCurrent = Convert.ToInt32((double?)iBaptismsLastYear * GoalMultiplier);
                iPartnersGoalCurrent = Convert.ToInt32((double?)iPartnersLastYear * GoalMultiplier);
                iCommitmentsGoalCurrent = Convert.ToInt32((double?)iCommitmentsLastYear * GoalMultiplier);
                iRecommitmentsGoalCurrent = Convert.ToInt32((double?)iRecommitmentsLastYear * GoalMultiplier);
                iVolunteersGoalCurrent = Convert.ToInt32((double?)iVolunteersLastYear * GoalMultiplier);
                iSmallGroupParticipantsGoalCurrent = Convert.ToInt32((double?)iSmallGroupParticipantsLastYear * GoalMultiplier);
                iSmallGroupLeadersGoalCurrent = Convert.ToInt32((double?)iSmallGroupLeadersLastYear * GoalMultiplier);
                iNewtoNewPointeGoalCurrent = Convert.ToInt32((double?)iNewtoNewPointeLastYear * GoalMultiplier);
                iDiscoverGroupsGoalCurrent = Convert.ToInt32((double?)iDiscoverGroupsLastYear * GoalMultiplier);
                iCampusGroupsGoalCurrent = Convert.ToInt32((double?)iCampusGroupsLastYear * GoalMultiplier);
                iNewHereGoalCurrent = Convert.ToInt32((double?)iNewHereLastYear * GoalMultiplier);
                iSmallGroupsGoalCurrent = Convert.ToInt32((double?)iSmallGroupsLastYear * GoalMultiplier);

            }
            else
            {
                iBaptismsGoalCurrent = GetMetrics(11, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iPartnersGoalCurrent = GetMetricsLatest(20, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iCommitmentsGoalCurrent = GetMetrics(12, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iRecommitmentsGoalCurrent = GetMetrics(13, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iVolunteersGoalCurrent = GetMetricsLatest(16, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iSmallGroupParticipantsGoalCurrent = GetMetricsLatest(17, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iSmallGroupLeadersGoalCurrent = GetMetricsLatest(17, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iNewtoNewPointeGoalCurrent = GetMetrics(21, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iDiscoverGroupsGoalCurrent = GetMetrics(22, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iCampusGroupsGoalCurrent = GetMetrics(24, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iNewHereGoalCurrent = GetMetrics(14, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
                iSmallGroupsGoalCurrent = GetMetrics(34, CampusId, lastFiscalYearStartDate, fiscalYearEndDate, 1);
            }

            //-------AllCommitments-------

            //YTD
            iAllCommitments = iCommitments + iRecommitments;

            //LastYTD
            iAllCommitmentsLastYear = iCommitmentsLastYear + iRecommitmentsLastYear;

            //Current Goal
            iAllCommitmentsGoalCurrent = iCommitmentsGoalCurrent + iRecommitmentsGoalCurrent;

            //2020 Goal
            iAllCommitmentsGoal2020 = iCommitmentsGoal2020 + iRecommitmentsGoal2020;

            //Current Goal Progress
            iAllCommitmentsGoalProgress = ((double)iCommitments + (double)iRecommitments) / (((double)iCommitmentsGoalCurrent + (double)iRecommitmentsGoalCurrent) * GoalOffsetMultiplier) * 100;

            //8Wk
            iAllCommitments8Wk = iCommitments8Wk + iRecommitments8Wk;
            iAllCommitments8WkLy = iCommitments8WkLy + iRecommitments8WkLy;
            dAllCommitments8WkProgress = (((double)iAllCommitments8Wk - (double)iAllCommitments8WkLy) / (double)iAllCommitments8WkLy) * 100;

            // Caculate the progress toeards the goals
            iBaptismsGoalProgress = (double)iBaptisms / ((double)iBaptismsGoalCurrent * GoalOffsetMultiplier) * 100;
            iPartnersGoalProgress = (double)iPartners / ((double)iPartnersGoalCurrent * GoalOffsetMultiplier) * 100;
            iCommitmentsGoalProgress = (double)iCommitments / ((double)iCommitmentsGoalCurrent * GoalOffsetMultiplier) * 100;
            iRecommitmentsGoalProgress = (double)iRecommitments / ((double)iRecommitmentsGoalCurrent * GoalOffsetMultiplier) * 100;
            iVolunteersGoalProgress = (double)iVolunteers / ((double)iVolunteersGoalCurrent * GoalOffsetMultiplier) * 100;
            iSmallGroupParticipantsGoalProgress = (double)iSmallGroupParticipants / ((double)iSmallGroupParticipantsGoalCurrent * GoalOffsetMultiplier) * 100;
            iSmallGroupLeadersGoalProgress = (double)iSmallGroupLeaders / ((double)iSmallGroupLeadersGoalCurrent * GoalOffsetMultiplier) * 100;
            iNewtoNewPointeGoalProgress = (double)iNewtoNewPointe / ((double)iNewtoNewPointeGoalCurrent * GoalOffsetMultiplier) * 100;
            iDiscoverGroupsGoalProgress = (double)iDiscoverGroups / ((double)iDiscoverGroupsGoalCurrent * GoalOffsetMultiplier) * 100;
            iCampusGroupsGoalProgress = (double)iCampusGroups / ((double)iCampusGroupsGoalCurrent * GoalOffsetMultiplier) * 100;
            iNewHereGoalProgress = (double)iNewHere / ((double)iNewHereGoalCurrent * GoalOffsetMultiplier) * 100;
            iSmallGroupsGoalProgress = (double)iSmallGroups / ((double)iSmallGroupsGoalCurrent * GoalOffsetMultiplier) * 100;

            //Calculate the Composite Score
            CompositeScore = CalculateCompositeScore();

            //Save to the database

            if (CompositeScore > 0 && CompositeScore < 1000)
            {
                MetricValueService metricValueService = new MetricValueService(rockContext);

                MetricValue metricValue;

                metricValue = new MetricValue();
                metricValueService.Add(metricValue);
                metricValue.MetricId = 39;
                metricValue.Metric = metricValue.Metric ?? new MetricService(rockContext).Get(metricValue.MetricId);

                metricValue.MetricValueType = (MetricValueType)0;
                metricValue.XValue = null;
                metricValue.YValue = (decimal)CompositeScore;
                metricValue.Note = "";
                metricValue.MetricValueDateTime = dt;
                metricValue.EntityId = CampusId;

                rockContext.SaveChanges();
            }
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int campusEntityTypeId   = EntityTypeCache.Get(typeof(Rock.Model.Campus)).Id;
            int scheduleEntityTypeId = EntityTypeCache.Get(typeof(Rock.Model.Schedule)).Id;

            int?     campusId   = bddlCampus.SelectedValueAsInt();
            int?     scheduleId = bddlService.SelectedValueAsInt();
            DateTime?weekend    = bddlWeekend.SelectedValue.AsDateTime();

            if (campusId.HasValue && scheduleId.HasValue && weekend.HasValue)
            {
                using (var rockContext = new RockContext())
                {
                    var metricService      = new MetricService(rockContext);
                    var metricValueService = new MetricValueService(rockContext);

                    weekend = GetWeekendDate(scheduleId, weekend, rockContext);

                    foreach (RepeaterItem item in rptrMetric.Items)
                    {
                        var hfMetricIId   = item.FindControl("hfMetricId") as HiddenField;
                        var nbMetricValue = item.FindControl("nbMetricValue") as NumberBox;

                        if (hfMetricIId != null && nbMetricValue != null)
                        {
                            var metricYValue = nbMetricValue.Text.AsDecimalOrNull();

                            // If no value was provided and the block is not configured to default to "0" then just skip this metric.
                            if (metricYValue == null && !GetAttributeValue(AttributeKey.DefaultToZero).AsBoolean())
                            {
                                continue;
                            }

                            int metricId = hfMetricIId.ValueAsInt();
                            var metric   = new MetricService(rockContext).Get(metricId);

                            if (metric != null)
                            {
                                int campusPartitionId   = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId).Select(p => p.Id).FirstOrDefault();
                                int schedulePartitionId = metric.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId).Select(p => p.Id).FirstOrDefault();

                                var metricValue = metricValueService
                                                  .Queryable()
                                                  .Where(v =>
                                                         v.MetricId == metric.Id &&
                                                         v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value &&
                                                         v.MetricValuePartitions.Count == 2 &&
                                                         v.MetricValuePartitions.Any(p => p.MetricPartitionId == campusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value) &&
                                                         v.MetricValuePartitions.Any(p => p.MetricPartitionId == schedulePartitionId && p.EntityId.HasValue && p.EntityId.Value == scheduleId.Value))
                                                  .FirstOrDefault();

                                if (metricValue == null)
                                {
                                    metricValue = new MetricValue();
                                    metricValue.MetricValueType     = MetricValueType.Measure;
                                    metricValue.MetricId            = metric.Id;
                                    metricValue.MetricValueDateTime = weekend.Value;
                                    metricValueService.Add(metricValue);

                                    var campusValuePartition = new MetricValuePartition();
                                    campusValuePartition.MetricPartitionId = campusPartitionId;
                                    campusValuePartition.EntityId          = campusId.Value;
                                    metricValue.MetricValuePartitions.Add(campusValuePartition);

                                    var scheduleValuePartition = new MetricValuePartition();
                                    scheduleValuePartition.MetricPartitionId = schedulePartitionId;
                                    scheduleValuePartition.EntityId          = scheduleId.Value;
                                    metricValue.MetricValuePartitions.Add(scheduleValuePartition);
                                }

                                if (metricYValue == null)
                                {
                                    metricValue.YValue = 0;
                                }
                                else
                                {
                                    metricValue.YValue = metricYValue;
                                }

                                metricValue.Note = tbNote.Text;
                            }
                        }
                    }

                    rockContext.SaveChanges();
                }

                nbMetricsSaved.Text = string.Format("Your metrics for the {0} service on {1} at the {2} Campus have been saved.",
                                                    bddlService.SelectedItem.Text, bddlWeekend.SelectedItem.Text, bddlCampus.SelectedItem.Text);
                nbMetricsSaved.Visible = true;

                BindMetrics();
            }
        }
Esempio n. 39
0
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );

            if ( Page.IsPostBack )
            {
                // create dynamic controls
                var rockContext = new RockContext();
                var metricValue = new MetricValueService( rockContext ).Get( hfMetricValueId.Value.AsInteger() );
                if ( metricValue == null )
                {
                    metricValue = new MetricValue { MetricId = hfMetricId.Value.AsInteger() };
                    metricValue.Metric = new MetricService( rockContext ).Get( metricValue.MetricId );
                }

                CreateDynamicControls( metricValue, false, false );
            }

            if ( !Page.IsPostBack )
            {
                int? metricValueId = PageParameter( "MetricValueId" ).AsIntegerOrNull();

                // in case called with MetricId as the parent id parameter
                int? metricId = PageParameter( "MetricId" ).AsIntegerOrNull();

                // in case called with MetricCategoryId as the parent id parameter
                int? metricCategoryId = PageParameter( "MetricCategoryId" ).AsIntegerOrNull();
                MetricCategory metricCategory = null;
                if ( metricCategoryId.HasValue )
                {
                    if ( metricCategoryId.Value > 0 )
                    {
                        // editing a metric, but get the metricId from the metricCategory
                        metricCategory = new MetricCategoryService( new RockContext() ).Get( metricCategoryId.Value );
                        if ( metricCategory != null )
                        {
                            metricId = metricCategory.MetricId;
                        }
                    }
                    else
                    {
                        // adding a new metric. Block will (hopefully) not be shown
                        metricId = 0;
                    }
                }

                hfMetricCategoryId.Value = metricCategoryId.ToString();

                if ( metricValueId.HasValue )
                {
                    ShowDetail( metricValueId.Value, metricId );
                }
                else
                {
                    pnlDetails.Visible = false;
                }
            }
        }
Esempio n. 40
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute( IJobExecutionContext context )
        {
            var metricSourceValueTypeDataviewGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_DATAVIEW.AsGuid();
            var metricSourceValueTypeSqlGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_SQL.AsGuid();

            var metricsQry = new MetricService( new RockContext() ).Queryable().AsNoTracking().Where(
                a => a.ScheduleId.HasValue
                && a.SourceValueTypeId.HasValue
                && ( a.SourceValueType.Guid == metricSourceValueTypeDataviewGuid || a.SourceValueType.Guid == metricSourceValueTypeSqlGuid ) );

            var metricIdList = metricsQry.OrderBy( a => a.Title ).ThenBy( a => a.Subtitle ).Select( a => a.Id ).ToList();

            var metricExceptions = new List<Exception>();
            int metricsCalculated = 0;
            int metricValuesCalculated = 0;
            Metric metric = null;

            foreach ( var metricId in metricIdList )
            {
                try
                {
                    using ( var rockContextForMetricEntity = new RockContext() )
                    {
                        var metricService = new MetricService( rockContextForMetricEntity );

                        metric = metricService.Get( metricId );
                        var lastRunDateTime = metric.LastRunDateTime ?? metric.CreatedDateTime ?? metric.ModifiedDateTime;
                        if ( lastRunDateTime.HasValue )
                        {
                            var currentDateTime = RockDateTime.Now;

                            // get all the schedule times that were supposed to run since that last time it was scheduled to run
                            var scheduledDateTimesToProcess = metric.Schedule.GetScheduledStartTimes( lastRunDateTime.Value, currentDateTime ).Where( a => a > lastRunDateTime.Value ).ToList();
                            foreach ( var scheduleDateTime in scheduledDateTimesToProcess )
                            {
                                using ( var rockContextForMetricValues = new RockContext() )
                                {
                                    var metricPartitions = new MetricPartitionService( rockContextForMetricValues ).Queryable().Where( a => a.MetricId == metric.Id ).ToList();
                                    var metricValueService = new MetricValueService( rockContextForMetricValues );
                                    List<ResultValue> resultValues = new List<ResultValue>();
                                    bool getMetricValueDateTimeFromResultSet = false;
                                    if ( metric.SourceValueType.Guid == metricSourceValueTypeDataviewGuid )
                                    {
                                        // get the metric value from the DataView
                                        if ( metric.DataView != null )
                                        {
                                            var errorMessages = new List<string>();
                                            var qry = metric.DataView.GetQuery( null, null, out errorMessages );
                                            if ( metricPartitions.Count > 1 || metricPartitions.First().EntityTypeId.HasValue )
                                            {
                                                throw new NotImplementedException( "Partitioned Metrics using DataViews is not supported." );
                                            }
                                            else
                                            {
                                                var resultValue = new ResultValue();
                                                resultValue.Value = Convert.ToDecimal( qry.Count() );
                                                resultValue.Partitions = new List<ResultValuePartition>();
                                                resultValue.MetricValueDateTime = scheduleDateTime;
                                                resultValues.Add( resultValue );
                                            }
                                        }
                                    }
                                    else if ( metric.SourceValueType.Guid == metricSourceValueTypeSqlGuid )
                                    {
                                        //// calculate the metricValue using the results from the SQL
                                        //// assume SQL is in one of the following forms:
                                        //// -- "SELECT Count(*), Partion0EntityId, Partion1EntityId, Partion2EntityId,.. FROM ..."
                                        //// -- "SELECT Count(*), [MetricValueDateTime], Partion0EntityId, Partion1EntityId, Partion2EntityId,.. FROM ..."
                                        if ( !string.IsNullOrWhiteSpace( metric.SourceSql ) )
                                        {
                                            string formattedSql = metric.SourceSql.ResolveMergeFields( metric.GetMergeObjects( scheduleDateTime ) );
                                            var tableResult = DbService.GetDataTable( formattedSql, System.Data.CommandType.Text, null );

                                            if ( tableResult.Columns.Count >= 2 && tableResult.Columns[1].ColumnName == "MetricValueDateTime" )
                                            {
                                                getMetricValueDateTimeFromResultSet = true;
                                            }

                                            foreach ( var row in tableResult.Rows.OfType<System.Data.DataRow>() )
                                            {
                                                var resultValue = new ResultValue();

                                                resultValue.Value = Convert.ToDecimal( row[0] );
                                                if ( getMetricValueDateTimeFromResultSet )
                                                {
                                                    resultValue.MetricValueDateTime = Convert.ToDateTime( row[1] );
                                                }
                                                else
                                                {
                                                    resultValue.MetricValueDateTime = scheduleDateTime;
                                                }

                                                resultValue.Partitions = new List<ResultValuePartition>();
                                                int partitionPosition = 0;
                                                int partitionFieldIndex = getMetricValueDateTimeFromResultSet ? 2 : 1;
                                                int partitionColumnCount = tableResult.Columns.Count - 1;
                                                while ( partitionFieldIndex <= partitionColumnCount )
                                                {
                                                    resultValue.Partitions.Add( new ResultValuePartition
                                                    {
                                                        PartitionPosition = partitionPosition,
                                                        EntityId = row[partitionFieldIndex] as int?
                                                    } );

                                                    partitionPosition++;
                                                    partitionFieldIndex++;
                                                }

                                                resultValues.Add( resultValue );
                                            }
                                        }
                                    }

                                    metric.LastRunDateTime = scheduleDateTime;
                                    metricsCalculated++;
                                    metricValuesCalculated += resultValues.Count();

                                    if ( resultValues.Any() )
                                    {
                                        List<MetricValue> metricValuesToAdd = new List<MetricValue>();
                                        foreach ( var resultValue in resultValues )
                                        {
                                            var metricValue = new MetricValue();
                                            metricValue.MetricId = metric.Id;
                                            metricValue.MetricValueDateTime = resultValue.MetricValueDateTime;
                                            metricValue.MetricValueType = MetricValueType.Measure;
                                            metricValue.YValue = resultValue.Value;
                                            metricValue.CreatedDateTime = RockDateTime.Now;
                                            metricValue.ModifiedDateTime = RockDateTime.Now;
                                            metricValue.MetricValuePartitions = new List<MetricValuePartition>();
                                            var metricPartitionsByPosition = metricPartitions.OrderBy( a => a.Order ).ToList();

                                            if ( !resultValue.Partitions.Any() && metricPartitionsByPosition.Count() == 1 && !metricPartitionsByPosition[0].EntityTypeId.HasValue )
                                            {
                                                // a metric with just the default partition (not partitioned by Entity)
                                                var metricPartition = metricPartitionsByPosition[0];
                                                var metricValuePartition = new MetricValuePartition();
                                                metricValuePartition.MetricPartition = metricPartition;
                                                metricValuePartition.MetricPartitionId = metricPartition.Id;
                                                metricValuePartition.MetricValue = metricValue;
                                                metricValuePartition.EntityId = null;
                                                metricValue.MetricValuePartitions.Add( metricValuePartition );
                                            }
                                            else
                                            {
                                                foreach ( var partitionResult in resultValue.Partitions )
                                                {
                                                    if ( metricPartitionsByPosition.Count > partitionResult.PartitionPosition )
                                                    {
                                                        var metricPartition = metricPartitionsByPosition[partitionResult.PartitionPosition];
                                                        var metricValuePartition = new MetricValuePartition();
                                                        metricValuePartition.MetricPartition = metricPartition;
                                                        metricValuePartition.MetricPartitionId = metricPartition.Id;
                                                        metricValuePartition.MetricValue = metricValue;
                                                        metricValuePartition.EntityId = partitionResult.EntityId;
                                                        metricValue.MetricValuePartitions.Add( metricValuePartition );
                                                    }
                                                }
                                            }

                                            if ( metricValue.MetricValuePartitions == null || !metricValue.MetricValuePartitions.Any() )
                                            {
                                                // shouldn't happen, but just in case
                                                throw new Exception( "MetricValue requires at least one Partition Value" );
                                            }
                                            else
                                            {
                                                metricValuesToAdd.Add( metricValue );
                                            }
                                        }

                                        // if a single metricValueDateTime was specified, delete any existing metric values for this date and refresh with the current results
                                        var dbTransaction = rockContextForMetricValues.Database.BeginTransaction();
                                        if ( getMetricValueDateTimeFromResultSet )
                                        {
                                            var metricValueDateTimes = metricValuesToAdd.Select( a => a.MetricValueDateTime ).Distinct().ToList();
                                            foreach ( var metricValueDateTime in metricValueDateTimes )
                                            {
                                                bool alreadyHasMetricValues = metricValueService.Queryable().Where( a => a.MetricId == metric.Id && a.MetricValueDateTime == metricValueDateTime ).Any();
                                                if ( alreadyHasMetricValues )
                                                {
                                                    // use direct SQL to clean up any existing metric values
                                                    rockContextForMetricValues.Database.ExecuteSqlCommand( @"
                                                        DELETE
                                                        FROM MetricValuePartition
                                                        WHERE MetricValueId IN (
                                                            SELECT Id
                                                            FROM MetricValue
                                                            WHERE MetricId = @metricId
                                                            AND MetricValueDateTime = @metricValueDateTime
                                                        )
                                                    ", new SqlParameter( "@metricId", metric.Id ), new SqlParameter( "@metricValueDateTime", metricValueDateTime ) );

                                                    rockContextForMetricValues.Database.ExecuteSqlCommand( @"
                                                        DELETE
                                                        FROM MetricValue
                                                        WHERE MetricId = @metricId
                                                        AND MetricValueDateTime = @metricValueDateTime
                                                    ", new SqlParameter( "@metricId", metric.Id ), new SqlParameter( "@metricValueDateTime", metricValueDateTime ) );
                                                }
                                            }
                                        }

                                        metricValueService.AddRange( metricValuesToAdd );

                                        // disable savechanges PrePostProcessing since there could be hundreds or thousands of metric values getting inserted/updated
                                        rockContextForMetricValues.SaveChanges( true );
                                        dbTransaction.Commit();
                                    }

                                    rockContextForMetricEntity.SaveChanges();
                                }
                            }
                        }
                    }
                }
                catch ( Exception ex )
                {
                    metricExceptions.Add( new Exception( string.Format( "Exception when calculating metric for {0} ", metric ), ex ) );
                }
            }

            context.Result = string.Format( "Calculated a total of {0} metric values for {1} metrics", metricValuesCalculated, metricsCalculated );

            if ( metricExceptions.Any() )
            {
                throw new AggregateException( "One or more metric calculations failed ", metricExceptions );
            }
        }
Esempio n. 41
0
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="metricValueId">The metric value identifier.</param>
        /// <param name="metricId">The metric identifier.</param>
        public void ShowDetail( int metricValueId, int? metricId )
        {
            pnlDetails.Visible = true;

            // Load depending on Add(0) or Edit
            MetricValue metricValue = null;
            if ( !metricValueId.Equals( 0 ) )
            {
                metricValue = new MetricValueService( new RockContext() ).Get( metricValueId );
                lActionTitle.Text = ActionTitle.Edit( MetricValue.FriendlyTypeName ).FormatAsHtmlTitle();
                pdAuditDetails.SetEntity( metricValue, ResolveRockUrl( "~" ) );
            }

            if ( metricValue == null && metricId.HasValue )
            {
                metricValue = new MetricValue { Id = 0, MetricId = metricId.Value };
                metricValue.Metric = metricValue.Metric ?? new MetricService( new RockContext() ).Get( metricValue.MetricId );
                lActionTitle.Text = ActionTitle.Add( MetricValue.FriendlyTypeName ).FormatAsHtmlTitle();
                // hide the panel drawer that show created and last modified dates
                pdAuditDetails.Visible = false;
            }

            hfMetricValueId.Value = metricValue.Id.ToString();

            LoadDropDowns();

            ddlMetricValueType.SelectedValue = metricValue.MetricValueType.ConvertToInt().ToString();
            tbXValue.Text = metricValue.XValue;
            tbYValue.Text = metricValue.YValue.ToString();
            hfMetricId.Value = metricValue.MetricId.ToString();
            tbNote.Text = metricValue.Note;
            dpMetricValueDateTime.SelectedDate = metricValue.MetricValueDateTime;

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;

            bool canEdit = UserCanEdit;
            if ( !canEdit && metricId.HasValue && metricId.Value > 0 )
            {
                var metric = new MetricService( new RockContext() ).Get( metricId.Value );
                if ( metric != null && metric.IsAuthorized( Authorization.EDIT, CurrentPerson ) )
                {
                    canEdit = true;
                }
            }

            if ( !canEdit )
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( MetricValue.FriendlyTypeName );
            }

            if ( readOnly )
            {
                lActionTitle.Text = ActionTitle.View( MetricValue.FriendlyTypeName );
                btnCancel.Text = "Close";
            }

            CreateDynamicControls( metricValue, true, readOnly );

            ddlMetricValueType.Enabled = !readOnly;
            tbXValue.ReadOnly = readOnly;
            tbYValue.ReadOnly = readOnly;
            tbNote.ReadOnly = readOnly;
            dpMetricValueDateTime.Enabled = !readOnly;

            btnSave.Visible = !readOnly;
        }
Esempio n. 42
0
        private List <MetricValue> readMetricValuesIntoEntity(MOBILEEntityBase entity, JobTarget jobTarget, string metricName, AppDRESTMetric appDRESTMetric, int timerangeDuration)
        {
            List <MetricValue> metricValues = new List <MetricValue>(appDRESTMetric.metricValues.Count);

            foreach (AppDRESTMetricValue appDRESTMetricValue in appDRESTMetric.metricValues)
            {
                // Populate metrics into the list for output into CSV
                MetricValue metricValue = new MetricValue();

                metricValue.Controller      = jobTarget.Controller;
                metricValue.ApplicationID   = jobTarget.ApplicationID;
                metricValue.ApplicationName = jobTarget.Application;

                if (entity != null)
                {
                    if (entity is MOBILEApplication)
                    {
                        MOBILEApplication mobileApplication = (MOBILEApplication)entity;

                        metricValue.EntityID   = mobileApplication.ApplicationID;
                        metricValue.EntityName = mobileApplication.ApplicationName;
                        metricValue.EntityType = MOBILEApplication.ENTITY_TYPE;
                    }
                    else if (entity is MOBILENetworkRequest)
                    {
                        MOBILENetworkRequest networkRequest = (MOBILENetworkRequest)entity;

                        metricValue.EntityID   = networkRequest.RequestID;
                        metricValue.EntityName = networkRequest.RequestName;
                        metricValue.EntityType = MOBILENetworkRequest.ENTITY_TYPE;
                    }
                }

                metricValue.EventTimeStampUtc = UnixTimeHelper.ConvertFromUnixTimestamp(appDRESTMetricValue.startTimeInMillis);
                metricValue.EventTimeStamp    = metricValue.EventTimeStampUtc.ToLocalTime();
                metricValue.EventTime         = metricValue.EventTimeStamp;

                metricValue.MetricName = metricName;
                metricValue.MetricID   = appDRESTMetric.metricId;
                switch (appDRESTMetric.frequency)
                {
                case "SIXTY_MIN":
                {
                    metricValue.MetricResolution = 60;
                    break;
                }

                case "TEN_MIN":
                {
                    metricValue.MetricResolution = 10;
                    break;
                }

                case "ONE_MIN":
                {
                    metricValue.MetricResolution = 1;
                    break;
                }

                default:
                {
                    metricValue.MetricResolution = 1;
                    break;
                }
                }

                metricValue.Count       = appDRESTMetricValue.count;
                metricValue.Min         = appDRESTMetricValue.min;
                metricValue.Max         = appDRESTMetricValue.max;
                metricValue.Occurrences = appDRESTMetricValue.occurrences;
                metricValue.Sum         = appDRESTMetricValue.sum;
                metricValue.Value       = appDRESTMetricValue.value;

                metricValues.Add(metricValue);
            }

            return(metricValues);
        }
Esempio n. 43
0
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="metricValueId">The metric value identifier.</param>
        /// <param name="metricId">The metric identifier.</param>
        public void ShowDetail( int metricValueId, int? metricId )
        {
            pnlDetails.Visible = true;

            // Load depending on Add(0) or Edit
            MetricValue metricValue = null;
            if ( !metricValueId.Equals( 0 ) )
            {
                metricValue = new MetricValueService( new RockContext() ).Get( metricValueId );
                lActionTitle.Text = ActionTitle.Edit( MetricValue.FriendlyTypeName ).FormatAsHtmlTitle();
            }

            if ( metricValue == null && metricId.HasValue )
            {
                metricValue = new MetricValue { Id = 0, MetricId = metricId.Value };
                metricValue.Metric = metricValue.Metric ?? new MetricService( new RockContext() ).Get( metricValue.MetricId );
                lActionTitle.Text = ActionTitle.Add( MetricValue.FriendlyTypeName ).FormatAsHtmlTitle();
            }

            hfMetricValueId.Value = metricValue.Id.ToString();

            LoadDropDowns();

            ddlMetricValueType.SelectedValue = metricValue.MetricValueType.ConvertToInt().ToString();
            tbXValue.Text = metricValue.XValue;
            tbYValue.Text = metricValue.YValue.ToString();
            hfMetricId.Value = metricValue.MetricId.ToString();
            tbNote.Text = metricValue.Note;
            dpMetricValueDateTime.SelectedDate = metricValue.MetricValueDateTime;

            var metricEntityType = EntityTypeCache.Read( metricValue.Metric.EntityTypeId ?? 0 );

            // Setup EntityType UI controls
            Control entityTypeEditControl = null;
            if ( metricEntityType != null && metricEntityType.SingleValueFieldType != null )
            {
                hfSingleValueFieldTypeId.Value = metricEntityType.SingleValueFieldType.Id.ToString();
                FieldTypeCache fieldType = FieldTypeCache.Read( hfSingleValueFieldTypeId.Value.AsInteger() );
                entityTypeEditControl = fieldType.Field.EditControl( new Dictionary<string, Rock.Field.ConfigurationValue>(), "entityTypeEditControl" );

                if ( entityTypeEditControl is IRockControl )
                {
                    ( entityTypeEditControl as IRockControl ).Label = fieldType.Name;
                }

                phEntityTypeEntityIdValue.Controls.Add( entityTypeEditControl );
                IEntityFieldType entityFieldType = metricEntityType.SingleValueFieldType.Field as IEntityFieldType;
                if ( entityFieldType != null )
                {
                    entityFieldType.SetEditValueFromEntityId( entityTypeEditControl, new Dictionary<string, ConfigurationValue>(), metricValue.EntityId );
                }
            }

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if ( !IsUserAuthorized( Authorization.EDIT ) )
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( MetricValue.FriendlyTypeName );
            }

            if ( readOnly )
            {
                lActionTitle.Text = ActionTitle.View( MetricValue.FriendlyTypeName );
                btnCancel.Text = "Close";
            }

            ddlMetricValueType.Enabled = !readOnly;
            tbXValue.ReadOnly = readOnly;
            tbYValue.ReadOnly = readOnly;
            tbNote.ReadOnly = readOnly;
            dpMetricValueDateTime.Enabled = !readOnly;
            if ( entityTypeEditControl is WebControl )
            {
                ( entityTypeEditControl as WebControl ).Enabled = !readOnly;
            }

            btnSave.Visible = !readOnly;
        }
Esempio n. 44
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            var dataMap        = context.JobDetail.JobDataMap;
            var commandTimeout = dataMap.GetString(AttributeKey.CommandTimeout).AsIntegerOrNull() ?? 300;
            var metricSourceValueTypeDataviewGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_DATAVIEW.AsGuid();
            var metricSourceValueTypeSqlGuid      = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_SQL.AsGuid();
            var metricSourceValueTypeLavaGuid     = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_LAVA.AsGuid();

            Guid[] calculatedSourceTypes = new Guid[] {
                metricSourceValueTypeDataviewGuid,
                metricSourceValueTypeSqlGuid,
                metricSourceValueTypeLavaGuid
            };

            var metricsQry = new MetricService(new RockContext()).Queryable().AsNoTracking().Where(
                a => a.ScheduleId.HasValue &&
                a.SourceValueTypeId.HasValue &&
                calculatedSourceTypes.Contains(a.SourceValueType.Guid));

            var metricIdList = metricsQry.OrderBy(a => a.Title).ThenBy(a => a.Subtitle).Select(a => a.Id).ToList();

            var    metricExceptions       = new List <Exception>();
            int    metricsCalculated      = 0;
            int    metricValuesCalculated = 0;
            Metric metric = null;

            foreach (var metricId in metricIdList)
            {
                try
                {
                    using (var rockContextForMetricEntity = new RockContext())
                    {
                        rockContextForMetricEntity.Database.CommandTimeout = commandTimeout;
                        var metricService = new MetricService(rockContextForMetricEntity);

                        metric = metricService.Get(metricId);

                        /*  Get the last time the metric has run, if it has never run then set it as the first day of the current week to give it a chance to run now.
                         *  NOTE: This is being used instead of Min Date to prevent a schedule with a start date of months or years back from running for each period since then.
                         *  This would be the case for the "out-of-the-box" Rock daily metrics "Active Records", "Active Families", and "Active Connection Requests" if they
                         *  have never run before. Or if a new user created metric uses a schedule that has an older start date.
                         */
                        DateTime currentDateTime = RockDateTime.Now;
                        DateTime?startOfWeek     = currentDateTime.StartOfWeek(RockDateTime.FirstDayOfWeek);
                        DateTime?lastRunDateTime = metric.LastRunDateTime ?? startOfWeek;

                        // get all the schedule times that were supposed to run since that last time it was scheduled to run
                        var scheduledDateTimesToProcess = metric.Schedule.GetScheduledStartTimes(lastRunDateTime.Value, currentDateTime).Where(a => a > lastRunDateTime.Value).ToList();
                        foreach (var scheduleDateTime in scheduledDateTimesToProcess)
                        {
                            using (var rockContextForMetricValues = new RockContext())
                            {
                                rockContextForMetricValues.Database.CommandTimeout = commandTimeout;
                                var metricPartitions                     = new MetricPartitionService(rockContextForMetricValues).Queryable().Where(a => a.MetricId == metric.Id).ToList();
                                var metricValueService                   = new MetricValueService(rockContextForMetricValues);
                                List <ResultValue> resultValues          = new List <ResultValue>();
                                bool getMetricValueDateTimeFromResultSet = false;
                                if (metric.SourceValueType.Guid == metricSourceValueTypeDataviewGuid)
                                {
                                    // get the metric value from the DataView
                                    if (metric.DataView != null)
                                    {
                                        bool parseCampusPartition = metricPartitions.Count == 1 &&
                                                                    metric.AutoPartitionOnPrimaryCampus &&
                                                                    metric.DataView.EntityTypeId == Web.Cache.EntityTypeCache.GetId(SystemGuid.EntityType.PERSON) &&
                                                                    metricPartitions[0].EntityTypeId == Web.Cache.EntityTypeCache.GetId(SystemGuid.EntityType.CAMPUS);

                                        // Dataview metrics can be partitioned by campus only and AutoPartitionOnPrimaryCampus must be selected.
                                        if (metricPartitions.Count > 1 ||
                                            (metricPartitions[0].EntityTypeId.HasValue && parseCampusPartition == false))
                                        {
                                            throw new NotImplementedException("Partitioned Metrics using DataViews is only supported for Person data views using a single partition of type 'Campus' with 'Auto Partition on Primary Campus' checked. Any other dataview partition configuration is not supported.");
                                        }

                                        Stopwatch stopwatch = Stopwatch.StartNew();
                                        var       qry       = metric.DataView.GetQuery(new DataViewGetQueryArgs());

                                        if (parseCampusPartition)
                                        {
                                            // Create a dictionary of campus IDs with a person count
                                            var campusPartitionValues = new Dictionary <int, int>();
                                            foreach (var person in qry.ToList())
                                            {
                                                var iPerson  = ( Person )person;
                                                var campusId = iPerson.GetCampus()?.Id ?? -1;
                                                campusPartitionValues.TryGetValue(campusId, out var currentCount);
                                                campusPartitionValues[campusId] = currentCount + 1;
                                            }

                                            // Use the dictionary to create the ResultValues for each campus (partition)
                                            foreach (var campusPartitionValue in campusPartitionValues)
                                            {
                                                var resultValue = new ResultValue
                                                {
                                                    MetricValueDateTime = scheduleDateTime,
                                                    Partitions          = new List <ResultValuePartition>(),
                                                    Value = campusPartitionValue.Value
                                                };

                                                int?entityId = campusPartitionValue.Key;
                                                if (entityId == -1)
                                                {
                                                    entityId = null;
                                                }

                                                resultValue.Partitions.Add(new ResultValuePartition
                                                {
                                                    PartitionPosition = 0,
                                                    EntityId          = entityId
                                                });

                                                resultValues.Add(resultValue);
                                            }
                                        }
                                        else
                                        {
                                            // Put the entire set in one result since there is no partition
                                            resultValues.Add(new ResultValue
                                            {
                                                Value = Convert.ToDecimal(qry.Count()),
                                                MetricValueDateTime = scheduleDateTime,
                                                Partitions          = new List <ResultValuePartition>()
                                            });
                                        }

                                        stopwatch.Stop();
                                        DataViewService.AddRunDataViewTransaction(metric.DataView.Id, Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds));
                                    }
                                }
                                else if (metric.SourceValueType.Guid == metricSourceValueTypeSqlGuid)
                                {
                                    //// calculate the metricValue using the results from the SQL
                                    //// assume SQL is in one of the following forms:
                                    //// -- "SELECT Count(*) FROM ..."
                                    //// -- "SELECT Count(*), [MetricValueDateTime] FROM ..."
                                    //// -- "SELECT Count(*), Partition0EntityId, Partition1EntityId, Partition2EntityId,.. FROM ..."
                                    //// -- "SELECT Count(*), [MetricValueDateTime], Partition0EntityId, Partition1EntityId, Partition2EntityId,.. FROM ..."
                                    if (!string.IsNullOrWhiteSpace(metric.SourceSql))
                                    {
                                        string formattedSql = metric.SourceSql.ResolveMergeFields(metric.GetMergeObjects(scheduleDateTime));
                                        var    tableResult  = DbService.GetDataTable(formattedSql, System.Data.CommandType.Text, null);

                                        if (tableResult.Columns.Count >= 2 && tableResult.Columns[1].ColumnName == "MetricValueDateTime")
                                        {
                                            getMetricValueDateTimeFromResultSet = true;
                                        }

                                        foreach (var row in tableResult.Rows.OfType <System.Data.DataRow>())
                                        {
                                            var resultValue = new ResultValue();

                                            resultValue.Value = Convert.ToDecimal(row[0] == DBNull.Value ? 0 : row[0]);
                                            if (getMetricValueDateTimeFromResultSet)
                                            {
                                                resultValue.MetricValueDateTime = Convert.ToDateTime(row[1]);
                                            }
                                            else
                                            {
                                                resultValue.MetricValueDateTime = scheduleDateTime;
                                            }

                                            resultValue.Partitions = new List <ResultValuePartition>();
                                            int partitionPosition    = 0;
                                            int partitionFieldIndex  = getMetricValueDateTimeFromResultSet ? 2 : 1;
                                            int partitionColumnCount = tableResult.Columns.Count - 1;
                                            while (partitionFieldIndex <= partitionColumnCount)
                                            {
                                                resultValue.Partitions.Add(new ResultValuePartition
                                                {
                                                    PartitionPosition = partitionPosition,
                                                    EntityId          = row[partitionFieldIndex] as int?
                                                });

                                                partitionPosition++;
                                                partitionFieldIndex++;
                                            }

                                            resultValues.Add(resultValue);
                                        }
                                    }
                                }
                                else if (metric.SourceValueType.Guid == metricSourceValueTypeLavaGuid)
                                {
                                    //// calculate the metricValue using the results from Lava
                                    //// assume Lava Output is in one of the following forms:
                                    //// A single Count
                                    //// 42
                                    //// A List of Count, MetricValueDateTime
                                    //// 42, 1/1/2017
                                    //// 40, 1/2/2017
                                    //// 49, 1/3/2017
                                    //// A List of Count, Partition0EntityId, Partition1EntityId, Partition2EntityId
                                    //// 42, 201, 450, 654
                                    //// 42, 202, 450, 654
                                    //// 42, 203, 450, 654
                                    //// A List of Count, MetricValueDateTime,  Partition0EntityId, Partition1EntityId, Partition2EntityId
                                    //// 42, 1/1/2017, 201, 450, 654
                                    //// 42, 1/2/2017, 202, 450, 654
                                    //// 42, 1/3/2017, 203, 450, 654
                                    if (!string.IsNullOrWhiteSpace(metric.SourceLava))
                                    {
                                        var           mergeObjects = metric.GetMergeObjects(scheduleDateTime);
                                        string        lavaResult   = metric.SourceLava.ResolveMergeFields(mergeObjects, enabledLavaCommands: "All", throwExceptionOnErrors: true);
                                        List <string> resultLines  = lavaResult.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                                                     .Select(a => a.Trim()).Where(a => !string.IsNullOrEmpty(a)).ToList();
                                        List <string[]> resultList = resultLines.Select(a => a.SplitDelimitedValues()).ToList();

                                        if (resultList.Any())
                                        {
                                            if (resultList[0].Length >= 2)
                                            {
                                                // if the value of the data in the 2nd column is a Date, assume that is is the MetricValueDateTime
                                                if (resultList[0][1].AsDateTime().HasValue)
                                                {
                                                    getMetricValueDateTimeFromResultSet = true;
                                                }
                                            }
                                        }

                                        foreach (var row in resultList)
                                        {
                                            var resultValue = new ResultValue();

                                            resultValue.Value = row[0].AsDecimal();
                                            if (getMetricValueDateTimeFromResultSet)
                                            {
                                                resultValue.MetricValueDateTime = row[1].AsDateTime() ?? scheduleDateTime;
                                            }
                                            else
                                            {
                                                resultValue.MetricValueDateTime = scheduleDateTime;
                                            }

                                            resultValue.Partitions = new List <ResultValuePartition>();
                                            int partitionPosition    = 0;
                                            int partitionFieldIndex  = getMetricValueDateTimeFromResultSet ? 2 : 1;
                                            int partitionColumnCount = row.Length - 1;
                                            while (partitionFieldIndex <= partitionColumnCount)
                                            {
                                                resultValue.Partitions.Add(new ResultValuePartition
                                                {
                                                    PartitionPosition = partitionPosition,
                                                    EntityId          = row[partitionFieldIndex].AsIntegerOrNull()
                                                });

                                                partitionPosition++;
                                                partitionFieldIndex++;
                                            }

                                            resultValues.Add(resultValue);
                                        }
                                    }
                                }

                                metric.LastRunDateTime = scheduleDateTime;
                                metricsCalculated++;
                                metricValuesCalculated += resultValues.Count();

                                if (resultValues.Any())
                                {
                                    List <MetricValue> metricValuesToAdd = new List <MetricValue>();
                                    foreach (var resultValue in resultValues)
                                    {
                                        var metricValue = new MetricValue();
                                        metricValue.MetricId            = metric.Id;
                                        metricValue.MetricValueDateTime = resultValue.MetricValueDateTime;
                                        metricValue.MetricValueType     = MetricValueType.Measure;
                                        metricValue.YValue                = resultValue.Value;
                                        metricValue.CreatedDateTime       = RockDateTime.Now;
                                        metricValue.ModifiedDateTime      = RockDateTime.Now;
                                        metricValue.MetricValuePartitions = new List <MetricValuePartition>();
                                        var metricPartitionsByPosition = metricPartitions.OrderBy(a => a.Order).ToList();

                                        if (!resultValue.Partitions.Any() && metricPartitionsByPosition.Count() == 1 && !metricPartitionsByPosition[0].EntityTypeId.HasValue)
                                        {
                                            // a metric with just the default partition (not partitioned by Entity)
                                            var metricPartition      = metricPartitionsByPosition[0];
                                            var metricValuePartition = new MetricValuePartition();
                                            metricValuePartition.MetricPartition   = metricPartition;
                                            metricValuePartition.MetricPartitionId = metricPartition.Id;
                                            metricValuePartition.MetricValue       = metricValue;
                                            metricValuePartition.EntityId          = null;
                                            metricValue.MetricValuePartitions.Add(metricValuePartition);
                                        }
                                        else
                                        {
                                            foreach (var partitionResult in resultValue.Partitions)
                                            {
                                                if (metricPartitionsByPosition.Count > partitionResult.PartitionPosition)
                                                {
                                                    var metricPartition      = metricPartitionsByPosition[partitionResult.PartitionPosition];
                                                    var metricValuePartition = new MetricValuePartition();
                                                    metricValuePartition.MetricPartition   = metricPartition;
                                                    metricValuePartition.MetricPartitionId = metricPartition.Id;
                                                    metricValuePartition.MetricValue       = metricValue;
                                                    metricValuePartition.EntityId          = partitionResult.EntityId;
                                                    metricValue.MetricValuePartitions.Add(metricValuePartition);
                                                }
                                            }
                                        }

                                        if (metricValue.MetricValuePartitions == null || !metricValue.MetricValuePartitions.Any())
                                        {
                                            // shouldn't happen, but just in case
                                            throw new Exception("MetricValue requires at least one Partition Value");
                                        }
                                        else
                                        {
                                            metricValuesToAdd.Add(metricValue);
                                        }
                                    }

                                    // if a single metricValueDateTime was specified, delete any existing metric values for this date and refresh with the current results
                                    var dbTransaction = rockContextForMetricValues.Database.BeginTransaction();
                                    if (getMetricValueDateTimeFromResultSet)
                                    {
                                        var metricValueDateTimes = metricValuesToAdd.Select(a => a.MetricValueDateTime).Distinct().ToList();
                                        foreach (var metricValueDateTime in metricValueDateTimes)
                                        {
                                            bool alreadyHasMetricValues = metricValueService.Queryable().Where(a => a.MetricId == metric.Id && a.MetricValueDateTime == metricValueDateTime).Any();
                                            if (alreadyHasMetricValues)
                                            {
                                                // use direct SQL to clean up any existing metric values
                                                rockContextForMetricValues.Database.ExecuteSqlCommand(@"
                                                    DELETE
                                                    FROM MetricValuePartition
                                                    WHERE MetricValueId IN (
                                                        SELECT Id
                                                        FROM MetricValue
                                                        WHERE MetricId = @metricId
                                                        AND MetricValueDateTime = @metricValueDateTime
                                                    )
                                                ", new SqlParameter("@metricId", metric.Id), new SqlParameter("@metricValueDateTime", metricValueDateTime));

                                                rockContextForMetricValues.Database.ExecuteSqlCommand(@"
                                                    DELETE
                                                    FROM MetricValue
                                                    WHERE MetricId = @metricId
                                                    AND MetricValueDateTime = @metricValueDateTime
                                                ", new SqlParameter("@metricId", metric.Id), new SqlParameter("@metricValueDateTime", metricValueDateTime));
                                            }
                                        }
                                    }

                                    metricValueService.AddRange(metricValuesToAdd);

                                    // disable savechanges PrePostProcessing since there could be hundreds or thousands of metric values getting inserted/updated
                                    rockContextForMetricValues.SaveChanges(true);
                                    dbTransaction.Commit();
                                }

                                rockContextForMetricEntity.SaveChanges();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    metricExceptions.Add(new Exception($"Exception when calculating metric for {metric}: {ex.Message}", ex));
                }
            }

            context.Result = string.Format("Calculated a total of {0} metric values for {1} metrics", metricValuesCalculated, metricsCalculated);

            if (metricExceptions.Any())
            {
                throw new AggregateException("One or more metric calculations failed ", metricExceptions);
            }
        }
Esempio n. 45
0
        /// <summary>
        /// Shows the edit modal.
        /// </summary>
        /// <param name="valueId">The value unique identifier.</param>
        protected void ShowEdit( int valueId )
        {
            var metricId = hfMetricId.ValueAsInt();
            var metric = new MetricService().Get( metricId );

            MetricValue metricValue = null;
            if ( !valueId.Equals( 0 ) )
            {
                metricValue = new MetricValueService().Get( valueId );
                if ( metric != null )
                {
                    lActionTitle.Text = ActionTitle.Edit( "metric value for " + metric.Title );
                }
            }
            else
            {
                metricValue = new MetricValue { Id = 0 };
                metricValue.MetricId = metricId;
                if ( metric != null )
                {
                    lActionTitle.Text = ActionTitle.Add( "metric value for " + metric.Title );
                }
            }

            hfMetricValueId.SetValue( metricValue.Id );
            ddlMetricFilter.SelectedValue = hfMetricId.Value;
            tbValue.Text = metricValue.Value;
            tbValueDescription.Text = metricValue.Description;
            tbXValue.Text = metricValue.xValue;
            tbLabel.Text = metricValue.Label;
            cbIsDateBased.Checked = metricValue.isDateBased;

            mdValueDialog.Show();
        }
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute( IJobExecutionContext context )
        {
            var rockContext = new RockContext();
            var metricService = new MetricService( rockContext );
            var metricValueService = new MetricValueService( rockContext );

            var metricSourceValueTypeDataviewGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_DATAVIEW.AsGuid();
            var metricSourceValueTypeSqlGuid = Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_SQL.AsGuid();

            var metricsQry = metricService.Queryable( "Schedule" ).Where(
                a => a.ScheduleId.HasValue
                && a.SourceValueTypeId.HasValue
                && ( a.SourceValueType.Guid == metricSourceValueTypeDataviewGuid || a.SourceValueType.Guid == metricSourceValueTypeSqlGuid ) );

            var metricsList = metricsQry.OrderBy( a => a.Title ).ThenBy( a => a.Subtitle ).ToList();

            var metricExceptions = new List<Exception>();

            foreach ( var metric in metricsList )
            {
                try
                {
                    var lastRunDateTime = metric.LastRunDateTime ?? metric.CreatedDateTime ?? metric.ModifiedDateTime;
                    if ( lastRunDateTime.HasValue )
                    {
                        var currentDateTime = RockDateTime.Now;
                        // get all the schedule times that were supposed to run since that last time it was scheduled to run
                        var scheduledDateTimesToProcess = metric.Schedule.GetScheduledStartTimes( lastRunDateTime.Value, currentDateTime ).Where( a => a > lastRunDateTime.Value ).ToList();
                        foreach ( var scheduleDateTime in scheduledDateTimesToProcess )
                        {
                            Dictionary<int, decimal> resultValues = new Dictionary<int, decimal>();
                            if ( metric.SourceValueType.Guid == metricSourceValueTypeDataviewGuid )
                            {
                                // get the metric value from the DataView
                                if ( metric.DataView != null )
                                {
                                    var errorMessages = new List<string>();
                                    var qry = metric.DataView.GetQuery( null, null, out errorMessages );
                                    if ( metric.EntityTypeId.HasValue )
                                    {
                                        throw new NotImplementedException( "Partitioned Metrics using DataViews is not supported." );
                                    }
                                    else
                                    {
                                        resultValues.Add( 0, qry.Count() );
                                    }
                                }
                            }
                            else if ( metric.SourceValueType.Guid == metricSourceValueTypeSqlGuid )
                            {
                                // calculate the metricValue assuming that the SQL returns one row with one numeric field
                                if ( !string.IsNullOrWhiteSpace( metric.SourceSql ) )
                                {
                                    string formattedSql = metric.SourceSql.ResolveMergeFields( metric.GetMergeObjects( scheduleDateTime ) );
                                    var tableResult = DbService.GetDataTable( formattedSql, System.Data.CommandType.Text, null );
                                    foreach ( var row in tableResult.Rows.OfType<System.Data.DataRow>() )
                                    {
                                        int entityId = 0;
                                        decimal countValue;
                                        if ( tableResult.Columns.Count >= 2 )
                                        {
                                            if ( tableResult.Columns.Contains( "EntityId" ) )
                                            {
                                                entityId = Convert.ToInt32( row["EntityId"] );
                                            }
                                            else
                                            {
                                                // assume SQL is in the form "SELECT Count(*), EntityId FROM ..."
                                                entityId = Convert.ToInt32( row[1] );
                                            }
                                        }

                                        if ( tableResult.Columns.Contains( "Value" ) )
                                        {
                                            countValue = Convert.ToDecimal( row["Value"] );
                                        }
                                        else
                                        {
                                            // assume SQL is in the form "SELECT Count(*), EntityId FROM ..."
                                            countValue = Convert.ToDecimal( row[0] );
                                        }

                                        resultValues.Add( entityId, countValue );
                                    }
                                }
                            }

                            metric.LastRunDateTime = scheduleDateTime;

                            if ( resultValues.Any() )
                            {
                                foreach ( var resultValue in resultValues )
                                {
                                    var metricValue = new MetricValue();
                                    metricValue.MetricId = metric.Id;
                                    metricValue.MetricValueDateTime = scheduleDateTime;
                                    metricValue.MetricValueType = MetricValueType.Measure;
                                    metricValue.YValue = resultValue.Value;
                                    metricValue.EntityId = resultValue.Key > 0 ? resultValue.Key : (int?)null;

                                    metricValueService.Add( metricValue );
                                }
                            }

                            rockContext.SaveChanges();
                        }
                    }
                }
                catch ( Exception ex )
                {
                    metricExceptions.Add( new Exception( string.Format( "Exception when calculating metric for ", metric ), ex ) );
                }
            }

            if ( metricExceptions.Any() )
            {
                throw new AggregateException( "One or more metric calculations failed ", metricExceptions );
            }
        }