BB.Caching.Redis.Analytics.BitwiseAnalytics.GetQuarterAsync C# (CSharp) Method

GetQuarterAsync() public static method

Gets the keys for the months in the quarter covered by the DateTime supplied, creating the data at the key if necessary.
public static GetQuarterAsync ( IDatabase database, string category, string action, System.DateTime dateTime ) : Task
database IDatabase /// The database where the query will be performed. This is passed so that we can reuse the same database to /// perform multiple bitwise operations. Doing this with the same connection will guarantee that performance /// is good. ///
category string /// Typically the object that was interacted with (e.g. button) ///
action string /// The type of interaction (e.g. click) ///
dateTime System.DateTime /// The DateTime. ///
return Task
        public static async Task<RedisKey> GetQuarterAsync(
            IDatabase database, string category, string action, DateTime dateTime)
        {
            // get the key
            string quarter = BitwiseAnalytics.DateTimeUtil.QuarterNumber(dateTime);
            RedisKey key = EventKey(category, action, quarter);

            // return it if there's already data for this quarter
            bool quarterExists = await BitwiseAnalytics.ExistsAsync(database, key);
            if (quarterExists)
            {
                return key;
            }

            // no data for the month, so we need to create it from the days
            string[] monthsInQuarter = BitwiseAnalytics.DateTimeUtil.MonthsInQuarter(dateTime);

            var keyTasks = new List<Task<RedisKey>>();

            // make sure each day exists
            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (string month in monthsInQuarter)
            {
                keyTasks.Add(GetMonthAsync(
                    database, category, action, DateTime.ParseExact(month, "yyyyMM", CultureInfo.InvariantCulture)));
            }

            await Task.WhenAll(keyTasks);

            // combine the days to form one month
            await BitwiseAnalytics.BitwiseOrAsync(
                database,
                key,
                monthsInQuarter.Select(x => EventKey(category, action, x)).ToArray());

            return key;
        }