Esempio n. 1
0
        /// <summary>
        /// Gets the metric values.
        /// </summary>
        /// <param name="isPrimary">if set to <c>true</c> [is primary].</param>
        /// <returns></returns>
        protected List <MetricValue> GetMetricValues(bool isPrimary)
        {
            var         rockContext               = new RockContext();
            var         metricService             = new MetricService(rockContext);
            List <Guid> sourceGuids               = null;
            var         preKey                    = isPrimary ? string.Empty : "Comparison";
            IQueryable <MetricValue> metricValues = null;

            var attributeValue = GetAttributeValue(preKey + "MetricSource");

            if (string.IsNullOrWhiteSpace(attributeValue))
            {
                attributeValue = string.Empty;
            }

            var pairs = MetricCategoriesFieldAttribute.GetValueAsGuidPairs(attributeValue);

            sourceGuids = pairs.Select(p => p.MetricGuid).ToList();

            if (sourceGuids.Any())
            {
                metricValues = metricService.GetByGuids(sourceGuids).SelectMany(m => m.MetricValues);
            }
            else
            {
                nbMetricWarning.Visible  = true;
                pnlMetricDisplay.Visible = false;
                return(null);
            }

            if (GetAttributeValue(preKey + "RespectCampusContext").AsBoolean())
            {
                var campusContext = RockPage.GetCurrentContext(EntityTypeCache.Get(typeof(Campus)));

                if (campusContext != null)
                {
                    metricValues = FilterMetricValuesByPartition(metricValues, "Campus", campusContext.Id);
                }
            }

            if (GetAttributeValue(preKey + "RespectGroupContext").AsBoolean())
            {
                var groupTypeContext = RockPage.GetCurrentContext(EntityTypeCache.Get(typeof(GroupType)));
                var groupContext     = RockPage.GetCurrentContext(EntityTypeCache.Get(typeof(Group)));

                if (groupContext != null)
                {
                    metricValues = FilterMetricValuesByPartition(metricValues, "Group", groupContext.Id);
                }
                else if (groupTypeContext != null)
                {
                    var groupTypeIds = new GroupTypeService(rockContext).GetAllAssociatedDescendents(groupTypeContext.Id).Select(gt => gt.Id);
                    var groupIds     = new GroupService(rockContext).Queryable().Where(g => groupTypeIds.Contains(g.GroupTypeId)).Select(g => g.Id);
                    metricValues = metricValues.Where(a => a.MetricValuePartitions.Any(mvp => mvp.MetricPartition.Label == "Group" && groupIds.Any(i => i == mvp.EntityId)));
                }
            }

            if (GetAttributeValue(preKey + "RespectDateContext").AsBoolean())
            {
                var dateRangeString = RockPage.GetUserPreference(ContextPreferenceName);

                if (!string.IsNullOrWhiteSpace(dateRangeString))
                {
                    var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(dateRangeString);
                    metricValues = metricValues.Where(v => v.MetricValueDateTime >= dateRange.Start && v.MetricValueDateTime <= dateRange.End);
                }
            }

            if (GetAttributeValue(preKey + "RespectScheduleContext").AsBoolean())
            {
                var scheduleContext = RockPage.GetCurrentContext(EntityTypeCache.Get(typeof(Schedule)));

                if (scheduleContext != null)
                {
                    metricValues = FilterMetricValuesByPartition(metricValues, "Schedule", scheduleContext.Id);
                }
            }

            return(metricValues.ToList());
        }
