protected UserActivityTrackTests(ActivityTimeframe timeframe) { UserActivity = new UserActivity( new ActivitySettings(1, timeframe)); UserActivity.Reset().Wait(); UserActivity.Track(EventName, Timestamp, 1, 2, 3).Wait(); }
protected EventActivityTrackTests(ActivityTimeframe timeframe) { EventActivity = new EventActivity( new ActivitySettings(1, timeframe)); EventActivity.Reset().Wait(); EventActivity.Track(EventName, Timestamp).Wait(); }
public virtual async Task <long[]> Counts( string eventName, DateTime startTimestamp, DateTime endTimestamp, ActivityTimeframe timeframe) { Validation.ValidateEventName(eventName); var dates = startTimestamp.Range(endTimestamp, timeframe); var key = GenerateKey(eventName, timeframe.ToString()); var fields = dates.Select(d => GenerateTimeframeFields(timeframe, d) .ElementAt((int)timeframe)) .ToArray(); string[] values; using (var connection = await ConnectionFactories.Open()) { values = await connection.Hashes.GetString( Settings.Db, key, fields); } var result = values.Select(value => value == null ? 0L : long.Parse(value)) .ToArray(); return(result); }
public ActivitySettings(ActivityTimeframe timeframe) : this( DefaultDb, timeframe, DefaultKeyPrefix, DefaultKeySeparator) { }
private static void Test( int interval, Func <int, DateTime> func, ActivityTimeframe timeframe) { Assert.Equal( Math.Abs(interval) + 1, Now.Range(func(interval), timeframe).Count()); }
public virtual UserActivityReport Report( string eventName, ActivityTimeframe timeframe, DateTime timestamp) { Validation.ValidateEventName(eventName); var eventKey = GenerateEventTimeframeKeys( eventName, timeframe, timestamp).ElementAt((int)timeframe); return(new UserActivityReport(Settings.Db, eventKey)); }
public ActivitySettings( int db, ActivityTimeframe timeframe, string keyPrefix, string keySeparator) { Validation.ValidateDb(db); Db = db; Timeframe = timeframe; KeyPrefix = keyPrefix ?? string.Empty; KeySeparator = keySeparator ?? string.Empty; }
protected async Task TestExists(ActivityTimeframe timeframe) { var key = UserActivity.GenerateEventTimeframeKeys( EventName, timeframe, Timestamp) .ElementAt((int)timeframe); using (var connection = await ConnectionFactories.Open()) { var result = await connection.Strings.Get( UserActivity.Settings.Db, key); Assert.NotNull(result); } }
public void DayRange() { const ActivityTimeframe Timeframe = ActivityTimeframe.Day; Func <int, DateTime> func = n => Now.AddDays(n); // Positive Test(27, func, Timeframe); Test(7, func, Timeframe); Test(143, func, Timeframe); // Negative Test(-27, func, Timeframe); Test(-7, func, Timeframe); Test(-143, func, Timeframe); // Same Test(0, func, Timeframe); }
public void MonthRange() { const ActivityTimeframe Timeframe = ActivityTimeframe.Month; Func <int, DateTime> func = n => Now.AddMonths(n); // Positive Test(13, func, Timeframe); Test(2, func, Timeframe); Test(23, func, Timeframe); // Negative Test(-13, func, Timeframe); Test(-2, func, Timeframe); Test(-23, func, Timeframe); // Same Test(0, func, Timeframe); }
public void HourRange() { const ActivityTimeframe Timeframe = ActivityTimeframe.Hour; Func <int, DateTime> func = n => Now.AddHours(n); // Positive Test(12, func, Timeframe); Test(3, func, Timeframe); Test(56, func, Timeframe); // Negative Test(-12, func, Timeframe); Test(-3, func, Timeframe); Test(-56, func, Timeframe); // Same Test(0, func, Timeframe); }
protected async Task TestExists(ActivityTimeframe timeframe) { var key = EventActivity.GenerateKey( EventName, EventActivity.Settings.Timeframe.ToString()); var field = EventActivity.GenerateTimeframeFields( timeframe, Timestamp) .ElementAt((int)timeframe); using (var connection = await ConnectionFactories.Open()) { var result = await connection.Hashes .Exists(EventActivity.Settings.Db, key, field); Assert.True(result); } }
public static async Task <long> Count( this IEventActivity instance, string eventName, DateTime startTimestamp, DateTime endTimestamp, ActivityTimeframe timeframe) { if (instance == null) { throw new ArgumentNullException("instance"); } var result = await instance.Counts( eventName, startTimestamp, endTimestamp, timeframe); return(result.First()); }
public void YearRange() { const ActivityTimeframe Timeframe = ActivityTimeframe.Year; Func <int, DateTime> func = n => Now.AddYears(n); // Positive Test(2, func, Timeframe); Test(7, func, Timeframe); Test(11, func, Timeframe); Assert.Equal(2, Now.Range(func(1), Timeframe).Count()); Assert.Equal(7, Now.Range(func(6), Timeframe).Count()); Assert.Equal(11, Now.Range(func(10), Timeframe).Count()); // Negative Test(-2, func, Timeframe); Test(-7, func, Timeframe); Test(-11, func, Timeframe); // Same Test(0, func, Timeframe); }
public virtual async Task <long> Track( string eventName, ActivityTimeframe timeframe, DateTime timestamp, bool publishable) { // There are three tasks that we have to do here. // First, we have to maintain a list of events that has // been tracked, so that, the same event list can be // returned in the EventName method. Next. we also have to // maintain another list that is for time frames of the event, // please note that we are only going to maintain // the explicit timeframes, the Timeframes method returns the // explicit tracked timeframes for a given event name. // Second, increase the count for the matching timeframe. And at // last publish the event to redis so that the subscriber can be // notified. Validation.ValidateEventName(eventName); var eventsKey = GenerateKey(); var publishedEventsKey = eventsKey + Settings.KeySeparator + "published"; var key = GenerateKey(eventName, timeframe.ToString()); var fields = GenerateTimeframeFields(timeframe, timestamp).ToList(); var db = Settings.Db; using (var connection = await ConnectionFactories.Open()) { var eventTasks = new List <Task> { connection.Sets.Add(db, eventsKey, eventName), connection.Sets.Add( db, eventsKey + Settings.KeySeparator + eventName, timeframe.ToString()) }; if (publishable) { eventTasks.Add( connection.Sets.Add( db, publishedEventsKey, eventName)); } await Task.WhenAll(eventTasks); var fieldTasks = fields .Select(field => connection.Hashes .Increment(db, key, field)) .ToList(); var counts = await Task.WhenAll(fieldTasks); var count = counts.ElementAt((int)timeframe); if (!publishable) { return(count); } var channel = eventsKey + Settings.KeySeparator + eventName.ToUpperInvariant(); var payload = new EventActivitySubscriptionInfo { EventName = eventName, Timestamp = timestamp, Timeframe = timeframe, Count = counts.ElementAt((int)timeframe) }.Serialize(); await connection.Publish(channel, payload); return(count); } }
public virtual async Task Track( string eventName, ActivityTimeframe timeframe, DateTime timestamp, bool publishable, params long[] users) { Validation.ValidateEventName(eventName); Validation.ValidateUsers(users); var eventsKey = GenerateKey(); var publishedEventsKey = eventsKey + Settings.KeySeparator + "published"; string channel = null; byte[] payload = null; if (publishable) { channel = eventsKey + Settings.KeySeparator + eventName.ToUpperInvariant(); payload = new UserActivitySubscriptionInfo { EventName = eventName, Timestamp = timestamp, Timeframe = timeframe, Users = users }.Serialize(); } var timeframeKeys = GenerateEventTimeframeKeys( eventName, timeframe, timestamp).ToList(); var db = Settings.Db; using (var connection = await ConnectionFactories.Open()) { var eventTasks = new List <Task> { connection.Sets.Add(db, eventsKey, eventName), connection.Sets.Add( db, eventsKey + Settings.KeySeparator + eventName, timeframe.ToString()) }; if (publishable) { eventTasks.Add( connection.Sets.Add( db, publishedEventsKey, eventName)); } await Task.WhenAll(eventTasks); var bitTasks = new List <Task>(); foreach (var timeframeKey in timeframeKeys) { bitTasks.AddRange(users.Select(user => connection.Strings.SetBit( db, timeframeKey, user, true))); } await Task.WhenAll(bitTasks); if (publishable) { await connection.Publish(channel, payload); } } }
protected EventActivityHourTrackTests( ActivityTimeframe timeframe) : base(timeframe) { }
public static IEnumerable <DateTime> Range( this DateTime instance, DateTime other, ActivityTimeframe timeframe) { var backward = instance > other; var increment = backward ? -1 : 1; var outer = backward ? Greater : Lesser; var inner = backward ? Lesser : Greater; yield return(instance); var counter = instance; while (outer(counter, other)) { switch (timeframe) { case ActivityTimeframe.Hour: { counter = counter.AddHours(increment); break; } case ActivityTimeframe.Day: { counter = counter.AddDays(increment); break; } case ActivityTimeframe.Month: { counter = counter.AddMonths(increment); break; } case ActivityTimeframe.Minute: { counter = counter.AddMinutes(increment); break; } case ActivityTimeframe.Year: { counter = counter.AddYears(increment); break; } case ActivityTimeframe.Second: { counter = counter.AddSeconds(increment); break; } } if (inner(counter, other)) { continue; } yield return(counter); } }
public static async Task<long> Count( this IEventActivity instance, string eventName, DateTime startTimestamp, DateTime endTimestamp, ActivityTimeframe timeframe) { if (instance == null) { throw new ArgumentNullException("instance"); } var result = await instance.Counts( eventName, startTimestamp, endTimestamp, timeframe); return result.First(); }
protected UserActivityMonthTrackTests( ActivityTimeframe timeframe) : base(timeframe) { }
internal IEnumerable <string> GenerateTimeframeFields( ActivityTimeframe timeframe, DateTime timestamp) { yield return(timestamp.FormatYear()); var separator = Settings.KeySeparator; var type = (int)timeframe; if (type > (int)ActivityTimeframe.Year) { yield return(timestamp.FormatYear() + separator + timestamp.FormatMonth()); } if (type > (int)ActivityTimeframe.Month) { yield return(timestamp.FormatYear() + separator + timestamp.FormatMonth() + separator + timestamp.FormatDay()); } if (type > (int)ActivityTimeframe.Day) { yield return(timestamp.FormatYear() + separator + timestamp.FormatMonth() + separator + timestamp.FormatDay() + separator + timestamp.FormatHour()); } if (type > (int)ActivityTimeframe.Hour) { yield return(timestamp.FormatYear() + separator + timestamp.FormatMonth() + separator + timestamp.FormatDay() + separator + timestamp.FormatHour() + separator + timestamp.FormatMinute()); } if (type > (int)ActivityTimeframe.Minute) { yield return(timestamp.FormatYear() + separator + timestamp.FormatMonth() + separator + timestamp.FormatDay() + separator + timestamp.FormatHour() + separator + timestamp.FormatMinute() + separator + timestamp.FormatSecond()); } }
internal IEnumerable <string> GenerateEventTimeframeKeys( string eventName, ActivityTimeframe timeframe, DateTime timestamp) { yield return(GenerateKey( eventName, timestamp.FormatYear())); var type = (int)timeframe; if (type > (int)ActivityTimeframe.Year) { yield return(GenerateKey( eventName, timestamp.FormatYear(), timestamp.FormatMonth())); } if (type > (int)ActivityTimeframe.Month) { yield return(GenerateKey( eventName, timestamp.FormatYear(), timestamp.FormatMonth(), timestamp.FormatDay())); } if (type > (int)ActivityTimeframe.Day) { yield return(GenerateKey( eventName, timestamp.FormatYear(), timestamp.FormatMonth(), timestamp.FormatDay(), timestamp.FormatHour())); } if (type > (int)ActivityTimeframe.Hour) { yield return(GenerateKey( eventName, timestamp.FormatYear(), timestamp.FormatMonth(), timestamp.FormatDay(), timestamp.FormatHour(), timestamp.FormatMinute())); } if (type > (int)ActivityTimeframe.Minute) { yield return(GenerateKey( eventName, timestamp.FormatYear(), timestamp.FormatMonth(), timestamp.FormatDay(), timestamp.FormatHour(), timestamp.FormatMinute(), timestamp.FormatSecond())); } }
protected EventActivityMinuteTrackTests( ActivityTimeframe timeframe) : base(timeframe) { }
private static void Test( int interval, Func<int, DateTime> func, ActivityTimeframe timeframe) { Assert.Equal( Math.Abs(interval) + 1, Now.Range(func(interval), timeframe).Count()); }
public static IEnumerable<DateTime> Range( this DateTime instance, DateTime other, ActivityTimeframe timeframe) { var backward = instance > other; var increment = backward ? -1 : 1; var outer = backward ? Greater : Lesser; var inner = backward ? Lesser : Greater; yield return instance; var counter = instance; while (outer(counter, other)) { switch (timeframe) { case ActivityTimeframe.Hour: { counter = counter.AddHours(increment); break; } case ActivityTimeframe.Day: { counter = counter.AddDays(increment); break; } case ActivityTimeframe.Month: { counter = counter.AddMonths(increment); break; } case ActivityTimeframe.Minute: { counter = counter.AddMinutes(increment); break; } case ActivityTimeframe.Year: { counter = counter.AddYears(increment); break; } case ActivityTimeframe.Second: { counter = counter.AddSeconds(increment); break; } } if (inner(counter, other)) { continue; } yield return counter; } }