public Task IncAsync(TimeSeriesRollupsInsertOptions options)
        {
            var day = new DateTime(options.DateTime.Year, options.DateTime.Month, options.DateTime.Day, 0, 0, 0);

            BoundStatement boundStatement = _incStatement.Value.Bind(options.EventId, day);
            return _session.Get().ExecuteAsync(boundStatement);
        }
        public Task IncAsync(TimeSeriesRollupsInsertOptions options)
        {
            string rowKey = BuildRowKey(options.EventId, options.DateTime.Hour);
            var minute = new DateTime(options.DateTime.Year, options.DateTime.Month, options.DateTime.Day, options.DateTime.Hour, options.DateTime.Minute, 0);

            BoundStatement boundStatement = _incStatement.Value.Bind(rowKey, minute);
            return _session.Get().ExecuteAsync(boundStatement);
        }
        public Task AddViewAsync(string space, string itemId, string userId)
        {
            if (string.IsNullOrEmpty(itemId))
            {
                throw new ArgumentNullException("itemId");
            }

            // Writing to all required key families
            const SignalType signal = SignalType.View;
            DateTime dateTime = DateTime.UtcNow;
            var tasks = new List<Task>();


            // 1. Item and user relations

            // Building composite row key to isolated different spaces of statistics
            string itemRowKey = BuildGlobalId(space, itemId);

            // Item cumulative counts
            tasks.Add(_itemCountsRepository.IncViewsAsync(itemRowKey));

            if (!string.IsNullOrEmpty(userId))
            {
                // Building composite row key to isolated different spaces of statistics
                string userRowKey = BuildGlobalId(space, userId);

                // User cumulative counts
                tasks.Add(_userCountsRepository.IncViewsAsync(userRowKey));

                // Users who viewed an item
                tasks.Add(_itemSignalsRepository.AddAsync(new ItemSignalsInsertDeleteOptions { ItemId = itemRowKey, UserId = userId, DateTime = dateTime, Signal = signal }));

                // User viewed items
                tasks.Add(Task.Run(() =>
                {
                    // add new view signal
                    var addTasks = new List<Task>();

                    var userSignal = new UserSignalsInsertDeleteOptions { ItemId = itemId, UserId = userRowKey, DateTime = dateTime, Signal = signal };
                    addTasks.Add(_userSignalsRepository.AddAsync(userSignal));
                    addTasks.Add(_userSignalsUnorderedRepository.AddAsync(userSignal));

                    return Task.WhenAll(addTasks);
                }));
            }


            // 2. Affinity groups
            IEnumerable<string> affinityGroups = BuildAffinityGroups(dateTime);
            foreach (string affinityGroup in affinityGroups)
            {
                // Building composite row keys to isolated different spaces of statistics
                string groupId = BuildGlobalId(space, affinityGroup);

                // Affinity group most viewed
                tasks.Add(Task.Run(async () =>
                {
                    // getting current version of most viewed items
                    AffinityGroupMostSignaledVersionEntity currentVersion = await _affinityGroupMostSignaledVersionRepository.GetAsync(groupId, signal);

                    // getting item counts
                    IEnumerable<AffinityGroupItemCountsEntity> itemCounts = await _affinityGroupItemCountsRepository.GetSequenceAsync(groupId, signal, 1000);

                    // building new version of most viewed items
                    Dictionary<string, AffinityGroupItemCountsEntity> itemCountsDict = itemCounts.ToDictionary(i => i.ItemId);
                    var insertOptions = new List<AffinityGroupMostSignaledInsertOptions>();
                    foreach (var i in itemCountsDict)
                    {
                        var insertItem = new AffinityGroupMostSignaledInsertOptions { ItemId = i.Key, Count = i.Value.Count };
                        if (insertItem.ItemId == itemId)
                        {
                            insertItem.Count = insertItem.Count + 1;
                        }

                        insertOptions.Add(insertItem);
                    }

                    // ensuring current item
                    if (!itemCountsDict.ContainsKey(itemId))
                    {
                        insertOptions.Add(new AffinityGroupMostSignaledInsertOptions { ItemId = itemId, Count = 1 });
                    }

                    await _affinityGroupMostSignaledRepository.AddAsync(groupId, signal, currentVersion.Version + 1, insertOptions);

                    // incrementing counter of itemId
                    await _affinityGroupItemCountsRepository.IncAsync(groupId, signal, itemId);

                    // incrementing version
                    await _affinityGroupMostSignaledVersionRepository.UpdateAsync(groupId, signal, currentVersion.Version + 1);
                }));

                // incrementing affinity group cumulative counts
                tasks.Add(_affinityGroupCountsRepository.IncAsync(groupId, signal));
            }


            // 3. Time series
            string eventId = BuildTimeSeriesRowKey(itemRowKey, signal);

            // Rollups
            var rollupOptions = new TimeSeriesRollupsInsertOptions
            {
                EventId = eventId,
                DateTime = dateTime
            };

            // Rollups for days
            tasks.Add(_timeSeriesRollupsDayRepository.IncAsync(rollupOptions));

            // Rollups for hour
            tasks.Add(_timeSeriesRollupsHourRepository.IncAsync(rollupOptions));

            // Rollups for minute
            tasks.Add(_timeSeriesRollupsMinuteRepository.IncAsync(rollupOptions));


            return Task.WhenAll(tasks);
        }