/// <summary> /// Binds the metrics. /// </summary> private void BindMetrics() { var serviceMetricValues = new List <ServiceMetric>(); 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(); var notes = new List <string>(); if (campusId.HasValue && scheduleId.HasValue && weekend.HasValue) { SetBlockUserPreference("CampusId", campusId.HasValue ? campusId.Value.ToString() : ""); SetBlockUserPreference("ScheduleId", scheduleId.HasValue ? scheduleId.Value.ToString() : ""); var metricCategories = MetricCategoriesFieldAttribute.GetValueAsGuidPairs(GetAttributeValue("MetricCategories")); var metricGuids = metricCategories.Select(a => a.MetricGuid).ToList(); using (var rockContext = new RockContext()) { var metricValueService = new MetricValueService(rockContext); foreach (var metric in new MetricService(rockContext) .GetByGuids(metricGuids) .Where(m => m.MetricPartitions.Count == 2 && m.MetricPartitions.Any(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId) && m.MetricPartitions.Any(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId)) .OrderBy(m => m.Title) .Select(m => new { m.Id, m.Title, CampusPartitionId = m.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId).Select(p => p.Id).FirstOrDefault(), SchedulePartitionId = m.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId).Select(p => p.Id).FirstOrDefault(), })) { var serviceMetric = new ServiceMetric(metric.Id, metric.Title); if (campusId.HasValue && weekend.HasValue && scheduleId.HasValue) { var metricValue = metricValueService .Queryable().AsNoTracking() .Where(v => v.MetricId == metric.Id && v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value && v.MetricValuePartitions.Count == 2 && v.MetricValuePartitions.Any(p => p.MetricPartitionId == metric.CampusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value) && v.MetricValuePartitions.Any(p => p.MetricPartitionId == metric.SchedulePartitionId && p.EntityId.HasValue && p.EntityId.Value == scheduleId.Value)) .FirstOrDefault(); if (metricValue != null) { serviceMetric.Value = metricValue.YValue; if (!string.IsNullOrWhiteSpace(metricValue.Note) && !notes.Contains(metricValue.Note)) { notes.Add(metricValue.Note); } } } serviceMetricValues.Add(serviceMetric); } } } rptrMetric.DataSource = serviceMetricValues; rptrMetric.DataBind(); tbNote.Text = notes.AsDelimited(Environment.NewLine + Environment.NewLine); }
/// <summary> /// Binds the metrics. /// </summary> private void BindMetrics() { var serviceMetricValues = new List<ServiceMetric>(); 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(); var notes = new List<string>(); if ( campusId.HasValue && scheduleId.HasValue && weekend.HasValue ) { SetBlockUserPreference( "CampusId", campusId.HasValue ? campusId.Value.ToString() : "" ); SetBlockUserPreference( "ScheduleId", scheduleId.HasValue ? scheduleId.Value.ToString() : "" ); var metricCategories = MetricCategoriesFieldAttribute.GetValueAsGuidPairs( GetAttributeValue( "MetricCategories" ) ); var metricGuids = metricCategories.Select( a => a.MetricGuid ).ToList(); using ( var rockContext = new RockContext() ) { var metricValueService = new MetricValueService( rockContext ); foreach ( var metric in new MetricService( rockContext ) .GetByGuids( metricGuids ) .Where( m => m.MetricPartitions.Count == 2 && m.MetricPartitions.Any( p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId ) && m.MetricPartitions.Any( p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId ) ) .OrderBy( m => m.Title ) .Select( m => new { m.Id, m.Title, CampusPartitionId = m.MetricPartitions.Where( p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId ).Select( p => p.Id ).FirstOrDefault(), SchedulePartitionId = m.MetricPartitions.Where( p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == scheduleEntityTypeId ).Select( p => p.Id ).FirstOrDefault(), } ) ) { var serviceMetric = new ServiceMetric( metric.Id, metric.Title ); if ( campusId.HasValue && weekend.HasValue && scheduleId.HasValue ) { var metricValue = metricValueService .Queryable().AsNoTracking() .Where( v => v.MetricId == metric.Id && v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value && v.MetricValuePartitions.Count == 2 && v.MetricValuePartitions.Any( p => p.MetricPartitionId == metric.CampusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value ) && v.MetricValuePartitions.Any( p => p.MetricPartitionId == metric.SchedulePartitionId && p.EntityId.HasValue && p.EntityId.Value == scheduleId.Value ) ) .FirstOrDefault(); if ( metricValue != null ) { serviceMetric.Value = metricValue.YValue; if ( !string.IsNullOrWhiteSpace ( metricValue.Note) && !notes.Contains( metricValue.Note ) ) { notes.Add( metricValue.Note ); } } } serviceMetricValues.Add( serviceMetric ); } } } rptrMetric.DataSource = serviceMetricValues; rptrMetric.DataBind(); tbNote.Text = notes.AsDelimited( Environment.NewLine + Environment.NewLine ); }