-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
263 lines (225 loc) · 7.2 KB
/
Copy pathProgram.cs
File metadata and controls
263 lines (225 loc) · 7.2 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System.Text.Json;
var modUpdates = new Dictionary<string, List<DownloadCountUpdate>>(256);
var hunkLines = new List<string>(64);
DateTime commitTime = default;
bool inDatabaseDiff = false;
bool pastFirstHunkHeader = false;
using (var fs = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 1024, FileOptions.SequentialScan))
using (var reader = new StreamReader(fs))
{
string? line;
while ((line = reader.ReadLine()) is not null)
{
if (line.StartsWith("Date:"))
{
commitTime = ParseGitDate(line);
continue;
}
if (line == "+++ b/database.json")
{
inDatabaseDiff = true;
pastFirstHunkHeader = false;
hunkLines.Clear();
continue;
}
if (!inDatabaseDiff) continue;
if (!pastFirstHunkHeader)
{
if (line.StartsWith("@@"))
{
pastFirstHunkHeader = true;
hunkLines.Clear();
}
continue;
}
if (line.StartsWith("@@"))
{
ProcessHunk(hunkLines, commitTime, modUpdates);
hunkLines.Clear();
continue;
}
if (line.StartsWith("commit ") || line.StartsWith("diff --git"))
{
ProcessHunk(hunkLines, commitTime, modUpdates);
hunkLines.Clear();
inDatabaseDiff = false;
pastFirstHunkHeader = false;
continue;
}
hunkLines.Add(line);
}
if (hunkLines.Count > 0)
ProcessHunk(hunkLines, commitTime, modUpdates);
}
foreach (var (newRepo, oldRepoList) in Offsets.oldRepos)
{
if (!modUpdates.TryGetValue(newRepo, out var newUpdates))
{
newUpdates = [];
modUpdates[newRepo] = newUpdates;
}
foreach (var oldRepo in oldRepoList)
{
if (modUpdates.TryGetValue(oldRepo, out var oldUpdates))
{
foreach (var u in oldUpdates)
newUpdates.Add(new DownloadCountUpdate(u.UnixTimestamp, u.DownloadCount));
}
}
}
foreach (var (repo, betweens) in Offsets.offsetBetween)
{
if (!modUpdates.TryGetValue(repo, out var updates)) continue;
foreach (var update in updates)
{
foreach (var between in betweens)
{
if (update.UnixTimestamp > between.AfterUnixTimestamp && update.UnixTimestamp <= between.BeforeUnixTimestamp)
update.DownloadCount += between.DownloadCount;
}
}
}
foreach (var (repo, staticUpdates) in Offsets.offsets)
{
if (!modUpdates.TryGetValue(repo, out var updates))
{
updates = [];
modUpdates[repo] = updates;
}
foreach (var u in staticUpdates)
updates.Add(new DownloadCountUpdate(u.UnixTimestamp, u.DownloadCount));
}
using var stdout = Console.OpenStandardOutput();
using var buffered = new BufferedStream(stdout, 1024 * 1024);
using var writer = new Utf8JsonWriter(buffered, new JsonWriterOptions { SkipValidation = true });
writer.WriteStartArray();
foreach (var (repo, updates) in modUpdates)
{
writer.WriteStartObject();
writer.WriteString("Repo"u8, repo);
writer.WriteStartArray("Updates"u8);
foreach (var u in updates)
{
writer.WriteStartObject();
writer.WriteNumber("UnixTimestamp"u8, u.UnixTimestamp);
writer.WriteNumber("DownloadCount"u8, u.DownloadCount);
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
writer.WriteEndArray();
static void ProcessHunk(List<string> lines, DateTime time, Dictionary<string, List<DownloadCountUpdate>> modUpdates)
{
if (lines.Count == 0) return;
string? contextRepo = null, addedRepo = null, removedRepo = null, installerRepo = null;
bool foundDownloadCount = false;
int downloadCount = 0;
foreach (var line in lines)
{
if (line.Length == 0) continue;
if (line.Contains("\"repo\":"))
{
var val = ExtractJsonString(line, "\"repo\":");
if (val != null)
{
switch (line[0])
{
case '+': addedRepo ??= val; break;
case '-': removedRepo ??= val; break;
default: contextRepo ??= val; break;
}
}
}
if (installerRepo == null && line.Contains("\"installerDownloadUrl\":"))
{
var url = ExtractJsonString(line, "\"installerDownloadUrl\":");
if (url != null)
installerRepo = TruncateToRepo(url);
}
if (!foundDownloadCount && line[0] == '+' && line.Contains("\"downloadCount\":"))
{
downloadCount = ExtractInt(line, "\"downloadCount\":");
foundDownloadCount = true;
}
}
var repo = contextRepo ?? addedRepo ?? removedRepo ?? installerRepo;
if (repo == null || !foundDownloadCount) return;
long unixTime = ((DateTimeOffset)DateTime.SpecifyKind(time, DateTimeKind.Utc)).ToUnixTimeSeconds();
if (!modUpdates.TryGetValue(repo, out var updates))
{
updates = [];
modUpdates[repo] = updates;
}
updates.Add(new DownloadCountUpdate(unixTime, downloadCount));
}
static string? ExtractJsonString(string line, string key)
{
int keyIdx = line.IndexOf(key, StringComparison.Ordinal);
if (keyIdx < 0) return null;
int openQuote = line.IndexOf('"', keyIdx + key.Length);
if (openQuote < 0) return null;
int closeQuote = line.IndexOf('"', openQuote + 1);
if (closeQuote < 0) return null;
return line[(openQuote + 1)..closeQuote];
}
static int ExtractInt(string line, string key)
{
int keyIdx = line.IndexOf(key, StringComparison.Ordinal);
if (keyIdx < 0) return 0;
var span = line.AsSpan()[(keyIdx + key.Length)..].Trim();
if (span.EndsWith(","))
span = span[..^1];
return int.TryParse(span, out int result) ? result : 0;
}
static string TruncateToRepo(string url)
{
int slashCount = 0;
for (int i = 0; i < url.Length; i++)
{
if (url[i] == '/')
{
slashCount++;
if (slashCount == 5)
return url[..i];
}
}
return url;
}
static DateTime ParseGitDate(string line)
{
var span = line.AsSpan();
int colonIdx = span.IndexOf(':');
span = span[(colonIdx + 1)..].Trim();
// e.g. "Tue Mar 29 23:53:02 2022 +0000"
// Skip day-of-week (3 chars + space)
span = span[4..];
// "Mar 29 23:53:02 2022 +0000" or "Mar 5 23:53:02 2022 +0000"
int month = span[..3] switch
{
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12,
_ => 0
};
span = span[3..].TrimStart();
// "29 23:53:02 2022 +0000"
int spaceIdx = span.IndexOf(' ');
int day = int.Parse(span[..spaceIdx]);
span = span[(spaceIdx + 1)..];
// "23:53:02 2022 +0000"
int hours = int.Parse(span[..2]);
int minutes = int.Parse(span[3..5]);
int seconds = int.Parse(span[6..8]);
int year = int.Parse(span[9..13]);
return new DateTime(year, month, day, hours, minutes, seconds, DateTimeKind.Utc);
}