コード例 #1
0
 protected UserActivityTrackTests(ActivityTimeframe timeframe)
 {
     UserActivity = new UserActivity(
         new ActivitySettings(1, timeframe));
     UserActivity.Reset().Wait();
     UserActivity.Track(EventName, Timestamp, 1, 2, 3).Wait();
 }
コード例 #2
0
 protected EventActivityTrackTests(ActivityTimeframe timeframe)
 {
     EventActivity = new EventActivity(
         new ActivitySettings(1, timeframe));
     EventActivity.Reset().Wait();
     EventActivity.Track(EventName, Timestamp).Wait();
 }
コード例 #3
0
 protected UserActivityTrackTests(ActivityTimeframe timeframe)
 {
     UserActivity = new UserActivity(
         new ActivitySettings(1, timeframe));
     UserActivity.Reset().Wait();
     UserActivity.Track(EventName, Timestamp, 1, 2, 3).Wait();
 }
コード例 #4
0
 protected EventActivityTrackTests(ActivityTimeframe timeframe)
 {
     EventActivity = new EventActivity(
         new ActivitySettings(1, timeframe));
     EventActivity.Reset().Wait();
     EventActivity.Track(EventName, Timestamp).Wait();
 }
コード例 #5
0
        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);
        }
コード例 #6
0
 public ActivitySettings(ActivityTimeframe timeframe)
     : this(
         DefaultDb,
         timeframe,
         DefaultKeyPrefix,
         DefaultKeySeparator)
 {
 }
コード例 #7
0
 public ActivitySettings(ActivityTimeframe timeframe)
     : this(
     DefaultDb,
     timeframe,
     DefaultKeyPrefix,
     DefaultKeySeparator)
 {
 }
コード例 #8
0
 private static void Test(
     int interval,
     Func <int, DateTime> func,
     ActivityTimeframe timeframe)
 {
     Assert.Equal(
         Math.Abs(interval) + 1,
         Now.Range(func(interval), timeframe).Count());
 }
コード例 #9
0
        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));
        }
コード例 #10
0
        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;
        }
コード例 #11
0
        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;
        }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
        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);
            }
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
            }
        }
コード例 #18
0
        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);
            }
        }
コード例 #19
0
        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());
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
            }
        }
コード例 #22
0
        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);
                }
            }
        }
コード例 #23
0
 protected EventActivityHourTrackTests(
     ActivityTimeframe timeframe)
     : base(timeframe)
 {
 }
コード例 #24
0
        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);
            }
        }
コード例 #25
0
        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();
        }
コード例 #26
0
 protected UserActivityMonthTrackTests(
     ActivityTimeframe timeframe)
     : base(timeframe)
 {
 }
コード例 #27
0
        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());
            }
        }
コード例 #28
0
        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()));
            }
        }
コード例 #29
0
 protected EventActivityMinuteTrackTests(
     ActivityTimeframe timeframe)
     : base(timeframe)
 {
 }
コード例 #30
0
 private static void Test(
     int interval,
     Func<int, DateTime> func,
     ActivityTimeframe timeframe)
 {
     Assert.Equal(
         Math.Abs(interval) + 1,
         Now.Range(func(interval), timeframe).Count());
 }
コード例 #31
0
        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;
            }
        }