public ResearcherResponse GetResearcher(string mnumber)
        {
            var dbContext = GetDBContext();

            mnumber = mnumber.ToUpper();

            Person             person   = dbContext.Person.Find(mnumber);
            ResearcherResponse response = new ResearcherResponse
            {
                Mnumber      = person.Mnumber,
                FirstName    = person.FirstName,
                Email        = person.Email,
                Department   = person.Department,
                LastName     = person.LastName,
                LastMeasures = new List <ResearcherLatestMeasureItem>()
            };

            List <Measure> measures = dbContext.Measure.Where(m => m.PersonMeasured == response.Mnumber).ToList();

            foreach (var measure in measures)
            {
                GlobalMeasure globalMeasure = dbContext.GlobalMeasure.Where(m => m.AggregateId == measure.AggregateMeasured).SingleOrDefault();
                if (globalMeasure == default(GlobalMeasure))
                {
                    continue;
                }

                Aggregation aggregation = dbContext.Aggregation.Find(measure.AggregateMeasured);

                Threshold threshold = dbContext.Threshold.Where(t => t.Aggregation == measure.AggregateMeasured)
                                      .Where(t => t.ThresholdStart < measure.Value && measure.Value <= t.ThresholdEnd).SingleOrDefault();

                ResearcherLatestMeasureItem researcherLatestMeasure = new ResearcherLatestMeasureItem
                {
                    AggregateId   = aggregation.Id.ToString(),
                    AggregateName = aggregation.Name,
                    AggregateType = aggregation.Type,
                    ThresholdId   = (threshold != default(Threshold)) ? threshold.Id.ToString() : null,
                    ThresholdName = (threshold != default(Threshold)) ? threshold.ThresholdName : "No matching threshold",
                    DateMeasured  = measure.DateMeasured.ToString("yyyy-mm-dd"),
                    value         = measure.Value,
                    GlobalMax     = globalMeasure.MaximumValue,
                    GlobalMin     = globalMeasure.MinimumValue,
                    GlobalMean    = globalMeasure.Mean,
                    GlobalMedian  = globalMeasure.Median,
                    GlobalStdDev  = globalMeasure.StandardDeviation,
                    Id            = measure.Id.ToString()
                };
                response.LastMeasures.Add(researcherLatestMeasure);
            }

            return(response);
        }
        public LatestResponse GetLatest()
        {
            var dbContext = GetDBContext();

            LatestResponse             response             = new LatestResponse();
            List <LatestAggregateItem> latestAggregateItems = new List <LatestAggregateItem>();

            List <Aggregation> aggregations = dbContext.Aggregation.ToList();

            foreach (Aggregation aggregation in aggregations)
            {
                GlobalMeasure global = dbContext.GlobalMeasure
                                       .Where(g => g.AggregateId == aggregation.Id && g.DateMeasured == DateTime.Today).SingleOrDefault();
                if (global == default(GlobalMeasure))
                {
                    break;
                }
                List <LatestMeasureItem> latestMeasures = new List <LatestMeasureItem>();

                List <Measure> measures = dbContext.Measure
                                          .Where(m => m.AggregateMeasured == aggregation.Id && m.DateMeasured == DateTime.Today).ToList();

                foreach (Measure measure in measures)
                {
                    Threshold threshold = dbContext.Threshold.Where(t => t.Aggregation == aggregation.Id)
                                          .Where(t => t.ThresholdStart < measure.Value && measure.Value <= t.ThresholdEnd).Single();
                    Person            person        = dbContext.Person.Find(measure.PersonMeasured);
                    Random            random        = new Random();
                    LatestMeasureItem latestMeasure = new LatestMeasureItem
                    {
                        Id            = measure.Id.ToString(),
                        PersonId      = person.Mnumber,
                        FirstName     = person.FirstName,
                        LastName      = person.LastName,
                        ThresholdId   = threshold.Id.ToString(),
                        ThresholdName = threshold.ThresholdName,
                        Value         = measure.Value + random.Next(-5000, 5000)
                    };
                    latestMeasures.Add(latestMeasure);
                }
                LatestAggregateItem latestAggregateItem = new LatestAggregateItem
                {
                    AggregateId   = aggregation.Id.ToString(),
                    AggregateName = aggregation.Name,
                    DateMeasured  = DateTime.Today.ToString("yyyy-MM-dd"),
                    GlobalMean    = global.Mean,
                    GlobalMedian  = global.Median,
                    GlobalMax     = global.MaximumValue,
                    GlobalMin     = global.MinimumValue,
                    GlobalStdDev  = global.StandardDeviation,
                    LastMeasures  = latestMeasures
                };
                latestAggregateItems.Add(latestAggregateItem);
            }
            LatestResponse latestResponse = new LatestResponse
            {
                aggregates = latestAggregateItems
            };

            return(latestResponse);
        }