-
Notifications
You must be signed in to change notification settings - Fork 41
/
analyze.py
1297 lines (1041 loc) · 44.6 KB
/
analyze.py
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2018 - 2020 Dr. Jan-Philip Gehrcke
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import argparse
import logging
import os
import textwrap
import json
import glob
import subprocess
import shutil
import sys
import tempfile
from datetime import datetime
from io import StringIO
import pandas as pd
from github import Github
import retrying
import pytz
import altair as alt
import matplotlib
"""
makes use of code and methods from my other projects at
https://github.com/jgehrcke/dcos-dev-prod-analysis
https://github.com/jgehrcke/bouncer-log-analysis
https://github.com/jgehrcke/goeffel
"""
log = logging.getLogger()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s.%(msecs)03d %(levelname)s: %(message)s",
datefmt="%y%m%d-%H:%M:%S",
)
NOW = datetime.utcnow()
TODAY = NOW.strftime("%Y-%m-%d")
OUTDIR = None
ARGS = None
# Individual code sections are supposed to add to this in-memory Markdown
# document as they desire.
MD_REPORT = StringIO()
JS_FOOTER_LINES = []
# https://github.com/vega/vega-embed#options -- use SVG renderer so that PDF
# export (print) from browser view yields arbitrarily scalable (vector)
# graphics embedded in the PDF doc, instead of rasterized graphics.
VEGA_EMBED_OPTIONS_JSON = json.dumps({"actions": False, "renderer": "svg"})
def main():
if not os.environ.get("GHRS_GITHUB_API_TOKEN", None):
sys.exit("error: environment variable GHRS_GITHUB_API_TOKEN empty or not set")
parse_args()
configure_altair()
df_stargazers = get_stars_over_time()
df_forks = get_forks_over_time()
# Sync up the time window shown in the plots for forks and stars over time.
sf_date_axis_lim = gen_date_axis_lim((df_stargazers, df_forks))
log.info("time window for stargazer/fork plots: %s", sf_date_axis_lim)
gen_report_preamble()
analyse_view_clones_ts_fragments()
report_pdf_pagebreak()
add_stargazers_section(df_stargazers, sf_date_axis_lim)
add_fork_section(df_forks, sf_date_axis_lim)
report_pdf_pagebreak()
MD_REPORT.write(
textwrap.dedent(
f"""
## Top referrers and paths
Note: Each data point in the plots shown below is influenced by the 14 days
leading up to it. Each data point is the arithmetic mean of the "unique
visitors per day" metric, built from a time window of 14 days width, and
plotted at the right edge of that very time window. That is, these plots
respond slowly to change (narrow peaks are smoothed out).
"""
)
)
analyse_top_x_snapshots("referrer")
analyse_top_x_snapshots("path")
gen_report_footer()
finalize_and_render_report()
def gen_date_axis_lim(dfs):
# Find minimal first timestamp across dataframes, and maximal last
# timestamp. Return in string representation, example:
# ['2020-03-18', '2021-01-03']
# Can be used for setting time axis limits in Altair.
return [
pd.to_datetime(min(df.index.values[0] for df in dfs)).strftime("%Y-%m-%d"),
pd.to_datetime(max(df.index.values[-1] for df in dfs)).strftime("%Y-%m-%d"),
]
def configure_altair():
# https://github.com/carbonplan/styles
alt.themes.enable("carbonplan_light")
# https://github.com/altair-viz/altair/issues/673#issuecomment-566567828
alt.renderers.set_embed_options(actions=False)
def gen_report_footer():
js_footer = "\n".join(JS_FOOTER_LINES)
MD_REPORT.write(
textwrap.dedent(
f"""
<script type="text/javascript">
{js_footer}
</script>
"""
).strip()
)
def gen_report_preamble():
now_text = NOW.strftime("%Y-%m-%d %H:%M UTC")
MD_REPORT.write(
textwrap.dedent(
f"""
% Statistics for {ARGS.repospec}
% Generated with [jgehrcke/github-repo-stats](https://github.com/jgehrcke/github-repo-stats) at {now_text}.
"""
).strip()
)
def report_pdf_pagebreak():
# This adds a div to the HTML report output that will only take effect
# upon print, i.e. for PDF generation.
# https://stackoverflow.com/a/1664058/145400
MD_REPORT.write('\n\n<div class="pagebreak-for-print"> </div>\n\n')
def finalize_and_render_report():
md_report_filepath = os.path.join(OUTDIR, f"{ARGS.outfile_prefix}report.md")
log.info("Write generated Markdown report to: %s", md_report_filepath)
with open(md_report_filepath, "wb") as f:
f.write(MD_REPORT.getvalue().encode("utf-8"))
log.info("Copy resources directory into output directory")
shutil.copytree(ARGS.resources_directory, os.path.join(OUTDIR, "resources"))
# Generate HTML doc for browser view
html_template_filepath = gen_pandoc_html_template("html_browser_view")
run_pandoc(
md_report_filepath,
html_template_filepath,
html_output_filepath=os.path.splitext(md_report_filepath)[0] + ".html",
)
os.unlink(html_template_filepath)
# Generate HTML doc that will be used for rendering a PDF doc.
html_template_filepath = gen_pandoc_html_template("html_pdf_view")
run_pandoc(
md_report_filepath,
html_template_filepath,
html_output_filepath=os.path.splitext(md_report_filepath)[0] + "_for_pdf.html",
)
os.unlink(html_template_filepath)
def run_pandoc(md_report_filepath, html_template_filepath, html_output_filepath):
pandoc_cmd = [
ARGS.pandoc_command,
# For allowing raw HTML in Markdown, ref
# https://stackoverflow.com/a/39229302/145400.
"--from=markdown+pandoc_title_block+native_divs",
"--toc",
"--standalone",
f"--template={html_template_filepath}",
md_report_filepath,
"-o",
html_output_filepath,
]
log.info("Running command: %s", " ".join(pandoc_cmd))
p = subprocess.run(pandoc_cmd)
if p.returncode == 0:
log.info("Pandoc terminated indicating success")
else:
log.info("Pandoc terminated indicating error: exit code %s", p.returncode)
def gen_pandoc_html_template(target):
# Generally, a lot could be done with the same pandoc HTML template and
# using CSS @media print. Took the more flexible and generic approach
# here, though, where we're able to generate two completely different
# HTML templates, if needed.
assert target in ["html_browser_view", "html_pdf_view"]
if target == "html_browser_view":
main_style_block = textwrap.dedent(
"""
<style>
body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
div.full-width-chart {
width: 100%;
}
</style>
"""
)
if target == "html_pdf_view":
main_style_block = textwrap.dedent(
"""
<style>
@media print {
.pagebreak-for-print {
clear: both;
page-break-after: always;
}
}
body {
margin: 0;
padding: 0;
}
div.full-width-chart {
width: 100%;
}
</style>
"""
)
with open(os.path.join(ARGS.resources_directory, "template.html"), "rb") as f:
tpl_text = f.read().decode("utf-8")
# Do simple string replacement instead of picking one of the established
# templating methods: the pandoc template language uses dollar signs, and
# the CSS in the file uses curly braces.
rendered_pandoc_template = tpl_text.replace("MAIN_STYLE_BLOCK", main_style_block)
# Do a pragmatic close/unlink effort at end of program. It's not so bad in
# this case when either does not happen. Note that if the temp file path
# has no extension then pandoc seems to append `.html` before opening the
# file -- which the fails with ENOENT.
tmpf = tempfile.NamedTemporaryFile(delete=False, suffix=".html")
log.info("creating %s", tmpf.name)
tmpf.write(rendered_pandoc_template.encode("utf-8"))
tmpf.close()
# Return path to pandoc template.
return tmpf.name
def top_x_snapshots_rename_columns(df):
# mutate in-place.
# As always, naming is hard. Names get clearer over time. Work with data
# files that have non-ideal names. Semantically, there is a column name
# oversight -- plural vs. singular. Maybe fix in CSVs? Either one of both
# renames or both renames are OK to fail.
try:
df.rename(columns={"referrers": "referrer"}, inplace=True)
except ValueError:
pass
try:
df.rename(columns={"url_path": "path"}, inplace=True)
except ValueError:
pass
try:
df.rename(columns={"count_unique": "views_unique"}, inplace=True)
except ValueError:
pass
try:
df.rename(columns={"count_total": "views_total"}, inplace=True)
except ValueError:
pass
def _get_snapshot_time_from_path(p, basename_suffix):
# Expect each filename (basename) to have a prefix of format
# %Y-%m-%d_%H%M%S encoding the snapshot time (in UTC). Isolate that as
# tz-aware datetime object, return.
basename_prefix = os.path.basename(p).split(basename_suffix)[0]
t = pytz.timezone("UTC").localize(
datetime.strptime(basename_prefix, "%Y-%m-%d_%H%M%S")
)
log.info("parsed timestamp from path: %s", t)
return t
def _get_snapshot_dfs(csvpaths, basename_suffix):
snapshot_dfs = []
column_names_seen = set()
for p in csvpaths:
log.info("attempt to parse %s", p)
snapshot_time = _get_snapshot_time_from_path(p, basename_suffix)
df = pd.read_csv(p)
# mutate column names in-place.
top_x_snapshots_rename_columns(df)
# attach snapshot time as meta data prop to df
df.attrs["snapshot_time"] = snapshot_time
# Add new column to each dataframe: `time`, with the same value for
# every row: the snapshot time.
df["time"] = snapshot_time
if column_names_seen and set(df.columns) != column_names_seen:
log.error("columns seen so far: %s", column_names_seen)
log.error("columns in %s: %s", p, df.columns)
log.error("inconsistent set of column names across CSV files")
sys.exit(1)
column_names_seen.update(df.columns)
snapshot_dfs.append(df)
return snapshot_dfs
def _build_entity_dfs(dfa, entity_type, unique_entity_names):
cmn_ename_prefix = os.path.commonprefix(list(unique_entity_names))
log.info("cmn_ename_prefix: %s", cmn_ename_prefix)
entity_dfs = {}
for ename in unique_entity_names:
log.info("create dataframe for %s: %s", entity_type, ename)
# Do a subselection
edf = dfa[dfa[entity_type] == ename]
# Now use datetime column as index
newindex = edf["time"]
edf = edf.drop(columns=["time"])
edf.index = newindex
edf = edf.sort_index()
# Do entity name processing
if entity_type == "path":
entity_name_transformed = ename[len(cmn_ename_prefix) :]
# The root path (e.g., `owner/repo`) is now an empty string. That's
# not so cool, make the root be represented by a single slash.
if entity_name_transformed == "":
entity_name_transformed = "/"
edf.rename(columns={ename: entity_name_transformed}, inplace=True)
# Also change `ename` from here on, so that `entity_dfs` is built
# up using the transformed ename.
ename = entity_name_transformed
# Make it so that there is at most one data point per day, in case
# individual snapshots were taken with higher frequency.
n_hour_bins = 24
log.info("len(edf): %s", len(edf))
log.info("downsample entity DF into %s-hour bins", n_hour_bins)
# Resample the DF into N-hour bins. Take max() for each group. Do
# `dropna()` on the resampler to remove all up-sampled data points (in
# case snapshots were taken at much lower frequency). Default behavior
# of the resampling operation is to note the value for each bin at the
# left edge of the bin, and to have the bin be closed on the left edge
# (right edge of the bin belongs to next bin).
edf = edf.resample(f"{n_hour_bins}h").max().dropna()
log.info("len(edf): %s", len(edf))
# print(edf)
entity_dfs[ename] = edf
return entity_dfs
def _glob_csvpaths(basename_suffix):
basename_pattern = f"*{basename_suffix}"
csvpaths = glob.glob(os.path.join(ARGS.snapshotdir, basename_pattern))
log.info(
"number of CSV files discovered for %s: %s",
basename_pattern,
len(csvpaths),
)
return csvpaths
def analyse_top_x_snapshots(entity_type):
assert entity_type in ["referrer", "path"]
log.info("read 'top %s' snapshots (CSV docs)", entity_type)
basename_suffix = f"_top_{entity_type}s_snapshot.csv"
csvpaths = _glob_csvpaths(basename_suffix)
snapshot_dfs = _get_snapshot_dfs(csvpaths, basename_suffix)
# for df in snapshot_dfs:
# print(df)
# Keep in mind: an entity_type is either a top 'referrer', or a top 'path'.
# Find all entities seen across snapshots, by their name. For type referrer
# a specific entity(referrer) name might be `github.com`.
def _get_uens(snapshot_dfs):
unique_entity_names = set()
for df in snapshot_dfs:
unique_entity_names.update(df[entity_type].values)
return unique_entity_names
unique_entity_names = _get_uens(snapshot_dfs)
log.info("all %s entities seen: %s", entity_type, unique_entity_names)
# Clarification: each snapshot dataframe corresponds to a single point in
# time (the snapshot time) and contains information about multiple top
# referrers/paths. Now, invert that structure: work towards individual
# dataframes where each dataframe corresponds to a single referrer/path,
# and contains imformation about multiple timestamps
# First, create a dataframe containing all information.
dfa = pd.concat(snapshot_dfs)
if len(dfa) == 0:
log.info("leave early: no data for entity of type %s", entity_type)
# Build a dict: key is path/referrer name, and value is DF with
# corresponding raw time series.
entity_dfs = _build_entity_dfs(dfa, entity_type, unique_entity_names)
# It's important to clarify what each data point in a per-referrer raw time
# series means. Each data point has been returned by the GitHub traffic
# API. Each sample (row in the df) I think it can/should be looked at as
# the result of a rolling window analysis that shows cumulative values
# summed up over a period of 14 days; noted at the _right edge_ of the
# rolling time window.
# Should see further verification, but I think the boundaries of the time
# window actually move with sub-day resolution, i.e. the same query
# performed within the same day may yield different outcomes. If that's
# true, the rolling time window analysis performed internally at GitHub can
# be perfectly inversed; yielding per-referrer traffic statistics at a
# sub-day time resolution. That of course will require predictable,
# periodic sampling. Let's keep that in mind for now.
# One interesting way to look at the data: find the top 5 referrers based
# on unique views, and for the entire time range seen.
max_vu_map = {}
for ename, edf in entity_dfs.items():
max_vu_map[ename] = edf["views_unique"].max()
del ename
# Sort dict so that the first item is the referrer/path with the highest
# views_unique seen.
sorted_dict = {
k: v for k, v in sorted(max_vu_map.items(), key=lambda i: i[1], reverse=True)
}
top_n = 10
top_n_enames = list(sorted_dict.keys())[:top_n]
# simulate a case where there are different timestamps across per-referrer
# dfs: copy a 'row', and re-insert it with a different timestamp.
# row = referrer_dfs["t.co"].take([-1])
# print(row)
# referrer_dfs["t.co"].loc["2020-12-30 12:25:08+00:00"] = row.iloc[0]
# print(referrer_dfs["t.co"])
df_top_vu = pd.DataFrame()
for ename in top_n_enames:
edf = entity_dfs[ename]
# print(edf)
df_top_vu[ename] = edf["views_unique"]
# del ename
log.info(
"The top %s %s based on unique views, for the entire time range seen:\n%s",
top_n,
entity_type,
df_top_vu,
)
# For plotting with Altair, reshape the data using pd.melt() to combine the
# multiple columns into one, where the referrer name is not a column label,
# but a value in a column. Ooor we could use the
# transform_fold() technique
# https://altair-viz.github.io/user_guide/data.html#converting-between-long-form-and-wide-form-pandas
# with .transform_fold(top_n_rnames, as_=["referrer", "views_unique"])
# Also copy index into a normal column via `reset_index()` for
# https://altair-viz.github.io/user_guide/data.html#including-index-data
df_melted = df_top_vu.melt(
var_name=entity_type, value_name="views_unique", ignore_index=False
).reset_index()
# print(df_melted)
# Normalize main metric to show a view count _per day_, and clarify in the
# plot that this is a _mean_ value derived from the _last 14 days_.
df_melted["views_unique_norm"] = df_melted["views_unique"] / 14.0
# For paths, it's relevant to identify the common prefix (repo owner/name)
# cmn_ename_prefix = os.path.commonprefix(list(unique_entity_names))
# log.info("cmn_ename_prefix: %s", cmn_ename_prefix)
# if entity_type == "path":
# log.info("remove common path prefix")
# df_melted["path"] = df_melted["path"].str.slice(start=len(cmn_ename_prefix))
# # The root path (e.g., `owner/repo`) is not an empty string. That's
# # not so cool, make the root be represented by a single slash.
# # df_melted[df_melted["path"] == ""]["path"] = "/"
# df_melted["path"].replace("", "/", inplace=True)
panel_props = {"height": 300, "width": "container", "padding": 10}
chart = (
alt.Chart(df_melted)
.mark_line(point=True)
# .encode(x="time:T", y="views_unique:Q", color="referrer:N")
# the pandas dataframe datetimeindex contains timing information at
# much higher resolution than 1 day. The resulting vega spec may
# then see time values like this: `"time": "2021-01-03T00:00:00+00:00"`
# -- suggesting to vega that we care about showing hours and minutes.
# instruct vega to only care about _days_ (dates), via an altair-based
# timeout unit transformation. Ref:
# https://altair-viz.github.io/user_guide/transform/timeunit.html
.encode(
alt.X("time", type="temporal", title="date", timeUnit="yearmonthdate"),
alt.Y(
"views_unique_norm",
type="quantitative",
title="unique visitors per day (mean from last 14 days)",
scale=alt.Scale(
domain=(0, df_melted["views_unique_norm"].max() * 1.1),
zero=True,
),
),
alt.Color(
entity_type,
type="nominal",
sort=alt.SortField("order"),
),
)
.configure_point(size=100)
.properties(**panel_props)
)
chart_spec = chart.to_json(indent=None)
# From
# https://altair-viz.github.io/user_guide/customization.html
# "Note that this will only scale with the container if its parent element
# has a size determined outside the chart itself; For example, the
# container may be a <div> element that has style width: 100%; height:
# 300px.""
heading = "Top referrers" if entity_type == "referrer" else "Top paths"
# Textual form: larger N, and no cutoff (arbitrary length and legend of
# plot don't go well with each other).
top_n = 15
top_n_enames = list(sorted_dict.keys())[:top_n]
top_n_enames_string_for_md = ", ".join(
f"{str(i).zfill(2)}: `{n}`" for i, n in enumerate(top_n_enames, 1)
)
MD_REPORT.write(
textwrap.dedent(
f"""
#### {heading}
<div id="chart_{entity_type}s_top_n_alltime" class="full-width-chart"></div>
Top {top_n} {entity_type}s: {top_n_enames_string_for_md}
"""
)
)
JS_FOOTER_LINES.append(
f"vegaEmbed('#chart_{entity_type}s_top_n_alltime', {chart_spec}, {VEGA_EMBED_OPTIONS_JSON}).catch(console.error);"
)
def analyse_view_clones_ts_fragments():
log.info("read views/clones time series fragments (CSV docs)")
basename_suffix = f"_views_clones_series_fragment.csv"
csvpaths = _glob_csvpaths(basename_suffix)
dfs = []
column_names_seen = set()
for p in csvpaths:
log.info("attempt to parse %s", p)
snapshot_time = _get_snapshot_time_from_path(p, basename_suffix)
df = pd.read_csv(
p,
index_col=["time_iso8601"],
date_parser=lambda col: pd.to_datetime(col, utc=True),
)
# A time series fragment might look like this:
#
# df_views_clones:
# clones_total ... views_unique
# time_iso8601 ...
# 2020-12-21 00:00:00+00:00 NaN ... 2
# 2020-12-22 00:00:00+00:00 2.0 ... 23
# 2020-12-23 00:00:00+00:00 2.0 ... 20
# ...
# 2021-01-03 00:00:00+00:00 8.0 ... 21
# 2021-01-04 00:00:00+00:00 7.0 ... 18
#
# Note the NaN and the floaty type.
# All metrics are known to be integers by definition here. The NaN are
# expected only at the boundaries of each fragment (first and maybe
# last sample). Drop the rows where at least one element is missing,
# and make sure numbers are treated as integers from here on (this
# actually matters (in a cosmetic way) only for outputting the
# aggregate CSV later on -- not for plotting, etc).
df = df.dropna()
df = df.astype(int)
# attach snapshot time as meta data prop to df
df.attrs["snapshot_time"] = snapshot_time
# The index is not of string type anymore, but of type
# `pd.DatetimeIndex`. Reflect that in the name.
df.index.rename("time", inplace=True)
if column_names_seen and set(df.columns) != column_names_seen:
log.error("columns seen so far: %s", column_names_seen)
log.error("columns in %s: %s", p, df.columns)
sys.exit(1)
column_names_seen.update(df.columns)
df = df.sort_index()
# Sanity check: snapshot time _after_ latest timestamp in time series?
# This could hit in on a machine with a bad time setting when fetching
# data.
if df.index.max() > snapshot_time:
log.error(
"for CSV file %s the snapshot time %s is older than the newest sample",
p,
snapshot_time,
)
sys.exit(1)
dfs.append(df)
# for df in dfs:
# print(df)
log.info("total sample count: %s", sum(len(df) for df in dfs))
newest_snapshot_time = max(df.attrs["snapshot_time"] for df in dfs)
df_prev_agg = None
if ARGS.views_clones_aggregate_inpath:
log.info("read previous aggregate: %s", ARGS.views_clones_aggregate_inpath)
df_prev_agg = pd.read_csv(
ARGS.views_clones_aggregate_inpath,
index_col=["time_iso8601"],
date_parser=lambda col: pd.to_datetime(col, utc=True),
)
df_prev_agg.index.rename("time", inplace=True)
log.info("time of newest snapshot: %s", newest_snapshot_time)
log.info("build aggregate, drop duplicate data")
# Each dataframe in `dfs` corresponds to one time series fragment
# ("snapshot") obtained from the GitHub API. Each time series fragment
# contains 15 samples (rows), with two adjacent samples being 24 hours
# apart. Ideally, the time series fragments overlap in time. They overlap
# potentially by a lot, depending on when the individual snapshots were
# taken (think: take one snapshot per day; then 14 out of 15 data points
# are expected to be "the same" as in the snapshot taken the day before).
# Stich these fragments together (with a buch of "duplicate samples), and
# then sort this result by time.
log.info("pd.concat(dfs)")
dfall = pd.concat(dfs)
if df_prev_agg is not None:
if set(df_prev_agg.columns) != set(dfall.columns):
log.error(
"set(df_prev_agg.columns) != set (dfall.columns): %s, %s",
df_prev_agg.columns,
)
sys.exit(1)
log.info("pd.concat(dfall, df_prev_agg)")
dfall = pd.concat([dfall, df_prev_agg])
dfall.sort_index(inplace=True)
log.info("shape of dataframe before dropping duplicates: %s", dfall.shape)
# print(dfall)
# Now, the goal is to drop duplicate data. And again, as of a lot of
# overlap between snapshots there's a lot of duplicate data to be expected.
# What does "duplicat data" mean? We expect that there are multiple samples
# from different snapshots with equivalent timestamp. OK, we should just
# take any one of them. They should all be the same, right? They are not
# all equivalent. I've found that at the boundaries of each time series
# fragment, the values returned by the GitHub API are subject to a
# non-obvious cutoff effect: for example, in a snapshot obtained on Dec 15,
# the sample for Dec 7 is within the mid part of the fragment and shows a
# value of 73 for `clones_total`. The snapshot obtained on Dec 21 has the
# sample for Dec 7 at the boundary (left-hand, towards the past), and that
# shows a value of 18 for `clones_total`. 73 vs 18 -- how is that possible?
# That's easily possible, assuming that GitHub uses a rolling window of 14
# days width with a precision higher than 1 day and after all the cutoff
# for the data points at the boundary depends on the _exact time_ when the
# snapshot was taken. That is, for aggregation (for dropping duplicate/bad
# data) we want to look for the maximum data value for any given timestamp.
# Using that method, we effectively ignore said cutoff artifact. In short:
# group by timestamp (index), take the maximum.
df_agg = dfall.groupby(dfall.index).max()
log.info("shape of dataframe after dropping duplicates: %s", df_agg.shape)
# Write aggregate
# agg_fname = (
# datetime.strftime(newest_snapshot_time, "%Y-%m-%d_%H%M%S")
# + "_views_clones_aggregate.csv"
# )
# agg_fpath = os.path.join(ARGS.snapshotdir, agg_fname)
if ARGS.views_clones_aggregate_outpath:
if os.path.exists(ARGS.views_clones_aggregate_outpath):
log.info("file exists: %s", ARGS.views_clones_aggregate_outpath)
if not ARGS.views_clones_aggregate_inpath:
log.error(
"would overwrite output aggregate w/o reading input aggregate -- you know what you're doing?"
)
sys.exit(1)
log.info("write aggregate to %s", ARGS.views_clones_aggregate_outpath)
# Pragmatic strategy against partial write / encoding problems.
tpath = ARGS.views_clones_aggregate_outpath + ".tmp"
df_agg.to_csv(tpath, index_label="time_iso8601")
os.rename(tpath, ARGS.views_clones_aggregate_outpath)
if ARGS.delete_ts_fragments:
# Iterate through precisely the set of files that was read above.
# If unlinkling fails at OS boundary then don't crash this program.
for p in csvpaths:
log.info("delete %s as of --delete-ts-fragments", p)
try:
os.unlink(p)
except Exception as e:
log.warning("could not unlink %s: %s", p, str(e))
# print(df_agg)
# matplotlib_config()
# log.info("aggregated sample count: %s", len(df_agg))
# df_agg.plot(
# linestyle="solid",
# marker="o",
# markersize=5,
# subplots=True,
# # ylabel="count",
# xlabel="",
# # logy="sym",
# )
# plt.ylim([0, None])
# plt.tight_layout()
# plt.show()
# Why reset_index()? See
# https://github.com/altair-viz/altair/issues/271#issuecomment-573480284
df_agg = df_agg.reset_index()
df_agg_views = df_agg.drop(columns=["clones_unique", "clones_total"])
df_agg_clones = df_agg.drop(columns=["views_unique", "views_total"])
PANEL_WIDTH = "container"
PANEL_HEIGHT = 200
panel_props = {"height": PANEL_HEIGHT, "width": PANEL_WIDTH, "padding": 10}
chart_clones_unique = (
(
alt.Chart(df_agg_clones)
.mark_line(point=True)
.encode(
alt.X("time", type="temporal", title="date", timeUnit="yearmonthdate"),
alt.Y(
"clones_unique",
type="quantitative",
title="unique clones per day",
scale=alt.Scale(
domain=(0, df_agg_clones["clones_unique"].max() * 1.1),
zero=True,
),
),
)
)
.configure_axisY(labelBound=True)
.configure_point(size=100)
.properties(**panel_props)
)
chart_clones_total = (
(
alt.Chart(df_agg_clones)
.mark_line(point=True)
.encode(
alt.X("time", type="temporal", title="date", timeUnit="yearmonthdate"),
alt.Y(
"clones_total",
type="quantitative",
title="total clones per day",
scale=alt.Scale(
domain=(0, df_agg_clones["clones_total"].max() * 1.1),
zero=True,
),
),
)
)
.configure_axisY(labelBound=True)
.configure_point(size=100)
.properties(**panel_props)
)
chart_views_unique = (
(
alt.Chart(df_agg_views)
.mark_line(point=True)
.encode(
alt.X("time", type="temporal", title="date", timeUnit="yearmonthdate"),
alt.Y(
"views_unique",
type="quantitative",
title="unique views per day",
scale=alt.Scale(
domain=(0, df_agg_views["views_unique"].max() * 1.1),
zero=True,
),
),
)
)
.configure_axisY(labelBound=True)
.configure_point(size=100)
.properties(**panel_props)
)
chart_views_total = (
(
alt.Chart(df_agg_views)
.mark_line(point=True)
.encode(
alt.X("time", type="temporal", title="date", timeUnit="yearmonthdate"),
alt.Y(
"views_total",
type="quantitative",
title="total views per day",
scale=alt.Scale(
domain=(0, df_agg_views["views_total"].max() * 1.1),
zero=True,
),
),
)
)
.configure_axisY(labelBound=True)
.configure_point(size=100)
.properties(**panel_props)
)
chart_views_unique_spec = chart_views_unique.to_json(indent=None)
chart_views_total_spec = chart_views_total.to_json(indent=None)
chart_clones_unique_spec = chart_clones_unique.to_json(indent=None)
chart_clones_total_spec = chart_clones_total.to_json(indent=None)
MD_REPORT.write(
textwrap.dedent(
f"""
## Views
#### Unique visitors
<div id="chart_views_unique" class="full-width-chart"></div>
#### Total views
<div id="chart_views_total" class="full-width-chart"></div>
<div class="pagebreak-for-print"> </div>
## Clones
#### Unique cloners
<div id="chart_clones_unique" class="full-width-chart"></div>
#### Total clones
<div id="chart_clones_total" class="full-width-chart"></div>
"""
)
)
JS_FOOTER_LINES.extend(
[
f"vegaEmbed('#chart_views_unique', {chart_views_unique_spec}, {VEGA_EMBED_OPTIONS_JSON}).catch(console.error);",
f"vegaEmbed('#chart_views_total', {chart_views_total_spec}, {VEGA_EMBED_OPTIONS_JSON}).catch(console.error);",
f"vegaEmbed('#chart_clones_unique', {chart_clones_unique_spec}, {VEGA_EMBED_OPTIONS_JSON}).catch(console.error);",
f"vegaEmbed('#chart_clones_total', {chart_clones_total_spec}, {VEGA_EMBED_OPTIONS_JSON}).catch(console.error);",
]
)
def add_stargazers_section(df, date_axis_lim):
# date_axis_lim is expected to be of the form ["2019-01-01", "2019-12-31"]
panel_props = {"height": 300, "width": "container", "padding": 10}
chart = (
alt.Chart(df.reset_index())
.mark_line(point=True)
.encode(
alt.X(
"time",
type="temporal",
title="date",
timeUnit="yearmonthdate",
scale=alt.Scale(domain=date_axis_lim),
),
alt.Y(
"stars_cumulative",
type="quantitative",
title="stargazer count (cumulative)",
scale=alt.Scale(
domain=(0, df["stars_cumulative"].max() * 1.1),
zero=True,
),
),
)
.configure_point(size=100)
.properties(**panel_props)
)
chart_spec = chart.to_json(indent=None)
MD_REPORT.write(
textwrap.dedent(
f"""
## Stargazers
Each data point corresponds to at least one stargazer event.
The time resolution is one day.
<div id="chart_stargazers" class="full-width-chart"></div>
"""
)