Example #1
0
        public Task <Counter> GetCounter(string product, PeriodKind periodKind = PeriodKind.Daily, DateTimeOffset?periodStart = null,
                                         CounterKind kind = CounterKind.Total)
        {
            periodStart = periodStart == null
                ? DateTimeOffset.Now.Date
                : periodStart.Value.Date;

            int dayOffset = 0;

            var calendar = CultureInfo.GetCultureInfo("ru-RU").Calendar;

            switch (periodKind)
            {
            case PeriodKind.Daily:
                dayOffset = 0;
                break;

            case PeriodKind.Weekly:
                var dayOfWeek = calendar.GetDayOfWeek(periodStart.Value.DateTime);
                dayOffset = DayOfWeek.Monday - dayOfWeek;

                if (dayOffset > 0)
                {
                    dayOffset -= 7;
                }
                break;

            case PeriodKind.Monthly:
                dayOffset = periodStart.Value.Day == 1 ? 0 : 1 - periodStart.Value.Day;
                break;
            }

            return(_dataStorage.GetCounterOrDefault(product, periodKind, calendar.AddDays(periodStart.Value.DateTime, dayOffset), kind));
        }
Example #2
0
        public IActionResult OnPost()
        {
            if (ModelState.IsValid)
            {
                var        periodTypes = db.PeriodKinds;
                PeriodKind periodType  = new PeriodKind();
                foreach (var item in periodTypes)
                {
                    if (item.Name == PeriodType)
                    {
                        periodType = item;
                    }
                }

                SchoolGroup group = new SchoolGroup()
                {
                    Name         = helper.CreateName(),
                    IdPeriodKind = periodType.IdPeriodKind,
                    StartDate    = helper.RoundDate(DateTime.Now),
                    CloseDate    = helper.RoundDate(DateTime.Now.AddMonths(periodType.Months))
                };

                db.SchoolGroups.Add(group);
                db.SaveChanges();
                return(RedirectToPage("../search/groups"));
            }

            HasError = true;
            return(RedirectToPage("add_group", new { HasError }));
        }
Example #3
0
        /// <summary>
        /// 두 날짜 값이 <paramref name="periodKind"/> 단위까지 같은지 판단합니다.
        /// </summary>
        public static bool IsSameTime(this DateTime left, DateTime right, PeriodKind periodKind) {
            switch(periodKind) {
                case PeriodKind.Year:
                    return IsSameYear(left, right);

                case PeriodKind.Halfyear:
                    return IsSameHalfyear(left, right);

                case PeriodKind.Quarter:
                    return IsSameQuarter(left, right);

                case PeriodKind.Month:
                    return IsSameMonth(left, right);

                case PeriodKind.Week:
                    return IsSameWeek(left, right, null, WeekOfYearRuleKind.Calendar);

                case PeriodKind.Day:
                    return IsSameDay(left, right);

                case PeriodKind.Hour:
                    return IsSameHour(left, right);

                case PeriodKind.Minute:
                    return IsSameMinute(left, right);

                case PeriodKind.Second:
                    return IsSameSecond(left, right);

                case PeriodKind.Millisecond:
                default:
                    return IsSameDateTime(left, right);
            }
        }
Example #4
0
        public void DateTimePicker1Change(object sender, EventArgs e)
        {
            if (DateTimePicker1.Value > DateTime.Now)
            {
                DateTimePicker1.Value = DateTime.Now;
            }

            PeriodKind periodKind = (PeriodKind)cmbPeriod.SelectedIndex;

            if (object.Equals(sender, DateTimePicker1))
            {
                if (fCurrentDate.Date == DateTimePicker1.Value.Date)
                {
                    return;
                }
                fCurrentDate = DateTimePicker1.Value.Date;
            }
            else if (object.Equals(sender, tbDayPrior))
            {
                if (periodKind == PeriodKind.pkDay)
                {
                    fCurrentDate = fCurrentDate.AddDays(-1);
                }
                else if (periodKind == PeriodKind.pkWeek)
                {
                    fCurrentDate = fCurrentDate.AddDays(-7);
                }
                else
                {
                    MoveDateTime(PeriodMove.pmPrior);
                    txtSession.Text = Convert.ToString(fSession);
                }
                DateTimePicker1.Value = fCurrentDate;
            }
            else if (object.Equals(sender, tbDayNext))
            {
                if (periodKind == PeriodKind.pkDay)
                {
                    fCurrentDate = fCurrentDate.AddDays(+1);
                }
                else if (periodKind == PeriodKind.pkWeek)
                {
                    fCurrentDate = fCurrentDate.AddDays(+7);
                }
                else
                {
                    MoveDateTime(PeriodMove.pmNext);
                    txtSession.Text = Convert.ToString(fSession);
                }
                DateTimePicker1.Value = fCurrentDate;
            }

            RefreshTrends();
        }