Esempio n. 2
0
        /// <summary>
        /// Binds the metrics.
        /// </summary>
        private void BindMetrics()
        {
            var metricCategoryList = new List <MetricCategory>();

            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 we changed the campus, make sure we reload the services
            if (campusId != _selectedCampusId)
            {
                _selectedCampusId = GetBlockUserPreference("CampusId").AsIntegerOrNull();
                SaveViewState();
                //bddlService.Items.Clear();
                // Load service times
                foreach (var service in GetServices(CampusCache.Read(campusId.Value)))
                {
                    //    bddlService.Items.Add( new ListItem( service.Name, service.Id.ToString() ) );
                }
                //bddlService.SetValue( _selectedServiceId.Value );
            }

            var notes = new List <string>();

            if (campusId.HasValue && weekend.HasValue)
            {
                SetBlockUserPreference("CampusId", campusId.HasValue ? campusId.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);
                    var metrics            = new MetricService(rockContext)
                                             .GetByGuids(metricGuids)
                                             .Where(m =>
                                                    m.MetricPartitions.Count == 1 &&
                                                    m.MetricPartitions.Any(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId))
                                             .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(),
                        CategoryName        = m.MetricCategories.Select(c => c.Category.Name).FirstOrDefault()
                    });
                    foreach (var metric in metrics.OrderBy(m => m.CategoryName).ThenBy(m => m.Title))
                    {
                        MetricCategory category = null;
                        if (metricCategoryList.Any(mct => mct.CategoryName == metric.CategoryName))
                        {
                            category = metricCategoryList.Where(mct => mct.CategoryName == metric.CategoryName).FirstOrDefault();
                        }
                        else
                        {
                            category = new MetricCategory()
                            {
                                CategoryName = metric.CategoryName, Metrics = new List <Metric>()
                            };
                            metricCategoryList.Add(category);
                        }

                        var metricObj = new Metric(metric.Id, metric.Title);
                        if (campusId.HasValue && weekend.HasValue)
                        {
                            var metricValue = metricValueService
                                              .Queryable().AsNoTracking()
                                              .Where(v =>
                                                     v.MetricId == metric.Id &&
                                                     v.MetricValueDateTime.HasValue && v.MetricValueDateTime.Value == weekend.Value &&
                                                     v.MetricValuePartitions.Count == 1 &&
                                                     v.MetricValuePartitions.Any(p => p.MetricPartitionId == metric.CampusPartitionId && p.EntityId.HasValue && p.EntityId.Value == campusId.Value))
                                              .FirstOrDefault();

                            if (metricValue != null)
                            {
                                metricObj.Value = metricValue.YValue;

                                if (!string.IsNullOrWhiteSpace(metricValue.Note) &&
                                    !notes.Contains(metricValue.Note))
                                {
                                    notes.Add(metricValue.Note);
                                }
                            }
                        }

                        category.Metrics.Add(metricObj);
                    }
                }
            }
            rptrMetricCategory.DataSource = metricCategoryList;
            rptrMetricCategory.DataBind();

            if (campusId.HasValue)
            {
                rptrService.DataSource = GetServices(CampusCache.Read(campusId.Value));
                rptrService.DataBind();
                rptrService.Visible = rptrService.Items.Count > 0;
            }


            tbNote.Text = notes.AsDelimited(Environment.NewLine + Environment.NewLine);
        }
        private static List <Metric> MetricListConverter(string val, MetricListConverterData data)
        {
            List <Metric> mList = data.MetricService.GetByGuids(MetricCategoriesFieldAttribute.GetValueAsGuidPairs(val.ResolveMergeFields(data.MergeFields)).Select(g => g.MetricGuid).ToList()).ToList();

            return(mList.Any() ? mList : null);
        }
Esempio n. 4
0
        /// <summary>
        /// Handles the ItemDataBound event of the rptrService control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs"/> instance containing the event data.</param>
        protected void rptrService_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Schedule schedule = e.Item.DataItem as Schedule;

                int campusEntityTypeId   = EntityTypeCache.Read(typeof(Rock.Model.Campus)).Id;
                int scheduleEntityTypeId = EntityTypeCache.Read(typeof(Rock.Model.Schedule)).Id;

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

                var notes = new List <string>();

                var serviceMetricValues = new List <Metric>();
                var rptrServiceMetric   = e.Item.FindControl("pnlwService").FindControl("rptrServiceMetric") as Repeater;

                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 Metric(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);
                    }
                }

                rptrServiceMetric.DataSource = serviceMetricValues;
                rptrServiceMetric.DataBind();
            }
        }
