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;
}