protected DateTimeRange FromRelationAmountTime(string relation, int amount, string size, DateTime now) {
            relation = relation.ToLower();
            size = size.ToLower();
            if (amount < 1)
                throw new ArgumentException("Time amount can't be 0.");

            TimeSpan intervalSpan = Helper.GetTimeSpanFromName(size);

            if (intervalSpan != TimeSpan.Zero) {
                var totalSpan = TimeSpan.FromTicks(intervalSpan.Ticks * amount);
                switch (relation) {
                    case "last":
                    case "past":
                    case "previous":
                        return new DateTimeRange(now.Floor(intervalSpan).SafeSubtract(totalSpan), now);
                    case "this":
                    case "next":
                        return new DateTimeRange(now, now.SafeAdd(totalSpan).Ceiling(intervalSpan).SubtractMilliseconds(1));
                }
            } else if (size == "week" || size == "weeks") {
                switch (relation) {
                    case "last":
                    case "past":
                    case "previous":
                        return new DateTimeRange(now.SubtractWeeks(amount).StartOfDay(), now);
                    case "this":
                    case "next":
                        return new DateTimeRange(now, now.AddWeeks(amount).EndOfDay());
                }
            } else if (size == "month" || size == "months") {
                switch (relation) {
                    case "last":
                    case "past":
                    case "previous":
                        return new DateTimeRange(now.SubtractMonths(amount).StartOfDay(), now);
                    case "this":
                    case "next":
                        return new DateTimeRange(now, now.AddMonths(amount).EndOfDay());
                }
            } else if (size == "year" || size == "years") {
                switch (relation) {
                    case "last":
                    case "past":
                    case "previous":
                        return new DateTimeRange(now.SubtractYears(amount).StartOfDay(), now);
                    case "this":
                    case "next":
                        return new DateTimeRange(now, now.AddYears(amount).EndOfDay());
                }
            }

            return null;
        }
Пример #2
0
        public void Floor_WhenExecuted_ReturnsFloorValue()
        {
            // Arrange
            DateTime date = new DateTime(2012, 4, 26, 12, 13, 14, 951);
            TimeSpan span = new TimeSpan((long)10000000);
            DateTime expected = new DateTime(2012, 4, 26, 12, 13, 14, 0);

            // Act
            DateTime actual = date.Floor(span);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Пример #3
0
 public static long GetTimeBucket(DateTime utcDateTime) {
     DateTime rounded = utcDateTime.Floor(TimeSpan.FromMinutes(15));
     return (rounded.Ticks % TimeSpan.TicksPerDay) / TimeSpan.TicksPerMinute;
 }
        public virtual void Execute(SCPTuple tuple)
        {
            last_receive_count++;
            global_receive_count++;

            var inputeventdata = (string)tuple.GetValue(0);
            
            try
            {
                if (inputeventdata != null)
                {
                    JToken token = JObject.Parse(inputeventdata);

                    //This assumes that you wish to respect the timestamp field in your tuple
                    //If you dont care on the order or timestamp of tuple, you can send a DateTime.UtcNow i.e. the receive time
                    //This will allow you to aggregate based on current time than original event time.
                    var timestampvalue = (string)token.SelectToken(this.appConfig.TimestampField);

                    var timestamp = new DateTime();
                    var result = DateTime.TryParse(timestampvalue, out timestamp);

                    //This computes an additional timestamp which is floored to your aggregation window
                    //This acts as an alternative strategy to TickTuples as this allows you to process multiple windows at same time 
                    //and events arriving slightly out of order. For events that are huge apart i.e. 
                    //do not even fit in multiple AggregationWindows can potentially overwrite your previous aggregations 
                    //if you dont handle it properly later in your topology by doing right merges.
                    //Based on your topology, you can choose which strategy suits you better
                    var aggregationTimestamp = timestamp.Floor(this.appConfig.AggregationWindow);

                    var primarykeyvalue = ((string)token.SelectToken(this.PrimaryKey)) ?? Utilities.DEFAULT_VALUE;
                    var secondarykeyvalue = ((string)token.SelectToken(this.SecondaryKey)) ?? Utilities.DEFAULT_VALUE;

                    //Aggregate the current input. The final argument can actually be a value of any field in your input, 
                    //allowing you to use this aggregation as sum than count.
                    //We emit the aggregated tuples as part of aggregation process and expiry of the window.
                    Aggregate(aggregationTimestamp, primarykeyvalue, secondarykeyvalue, 1);

                    //Ack the tuple to the spout so that the spout can move forward and remove the tuple from its cache.
                    //This is mandatory requirement if you use the default constructor for EventHubSpout as it uses the ack based PartitionManager
                    this.context.Ack(tuple);
                }
            }
            catch (Exception ex)
            {
                global_error_count++;
                Context.Logger.Error(ex.ToString());

                //Fail the tuple in spout if you were not able to deserialize or emit it.
                this.context.Fail(tuple);
            }
        }
Пример #5
0
 private string GetCacheKey(string name, DateTime now)
 {
     return String.Concat("throttling-lock:", name, ":", now.Floor(_throttlingPeriod).Ticks);
 }
        private List<MetricBucket> GetMetricBuckets(string metricType, string statName, DateTime start, DateTime end, TimeSpan? interval = null, string suffix = null) {
            if (interval == null)
                interval = _buckets[0].Size;

            start = start.Floor(interval.Value);
            end = end.Floor(interval.Value);

            DateTime current = start;
            var keys = new List<MetricBucket>();
            while (current <= end) {
                keys.Add(new MetricBucket { Key = GetBucketKey(metricType, statName, current, interval, suffix), Time = current });
                current = current.Add(interval.Value);
            }

            return keys;
        }
Пример #7
0
 public ActionResult ServingsOn(DateTime fordate)
 {
     //reduce the date precision to Date only by omiting the time part
     fordate = fordate.Floor(DateTimeHelper.DateTimePrecisionLevel.Days);
     var result = Repository.SelectBookedMenuItems(fordate);
     if (Request.IsAjaxRequest())
     {
         //If request is Ajax then send a JSON representation of the Dictionary, Also send the Date as a string so it can be easily parsed by client
         return
             Json(
                 result
                 .ToDictionary(item => fordate.ToISODateTimeString(), item => result)
                 .ToLookup(kvp => new { kvp.Key, kvp.Value }));
     }
     return View("ServingsOn", result);
 }