Exemplo n.º 1
0
        protected TimeSpan Duration(SortSpan span) {

            TimeSpan ts = TimeSpan.MaxValue;

            switch (span) { 
                case SortSpan.Hour:
                    ts = TimeSpan.FromHours(1);
                    break;
                case SortSpan.Day:
                    ts = TimeSpan.FromDays(1);
                    break;
                case SortSpan.Month:
                    ts = TimeSpan.FromDays(30);
                    break;
                case SortSpan.Week:
                    ts = TimeSpan.FromDays(7);
                    break;
                case SortSpan.Quarter:
                    ts = TimeSpan.FromDays(90);
                    break;
                case SortSpan.Year:
                    ts = TimeSpan.FromDays(365);
                    break;
            }

            return ts;

        }
Exemplo n.º 2
0
        protected TimeSpan Duration(SortSpan span)
        {
            TimeSpan ts = TimeSpan.MaxValue;

            switch (span)
            {
            case SortSpan.Hour:
                ts = TimeSpan.FromHours(1);
                break;

            case SortSpan.Day:
                ts = TimeSpan.FromDays(1);
                break;

            case SortSpan.Month:
                ts = TimeSpan.FromDays(30);
                break;

            case SortSpan.Week:
                ts = TimeSpan.FromDays(7);
                break;

            case SortSpan.Quarter:
                ts = TimeSpan.FromDays(90);
                break;

            case SortSpan.Year:
                ts = TimeSpan.FromDays(365);
                break;
            }

            return(ts);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The purpose of this function is to standardize inputs so that we can cache ranged queries. Currently ranges
        /// use the current date which contains diffrent minute, second, and ms with each call. This function converts to common
        /// start and end ranges (beginning and ending of days, hours, etc.)
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="sortSpan"></param>
        /// <returns></returns>

        //This code needs to be refactored to use Voat.Common.DateRange
        public static Tuple <DateTime, DateTime> ToRange(this DateTime dateTime, SortSpan sortSpan)
        {
            DateTime start = dateTime;
            DateTime end   = dateTime;

            switch (sortSpan)
            {
            case SortSpan.Hour:
                start = start.ToStartOfHour();
                end   = start.Add(TimeSpan.FromHours(-1));
                break;

            case SortSpan.Day:
                start = start.ToStartOfDay();
                end   = start.ToEndOfDay();
                break;

            case SortSpan.Week:
                start = start.ToStartOfWeek();
                end   = start.ToEndOfWeek();
                break;

            case SortSpan.Month:
                start = start.ToStartOfMonth();
                end   = start.ToEndOfMonth();
                break;

            case SortSpan.Quarter:
                var range = start.ToQuarterRange();
                start = range.Item1;
                end   = range.Item2;
                break;

            case SortSpan.Year:
                start = start.ToStartOfYear();
                end   = start.ToEndOfYear();
                break;

            default:
            case SortSpan.All:

                //Date Range shouldn't be processed for this span
                break;
            }

            return(new Tuple <DateTime, DateTime>(start, end));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns a range based on the span provided. Does not standardize range, simply subtracts offset.
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="sortSpan"></param>
        /// <returns></returns>
        //This code needs to be refactored to use Voat.Common.DateRange
        public static Tuple <DateTime, DateTime> ToRelativeRange(this DateTime dateTime, SortSpan sortSpan, SortDirection sortDirection = SortDirection.Reverse)
        {
            DateTime start = dateTime;
            DateTime end   = dateTime;
            var      directionMultiplier = sortDirection == SortDirection.Reverse ? -1 : 1;

            switch (sortSpan)
            {
            case SortSpan.Hour:
                end   = start.ToEndOfHour();
                start = end.AddHours(1 * directionMultiplier);
                break;

            case SortSpan.Day:
                end   = start.ToEndOfHour();
                start = end.AddHours(24 * directionMultiplier);
                break;

            case SortSpan.Week:
                end   = start.ToEndOfDay();
                start = end.AddDays(7 * directionMultiplier);
                break;

            case SortSpan.Month:
                end   = start.ToEndOfDay();
                start = end.AddDays(30 * directionMultiplier);
                break;

            case SortSpan.Quarter:
                end   = start.ToEndOfDay();
                start = end.AddDays(90 * directionMultiplier);
                break;

            case SortSpan.Year:
                end   = start.ToEndOfDay();
                start = end.AddDays(365 * directionMultiplier);
                break;

            default:
            case SortSpan.All:

                //Date Range shouldn't be processed for this span
                break;
            }

            return(new Tuple <DateTime, DateTime>(start, end));
        }
Exemplo n.º 5
0
        public SearchOptions(IEnumerable <KeyValuePair <string, string> > queryStrings, int?maxPageCount = null, bool useRelativeDateSpans = true) : this(maxPageCount, useRelativeDateSpans)
        {
            //TODO: Make sure the querystrings passed into this method from controller are url decoded values
            if (queryStrings == null)
            {
                return;
            }

            this._queryStrings = queryStrings;

            //List<KeyValuePair<string, string>> unknownPairs = new List<KeyValuePair<string, string>>();

            foreach (var kp in queryStrings)
            {
                string value = kp.Value;

                switch (kp.Key.ToLower())
                {
                case "period":
                case "span":
                    SortSpan sortPer = SortSpan.All;
                    if (Enum.TryParse(value, true, out sortPer))
                    {
                        this._span = sortPer;
                    }
                    break;

                case "sort":
                    SortAlgorithm sortAlg = SortAlgorithm.Rank;
                    if (Enum.TryParse(value, true, out sortAlg))
                    {
                        this.Sort = sortAlg;
                    }
                    break;

                case "sortdirection":
                case "direction":
                    SortDirection sortDir = SortDirection.Default;
                    if (Enum.TryParse(value, true, out sortDir))
                    {
                        this.SortDirection = sortDir;
                    }
                    break;

                case "date":
                case "startdate":
                case "datestart":
                    DateTime startDate;
                    if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out startDate))
                    {
                        this._startDate = startDate;
                    }
                    break;

                //No longer supported
                //case "enddate":
                //case "dateend":
                //    DateTime endDate;
                //    if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out endDate)) {
                //        this._endDate = endDate;
                //    }
                //    break;

                //No longer supported
                case "count":
                    //int count = 0;
                    //if (Int32.TryParse(value, out count))
                    //{
                    //    this.Count = count;
                    //}
                    break;

                //No longer supported
                case "index":
                    //case "currentindex":
                    ////UNDONE: Don't think we want consumers controlling indexing, forceing all paging through the page querystring
                    //    int index = 0;
                    //    if (Int32.TryParse(value, out index))
                    //    {
                    //        this.Index = index;
                    //    }
                    break;

                case "page":
                    int page = 0;
                    if (Int32.TryParse(value, out page))
                    {
                        this.Page = page;
                    }
                    break;

                case "phrase":
                case "search":
                case "q":
                    this._phrase = (String.IsNullOrEmpty(value) ? "" : value.Trim());
                    break;

                default:
                    _unknownPairs.Add(kp);
                    break;
                }
            }

            //process Period and Start End Times
            CalculateNewDateRange();
            ParseAdditionalKeyPairs(_unknownPairs);
        }