Esempio n. 5
0
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap      dataMap         = context.JobDetail.JobDataMap;
            var             rockContext     = new RockContext();
            var             jobService      = new ServiceJobService(rockContext);
            GroupService    groupService    = new GroupService(rockContext);
            CategoryService categoryService = new CategoryService(rockContext);
            MetricService   metricService   = new MetricService(rockContext);

            var       metricCategories = MetricCategoriesFieldAttribute.GetValueAsGuidPairs(dataMap.GetString("Metrics"));
            DateRange dateRange        = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(dataMap.GetString("DateRange"));

            var systemEmail = dataMap.GetString("Email").AsGuidOrNull();

            if (!systemEmail.HasValue)
            {
                throw new Exception("System Email is required!");
            }

            // get job type id
            int jobId = Convert.ToInt16(context.JobDetail.Description);
            var job   = jobService.Get(jobId);

            DateTime _midnightToday  = RockDateTime.Today.AddDays(1);
            var      currentDateTime = RockDateTime.Now;
            int      recipients      = 0;

            Group notificationGroup         = groupService.Get(dataMap.GetString("NotificationGroup").AsGuid());
            List <MetricCount> metricCounts = CampusCache.All().Select(c => new MetricCount()
            {
                Campus = c, TotalEntered = 0, TotalMetrics = 0
            }).ToList();
            List <Metric> metrics = new List <Metric>();


            // If we have some reasonable data, go ahead and run the job
            if (notificationGroup != null && metricCategories.Count > 0 && dateRange.Start.HasValue && dateRange.End.HasValue)
            {
                foreach (MetricCategoryPair metricCategoryPair in metricCategories)
                {
                    Metric metric = metricService.Get(metricCategoryPair.MetricGuid);
                    metrics.Add(metric);
                    // Split this by campus partition
                    if (metric.MetricPartitions.Any(mp => mp.EntityType.Name.Contains("Campus")))
                    {
                        foreach (CampusCache campus in CampusCache.All())
                        {
                            // Check to see if we also have a schedule partition
                            if (metric.MetricPartitions.Any(mp => mp.EntityType.Name.Contains("Schedule")))
                            {
                                var services = GetServices(campus, dataMap, dateRange);
                                metricCounts.Where(mc => mc.Campus == campus).FirstOrDefault().TotalMetrics += services.Count;
                                foreach (var service in services)
                                {
                                    var hasValues = metric.MetricValues.Where(mv =>
                                                                              mv.MetricValuePartitions.Any(mvp => mvp.MetricPartition.EntityType.Name.Contains("Campus") && mvp.EntityId == campus.Id) &&
                                                                              mv.MetricValuePartitions.Any(mvp => mvp.MetricPartition.EntityType.Name.Contains("Schedule") && mvp.EntityId == service.Id) &&
                                                                              mv.MetricValueDateTime >= dateRange.Start.Value &&
                                                                              mv.MetricValueDateTime <= dateRange.End.Value).Any();
                                    if (hasValues)
                                    {
                                        metricCounts.Where(mc => mc.Campus == campus).FirstOrDefault().TotalEntered++;
                                    }
                                }
                            }
                            else
                            {
                                // Add totals for metrics and, if values are entered, metrics entered.
                                metricCounts.Where(mc => mc.Campus == campus).FirstOrDefault().TotalMetrics++;
                                var hasValues = metric.MetricValues.Where(mv => mv.MetricValuePartitions.Any(mvp => mvp.MetricPartition.EntityType.Name.Contains("Campus") && mvp.EntityId == campus.Id) && mv.MetricValueDateTime >= dateRange.Start.Value && mv.MetricValueDateTime <= dateRange.End.Value).Any();
                                if (hasValues)
                                {
                                    metricCounts.Where(mc => mc.Campus == campus).FirstOrDefault().TotalEntered++;
                                }
                            }
                        }
                    }
                }

                // Create the merge fields
                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null);
                mergeFields.Add("MetricCounts", metricCounts);
                mergeFields.Add("Metrics", metrics);
                mergeFields.Add("DateRange", dateRange.ToString());
                mergeFields.Add("LastRunDate", job.LastSuccessfulRunDateTime);

                // Setup the email and send it out!
                RockEmailMessage message = new RockEmailMessage(systemEmail.Value);
                message.AdditionalMergeFields = mergeFields;
                foreach (GroupMember member in notificationGroup.Members)
                {
                    message.AddRecipient(RockEmailMessageRecipient.CreateAnonymous(member.Person.Email, mergeFields));
                    recipients++;
                }
                message.SendSeperatelyToEachRecipient = true;
                message.Send();
            }

            context.Result = string.Format("Sent " + recipients + " metric entry digest emails.");
        }