Example #5
0
        public void ForEachPeriodsTest(PeriodKind periodKind)
        {
            var count  = 0;
            var period = GetPeriod();

            if (period.HasPeriod)
            {
                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);
                periods.RunEach(item => { count++; });

                Assert.AreEqual(periods.Count(), count);
            }
        }
Example #6
0
        /// <summary>
        /// 지정된 기간(<paramref name="period"/>)을 <paramref name="periodKind"/> 단위로 열거하면서, <paramref name="runner"/>을 실행합니다.
        /// </summary>
        /// <typeparam name="T">실행한 결과 값의 수형</typeparam>
        /// <param name="period">전체 기간</param>
        /// <param name="periodKind">열거할 기간의 단위</param>
        /// <param name="runner">각 단위 기간별 실행할 델리게이트</param>
        /// <returns>각 단위 기간별 실행 결과</returns>
        /// <example>
        /// <code>
        ///     var calendar = CultureInfo.CurrentCulture.Calendar;
        ///		var results = RunEach(new YearRange(DateTime.Now), PeriodKind.Day, (day)=>calendar.GetDaysOfYear(day.Start));
        /// </code>
        /// </example>
        public static IEnumerable <T> RunPeriod <T>(this ITimePeriod period, PeriodKind periodKind, Func <ITimePeriod, T> runner)
        {
            period.ShouldNotBeNull("period");
            runner.ShouldNotBeNull("runner");
            Guard.Assert(period.HasPeriod, "period는 기간을 가져야 합니다. period=" + period);

            if (IsDebugEnabled)
            {
                log.Debug("기간[{0}] 을 [{1}] 단위로 열거하고, 함수를 실행합니다.", period, periodKind);
            }

            return(ForEachPeriods(period, periodKind).Select(p => runner(p)));
        }
Example #7
0
        /// <summary>
        /// 지정된 기간(<paramref name="period"/>)을 <paramref name="periodKind"/> 단위로 열거하면서, <paramref name="runner"/>을 비동기 방식으로 실행합니다.
        /// </summary>
        /// <typeparam name="T">실행한 결과 값의 수형</typeparam>
        /// <param name="period">전체 기간</param>
        /// <param name="periodKind">열거할 기간의 단위</param>
        /// <param name="runner">각 단위 기간별 실행할 델리게이트</param>
        /// <returns>각 단위 기간별 실행 결과</returns>
        /// <example>
        /// <code>
        ///     var calendar = CultureInfo.CurrentCulture.Calendar;
        ///		var results = RunEachAsync(new YearRange(DateTime.Now), PeriodKind.Day, (day)=>calendar.GetDaysOfYear(day.Start));
        /// </code>
        /// </example>
        /// <seealso cref="EnumerableTool.RunEachAsync{T,TResult}"/>
        public static IEnumerable <T> RunPeriodAsync <T>(this ITimePeriod period, PeriodKind periodKind, Func <ITimePeriod, T> runner)
        {
            period.ShouldNotBeNull("period");
            runner.ShouldNotBeNull("runner");
            AssertHasPeriod(period);

            if (IsDebugEnabled)
            {
                log.Debug("기간[{0}] 을 [{1}] 단위로 열거하고, 비동기 방식으로 함수를 실행합니다.", period, periodKind);
            }


            return(ForEachPeriods(period, periodKind).RunEachAsync(runner));
        }
Example #8
0
        public void RunPeriodTest(PeriodKind periodKind)
        {
            var period = GetPeriod();

            if (period.HasPeriod)
            {
                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);

                var count = 0;
                var max   = TimeTool.RunPeriod(period, periodKind, p => ++ count).Take(1000).Last();

                Assert.AreEqual(periods.Count(), max);
            }
        }
        public async Task <Counter> GetCounterOrDefault(string product, PeriodKind periodKind, DateTimeOffset periodStart, CounterKind counterKind)
        {
            var counter = (await
                           _conn.QueryAsync <Counter>(
                               "select top 1 * from Counter where Product=@product and PeriodStart=@periodStart and PeriodKind=@periodKind and CounterKind=@counterKind",
                               new { product, periodKind, periodStart = periodStart.Date, counterKind })).SingleOrDefault();

            return(counter ?? new Counter
            {
                Product = product,
                Kind = counterKind,
                PeriodStart = periodStart,
                PeriodKind = periodKind
            });
        }
