public OutageReport Generate(ReportOptions options)
        {
            bool isScope = false;
            List <Specification <ConsumerHistorical> > specs = new List <Specification <ConsumerHistorical> >();

            if (options.StartDate.HasValue)
            {
                specs.Add(new HistoricalConsumerStartDateQuery(options.StartDate.Value));
            }

            if (options.EndDate.HasValue)
            {
                specs.Add(new HistoricalConsumerEndDateQuery(options.EndDate.Value));
            }

            if (options.ElementId != null)
            {
                specs.Add(new HistoricalConsumerElementIdQuery((long)options.ElementId));
                isScope = true;
            }

            specs.Add(new HistoricalConsumerOperationQuery(DatabaseOperation.DELETE));

            IEnumerable <ConsumerHistorical> outages;

            if (specs.Count > 1)
            {
                AndSpecification <ConsumerHistorical> andQuery = new AndSpecification <ConsumerHistorical>(specs);
                outages = _outageRepository.Find(andQuery.IsSatisfiedBy).ToList();
            }
            else if (specs.Count == 1)
            {
                outages = _outageRepository.Find(specs[0].IsSatisfiedBy).ToList();
            }
            else
            {
                outages = _outageRepository.GetAll().ToList();
            }

            var type = DateHelpers.GetType(options.StartDate, options.EndDate);
            var outageReportGrouping = outages.GroupBy(o => type == "Monthly" ? o.OperationTime.Month : o.OperationTime.Year).Select(o => o).ToList();

            var numOfConsumers = 1;

            if (!isScope)
            {
                numOfConsumers = _consumerRepository.GetAll().Count();
            }

            var reportData = new Dictionary <string, float>();

            foreach (var outage in outageReportGrouping)
            {
                var outageCount = outage.Count();
                reportData.Add(type == "Monthly" ? DateHelpers.Months[outage.Key] : outage.Key.ToString(), (float)outageCount / (float)numOfConsumers);;
            }

            return(new OutageReport
            {
                Type = type,
                Data = reportData
            });
        }
Example #2
0
        public OutageReport Generate(ReportOptions options)
        {
            List <Specification <ConsumerHistorical> > specs = new List <Specification <ConsumerHistorical> >();

            if (options.StartDate.HasValue)
            {
                specs.Add(new HistoricalConsumerStartDateQuery(options.StartDate.Value));
            }

            if (options.EndDate.HasValue)
            {
                specs.Add(new HistoricalConsumerEndDateQuery(options.EndDate.Value));
            }

            if (options.ElementId != null)
            {
                specs.Add(new HistoricalConsumerElementIdQuery((long)options.ElementId));
            }

            IEnumerable <ConsumerHistorical> consumers;

            if (specs.Count > 1)
            {
                AndSpecification <ConsumerHistorical> andQuery = new AndSpecification <ConsumerHistorical>(specs);
                consumers = _outageRepository.Find(andQuery.IsSatisfiedBy).ToList();
            }
            else if (specs.Count == 1)
            {
                consumers = _outageRepository.Find(specs[0].IsSatisfiedBy).ToList();
            }
            else
            {
                consumers = _outageRepository.GetAll().ToList();
            }

            var sumirani = new List <Tuple <DateTime, float> >();
            var insert   = consumers.Where(c => c.DatabaseOperation == DatabaseOperation.INSERT).OrderBy(c => c.OperationTime).ToList();
            var delete   = consumers.Where(c => c.DatabaseOperation == DatabaseOperation.DELETE).OrderBy(c => c.OperationTime).ToList();

            foreach (var item in insert)
            {
                var consumer = delete.FirstOrDefault(c => c.ConsumerId == item.ConsumerId);
                if (consumer != null)
                {
                    sumirani.Add(new Tuple <DateTime, float>(consumer.OperationTime, (float)((consumer.OperationTime - item.OperationTime).TotalMinutes)));
                    delete.Remove(consumer);
                }
            }

            var reportData = new Dictionary <string, float>();
            var type       = DateHelpers.GetType(options.StartDate, options.EndDate);
            List <IGrouping <int, Tuple <DateTime, float> > > outageReportGrouping = null;

            if (type == "Yearly")
            {
                outageReportGrouping = sumirani.GroupBy(o => o.Item1.Month).Select(o => o).ToList();
            }
            else if (type == "Monthly")
            {
                outageReportGrouping = sumirani.GroupBy(o => o.Item1.Day).Select(o => o).ToList();
            }
            else
            {
                outageReportGrouping = sumirani.GroupBy(o => o.Item1.Hour).Select(o => o).ToList();
            }

            var numOfConsumers = _consumerRepository.GetAll().Count();

            foreach (var outage in outageReportGrouping)
            {
                var outageTime = outage.ToList().Sum(c => c.Item2);
                reportData.Add(type == "Yearly" ? DateHelpers.Months[outage.Key] : outage.Key.ToString(), (float)outageTime / (float)numOfConsumers);
            }

            return(new OutageReport
            {
                Type = type,
                Data = reportData
            });
        }