public async Task <IHttpActionResult> Put([FromODataUri] Guid key, DailyStatistic update)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (key != update.Id)
     {
         return(BadRequest());
     }
     db.Entry(update).State = EntityState.Modified;
     try
     {
         await db.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         if (!DailyStatisticExists(key))
         {
             return(NotFound());
         }
         else
         {
             throw;
         }
     }
     return(Updated(update));
 }
Beispiel #2
0
        public async Task <DailyStatistic> UpdateAsync(DailyStatistic dailyStatistic, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (dailyStatistic == null)
            {
                throw new ArgumentNullException(nameof(dailyStatistic));
            }

            // 清除子实体集合防止更新异常
            var dailyStatisticDetails = await Context.DailyStatisticItems.Where(w => w.DailyStatisticId == dailyStatistic.Id).ToListAsync(cancellationToken);

            if (dailyStatisticDetails != null)
            {
                foreach (var dailyStatisticDetail in dailyStatisticDetails)
                {
                    Context.DailyStatisticItems.Remove(dailyStatisticDetail);
                }
            }


            Context.DailyStatistics.Update(dailyStatistic);
            await SaveChanges(cancellationToken);

            return(dailyStatistic);
        }
Beispiel #3
0
 public async Task CreateStatistic()
 {
     var dailyStatistic = new DailyStatistic
     {
         NumberOfExperiences = await GetNumberOfLiveExperience(),
         TimeStamp           = DateTime.UtcNow.AddDays(-1) // subtract one day since we are calculating the day before midnight
     };
     await _statisticDataManager.Create(dailyStatistic);
 }
        public async Task <IHttpActionResult> Post(DailyStatistic dailyStatistic)
        {
            dailyStatistic.Id = Guid.NewGuid();
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            db.DailyStatistics.Add(dailyStatistic);
            await db.SaveChangesAsync();

            return(Created(dailyStatistic));
        }
Beispiel #5
0
        public async Task <DailyStatistic> CreateAsync(DailyStatistic dailyStatistic, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (dailyStatistic == null)
            {
                throw new ArgumentNullException(nameof(dailyStatistic));
            }

            Context.DailyStatistics.Add(dailyStatistic);
            await SaveChanges(cancellationToken);

            return(dailyStatistic);
        }
Beispiel #6
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DailyStatistic = await _context.DailyStatistics
                             .Include(d => d.Patient).FirstOrDefaultAsync(m => m.Id == id);

            if (DailyStatistic == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Beispiel #7
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DailyStatistic = await _context.DailyStatistics
                             .Include(d => d.Patient).FirstOrDefaultAsync(m => m.Id == id);

            if (DailyStatistic == null)
            {
                return(NotFound());
            }
            ViewData["PatientId"] = new SelectList(_context.Patients, "Id", "Id");
            return(Page());
        }
Beispiel #8
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DailyStatistic = await _context.DailyStatistics.FindAsync(id);

            if (DailyStatistic != null)
            {
                _context.DailyStatistics.Remove(DailyStatistic);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
        public async Task UpdateDailyStatistic(int newQueueSize)
        {
            //Statistics Stuff
            DailyStatistic todaysStat = await DailyStatistic.Get(string.Format("$filter=Date eq {0}", new DateTimeOffset(DateTime.Now.Year,
                                                                                                                         DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0, default(TimeSpan)).ToString("yyyy-MM-ddTHH:mm:ssZ")));

            if (todaysStat == null)
            {
                todaysStat                 = new DailyStatistic();
                todaysStat.Date            = new DateTimeOffset(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0, default(TimeSpan));
                todaysStat.LastQueueLength = newQueueSize;
                await todaysStat.Post();
            }
            else
            {
                todaysStat.LastQueueLength = newQueueSize;
                await todaysStat.Update();
            }
        }
Beispiel #10
0
 public Task Create(DailyStatistic dailyStatistic)
 {
     _context.DailyStatistics.Add(dailyStatistic);
     return(_context.SaveChangesAsync());
 }
        public static async Task <Dictionary <int, TimeSpan> > GetQueueSizeTimes(DateTimeOffset givenDate)
        {
            int    lastQueueSize;
            string jobQuery;

            if (Period == StatPeriod.Day)
            {
                givenDate = BeginningOfDay(givenDate);
                DailyStatistic previousStat = (await WebApiHelper.Get <DailyStatistic>(string.Format("$filter=Date lt {0}&$top=1&$orderby=Date desc", FilterFormatDTO(givenDate)))).FirstOrDefault();
                lastQueueSize = previousStat == null ? 0 : previousStat.LastQueueLength;
                jobQuery      = string.Format("$filter=(Entered gt {0} and Entered lt {1}) or (Finished gt {0} and Finished lt {1})", FilterFormatDTO(givenDate), FilterFormatDTO(givenDate.AddDays(1)));
            }
            else
            {
                givenDate = BeginningOfMonth(givenDate);
                DailyStatistic previousStat = (await WebApiHelper.Get <DailyStatistic>(string.Format("$filter=Date lt {0}&$top=1&$orderby=Date desc", FilterFormatDTO(givenDate)))).FirstOrDefault();
                lastQueueSize = previousStat == null ? 0 : previousStat.LastQueueLength;
                jobQuery      = string.Format("$filter=(Entered gt {0} and Entered lt {1}) or (Finished gt {0} and Finished lt {1})", FilterFormatDTO(givenDate), FilterFormatDTO(givenDate.AddMonths(1)));
            }

            var jobs = await WebApiHelper.Get <Job>(jobQuery);

            var entered = jobs.Where(x => givenDate <= x.Entered && x.Entered < givenDate.AddDays(1));
            var removed = jobs.Where(x => givenDate <= x.Finished && x.Finished < givenDate.AddDays(1));

            List <QueueChange> changes = new List <QueueChange>();

            foreach (var entry in entered)
            {
                changes.Add(new QueueChange()
                {
                    type = QueueChange.ChangeType.Entered,
                    date = entry.Entered
                });
            }

            foreach (var removal in removed)
            {
                changes.Add(new QueueChange()
                {
                    type = QueueChange.ChangeType.Removed,
                    date = removal.Finished
                });
            }

            var orderedChanges = changes.OrderBy(x => x.date).ToList();

            if (DateTimeOffset.Now < givenDate.AddDays(1))
            {
                orderedChanges.Add(new QueueChange()
                {
                    date = DateTimeOffset.Now
                });
            }
            else
            {
                orderedChanges.Add(new QueueChange()
                {
                    date = givenDate.AddDays(1)
                });
            }


            Dictionary <int, TimeSpan> sizeByTime = new Dictionary <int, TimeSpan>();
            int            prevSize = lastQueueSize;
            DateTimeOffset prevDate = givenDate;

            for (int i = 0; i < orderedChanges.Count(); i++)
            {
                if (!sizeByTime.ContainsKey(prevSize))
                {
                    sizeByTime[prevSize] = new TimeSpan();
                }
                sizeByTime[prevSize] += orderedChanges[i].date.DateTime - prevDate.DateTime;

                if (orderedChanges[i].type == QueueChange.ChangeType.Entered)
                {
                    prevSize++;
                }
                else
                {
                    prevSize--;
                }
            }

            return(sizeByTime);
        }