Example #10
0
        /// <summary>
        /// <paramref name="moment"/>이 속하면서, <paramref name="periodKind"/>에 해당하는 <see cref="ITimePeriod"/>을 반환합니다.
        /// </summary>
        /// <param name="moment"></param>
        /// <param name="periodKind"></param>
        /// <param name="timeCalendar"></param>
        /// <returns></returns>
        /// <seealso cref="GetPeriodsOf(System.DateTime,NFramework.TimePeriods.PeriodKind,int,NFramework.TimePeriods.ITimeCalendar)"/>
        public static ITimePeriod GetPeriodOf(this DateTime moment, PeriodKind periodKind, ITimeCalendar timeCalendar)
        {
            if (IsDebugEnabled)
            {
                log.Debug("날짜[{0}]가 속한 기간종류[{1}]의 기간을 구합니다. timeCalendar=[{2}]", moment.ToSortableString(), periodKind, timeCalendar);
            }

            timeCalendar = timeCalendar ?? TimeCalendar.New();

            switch (periodKind)
            {
            case PeriodKind.Year:
                return(GetYearRange(moment, timeCalendar));

            case PeriodKind.Halfyear:
                return(GetHalfyearRange(moment, timeCalendar));

            case PeriodKind.Quarter:
                return(GetQuarterRange(moment, timeCalendar));

            // return new QuarterRange(moment, timeCalendar);

            case PeriodKind.Month:
                return(GetMonthRange(moment, timeCalendar));

            case PeriodKind.Week:
                return(GetWeekRange(moment, timeCalendar));

            case PeriodKind.Day:
                return(GetDayRange(moment, timeCalendar));

            case PeriodKind.Hour:
                return(GetHourRange(moment, timeCalendar));

            case PeriodKind.Minute:
                return(GetMinuteRange(moment, timeCalendar));

            case PeriodKind.Second:
                return(new TimeRange(moment.TrimToMillisecond(), DurationUtil.Second));

            default:
                throw new NotSupportedException("지원하지 않는 TimePeriod 종류입니다. periodKind=" + periodKind);
            }
        }
Example #11
0
        public void RunPeriodAsParallelTest(PeriodKind periodKind)
        {
            var period = GetPeriod();

            if (period.HasPeriod)
            {
                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);

                //! 이건 좋은 예가 아닙니다. 병렬 작업 중에 locking을 수행하는 것은 좋은 코드가 아닙니다.
                //! 단지, 병렬로 작업이 처리될 수 있음을 테스트 하기 위한 코드입니다.
                //
                var count     = 0;
                var lastCount =
                    TimeTool.RunPeriodAsParallel(period, periodKind, p => Interlocked.Increment(ref count)).OrderBy(x => x).Take(1000).
                    Max();

                Assert.AreEqual(periods.Count(), lastCount);
            }
        }
Example #12
0
        public void RunPeriodAsyncTest(PeriodKind periodKind)
        {
            var period = GetPeriod();

            if (period.HasPeriod)
            {
                if (periodKind == PeriodKind.Day)
                {
                    period.ShrinkStartTo(period.End.AddYears(-1));
                }
                if (periodKind == PeriodKind.Hour || periodKind == PeriodKind.Minute)
                {
                    period.ShrinkStartTo(period.End.AddMonths(-1));
                }

                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);

                var count = 0;
                var max   = TimeTool.RunPeriodAsync(period, periodKind, p => Interlocked.Increment(ref count)).Take(1000).Last();

                Assert.AreEqual(periods.Count(), max);
            }
        }
Example #13
0
        /// <summary>
        /// 두 날짜 값이 <paramref name="periodKind"/> 단위까지 같은지 판단합니다.
        /// </summary>
        public static bool IsSameTime(this DateTime left, DateTime right, PeriodKind periodKind)
        {
            switch (periodKind)
            {
            case PeriodKind.Year:
                return(IsSameYear(left, right));

            case PeriodKind.Halfyear:
                return(IsSameHalfyear(left, right));

            case PeriodKind.Quarter:
                return(IsSameQuarter(left, right));

            case PeriodKind.Month:
                return(IsSameMonth(left, right));

            case PeriodKind.Week:
                return(IsSameWeek(left, right, null, WeekOfYearRuleKind.Calendar));

            case PeriodKind.Day:
                return(IsSameDay(left, right));

            case PeriodKind.Hour:
                return(IsSameHour(left, right));

            case PeriodKind.Minute:
                return(IsSameMinute(left, right));

            case PeriodKind.Second:
                return(IsSameSecond(left, right));

            case PeriodKind.Millisecond:
            default:
                return(IsSameDateTime(left, right));
            }
        }
