public static async Task<RedisKey> GetMonthAsync(
IDatabase database, string category, string action, DateTime dateTime)
{
// get the key
string month = BitwiseAnalytics.DateTimeUtil.OneMonth(dateTime);
RedisKey key = EventKey(category, action, month);
// return it if there's already data for this month
bool monthExists = await BitwiseAnalytics.ExistsAsync(database, key);
if (monthExists)
{
return month;
}
// no data for the month, so we need to create it from the days
string[] daysInMonth = BitwiseAnalytics.DateTimeUtil.DaysInMonth(dateTime);
var keyTasks = new List<Task<RedisKey>>();
// make sure each day exists
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (string day in daysInMonth)
{
keyTasks.Add(GetDayAsync(
database, category, action, DateTime.ParseExact(day, "yyyyMMdd", CultureInfo.InvariantCulture)));
}
await Task.WhenAll(keyTasks);
// combine the days to form one month
await BitwiseAnalytics.BitwiseOrAsync(
database,
key,
daysInMonth.Select(x => EventKey(category, action, x)).ToArray());
return key;
}