Esempio n. 6
0
        /// <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 groupMetricValues = new List <GroupMetric>();

            int groupEntityTypeId  = EntityTypeCache.Get(typeof(Rock.Model.Group)).Id;
            int campusEntityTypeId = EntityTypeCache.Get(typeof(Rock.Model.Campus)).Id;

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

            var notes = new List <string>();

            if (groupId.HasValue && weekend.HasValue)
            {
                SetBlockUserPreference("GroupId", groupId.HasValue ? groupId.Value.ToString() : "");

                var metricCategories = MetricCategoriesFieldAttribute.GetValueAsGuidPairs(GetAttributeValue("MetricCategories"));
                var metricGuids      = metricCategories.Select(a => a.MetricGuid).ToList();
                using (var rockContext = new RockContext())
                {
                    var group              = new GroupService(rockContext).Queryable().FirstOrDefault(g => g.Id == groupId);
                    int?campusId           = group.CampusId;
                    var metricValueService = new MetricValueService(rockContext);
                    foreach (var metric in new MetricService(rockContext)
                             .GetByGuids(metricGuids)
                             .OrderBy(m => m.Title)
                             .Select(m => new
                    {
                        m.Id,
                        m.Title,
                        GroupPartitionId = m.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == groupEntityTypeId).Select(p => p.Id).FirstOrDefault(),
                        CampusPartitionId = m.MetricPartitions.Where(p => p.EntityTypeId.HasValue && p.EntityTypeId.Value == campusEntityTypeId).Select(p => p.Id).FirstOrDefault()
                    }))
                    {
                        var groupMetric = new GroupMetric(metric.Id, metric.Title);

                        if (groupId.HasValue && weekend.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.GroupPartitionId && p.EntityId.HasValue && p.EntityId.Value == groupId.Value)
                                                         ) ||
                                                         (
                                                             v.MetricValuePartitions.Count == 1 &&
                                                             (
                                                                 v.MetricValuePartitions.Any(p => p.MetricPartitionId == metric.GroupPartitionId && p.EntityId.HasValue && p.EntityId.Value == groupId.Value)
                                                             )
                                                         )
                                                     )
                                                     )
                                              .FirstOrDefault();

                            if (metricValue != null)
                            {
                                groupMetric.Value = metricValue.YValue;

                                if (!string.IsNullOrWhiteSpace(metricValue.Note) &&
                                    !notes.Contains(metricValue.Note))
                                {
                                    notes.Add(metricValue.Note);
                                }
                            }
                        }

                        groupMetricValues.Add(groupMetric);
                    }
                }
            }

            rptrMetric.DataSource = groupMetricValues;
            rptrMetric.DataBind();

            tbNote.Text = notes.AsDelimited(Environment.NewLine + Environment.NewLine);
        }
Esempio n. 8
0
        /// <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();

            // If we changed the campus, make sure we reload the services
            if (campusId != _selectedCampusId)
            {
                _selectedCampusId = campusId;

                bddlService.Items.Clear();
                bddlService.ClearSelection();
                scheduleId = null;

                // Load service times
                foreach (var service in GetServices(CampusCache.Get(campusId.Value)))
                {
                    bddlService.Items.Add(new ListItem(service.Name, service.Id.ToString()));
                    if (_selectedServiceId == service.Id)
                    {
                        bddlService.SetValue(_selectedServiceId.Value);
                        scheduleId = _selectedServiceId.Value;
                    }
                }
                // Clear the repeater
                rptrMetricCategory.DataSource = null;
                rptrMetricCategory.DataBind();
                SaveViewState();
            }

            // If we changed the weekend, make sure we reload the services
            if (weekend != _selectedWeekend)
            {
                _selectedWeekend = weekend;

                bddlService.Items.Clear();
                bddlService.ClearSelection();
                scheduleId = null;

                // Load service times
                foreach (var service in GetServices(CampusCache.Get(campusId.Value)))
                {
                    bddlService.Items.Add(new ListItem(service.Name, service.Id.ToString()));
                    if (_selectedServiceId == service.Id)
                    {
                        bddlService.SetValue(_selectedServiceId.Value);
                        scheduleId = _selectedServiceId.Value;
                    }
                }
                // Clear the repeater
                rptrMetricCategory.DataSource = null;
                rptrMetricCategory.DataBind();
                SaveViewState();
            }


            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"));
                CategoryService categoryService  = new CategoryService(new RockContext());
                var             categories       = categoryService.GetByGuids(metricCategories.Where(mc => mc.CategoryGuid.HasValue).Select(mc => mc.CategoryGuid.Value).ToList());

                rptrMetricCategory.DataSource = categories.ToList();
                rptrMetricCategory.DataBind();
            }
        }