Exemplo n.º 6
0
        public SearchOptions(IEnumerable <KeyValuePair <string, string> > queryStrings)
        {
            //TODO: Make sure the querystrings passed into this method from controller are url decoded values
            if (queryStrings == null)
            {
                return;
            }

            this._queryStrings = queryStrings;

            foreach (var kp in queryStrings)
            {
                string value = kp.Value;

                switch (kp.Key.ToLower())
                {
                case "period":
                case "span":
                    SortSpan sortPer = SortSpan.All;
                    if (Enum.TryParse(value, true, out sortPer))
                    {
                        this.Span = sortPer;
                    }
                    break;

                case "sort":
                    SortAlgorithm sortAlg = SortAlgorithm.Hot;
                    if (Enum.TryParse(value, true, out sortAlg))
                    {
                        this.Sort = sortAlg;
                    }
                    break;

                case "sortdirection":
                case "direction":
                    SortDirection sortDir = SortDirection.Default;
                    if (Enum.TryParse(value, true, out sortDir))
                    {
                        this.SortDirection = sortDir;
                    }
                    break;

                case "startdate":
                case "datestart":
                    DateTime startDate;
                    if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out startDate))
                    {
                        this._startDate = startDate;
                    }
                    break;

                case "enddate":
                case "dateend":
                    DateTime endDate;
                    if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out endDate))
                    {
                        this._endDate = endDate;
                    }
                    break;

                case "count":
                    int count = 0;
                    if (Int32.TryParse(value, out count))
                    {
                        this.Count = count;
                    }
                    break;

                case "index":
                case "currentindex":
                    int index = 0;
                    if (Int32.TryParse(value, out index))
                    {
                        this.Index = index;
                    }
                    break;

                case "page":
                    int page = 0;
                    if (Int32.TryParse(value, out page))
                    {
                        this.Page = page;
                    }
                    break;

                case "depth":
                    int depth;
                    if (int.TryParse(value, out depth))
                    {
                        this.Depth = depth;
                    }
                    break;

                case "search":
                    this._search = (String.IsNullOrEmpty(value) ? "" : value.Trim());
                    break;
                }
            }

            //process Period and Start End Times
            if (this.Span != SortSpan.All)
            {
                if (this.EndDate.HasValue)
                {
                    //assume this was provided so we will use this date as the start date
                    this.StartDate = this.EndDate.Value.Subtract(Duration(this.Span));
                }
                else
                {
                    this.StartDate = DateTime.UtcNow.Subtract(Duration(this.Span));
                    this.EndDate   = DateTime.UtcNow;
                }
            }
        }
Exemplo n.º 7
0
        public static Tuple <DateTime, DateTime> RelativeRange(this DateTime dateTime, SortSpan sortSpan)
        {
            DateTime start = dateTime;
            DateTime end   = dateTime;

            switch (sortSpan)
            {
            case SortSpan.Hour:
                end   = start.ToEndOfHour();
                start = end.AddHours(-1);
                break;

            case SortSpan.Day:
                end   = start.ToEndOfHour();
                start = end.AddHours(-24);
                break;

            case SortSpan.Week:
                end   = start.ToEndOfDay();
                start = end.AddDays(-7);
                break;

            case SortSpan.Month:
                end   = start.ToEndOfDay();
                start = end.AddDays(-30);
                break;

            case SortSpan.Quarter:
                end   = start.ToEndOfDay();
                start = end.AddDays(-90);
                break;

            case SortSpan.Year:
                end   = start.ToEndOfDay();
                start = end.AddDays(-365);
                break;

            default:
            case SortSpan.All:

                //Date Range shouldn't be processed for this span
                break;
            }

            return(new Tuple <DateTime, DateTime>(start, end));
        }