-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKipHub.pyw
2663 lines (2234 loc) · 126 KB
/
KipHub.pyw
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/python3
# KipHub Traffic
# v2.1 2024-1-2
# Fred Rique (Farique) (c) 2021 - 2024
# www.github.com/farique1/kiphub-traffic
# Known Bugs:
# Plots not always update if not visible so they are always reconstructed.
# Popups appear at the top left of the screen when first called.
# Adding a tooltip to describe an entry on a table messes with the table sort routine.
# tooltips.append(dpg.add_tooltip(dpg.last_item()))
# dpg.add_text(parent=dpg.last_item(), default_value='Average of all repos average')
# Imports
import re
import os
import time
import json
import shutil
import pickle
import argparse
import requests
import webbrowser
import configparser
from itertools import zip_longest
from dataclasses import dataclass
from datetime import datetime, timedelta, date
try:
import dearpygui.dearpygui as dpg
dpg_module = True
except ModuleNotFoundError:
dpg_module = False
# Command line variables
# Credentials
username = 'farique1'
token = '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'
# User
use_cache = False # If True get API data from disk
keep_cache = False # Keep local API response copies
view_only = True # If True uses data from the aggregated JSON
cache = '' # Cache behavior. u=use k=keep v=view only
sort = 'c' # Sort order: default=names,uniques v=views c=clones o=count r=reverse
toggle = 'rp' # toggle view items: a=days without data, r=referrers, e=expand referrers, p=report, l=labels, d=daily, s=sum, v=views, c=clones, o=count, u=unique
mono = False # Show terminal output without colors
# Terminal colors
CYAN = '\033[38;2;00;255;255m'
YELLOW = '\033[38;2;255;255;00m'
GREEN = '\033[92m'
RED = '\033[91m'
BLUE = '\033[94m'
GRAY = '\033[90m'
MAGENTA = '\033[38;2;255;00;255m'
RESET = '\033[39m'
# Shared Variables
# Paths
base_url = 'https://api.github.com/repos'
user_url = username
repo_url = ''
referrers_url = 'traffic/popular/referrers'
views_url = 'traffic/views'
clones_url = 'traffic/clones'
repos_url = 'https://api.github.com/user/repos'
rate_url = 'https://api.github.com/rate_limit'
data_file = f'JSONs/{username}.json'
config_path = 'JSONs\\default.config'
users_file = 'JSONs\\users.ini'
local_path = os.path.split(os.path.abspath(__file__))[0]
users_path = os.path.join(local_path, users_file)
users_ini = configparser.ConfigParser()
config_path = os.path.join(local_path, config_path)
config_ini = configparser.ConfigParser()
# Accepted date format
match_date = r'^((\d{1,4}-)?\d{1,2}-)?\d{1,2}$'
# Sort
sort_view_clone = 0
sort_count_unique = 0
sort_reverse = False
# Misc
repo_count = 0
repo_max = 0
min_cust = None # Start date on the format 'y-m-d', 'm-d' or 'd'
max_cust = None # End date on the format 'y-m-d', 'm-d' or 'd'
period = 30 # number of days before last day to be shown (overrides min_cust) 0=all
gui = True # Show GUI instead of terminal output
# GUI Variables
# Settings
vp_width = 850
vp_height = 900
small_separator = 1
large_separator = 10
default_days = 30
sort_reverse = False
repo_filter = ''
plot_alpha = 64
plot_height = 0
show_dates = True
disable_days = False
tooltips_show = True
tooltips_delay = 0.5
show_views_a = True
show_views_u = True
show_clones_a = True
show_clones_u = True
gui_loaded = False
# Colors
WHITE = (255, 255, 255)
LIGHT = (112, 112, 112)
MEDIUM = (86, 86, 86)
B_YELLOW = (180, 164, 12)
B_RED = (184, 64, 77)
DARK = (60, 60, 60)
VIEW_A = (76, 114, 176)
VIEW_U = (221, 132, 82)
CLONE_A = (85, 168, 104)
CLONE_U = (196, 78, 82)
BG = (20, 142, 142)
WB_IDLE = (18, 109, 102)
WB_ACTIVE = (39, 226, 207)
WB_HOVERED = (31, 178, 163)
WB_DEEP = (13, 76, 70)
PLOT_BG = (246, 246, 246)
# Accent
ACC_ACTIVE = 40
ACC_HOVER = 20
# Lists
BUTTON_COLOR = [('button_med', MEDIUM),
('button_dark', DARK),
('button_yellow', B_YELLOW),
('button_red', B_RED)]
GRAPH_COLOR = [('button_view_a', VIEW_A),
('button_view_u', VIEW_U),
('button_clone_a', CLONE_A),
('button_clone_u', CLONE_U)]
BLANK_REFS = [('', '', '')]
bt_dark = []
bt_med = []
bt_work = []
# Info
credentials = ['', '']
sort_items = ('Name', 'Views A', 'Views U', 'Clones A', 'Clones U', 'Referrers', 'Reverse')
sort_item = 'Name'
board_types = ('Basic', 'Graph', 'Under', 'Compact', 'Referrer', 'Full')
board_type = 'Compact'
HELP_TEXT = [('KipHub Traffic\n'
'v2.0\n\n', WHITE),
('Getting started\n', WB_DEEP),
('When running for the first time, KipHub Traffic will ask for a GitHub username.\n'
'After that you must enter a valid, current GitHub API token with access to repository info.\n'
'Get the token on the GitHub page at Settings > Developer settings > Personal access tokens (classic) ticking the repo checkmark.\n'
'You can now download the GitHub data on Menu > Settings > Fetch from GitHub.\n'
'This is where you download updated GitHub data whenever you need.\n'
'Remember, the whole purpose of this program is to keep track of the data past the 14 days maximum on the GitHub page, so always fetch the data on intervals shorter than this period.\n\n', WHITE),
('The interface\n', WB_DEEP),
('The interface is composed of a menu bar, a header, a repository body and footer.\n\n', WHITE),
('The Menu\n', WB_DEEP),
('The menu offers complimentary actions to help the use of KipHub Traffic.\n'
'The File menu deals mostly with configuration. Most of the configuration done on KipHub Can be saved and restored later. You can create several custom configurations according to your taste or to better view certain aspects of the information in a way or another.\n'
'You can revert the configuration to the default one. Open a specific configuration file, quick open previously saved configurations, save a default configuration and save the current configuration to a different configuration file. Here you can also Exit the program.\n'
'The Settings menu is where you can enter a new token for the user (creating expiring tokens is a good practice), fetch new data from GitHib, amd show the API usage ratios. Here you can also configure some more obscure settings like small and large gaps show between the boards (Basic and Graph boards use the small separator, the rest use the large one), the delay of the tooltips (all in Deep Config), restore the width of the interface to the default size and enable or disable the tooltips.\n'
'In the Info menu you have information about KeepHub Traffic, a help window and you can display statistics about the repositories regarding the current period.\n'
'The statistic window show the most hits on a day and the average daily hits for each category (View All, View Uniques, Clones All, Clones Uniques) in the current period. It also show compiled information regarding all repositories. Average average show the average for all days averages and Sum average shows the sum of all hits averaged for the period. You can call several statistic windows for different periods for comparison.\n\n', WHITE),
('The Header\n', WB_DEEP),
('The header shows the username, the amount of repositories under that user, the last date the data was updated, how many days ago was the update (remember, after 14 days you start to lose data. the program will highlight this number in yellow and then in red to warn about the deadline), the first day of the shown data, the last day of the period and how many days are being shown.\n'
'You can change the initial and final dates by clicking on their icons (they cannot go earlier or later than the available data). Enter the date by clicking on the date widget or by entering a date below. The date format is y-m-d, m-d, or only d. The current period will be taken for the omitted ones. Years can have 2 or 4 digits.\n'
'Clicking in the days period will bring some options:\n'
'Disabled will turn on or of the enforcing of how many days are shown. If the period is enabled (unchecked) the current period of days entered will almost always be enforced (some actions might disable or enable this without notification).\n'
'Last day will take the ending date to the last date available.\n'
'The slider under Last day will set the desired period to show (this will only take effect when the period is enabled). Control/Command clicking this allows for manual entry.\n'
'All days takes the initial and final date to the earliest and latest available dates on the data, showing all available days at once.\n\n', WHITE),
('The Body\n', WB_DEEP),
('The body show boards with information about each repository. They can be Basic, Graph, Under, Compact, Referrer, Full.\n'
'The position of the information is different on each board ans not all of them show all information available.\n'
'The boards have a header with the name of the repository, a button <> to send the date period of this board (when zoomed) to all boards, and a combo box to change the type of this board.\n'
'GitHub provides information about the views and clones of each repository. This information is further detailed in "al" and "uniques". All is the total amount of views or clones on the repository. Uniques are the how many unique persons viewed or cloned the repository. All and Uniques are represented by ALL and UNQ.\n'
'GitHub provides information in two ways, a daily report detailing numbers for each day and a reported sum of the last 14 days (these numbers not always match). KipHub Traffic show all this information for comparison. GitHub also report the referrers (where visitors came from) from the last 14 days, all and unique, this information is also shown.\n'
'The daily collect data is color coded, View All is blue, View Uniques is orange, Clones All is green and Clones Uniques is red. You can click on each color to toggle each graphic view on the plot.\n'
'The plot can be manipulated in a number of ways. It can be dragged in all directions, can be zoomed in or out with the mouse wheel (placing the mouse on the X or Y labels while zooming will affect only this axis), double click will reset the view and the right mouse button can be used to drag a region to zoom or to call a menu with several options.\n\n', WHITE),
('The Footer\n', WB_DEEP),
('The footer is mostly about configuring the viewing of the information. There are combo boxes to change all boards to a certain type, a combo box with several options to sort the repositories (there is an entry to Revert the sorts), toggles for all four graphics that affects all repositories, button to configure the plots where you can change the intensity of the graphics fill, the height of the plots and if you want to see the dates on the X axis. There is also a filter box where you can enter text to filter which repositories are shown. A box next to it have all repository names as a shortcut for the filter and there is a button to clear the filter box.\n\n', WHITE),
('Resetting the User\n', WB_DEEP),
('If you want reset the user on KipHub Traffic, you must delete (or backup) all files inside \\JSONs and run KipHub Traffic Again.\n'
'A multi user version might come in the future.', WHITE)]
# Early functions (used by the initialization)
def dateEntry(date_entry, date_type, sender=None):
'''Check the date entry format and convert to a valid datetime
date_entry = the entry to check
date_type = the type of date (begin or end) to report on errors'''
if date_entry:
if not re.match(match_date, date_entry):
if sender:
dpg.focus_item(sender)
return
else:
print(f'\n Invalid {date_type} date entry format: {date_entry}\n')
raise SystemExit(0)
# Deal with partial date entry
data_format = date_entry.count('-')
if data_format == 0:
date_entry = f'{datetime.now().strftime("%Y-%m")}-{date_entry}'
if data_format == 1:
date_entry = f'{datetime.now().strftime("%Y")}-{date_entry}'
year = date_entry.split('-')[0]
if data_format == 2 and len(year) < 4:
date_entry = '2000'[0:(4 - len(year))] + date_entry
try:
date_entry = datetime.strptime(date_entry, r'%Y-%m-%d')
except ValueError:
if sender:
dpg.focus_item(sender)
return
else:
print(f'\n Invalid {date_type} date format: {date_entry}\n')
raise SystemExit(0)
return date_entry
def notStrings(str1, str2):
'''Toggle characters on a string based on a second string'''
for c in str2:
if c in str1:
str1 = str1.replace(c, '')
else:
str1 += c
return str1
# Command line arguments
ap = argparse.ArgumentParser(description='KipHub Traffic: download and save GitHub traffic data',
epilog='Fred Rique (farique) (c) 2021 - '
'github.com/farique/kiphub-traffic \n')
ap.add_argument('-d', '--down', action='store_false', default=view_only,
help='Download data from Github')
ap.add_argument('-c', '--cache', metavar='uk', default=cache,
help='Cache behavior: u=use k=keep (default="%(default)s")')
ap.add_argument('-b', '--begin', metavar='y-m-d', default=min_cust,
help='Custom start date (defaul=fisrt reported date)', )
ap.add_argument('-e', '--end', metavar='y-m-d', default=max_cust,
help='Custom end date (defaul=last updated date)')
ap.add_argument('-p', '--period', metavar='days', default=period, type=int,
help='Days before end date: 0=all (default=%(default)s)')
ap.add_argument('-s', '--sort', metavar='vcor', default=sort,
help='Sort order: v=views c=clones o=count r=reverse (default=names,uniques)')
ap.add_argument('-t', '--toggle', metavar='arepldsvcou',
help=('toggle view items: a=days without data, r=referers, e=expand referers, '
'p=report, l=labels, d=daily, s=sum, v=views, c=clones, o=count, u=unique'
' (default="%(default)s")'))
ap.add_argument('-m', '--mono', action='store_true', default=mono,
help='Show terminal output without colors')
ap.add_argument('-g', '--gui', action='store_false', default=gui,
help='Show GUI instead of terminal output')
args = ap.parse_args()
# Apply arguments
min_cust = dateEntry(args.begin, 'begin')
max_cust = dateEntry(args.end, 'end')
period = args.period
view_only = args.down
cache = args.cache.lower()
use_cache = True if 'u' in cache else use_cache
keep_cache = True if 'k' in cache else keep_cache
# Set values to add to the sort index amount
sort = args.sort.lower()
if 'v' in sort:
sort_view_clone = 1
sort_count_unique = 1
if 'c' in sort:
sort_view_clone = 3
sort_count_unique = 1
if 'o' in sort:
sort_count_unique = 0
if 'r' in sort:
sort_reverse = not sort_reverse
# Apply to variables
if args.toggle is not None:
toggle = notStrings(toggle.lower(), args.toggle.lower())
show_all_days = 'a' not in toggle
show_referers = 'r' not in toggle
expand_referers = 'e' not in toggle
show_report = 'p' not in toggle
Show_labels = 'l' not in toggle
show_daily = 'd' not in toggle
show_sum = 's' not in toggle
show_views = 'v' not in toggle
show_clones = 'c' not in toggle
show_count = 'o' not in toggle
show_uniqu = 'u' not in toggle
if args.mono:
# Reset colors
CYAN = ''
YELLOW = ''
GREEN = ''
RED = ''
BLUE = ''
GRAY = ''
MAGENTA = ''
RESET = ''
# -----------------------------------------------------------------------------
# Classes
# -----------------------------------------------------------------------------
@dataclass
class Users:
'''Username and token.'''
users: list
default_user: str
@dataclass
class Credentials:
'''Username and token.'''
username: str
token: str
@dataclass
class Config:
'''Configuration.'''
date_days: int
disable_days: bool
plot_alpha: int
plot_height: int
show_dates: bool
board_type: str
sort_item: str
sort_reverse: bool
repo_filter: str
tooltips_show: bool
tooltips_delay: float
show_views_a: bool
show_views_u: bool
show_clones_a: bool
show_clones_u: bool
small_separator: int
large_separator: int
@dataclass
class DataHead:
'''Header items.'''
repositories: int
repo_names: list
last_updated: datetime
earliest_day: datetime
latest_day: datetime
days_ago: int
date_from: datetime
date_to: datetime
date_days: int
time_period: list
day_peak: list
@dataclass
class DataBody:
'''Board items.'''
repository: str
referrers: list
rep_view_a: int
rep_view_u: int
rep_clone_a: int
rep_clone_u: int
views_a: list
views_u: list
clones_a: list
clones_u: list
col_view_a: int
col_view_u: int
col_clone_a: int
col_clone_u: int
class Boards:
'''Repository boards'''
def __init__(self, data_board, output_head, config, repo_filter_tag):
self.data_board = data_board
self.output_head = output_head
self.board_tooltip = []
self.board_type = config.board_type
self.repo_filter_tag = repo_filter_tag
self.plot_height = config.plot_height
self.show_dates = config.show_dates
self.show_views_a = config.show_views_a
self.show_views_u = config.show_views_u
self.show_clones_a = config.show_clones_a
self.show_clones_u = config.show_clones_u
self.small_separator = config.small_separator
self.large_separator = config.large_separator
self.create_board_groups()
# -----------------------------------------------------------------------------
# Board Functions
# -----------------------------------------------------------------------------
def create_board_groups(self):
self._group = dpg.add_group(filter_key=self.data_board.repository, parent=self.repo_filter_tag)
def populate_boards(self, board_type):
self.separator = self.small_separator
self.board_tooltip = []
if board_type:
self.board_type = board_type
self.button_theme_view_a = 'button_view_a' if self.show_views_a else 'button_working'
self.button_theme_view_u = 'button_view_u' if self.show_views_u else 'button_working'
self.button_theme_clone_a = 'button_clone_a' if self.show_clones_a else 'button_working'
self.button_theme_clone_u = 'button_clone_u' if self.show_clones_u else 'button_working'
if self.board_type == 'Basic' or self.board_type == 'Graph':
width = -260 if self.board_type == 'Graph' else -260
visibility = self.board_type == 'Graph'
with dpg.group(horizontal=True, parent=self._group):
self.insert_header(width=width, buttons=True)
with dpg.group(parent=self._group, show=visibility):
with dpg.child_window(width=-1, height=80 + self.plot_height * 28):
self.insert_plot(height=80)
elif self.board_type == 'Compact':
self.separator = self.large_separator
with dpg.group(horizontal=True, parent=self._group):
self.insert_header(width=-76)
with dpg.group(horizontal=True, parent=self._group):
with dpg.child_window(width=-154, height=170 + self.plot_height * 28):
self.insert_plot(height=167)
with dpg.child_window(no_scrollbar=True, no_scroll_with_mouse=True, height=168 + self.plot_height * 28):
self.insert_label(label=f'Collected {self.output_head.date_days} days', width=153, theme='button_dark', au=False, tooltip='collect')
self.insert_btn_view_col(61)
self.insert_btn_clone_col(61)
self.insert_label(label='Reported 14 days', width=153, theme='button_dark', au=False, tooltip='report')
self.insert_btn_view_ref(61)
self.insert_btn_clone_ref(61)
dpg.add_button(width=153, height=-1)
dpg.bind_item_theme(dpg.last_item(), 'button_med')
elif self.board_type == 'Under':
self.separator = self.large_separator
with dpg.group(horizontal=True, parent=self._group):
self.insert_header(width=-76)
with dpg.group(parent=self._group):
with dpg.child_window(width=-1, height=251 + self.plot_height * 28, border=False):
self.insert_plot(height=167)
with dpg.group(horizontal=True):
with dpg.group():
with dpg.group(horizontal=True):
self.insert_label(label=f'Collected in {self.output_head.date_days} days', width=180, theme='button_dark', au=True, tooltip='collect')
self.insert_label(label='Reported last 14 days', width=180, theme='button_dark', au=True, tooltip='report')
self.insert_label(label='Referrers', width=100, theme='button_med', au=True, grow_x=True)
with dpg.group(horizontal=True):
with dpg.group():
with dpg.group(horizontal=True):
self.insert_btn_view_col(180)
self.insert_btn_view_ref(180)
with dpg.group(horizontal=True):
self.insert_btn_clone_col(180)
self.insert_btn_clone_ref(180)
self.insert_referrers(amount=2, y_span=55, grow_y=False, grow_x=True)
elif self.board_type == 'Referrer':
self.separator = self.large_separator
with dpg.group(horizontal=True, parent=self._group):
self.insert_header(width=-76)
with dpg.group(horizontal=True, parent=self._group):
with dpg.group():
with dpg.child_window(width=-199, height=251 + self.plot_height * 28, border=False):
self.insert_plot(height=167)
with dpg.group(horizontal=True):
with dpg.group():
with dpg.group(horizontal=True):
self.insert_label(label=f'Collected in {self.output_head.date_days} days', width=190, theme='button_dark', au=True, tooltip='collect')
self.insert_label(label='Reported last 14 days', width=190, theme='button_dark', au=True, tooltip='report')
with dpg.group(horizontal=True):
self.insert_btn_view_col(190)
self.insert_btn_view_ref(190)
with dpg.group(horizontal=True):
self.insert_btn_clone_col(190)
self.insert_btn_clone_ref(190)
dpg.add_button(width=-1, height=83)
dpg.bind_item_theme(dpg.last_item(), 'button_med')
with dpg.group():
self.insert_label(label='Referrers', width=106, theme='button_med', au=True)
self.insert_referrers(amount=8, y_span=224, grow_y=True, grow_x=True)
elif self.board_type == 'Full':
self.separator = self.large_separator
with dpg.group(horizontal=True, parent=self._group):
self.insert_header(width=-76)
with dpg.group(horizontal=True, parent=self._group):
with dpg.child_window(width=-199, height=251 + self.plot_height * 28, border=False):
self.insert_plot(height=251)
with dpg.group():
self.insert_label(label=f'Collected in {self.output_head.date_days} days', width=198, theme='button_dark', au=False, tooltip='collect')
self.insert_btn_view_col(106)
self.insert_btn_clone_col(106)
self.insert_label(label='Reported last 14 days', width=198, theme='button_dark', au=False, tooltip='report')
self.insert_label(label='Referrers', width=106, theme='button_med', au=True)
self.insert_referrers(amount=2, y_span=55, grow_y=True)
self.insert_btn_view_ref(106)
self.insert_btn_clone_ref(106)
self.board_separator = dpg.add_child_window(height=self.separator, parent=self._group)
self.board_tooltip.append(dpg.add_tooltip(self.data_board.repository))
dpg.add_text(parent=dpg.last_item(), default_value='Send this date range to all repos')
self.board_tooltip.append(dpg.add_tooltip(f'type_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Change information layout for this repository')
self.board_tooltip.append(dpg.add_tooltip(f'view_a_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Toggle the View All graph on this repository')
self.board_tooltip.append(dpg.add_tooltip(f'view_u_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Toggle the View Unique graph on this repository')
self.board_tooltip.append(dpg.add_tooltip(f'clone_a_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Toggle the Clone All graph on this repository')
self.board_tooltip.append(dpg.add_tooltip(f'clone_u_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Toggle the Clone Unique graph on this repository')
if dpg.does_alias_exist(f'collect_{self.data_board.repository}'):
self.board_tooltip.append(dpg.add_tooltip(f'collect_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Data collected daily for those last days')
if dpg.does_alias_exist(f'report_{self.data_board.repository}'):
self.board_tooltip.append(dpg.add_tooltip(f'report_{self.data_board.repository}'))
dpg.add_text(parent=dpg.last_item(), default_value='Data bundle reported for the last 14 days')
for tt in self.board_tooltip:
dpg.configure_item(tt, hide_on_activity=True, delay=config.tooltips_delay, show=config.tooltips_show)
dpg.bind_item_theme(tt, 'check_and_tooltips')
def change_board(self, sender, app_data):
dpg.delete_item(self._group, children_only=True)
self.populate_boards(app_data)
def sort_boards(self):
dpg.move_item(self._group, parent=self.repo_filter_tag)
# -----------------------------------------------------------------------------
# Create Functions
# -----------------------------------------------------------------------------
def insert_plot(self, height):
with dpg.plot(anti_aliased=True, height=height + (self.plot_height * 28), width=-1):
dpg.bind_item_theme(dpg.last_item(), 'plot')
# dpg.add_plot_legend()
self.x_axis = dpg.add_plot_axis(dpg.mvXAxis, time=True, no_tick_labels=not self.show_dates)
self.y_axis = dpg.add_plot_axis(dpg.mvYAxis)
self.grp_view_a = dpg.add_line_series(self.output_head.time_period, self.data_board.views_a, label="Views All", parent=self.y_axis, show=self.show_views_a)
dpg.bind_item_theme(dpg.last_item(), 'button_view_a')
self.grp_view_u = dpg.add_line_series(self.output_head.time_period, self.data_board.views_u, label="Views Unique", parent=self.y_axis, show=self.show_views_u)
dpg.bind_item_theme(dpg.last_item(), 'button_view_u')
self.grp_clone_a = dpg.add_line_series(self.output_head.time_period, self.data_board.clones_a, label="Clones All", parent=self.y_axis, show=self.show_clones_a)
dpg.bind_item_theme(dpg.last_item(), 'button_clone_a')
self.grp_clone_u = dpg.add_line_series(self.output_head.time_period, self.data_board.clones_u, label="Clones Unique", parent=self.y_axis, show=self.show_clones_u)
dpg.bind_item_theme(dpg.last_item(), 'button_clone_u')
self.grp_view_a_f = dpg.add_shade_series(self.output_head.time_period, self.data_board.views_a, label="Views All", parent=self.y_axis, show=self.show_views_a)
dpg.bind_item_theme(dpg.last_item(), 'button_view_a')
self.grp_view_u_f = dpg.add_shade_series(self.output_head.time_period, self.data_board.views_u, label="Views Unique", parent=self.y_axis, show=self.show_views_u)
dpg.bind_item_theme(dpg.last_item(), 'button_view_u')
self.grp_clone_a_f = dpg.add_shade_series(self.output_head.time_period, self.data_board.clones_a, label="Clones All", parent=self.y_axis, show=self.show_clones_a)
dpg.bind_item_theme(dpg.last_item(), 'button_clone_a')
self.grp_clone_u_f = dpg.add_shade_series(self.output_head.time_period, self.data_board.clones_u, label="Clones Unique", parent=self.y_axis, show=self.show_clones_u)
dpg.bind_item_theme(dpg.last_item(), 'button_clone_u')
# Y axis becomes crowded when y is very high. It do not occlude intermediary labels.
# dpg.set_axis_ticks(self.y_axis, tuple(self.output_head.day_peak))
def insert_header(self, width, buttons=False):
with dpg.child_window(height=28, width=width, border=False):
with dpg.group(horizontal=True):
# width = 483 if buttons else 667
dpg.add_button(label='Repo', width=60)
dpg.bind_item_theme(dpg.last_item(), 'button_dark')
dpg.add_input_text(default_value=self.data_board.repository, width=-1, readonly=True)
dpg.bind_item_theme(dpg.last_item(), 'pad_text')
with dpg.child_window(height=28, border=False):
with dpg.group(horizontal=True):
if buttons:
self.insert_btn_view_col(0)
self.insert_btn_clone_col(0)
dpg.add_button(label='<>', width=30, tag=self.data_board.repository)
dpg.bind_item_handler_registry(dpg.last_item(), 'date_handler')
dpg.bind_item_theme(dpg.last_item(), 'button_working')
dpg.add_combo(board_types, default_value=self.board_type, width=45, callback=self.change_board, tag=f'type_{self.data_board.repository}')
dpg.bind_item_theme(dpg.last_item(), 'button_working')
def insert_referrers(self, amount, y_span, grow_y, grow_x=False):
child_w = -1 if grow_x else 198
grow_height = self.plot_height if grow_y else 0
with dpg.child_window(width=child_w, height=y_span + (grow_height * 28)):
referrer_list = self.data_board.referrers.copy()
if len(referrer_list) > 1:
t_all = 0
t_unq = 0
for item in referrer_list:
t_all += int(item[1])
t_unq += int(item[2])
referrer_list.append(('TOTAL', t_all, t_unq))
for referrer, blank in zip_longest(referrer_list, BLANK_REFS * (amount + grow_height)):
with dpg.group(horizontal=True):
if referrer:
ref_ref = referrer[0]
ref_all = referrer[1]
ref_unq = referrer[2]
else:
ref_ref = blank[0]
ref_all = blank[1]
ref_unq = blank[2]
dpg.add_input_text(default_value=ref_ref, width=-92, readonly=True)
dpg.add_button(label=ref_all, width=45)
dpg.add_button(label=ref_unq, width=45)
def insert_label(self, label, width, theme, au, grow_x=False, tooltip=None):
ref_r_w = -93 if grow_x else width
ref_a_w = -47 if grow_x else 45
ref_u_w = -1 if grow_x else 45
with dpg.group(horizontal=True):
if tooltip:
dpg.add_button(label=label, width=ref_r_w, tag=f'{tooltip}_{self.data_board.repository}')
# print(f'{tooltip}_{self.data_board.repository}')
else:
dpg.add_button(label=label, width=ref_r_w)
dpg.bind_item_theme(dpg.last_item(), theme)
if au:
dpg.add_button(label='ALL', width=ref_a_w)
dpg.add_button(label='UNQ', width=ref_u_w)
dpg.bind_item_theme(dpg.last_container(), 'button_med')
def insert_btn_view_ref(self, size):
with dpg.group(horizontal=True):
if size:
dpg.add_button(label='Views', width=size)
dpg.bind_item_theme(dpg.last_item(), 'button_med')
dpg.add_button(label=self.data_board.rep_view_a, width=45)
dpg.add_button(label=self.data_board.rep_view_u, width=45)
def insert_btn_clone_ref(self, size):
with dpg.group(horizontal=True):
if size:
dpg.add_button(label='Clones', width=size)
dpg.bind_item_theme(dpg.last_item(), 'button_med')
dpg.add_button(label=self.data_board.rep_clone_a, width=45)
dpg.add_button(label=self.data_board.rep_clone_u, width=45)
def insert_btn_view_col(self, size):
with dpg.group(horizontal=True):
if size:
dpg.add_button(label='Views', width=size)
dpg.bind_item_theme(dpg.last_item(), 'button_med')
self.btn_view_a = dpg.add_button(label=self.data_board.col_view_a, width=45, callback=self.toggle_graph_view_a, tag=f'view_a_{self.data_board.repository}')
dpg.bind_item_theme(dpg.last_item(), self.button_theme_view_a)
self.btn_view_u = dpg.add_button(label=self.data_board.col_view_u, width=45, callback=self.toggle_graph_view_u, tag=f'view_u_{self.data_board.repository}')
dpg.bind_item_theme(dpg.last_item(), self.button_theme_view_u)
def insert_btn_clone_col(self, size):
with dpg.group(horizontal=True):
if size:
dpg.add_button(label='Clones', width=size)
dpg.bind_item_theme(dpg.last_item(), 'button_med')
self.btn_clone_a = dpg.add_button(label=self.data_board.col_clone_a, width=45, callback=self.toggle_graph_clone_a, tag=f'clone_a_{self.data_board.repository}')
dpg.bind_item_theme(dpg.last_item(), self.button_theme_clone_a)
self.btn_clone_u = dpg.add_button(label=self.data_board.col_clone_u, width=45, callback=self.toggle_graph_clone_u, tag=f'clone_u_{self.data_board.repository}')
dpg.bind_item_theme(dpg.last_item(), self.button_theme_clone_u)
# -----------------------------------------------------------------------------
# Plot Functions
# -----------------------------------------------------------------------------
def change_plot_height(self, sender, app_data, user_data):
self.plot_height = app_data
self.change_board(None, None)
def toggle_graph_view_a(self):
self.show_views_a = not self.show_views_a
self.change_board(None, None)
def toggle_graph_view_u(self):
self.show_views_u = not self.show_views_u
self.change_board(None, None)
def toggle_graph_clone_a(self):
self.show_clones_a = not self.show_clones_a
self.change_board(None, None)
def toggle_graph_clone_u(self):
self.show_clones_u = not self.show_clones_u
self.change_board(None, None)
def toggle_x_label(self, hide_x_labels):
self.show_dates = hide_x_labels
dpg.configure_item(self.x_axis, no_tick_labels=not self.show_dates)
# -----------------------------------------------------------------------------
# Helper Functions
# -----------------------------------------------------------------------------
def loadData(data_file):
'''Load the consolidated JSON
If not found, generate a blank array
data_file = the JSON to load'''
try:
with open(data_file, 'r') as f:
traffic_in = json.load(f)
except FileNotFoundError:
traffic_in = []
return traffic_in
def saveData(data_file, data, credentials=None):
'''Save the consolidated JSON
data_file = the JSON to save'''
if credentials:
username = credentials[0]
if os.path.exists(data_file):
shutil.copy(f'JSONs/{username}.json', f'JSONs/{username}_bkp.json')
with open(data_file, 'w') as f:
json.dump(data, f, indent=4)
def getAPIdata(url, file=None, use_cache=use_cache, credentials=None, rate=False):
'''Get API JSON data from the web or a cached file
url = API point location
file = JSON file to load or save
use_cache = if True will load the file from disk'''
global repo_count
global repo_max
global username
global token
if credentials:
username = credentials[0]
token = credentials[1]
# create a re-usable session object with the user creds in-built
gh_session = requests.Session()
gh_session.auth = (username, token)
if not use_cache:
if not rate:
if credentials:
if not dpg.get_item_configuration('error')['show']:
return None, 'Fetching interrupted'
dpg.configure_item('error_text', default_value=f'Fetching {url}')
repo_count += 1
if repo_max > 0:
dpg.set_value('progress_repos', 1 / repo_max * repo_count)
dpg.configure_item('progress_repos', overlay=f"{repo_count}/{repo_max}")
else:
print(f'Fetching {url}')
response = json.loads(gh_session.get(url).text)
# Response error
if type(response) is dict:
message = response.get('message', None)
if message:
if credentials:
error = 'API error\n'
for key in response:
print(key)
error += f'\n{key}: {response[key]}'
error += f'\n\n{response}'
return error, True
else:
print('\nAPI error\n')
for key in response:
print(f'{key}: {response[key]}')
print(f'\n{response}\n')
raise SystemExit(0)
if file and keep_cache:
print(f'Saving {file}')
with open(file, 'w') as f:
json.dump(response, f, indent=4)
# Read from disk
else:
try:
with open(file, 'r') as f:
response = json.load(f)
except FileNotFoundError:
print(f'\n Cache file not found: {file}\n')
print()
raise SystemExit(0)
return response, None
def gatherData(traffic_data, credentials=None):
'''Consolidate all the API responses into a single JSON
merging with a previous consolidated JSON
traffic_data = the previous JSON'''
def gatherViewClone(data_type):
'''Read and format the repository information from views or clones
data_type = if 'views' or 'clones' '''
data, error = getAPIdata(f'{base_url}/{username}/{repo_name}/traffic/{data_type}',
f'JSONs/{repo_name}_{data_type}.json',
credentials=credentials)
if error:
return error, True
data_dict = {}
data_dict['count'] = data['count']
data_dict['uniques'] = data['uniques']
# Make a dictionary with the days names as keys for easy merging
# If the key (day) already exists the value will be replaced with the new one
days_dict = {}
if repo_index is not None:
days_dict = traffic_data[repo_index][data_type]['days']
if data['count'] > 0 or data['uniques'] > 0:
for day in data[data_type]:
short_date = day['timestamp'][:10]
days_dict[short_date] = [day['count'],
day['uniques']]
data_dict['days'] = days_dict
return data_dict
global repo_count
global repo_max
global username
repo_max = 0
repo_count = 0
if credentials:
username = credentials[0]
# Finds previous repo names and indexes them
repos_dict = {}
for count, repo in enumerate(traffic_data):
repos_dict[repo['name']] = count
repos, error = getAPIdata(repos_url, 'JSONs/repos.json', credentials=credentials)
if error:
return repos, True
repo_names = [(repo['name']) for repo in repos if repo['owner']['login'] == username]
repo_max = len(repo_names) * 3
for repo_name in repo_names:
repo_index = None
if repo_name in repos_dict:
repo_index = repos_dict[repo_name]
referrers, error = getAPIdata(f'{base_url}/{username}/{repo_name}/{referrers_url}',
f'JSONs/{repo_name}_referrers.json',
credentials=credentials)
if error:
return error, True
referrers_list = []
if len(referrers) > 0:
for referrer in referrers:
referrers_dict = {}
referrers_dict['name'] = referrer['referrer']
referrers_dict['count'] = referrer['count']
referrers_dict['uniques'] = referrer['uniques']
referrers_list.append(referrers_dict)
views_dict = gatherViewClone('views')
clones_dict = gatherViewClone('clones')
repo_dict = {}
repo_dict['name'] = repo_name
repo_dict['updated'] = datetime.now().strftime('%Y-%m-%d')
repo_dict['referrers'] = referrers_list
repo_dict['views'] = views_dict
repo_dict['clones'] = clones_dict
# If the repo already exists replace with merged, if not, create
if repo_index is not None:
traffic_data[repo_index] = repo_dict
else:
traffic_data.append(repo_dict)
return traffic_data, None
# -----------------------------------------------------------------------------
# Terminal Section
# -----------------------------------------------------------------------------
def buildConsoleOutput(traffic_data):
'''Build the output lists
traffic_data = the consolidated JSON with all the info
min_cust = the user defined start day date
max_cust = the user defined end day date'''
def getMinMaxdate(data_type, min_day, max_day):
'''Get the earlier and later days in the views and clones data
data_type = if 'views' or 'clones'
min_day = the earlier day in the data
max_day = the later day in the data'''
for repo in traffic_data:
if repo[data_type]['count'] > 0 or repo[data_type]['uniques'] > 0:
# Convert the dictionary into a list to be able to sort it
days_list = list(repo[data_type]['days'].items())
days_list = sorted(days_list)
min_tmp = datetime.strptime(days_list[0][0], r'%Y-%m-%d')
max_tmp = datetime.strptime(days_list[-1][0], r'%Y-%m-%d')
min_day = min(min_day, min_tmp)
max_day = max(max_day, max_tmp)
return min_day, max_day
def buildViewClone(data_type):
'''Assemble and shows the data for the views and the clones
data_type = if 'views' or 'clones' '''
repo_parc = []
l_title = f'{BLUE}{data_type.capitalize()}:'
tot_c = 0
tot_u = 0
l_days = ''
l_count = f' {BLUE}Cnt: '
l_uniqu = f' {BLUE}Unq: '
p_days = ''
p_count = ''
p_uniqu = ''
report = (f' {CYAN}Count: {GREEN}{repo[data_type]["count"]}'
f' {CYAN}Uniques: {GREEN}{repo[data_type]["uniques"]}'
f' {GRAY}(last 14 days){RESET}')
# Build display strings
o_day = 0
for d in range(interval.days + 1):
day = min_day + timedelta(days=d)
day_str = day.strftime('%Y-%m-%d')
c_day = day.day
if day_str in repo[data_type]['days'] or show_all_days:
# Separate months (only if show all days to avoid inconsistencies)
separator = ' '
if c_day < o_day and show_all_days:
separator = '|'
p_days += f'{BLUE}{separator}{CYAN}{str(c_day).zfill(2)}'
o_day = c_day
if day_str in repo[data_type]['days']:
day_view = repo[data_type]['days'][day_str][0]
tot_c += day_view
p_count += str(day_view).ljust(3)
day_view = repo[data_type]['days'][day_str][1]
tot_u += day_view