Created
April 24, 2012 11:00
-
-
Save phinett/2478785 to your computer and use it in GitHub Desktop.
Stats.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using HM.Core.Documents; | |
using HM.Core.Infrastructure.Indexes; | |
using Raven.Abstractions.Commands; | |
using Raven.Abstractions.Data; | |
using Raven.Abstractions.Linq; | |
using Raven.Json.Linq; | |
using Raven.Client.Linq; | |
namespace HM.AudioStatisticsUpdateTask | |
{ | |
public static class StatsTaskAlt | |
{ | |
public static void Execute() | |
{ | |
IList<string> dayYears = new List<string>(); | |
// this is so we can query on our index for the last 7 days | |
for (int i = 0; i < 7; i++) | |
dayYears.Add(DateTimeOffset.Now.DayOfYear - i + "-" + DateTimeOffset.Now.AddDays((i*-1)).Year); | |
// perform this for each tenant in our database | |
foreach (var tenant in Program.Tenants) | |
{ | |
int page = 0; | |
int counter = 0; | |
Console.WriteLine("Begin Tenant: " + tenant.Name); | |
while(true) | |
{ | |
int lastAudioWeeksCommentsSum = 0; | |
int lastAudioWeeksDownloadsSum = 0; | |
int lastAudioWeeksPlaysSum = 0; | |
int lastAudioWeeksFavouritesSum = 0; | |
int lastAudioWeeksLikesSum = 0; | |
string lastAudioIdFromPrevBatch = ""; | |
using (var session = Program.DocumentStore.OpenSession(tenant.TenantKey)) | |
{ | |
Stopwatch fetchTimer = Stopwatch.StartNew(); | |
RavenQueryStatistics outStats; | |
var weeksStats = session.Query<AudioWeeklyStatsCounter_Index.ReduceResult, AudioWeeklyStatsCounter_Index>() | |
.Statistics(out outStats) | |
.Where(x => x.DayYear.In(dayYears)) | |
//.Include(x => x.AudioId) | |
.OrderBy(x => x.AudioId) | |
.Skip(page * 1024) | |
.Take(1024) | |
.ToList(); | |
page++; | |
Console.WriteLine("Fetched records in: " + fetchTimer.ElapsedMilliseconds); | |
if (weeksStats.Count == 0) | |
break; | |
string lastAudioIdInThisBatch = weeksStats.LastOrDefault().AudioId; | |
// if we have some saved values from the last batch, but there are none in this batch, we still need to save them | |
if (lastAudioIdFromPrevBatch != weeksStats.FirstOrDefault().AudioId && lastAudioIdFromPrevBatch != "") | |
{ | |
UpdateAudioStats(tenant.TenantKey, lastAudioIdFromPrevBatch, lastAudioWeeksPlaysSum, lastAudioWeeksDownloadsSum, lastAudioWeeksLikesSum, lastAudioWeeksFavouritesSum, lastAudioWeeksCommentsSum); | |
// reset these values until we get to the next last item in the collection | |
lastAudioIdFromPrevBatch = ""; | |
lastAudioWeeksCommentsSum = 0; | |
lastAudioWeeksDownloadsSum = 0; | |
lastAudioWeeksFavouritesSum = 0; | |
lastAudioWeeksLikesSum = 0; | |
lastAudioWeeksPlaysSum = 0; | |
} | |
foreach (var stats in weeksStats.GroupBy(x => x.AudioId)) | |
{ | |
// we need to cover the situation that not all statistics will part of this batch of 1024 results, so for the last audioId in this batch | |
// we will save the values in variables and process them in the next batch | |
if (stats.Key == lastAudioIdInThisBatch) | |
{ | |
lastAudioWeeksCommentsSum = stats.Sum(x => x.WeeksComments); | |
lastAudioWeeksDownloadsSum = stats.Sum(x => x.WeeksDownloads); | |
lastAudioWeeksPlaysSum = stats.Sum(x => x.WeeksPlays); | |
lastAudioWeeksFavouritesSum = stats.Sum(x => x.WeeksFavourites); | |
lastAudioWeeksLikesSum = stats.Sum(x => x.WeeksLikes); | |
lastAudioIdFromPrevBatch = stats.Key; | |
} | |
UpdateAudioStats(tenant.TenantKey, stats.Key, | |
stats.Sum(x => x.WeeksPlays + lastAudioWeeksPlaysSum), | |
stats.Sum(x => x.WeeksDownloads + lastAudioWeeksDownloadsSum), | |
stats.Sum(x => x.WeeksLikes + lastAudioWeeksLikesSum), | |
stats.Sum(x => x.WeeksFavourites + lastAudioWeeksFavouritesSum), | |
stats.Sum(x => x.WeeksComments + lastAudioWeeksCommentsSum) | |
); | |
// reset these values | |
if (stats.Key == lastAudioIdFromPrevBatch) | |
{ | |
lastAudioIdFromPrevBatch = ""; | |
lastAudioWeeksCommentsSum = 0; | |
lastAudioWeeksDownloadsSum = 0; | |
lastAudioWeeksFavouritesSum = 0; | |
lastAudioWeeksLikesSum = 0; | |
lastAudioWeeksPlaysSum = 0; | |
} | |
counter++; | |
Console.WriteLine("Done: " + stats.Key + " " + counter + "/" + outStats.TotalResults); | |
} | |
// catch the last remaining audio document to be saved | |
if (lastAudioIdFromPrevBatch != "") | |
{ | |
UpdateAudioStats(tenant.TenantKey, lastAudioIdFromPrevBatch, lastAudioWeeksPlaysSum, lastAudioWeeksDownloadsSum, lastAudioWeeksLikesSum, lastAudioWeeksFavouritesSum, lastAudioWeeksCommentsSum); | |
} | |
//session.SaveChanges(); | |
} | |
} | |
} | |
} | |
private static void UpdateAudioStats(string tenantKey, string audioId, int plays, int downloads, int likes, int favs, int comments) | |
{ | |
Program.DocumentStore.DatabaseCommands.ForDatabase(tenantKey).Patch(audioId, | |
new PatchRequest[] | |
{ | |
new PatchRequest() | |
{ | |
Name = "WeeksPlays", | |
Type = PatchCommandType.Set, | |
Value = plays | |
}, | |
new PatchRequest() | |
{ | |
Name = "WeeksDownloads", | |
Type = PatchCommandType.Set, | |
Value = downloads | |
}, | |
new PatchRequest() | |
{ | |
Name = "WeeksComments", | |
Type = PatchCommandType.Set, | |
Value = comments | |
}, | |
new PatchRequest() | |
{ | |
Name = "WeeksLikes", | |
Type = PatchCommandType.Set, | |
Value = likes | |
}, | |
new PatchRequest() | |
{ | |
Name = "WeeksFavourites", | |
Type = PatchCommandType.Set, | |
Value = favs | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment