-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.d
445 lines (376 loc) · 10.1 KB
/
vue.d
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import std.stdio;
import std.array;
import std.container;
import std.conv;
import std.range;
import std.algorithm;
import std.datetime;
import std.traits;
import std.typecons;
static import std.math;
import std.complex;
import std.exception;
import std.format;
import local.getopt;
import post;
import view;
immutable auto dT=dur!"minutes"(10);
uint average=0;
bool weighted;
void main(string[] arg)
{
try {
enum Field { id, length, at, start, duration, views, marks, comms }
enum Mode { none, total, list, view, mark, comm, sum, raw, fit, dev }
int mid=0, cid=0, cm=0, raw, position;
int[] post_id;
Mode mode;
Field[] fmt;
Field sort_fn=Field.id;
bool force_all, invert, normalize, help;
Option[] opt;
getopt(opt, arg //,noThrow.yes
, "-p|--post", "post id's to analyze. If not given, use entire file\nexcept truncated posts", &post_id
, "-x|--exclude", "invert post list, exclude given list from the\nentire set", &invert
, "-A|--all", "don't remove truncated posts", &force_all
, "-t|--total", "display sum of all views (habrapulse)", delegate { mode=Mode.total; }
, "-l|--list", "list posts according to the format or just post id's\nif empty", delegate { mode=Mode.list; }
, "--format", "list format", &fmt
, "--sort", "list sort field", &sort_fn
, "-v|--view", "display selected posts views", delegate { mode=Mode.view; }
, "-m|--mark", "display selected posts marks", delegate { mode=Mode.mark; }
, "-c|--comm", "display selected posts comments", delegate { mode=Mode.comm; }
, "-s|--sum", "sum selected posts views", delegate { mode=Mode.sum; }
, "-r|--raw", "raw post data", delegate { mode=Mode.raw; }
, "-f|--fit", "view cross fit table", delegate { mode=Mode.fit; }
, "-d", "under development", delegate { mode=Mode.dev; }
, "-S|--smooth", "average data", &average
, "-N|--normalize", "normalize data", &normalize
, "-W|--weight", "weight data", &weighted
, "-h|-?|--help", "print this help", &help, true
);
if(help) {
writeln("vue: analyze habrahabr statistics");
writeln("Syntax: vue [OPTIONS | -h] [<file>]");
writeln(" If no file given, read stdin");
writeln("Options:\n",optionHelp(sort!("a.group < b.group || a.group == b.group && a.tag < b.tag")(opt)));
writeln("Examples:");
writeln(" vue -t post.list");
writeln(" vue -txp100000,100013 post.list");
writeln(" vue -lA --format=at,id,duration,length --sort=at");
return;
}
File fd;
if(arg.length < 2)
fd.fdopen(0,"r");
else
fd.open(arg[1], "r");
Post[uint] data, all=parse(fd);
fd.close;
if(post_id.empty) {
data=all.dup;
} else {
if(invert) {
data=all;
foreach(id; post_id)
data.remove(id);
} else {
foreach(id; post_id) {
if(id in all)
data[id]=all[id];
else
stderr.writeln("post ",id," ignored");
}
}
}
if(!force_all) {
foreach(post; data) {
if(post.start > 30 || post.end < 1440) {
//if(post.start > 30) {
//writeln("post ", post.id, " filtered out: started at ", post.start, ", finished at ", post.end);
data.remove(post.id);
}
}
}
enforce(data.length, "no valid posts");
//Post total=pulse(all.values).slice(data.values.interval);
Post total=pulse(all.values);
if(mode == Mode.total) {
auto view=total.view(dT, total.at).smooth(average);
if(normalize) view=view.normalize;
writeln("from ",view.start," to ",view.end);
foreach(v; view.range)
writeln(v.time," ", v.value);
} else if(mode == Mode.list) {
if(fmt.empty)
fmt~=Field.id;
auto cmp=&Post.byId;
switch(sort_fn) {
case Field.id: cmp=&Post.byId; break;
case Field.at: cmp=&Post.byAt; break;
case Field.start: cmp=&Post.byStart; break;
case Field.duration: cmp=&Post.byEnd; break;
case Field.length: cmp=&Post.byLength; break;
case Field.views: cmp=&Post.byViews; break;
default: assert(0);
}
foreach(post; data.byValue.array.sort!cmp) {
foreach(f; fmt)
switch(f) {
case Field.id: write(post.id, " "); break;
case Field.at: write(post.at, " "); break;
case Field.start: write(post.start, " "); break;
case Field.duration: write(post.end, " "); break;
case Field.length: write(post.length, " "); break;
case Field.views: write(post.views, " "); break;
case Field.marks: write(post.marks, " "); break;
case Field.comms: write(post.comms, " "); break;
default: assert(0);
}
writeln();
}
} else if(mode == Mode.view) {
auto tv=total.view(dT,total.at).smooth(50).normalize;
foreach(post; data) {
View view=post.view(dT,total.at).smooth(average);
if(weighted) view=view.weight(tv);
if(normalize) view=view.normalize;
writeln("at ",post.at);
foreach(v; view.range)
writeln(v.time," ", v.value);
}
} else if(mode == Mode.mark) {
auto tm=total.view(dT,total.at).smooth(50).normalize;
foreach(post; data) {
View mark=weighted?
post.mark(dT,total).smooth(average)
: post.mark(dT,total.at).smooth(average);
if(normalize) mark=mark.normalize;
writeln("at ",post.at);
//foreach(v; view.range(total.at))
foreach(v; mark.range)
writeln(v.time," ", v.value);
}
} else if(mode == Mode.sum) {
auto tv=total.view(dT,total.at).smooth(50).normalize;
float[Duration] z;
foreach(post; data) {
View view=post.view(dT,total.at);
if(weighted) view=view.weight(tv);
if(normalize) view=view.normalize;
auto t0=view.start;
foreach(v; view.range) {
auto t=v.ts-t0;
if(t !in z) z[t]=0;
z[t]+=v.value;
}
}
auto s=new View;
foreach(v; z.byKeyValue)
s.add(total.at+v.key, v.value);
s=s.smooth(average).normalize;
writeln(" ");
foreach(v; s.range)
writeln(v.time," ", v.value);
//if(v.value > .1) writeln(v.time," ", log(v.value));
} else if(mode == Mode.fit) {
auto tv=total.view(dT,total.at).smooth(50).normalize;
foreach(post; data) {
View base=post.view(dT, total.at).smooth(average);
if(weighted) base=base.weight(tv);
base=base.normalize;
foreach(sample; data) {
if(sample.id >= post.id)
continue;
View view=sample.view(dT,total.at).smooth(average);
if(weighted) view=view.weight(tv);
view=view.normalize;
auto f=fit(base, view);
writeln(sample.id," ",post.id," ",f);
writeln(post.id," ",sample.id," ",f);
}
}
} else if(mode == Mode.raw) {
foreach(post; data) {
writeln("post ",post.id);
foreach(v; post.range)
writeln(v[0]/60.," ",v[1]);
writeln("");
foreach(v; post.compress.range)
writeln(v[0]/60.," ",v[1]);
}
} else if(mode == Mode.dev) {
Complex!float[int] zz;
foreach(post; data) {
if(post.comms == 0) continue;
float v=post.views;
zz[post.id]=complex(post.marks,post.comms)*1000;
}
//uint[] rad=[50,50,50,40,40,20,20, 20, 20, 16, 10, 10, 10,18];
uint[] rad=[50,50,50,50,50,50,40,40,20];
foreach(n; rad) {
auto z=itr(zz, n);
writeln;
foreach(v; z.values) writeln(v.re," ",v.im);
foreach(id; z.byKey) stderr.write(id," "); stderr.writeln;
}
writeln;
//foreach(v; zz.values) writeln(v.re," ",v.im);
//stderr.writeln("left ",zz.length);
}
} catch(Exception x) {
writefln(x.msg);
}
}
auto itr(ref Complex!float[int] zz, uint n)
{
auto z=zz;
float l0=z.length;
Complex!float z0;
for(int i=0; z.length >= n && i < 1000; ++i) {
z0=middle(z.values);
z=exclude(z, z0, .9);
}
foreach(id; z.keys) zz.remove(id);
//stderr.writeln("at ",z0," ",distro(z.values));
return z;
}
auto middle(R)(R[] r)
{
R r0=0;
foreach(v; r) r0+=v;
return r0/r.length;
}
auto distro(R)(R[] r)
{
R r0=0;
foreach(v; r) r0+=v;
r0/=r.length;
double r2=0;
foreach(v; r) { auto t=abs(v-r0); r2+=t*t; }
return sqrt(r2/r.length);
}
auto exclude(R)(R[int] r, R r0, double wgt)
{
double d=0;
foreach(v; r.values) d=max(d, abs(v-r0));
d*=wgt;
R[int] t;
foreach(v; r.byKeyValue)
if(abs(v.value-r0) < d) t[v.key]=v.value;
return t;
}
auto fit(View base, View view)
{
static import std.math;
static import std.complex;
double f0=0, f1=0, f2=0;
foreach(v; zip(base.range, view.range)) {
f0+=v[0].value*v[0].value;
f1+=v[0].value*v[1].value;
f2+=v[1].value*v[1].value;
}
auto k=f1/f2;
double fit=0;
uint n=0;
foreach(v; zip(base.range, view.range)) {
auto f=v[0].value-k*v[1].value;
fit+=f*f;
++n;
}
fit=sqrt(fit/f0);
return fit;
}
auto fit0(View base, View view)
{
double f0=0, f1=0;
foreach(v; zip(base.range, view.range)) {
auto f=v[0].value-v[1].value;
f1+=f*f;
f0+=v[0].value*v[0].value;
}
return sqrt(f1/f0);
}
Post pulse(T)(T data)
if(is(ForeachType!T == Post))
{
// find sum of all post views
DateTime at=data.front.begin;
Post.Stat[DateTime] heap;
// sum all post INCREMENTS into assoc.array indexed by time
foreach(post; data) {
uint last=0;
at=min(at, post.begin);
foreach(stat; post.stat) {
if(stat.ts !in heap)
heap[stat.ts]=Post.Stat(stat.ts);
heap[stat.ts].view+=stat.view-last;
last=stat.view;
}
}
// convert the array into Post.Stat[] array
auto pulse=Post(0, DateTime(at.date));
foreach(ts; heap.keys.sort)
pulse.add(heap[ts]);
// convert view increments back to integral views
uint last=0;
foreach(ref stat; pulse.stat) {
stat.view+=last;
last=stat.view;
}
return pulse.compress;
}
/*
Post total(T)(T data)
if(is(ForeachType!T == Post))
{
Post.Stat[DateTime] heap;
foreach(post; data) {
auto stat=post._stat;
foreach(i; 0..post.length-1) {
auto t0=stat[i].ts, t1=stat[i+1].ts;
if(t0 !in heap)
heap[t0]=Post.Stat(t0);
heap[t0].view+=stat[i+1].view-stat[i].view;
heap[t0].mark+=stat[i+1].mark-stat[i].mark;
heap[t0].comm+=stat[i+1].comm-stat[i].comm;
}
}
Post total;
foreach(ts; heap.keys.sort)
total.add(heap[ts]);
writeln("raw total");
foreach(st; total._stat)
writeln(hr(DateTime(total.begin.date),st.ts)," ",st.view);
foreach(i; 1..total._stat.length)
total._stat[i].view+=total._stat[i-1].view;
total._at=DateTime(total.begin.date);
return total;
}
*/
/*
auto fit(View a, View b)
{
auto start=max(a.start,b.start);
auto end=min(a.end,b.end);
float A=0, F=0;
for(auto t=start; t < end; t+=.5) {
auto x=a(t), y=b(t);
A+=x*x;
F+=x*y;
}
return F/A;
}
auto diff(View a, View b)
{
auto start=max(a.start,b.start);
auto end=min(a.end,b.end);
float s=0, z=0;
for(auto t=start; t < end; t+=.5) {
auto x=a(t), y=b(t);
s+=(x-y)*(x-y);
z+=y*y;
}
return sqrt(s/z);
}
*/