Example #14
0
 /// <summary>
 /// <paramref name="moment"/>이 속하면서, <paramref name="periodKind"/>에 해당하는 <see cref="ICalendarTimeRange"/>을 구합니다.
 /// </summary>
 /// <param name="moment"></param>
 /// <param name="periodKind"></param>
 /// <param name="periodCount"></param>
 /// <returns></returns>
 public static ICalendarTimeRange GetPeriodsOf(this DateTime moment, PeriodKind periodKind, int periodCount) {
     return GetPeriodsOf(moment, periodKind, periodCount, TimeCalendar.New());
 }
Example #15
0
 /// <summary>
 /// <paramref name="moment"/>이 속하면서, <paramref name="periodKind"/>에 해당하는 <see cref="ITimePeriod"/>을 구합니다.
 /// </summary>
 /// <param name="moment"></param>
 /// <param name="periodKind"></param>
 /// <returns></returns>
 public static ITimePeriod GetPeriodOf(this DateTime moment, PeriodKind periodKind) {
     return GetPeriodOf(moment, periodKind, TimeCalendar.New());
 }
Example #16
0
        public void ForEachPeriodsTest(PeriodKind periodKind) {
            var count = 0;
            var period = GetPeriod();

            if(period.HasPeriod) {
                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);
                periods.RunEach(item => { count++; });

                Assert.AreEqual(periods.Count(), count);
            }
        }
Example #17
0
 public async Task <Counter> Get(string product, PeriodKind periodKind = PeriodKind.Daily, DateTimeOffset?periodStart = null, CounterKind counterKind = CounterKind.Total)
 {
     return(await _counterQuery.GetCounter(product, periodKind, periodStart, counterKind));
 }
Example #18
0
        public void RunPeriodTest(PeriodKind periodKind) {
            var period = GetPeriod();

            if(period.HasPeriod) {
                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);

                var count = 0;
                var max = TimeTool.RunPeriod(period, periodKind, p => ++count).Take(1000).Last();

                Assert.AreEqual(periods.Count(), max);
            }
        }
Example #19
0
        public void RunPeriodAsParallelTest(PeriodKind periodKind) {
            var period = GetPeriod();

            if(period.HasPeriod) {
                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);

                //! 이건 좋은 예가 아닙니다. 병렬 작업 중에 locking을 수행하는 것은 좋은 코드가 아닙니다.
                //! 단지, 병렬로 작업이 처리될 수 있음을 테스트 하기 위한 코드입니다.
                //
                var count = 0;
                var lastCount =
                    TimeTool.RunPeriodAsParallel(period, periodKind, p => Interlocked.Increment(ref count)).OrderBy(x => x).Take(1000).
                        Max();

                Assert.AreEqual(periods.Count(), lastCount);
            }
        }
Example #20
0
        /// <summary>
        ///  <paramref name="moment"/>이 속하면서, <paramref name="periodKind"/>에 해당하는 <see cref="ICalendarTimeRange"/>을 구합니다.
        /// </summary>
        /// <param name="moment"></param>
        /// <param name="periodKind"></param>
        /// <param name="periodCount"></param>
        /// <param name="timeCalendar"></param>
        /// <returns></returns>
        public static ICalendarTimeRange GetPeriodsOf(this DateTime moment, PeriodKind periodKind, int periodCount,
                                                      ITimeCalendar timeCalendar) {
            if(IsDebugEnabled)
                log.Debug("날짜[{0}]가 속한 기간종류[{1}]의 기간을 구합니다. periodCount=[{2}], timeCalendar=[{3}]",
                          moment.ToSortableString(), periodKind, periodCount, timeCalendar);

            switch(periodKind) {
                case PeriodKind.Year:
                    return GetYearRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Halfyear:
                    return GetHalfyearRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Quarter:
                    return GetQuarterRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Month:
                    return GetMonthRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Week:
                    return GetWeekRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Day:
                    return GetDayRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Hour:
                    return GetHourRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Minute:
                    return GetMinuteRanges(moment, periodCount, timeCalendar);

                case PeriodKind.Second:
                    return new CalendarTimeRange(moment.TrimToMillisecond(), DurationUtil.Seconds(periodCount), timeCalendar);

                default:
                    throw new NotSupportedException("지원하지 않는 TimePeriod 종류입니다. periodKind=" + periodKind);
            }
        }
