-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveUsersByMonth.cs
75 lines (64 loc) · 3.44 KB
/
ActiveUsersByMonth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//using LinqDb;
using LinqdbClient;
using StackData;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Testing
{
class ActiveUsersByMonth : ITest //active user - one that either asked or answered at least once
{
public void Do(string path)
{
var db = new Db(path, "admin", "admin");
//var res = db.Table<Question>().Where(f => f.OwnerUserId == null).GetIds().Ids.ToDictionary(f => f, f => 0);
//db.Table<Question>().Update(f => f.OwnerUserId, res);
//var res2 = db.Table<Answer>().Where(f => f.OwnerUserId == null).GetIds().Ids.ToDictionary(f => f, f => 0);
//db.Table<Answer>().Update(f => f.OwnerUserId, res2);
//db.Table<Question>().RemovePropertyMemoryIndex(f => f.OwnerUserId);
//db.Table<Answer>().RemovePropertyMemoryIndex(f => f.CreationDate);
//db.Table<Answer>().RemovePropertyMemoryIndex(f => f.OwnerUserId);
db.Table<Question>().CreatePropertyMemoryIndex(f => f.CreationDate);
db.Table<Question>().CreatePropertyMemoryIndex(f => f.OwnerUserId);
db.Table<Answer>().CreatePropertyMemoryIndex(f => f.CreationDate);
db.Table<Answer>().CreatePropertyMemoryIndex(f => f.OwnerUserId);
Stopwatch sw = new Stopwatch();
sw.Start();
Dictionary<DateTime, int> result = new Dictionary<DateTime, int>();
DateTime min_date = db.Table<Question>().OrderBy(f => f.CreationDate).Take(1).Select(f => new { f.CreationDate }).First().CreationDate;
for (DateTime cd = min_date; ; cd = cd.AddMonths(1))
{
DateTime from = new DateTime(cd.Year, cd.Month, 1);
DateTime to = new DateTime(cd.Year, cd.Month, DateTime.DaysInMonth(cd.Year, cd.Month), 23, 59, 59);
var users_asked_l = db.Table<Question>()
.Between(f => f.CreationDate, from, to, BetweenBoundaries.BothInclusive)
.Select(f => new { f.OwnerUserId })
.Select(f => f.OwnerUserId)
.Where(f => f != null);
if (!users_asked_l.Any())
{
break;
}
var users_asked = new HashSet<int?>(users_asked_l);
result[from] = users_asked.Count();
//answers
var users_answered = db.Table<Answer>()
.Between(f => f.CreationDate, from, to, BetweenBoundaries.BothInclusive)
.Select(f => new { f.OwnerUserId })
.Select(f => f.OwnerUserId)
.Where(f => f != null);
result[from] += users_answered.Where(f => !users_asked.Contains((int)f)).Distinct().Count();
}
foreach (var entry in result.OrderBy(f => f.Key))
{
Console.WriteLine("{0}-{1} - {2} active users", entry.Key.Year, entry.Key.Month, entry.Value);
}
sw.Stop();
Console.WriteLine("User activity per month: {0} sec", sw.Elapsed.TotalSeconds);
db.Dispose();
}
}
}