示例#1
0
        /// <summary>Returns the last X messages a user has posted through Ping.fm.</summary>
        /// <param name="limit">Number of messages to query</param>
        /// <param name="order">Order of results (ASC/DESC)</param>
        public LatestResponse GetLatest(int limit, string order)
        {
            string url      = "http://api.ping.fm/v1/user.latest";
            string postdata = "api_key={0}&user_app_key={1}";

            postdata = string.Format(postdata, api_key, user_application_key);
            if (limit > -1)
            {
                postdata += "&limit=" + limit.ToString();
            }
            if (!string.IsNullOrEmpty(order))
            {
                postdata += "&order=" + order;
            }
            string         response = GetWebResponse(url, postdata);
            XmlReader      xr       = XmlReader.Create(new System.IO.StringReader(response));
            LatestResponse r        = (LatestResponse)DeserializeObject(xr, typeof(LatestResponse));

            xr.Close();
            if (r != null)
            {
                r.DecodeMessages();
            }
            mLastResponse = r;
            return(r);
        }
        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);
        }