Example #21
0
        /// <summary>
        /// <paramref name="period"/>를 <paramref name="periodKind"/> 단위로 열거합니다.
        /// </summary>
        /// <param name="period">전체 기간</param>
        /// <param name="periodKind">열거할 기간의 단위</param>
        /// <returns>열거할 기간 단위를 가지는 기간의 컬렉션</returns>
        public static IEnumerable <ITimePeriod> ForEachPeriods(this ITimePeriod period, PeriodKind periodKind)
        {
            switch (periodKind)
            {
            case PeriodKind.Year:
                return(period.ForEachYears());

            case PeriodKind.Halfyear:
                return(period.ForEachHalfYears());

            case PeriodKind.Quarter:
                return(period.ForEachQuarters());

            case PeriodKind.Month:
                return(period.ForEachMonths());

            case PeriodKind.Week:
                return(period.ForEachWeeks());

            case PeriodKind.Day:
                return(period.ForEachDays());

            case PeriodKind.Hour:
                return(period.ForEachHours());

            case PeriodKind.Minute:
                return(period.ForEachMinutes());

            default:
                throw new InvalidOperationException("지원하지 않는 PeriodKind입니다. PeriodKind=" + periodKind);
            }
        }
Example #22
0
        public void RunPeriodAsyncTest(PeriodKind periodKind) {
            var period = GetPeriod();

            if(period.HasPeriod) {
                if(periodKind == PeriodKind.Day) {
                    period.ShrinkStartTo(period.End.AddYears(-1));
                }
                if(periodKind == PeriodKind.Hour || periodKind == PeriodKind.Minute) {
                    period.ShrinkStartTo(period.End.AddMonths(-1));
                }

                var periods = TimeTool.ForEachPeriods(period, periodKind).Take(1000);

                var count = 0;
                var max = TimeTool.RunPeriodAsync(period, periodKind, p => Interlocked.Increment(ref count)).Take(1000).Last();

                Assert.AreEqual(periods.Count(), max);
            }
        }
Example #23
0
 /// <summary>
 /// <paramref name="moment"/>이 속하면서, <paramref name="periodKind"/>에 해당하는 <see cref="ITimePeriod"/>을 구합니다.
 /// </summary>
 /// <param name="moment"></param>
 /// <param name="periodKind"></param>
 /// <returns></returns>
 public static ITimePeriod GetPeriodOf(this DateTime moment, PeriodKind periodKind)
 {
     return(GetPeriodOf(moment, periodKind, TimeCalendar.New()));
 }
Example #24
0
        /// <summary>
        /// 지정된 기간(<paramref name="period"/>)을 <paramref name="periodKind"/> 단위로 열거하면서, 병렬로 <paramref name="runner"/>을 실행합니다.
        /// </summary>
        /// <typeparam name="T">실행한 결과 값의 수형</typeparam>
        /// <param name="period">전체 기간</param>
        /// <param name="periodKind">열거할 기간의 단위</param>
        /// <param name="runner">각 단위 기간별 실행할 델리게이트</param>
        /// <returns>각 단위 기간별 실행 결과</returns>
        /// <example>
        /// <code>
        ///     var calendar = CultureInfo.CurrentCulture.Calendar;
        ///		var results = RunEachAsParallel(new YearRange(DateTime.Now), PeriodKind.Day, (day)=>calendar.GetDaysOfYear(day.Start));
        /// </code>
        /// </example>
        public static IEnumerable <T> RunPeriodAsParallel <T>(this ITimePeriod period, PeriodKind periodKind, Func <ITimePeriod, T> runner)
        {
            period.ShouldNotBeNull("period");
            runner.ShouldNotBeNull("runner");
            AssertHasPeriod(period);

            if (IsDebugEnabled)
            {
                log.Debug("기간[{0}] 을 [{1}] 단위로 열거하고, 병렬로 함수를 실행합니다.", period, periodKind);
            }

            return
                (ForEachPeriods(period, periodKind)
#if !SILVERLIGHT
                 .AsParallel()
                 .AsOrdered()
#endif
                 .Select(p => runner(p)));
        }
Example #25
0
 /// <summary>
 /// <paramref name="moment"/>이 속하면서, <paramref name="periodKind"/>에 해당하는 <see cref="ICalendarTimeRange"/>을 구합니다.
 /// </summary>
 /// <param name="moment"></param>
 /// <param name="periodKind"></param>
 /// <param name="periodCount"></param>
 /// <returns></returns>
 public static ICalendarTimeRange GetPeriodsOf(this DateTime moment, PeriodKind periodKind, int periodCount)
 {
     return(GetPeriodsOf(moment, periodKind, periodCount, TimeCalendar.New()));
 }
Example #26
0
 public async Task <Index> Get(string product, PeriodKind periodKind = PeriodKind.Daily)
 {
     return(await _dataStorage.GetIndex(product));
 }