public virtual async Task <long> Count()
 {
     using (var connection = await ConnectionFactories.Open())
     {
         return(await InternalCount(connection));
     }
 }
예제 #2
0
        public virtual async Task <long[]> Counts(
            string eventName,
            DateTime startTimestamp,
            DateTime endTimestamp,
            ActivityDrilldown drilldown)
        {
            Validation.ValidateEventName(eventName);

            var dates = startTimestamp.Range(endTimestamp, drilldown);
            var key   = GenerateKey(eventName, drilldown.ToString());

            var fields = dates.Select(d =>
                                      GenerateTimeframeFields(drilldown, d)
                                      .ElementAt((int)drilldown))
                         .ToArray();

            string[] values;

            using (var connection = await ConnectionFactories.Open())
            {
                values = await connection.Hashes.GetString(
                    Settings.Db,
                    key,
                    fields);
            }

            var result = values.Select(v =>
                                       string.IsNullOrWhiteSpace(v) ? 0L : long.Parse(v))
                         .ToArray();

            return(result);
        }
예제 #3
0
        public virtual async Task <IEnumerable <string> > EventNames(
            bool onlyPublished)
        {
            var eventsKey = GenerateKey();

            if (onlyPublished)
            {
                eventsKey += Settings.KeySeparator + "published";
            }

            string[] members;

            using (var connection = await ConnectionFactories.Open())
            {
                members = await connection.Sets.GetAllString(
                    Settings.Db,
                    eventsKey);
            }

            var result = members.Select(RemoveKeyPrefix)
                         .OrderBy(n => n)
                         .ToList();

            return(result);
        }
예제 #4
0
        public virtual async Task Track(
            string eventName,
            ActivityDrilldown drilldown,
            DateTime timestamp,
            bool publishable,
            params long[] users)
        {
            Validation.ValidateEventName(eventName);
            Validation.ValidateUsers(users);

            string channel = null;

            byte[] payload = null;

            var timeframeKeys = GenerateEventTimeframeKeys(
                eventName,
                drilldown,
                timestamp).ToList();

            var eventsKey = GenerateKey();

            var db    = Settings.Db;
            var tasks = new List <Task>();

            if (publishable)
            {
                channel = eventsKey +
                          Settings.KeySeparator +
                          eventName.ToUpperInvariant();

                payload = new UserActivitySubscriptionInfo
                {
                    EventName = eventName,
                    Timestamp = timestamp,
                    Users     = users
                }.Serialize();
            }

            using (var connection = await ConnectionFactories.Open())
            {
                foreach (var timeframeKey in timeframeKeys)
                {
                    tasks.AddRange(users.Select(user =>
                                                connection.Strings.SetBit(
                                                    db,
                                                    timeframeKey,
                                                    user,
                                                    true)));
                }

                tasks.Add(connection.Sets.Add(db, eventsKey, eventName));

                await Task.WhenAll(tasks);

                if (publishable)
                {
                    await connection.Publish(channel, payload);
                }
            }
        }
예제 #5
0
        public override async Task <long> Count()
        {
            using (var connection = await ConnectionFactories.Open())
            {
                await PerformBitOperation(connection);

                return(await InternalCount(connection));
            }
        }
        public virtual async Task <bool[]> Includes(params long[] users)
        {
            Validation.ValidateUsers(users);

            using (var connection = await ConnectionFactories.Open())
            {
                return(await InternalIncludes(connection, users));
            }
        }
예제 #7
0
        public virtual ISubscription <TInfo> CreateSubscription()
        {
            var prefix = GenerateKey() + Settings.KeySeparator;

            var subscription = new Subscription <TInfo>(
                ConnectionFactories.SubscriberFactory(),
                prefix);

            return(subscription);
        }
예제 #8
0
        public override async Task <bool[]> Includes(params long[] users)
        {
            Validation.ValidateUsers(users);

            using (var connection = await ConnectionFactories.Open())
            {
                await PerformBitOperation(connection);

                return(await InternalIncludes(connection, users));
            }
        }
        public virtual async Task <bool> Remove()
        {
            bool result;

            using (var connection = await ConnectionFactories.Open())
            {
                result = await connection.Keys.Remove(Db, Key);
            }

            return(result);
        }
예제 #10
0
        public virtual async Task <long> Track(
            string eventName,
            ActivityDrilldown drilldown,
            DateTime timestamp,
            bool publishable)
        {
            Validation.ValidateEventName(eventName);

            var key       = GenerateKey(eventName, drilldown.ToString());
            var eventsKey = GenerateKey();
            var fields    = GenerateTimeframeFields(drilldown, timestamp).ToList();
            var db        = Settings.Db;

            using (var connection = await ConnectionFactories.Open())
            {
                await connection.Sets.Add(db, eventsKey, eventName);

                var tasks = fields
                            .Select(field => connection.Hashes
                                    .Increment(db, key, field))
                            .ToList();

                var counts = await Task.WhenAll(tasks);

                var count = counts.ElementAt((int)drilldown);

                if (!publishable)
                {
                    return(count);
                }

                var channel = eventsKey +
                              Settings.KeySeparator +
                              eventName.ToUpperInvariant();

                var payload = new EventActivitySubscriptionInfo
                {
                    EventName = eventName,
                    Timestamp = timestamp,
                    Drilldown = drilldown,
                    Count     = counts.ElementAt((int)drilldown)
                }.Serialize();

                await connection.Publish(channel, payload);

                return(count);
            }
        }
예제 #11
0
        public virtual async Task <IEnumerable <string> > EventNames()
        {
            var eventsKey = GenerateKey();
            var db        = Settings.Db;

            string[] names;

            using (var connection = await ConnectionFactories.Open())
            {
                names = await connection.Sets.GetAllString(db, eventsKey);
            }

            var result = names.Select(RemoveKeyPrefix);

            return(result);
        }
예제 #12
0
        public virtual ISubscription CreateSubscription(
            string eventName,
            Action <TInfo> action)
        {
            Validation.ValidateEventName(eventName);

            var channel = GenerateKey() +
                          Settings.KeySeparator +
                          eventName.ToUpperInvariant();

            var subscription = new Subscription <TInfo>(
                ConnectionFactories.SubscriberFactory(),
                channel,
                action);

            return(subscription);
        }
예제 #13
0
        public virtual async Task <long> Reset()
        {
            var  wildcard = GenerateKey() + "*";
            var  db       = Settings.Db;
            long result   = 0;

            using (var connection = await ConnectionFactories.Open())
            {
                var keys = await connection.Keys.Find(db, wildcard);

                if (keys.Any())
                {
                    result = await connection.Keys.Remove(db, keys);
                }
            }

            return(result);
        }
예제 #14
0
        public virtual async Task <IEnumerable <ActivityTimeframe> > Timeframes(
            string eventName)
        {
            Validation.ValidateEventName(eventName);

            var key = GenerateKey() +
                      Settings.KeySeparator +
                      eventName;

            string[] members;

            using (var connection = await ConnectionFactories.Open())
            {
                members = await connection.Sets.GetAllString(Settings.Db, key);
            }

            var result = members.Select(m =>
                                        (ActivityTimeframe)Enum.Parse(typeof(ActivityTimeframe), m))
                         .ToList();

            return(result);
        }
        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);
            }
        }