This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 916
/
track_metrics.py
executable file
·171 lines (145 loc) · 5.06 KB
/
track_metrics.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import sys
import os
# Need to append a full path instead of relative path.
# NOTE this does not affect running directly in the shell.
sys.path.append(os.getcwd())
import argparse
import traceback
import logging
from datetime import datetime
from dateutil.parser import isoparse
from pymongo import MongoClient
from tools.repo_metrics.git_stats import Github
from tools.repo_metrics.config import (
GITHUB_TOKEN,
CONNECTION_STRING,
DATABASE,
COLLECTION_GITHUB_STATS,
COLLECTION_EVENTS,
)
format_str = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)s]: %(message)s"
format_time = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(level=logging.INFO, format=format_str, datefmt=format_time)
log = logging.getLogger()
def parse_args():
"""Argument parser.
Returns:
obj: Parser.
"""
parser = argparse.ArgumentParser(
description="Metrics Tracker",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--github_repo", type=str, help="GitHub repository")
parser.add_argument(
"--event",
type=str,
help="Input a general event that can be saved to the database",
)
parser.add_argument(
"--save_to_database",
action="store_true",
help="Whether or not to save the information to the database",
)
parser.add_argument(
"--event_date",
default=datetime.now().isoformat(),
type=isoparse,
help="Date for an event (format: YYYY-MM-DD)",
)
return parser.parse_args()
def connect(uri="mongodb://localhost"):
"""Mongo connector.
Args:
uri (str): Connection string.
Returns:
obj: Mongo client.
"""
client = MongoClient(uri, serverSelectionTimeoutMS=1000)
# Send a query to the server to see if the connection is working.
try:
client.server_info()
except Exception:
raise
return client
def event_as_dict(event, date):
"""Encodes an string event input as a dictionary with the date.
Args:
event (str): Details of a event.
date (datetime): Date of the event.
Returns:
dict: Dictionary with the event and the date.
"""
return {"date": date.strftime("%b %d %Y %H:%M:%S"), "event": event}
def github_stats_as_dict(github):
"""Encodes Github statistics as a dictionary with the date.
Args:
obj: Github object.
Returns:
dict: Dictionary with Github details and the date.
"""
return {
"date": datetime.now().strftime("%b %d %Y %H:%M:%S"),
"stars": github.stars,
"forks": github.forks,
"watchers": github.watchers,
"open_issues": github.open_issues,
"open_pull_requests": github.open_pull_requests,
"unique_views": github.number_unique_views,
"total_views": github.number_total_views,
"details_views": github.views,
"unique_clones": github.number_unique_clones,
"total_clones": github.number_total_clones,
"details_clones": github.clones,
"last_year_commit_frequency": github.last_year_commit_frequency,
"details_referrers": github.top_ten_referrers,
"total_referrers": github.number_total_referrers,
"unique_referrers": github.number_unique_referrers,
"details_content": github.top_ten_content,
"repo_size": github.repo_size,
"commits": github.number_commits,
"contributors": github.number_contributors,
"branches": github.number_branches,
"tags": github.number_tags,
"total_lines": github.number_total_lines,
"added_lines": github.number_added_lines,
"deleted_lines": github.number_deleted_lines,
}
def tracker(args):
"""Main function to track metrics.
Args:
args (obj): Parsed arguments.
"""
if args.github_repo:
# if there is an env variable, overwrite it
token = os.environ.get("GITHUB_TOKEN", GITHUB_TOKEN)
g = Github(token, args.github_repo)
git_doc = github_stats_as_dict(g)
log.info("GitHub stats -- {}".format(git_doc))
g.clean()
if args.event:
event_doc = event_as_dict(args.event, args.event_date)
log.info("Event -- {}".format(event_doc))
if args.save_to_database:
# if there is an env variable, overwrite it
connection = token = os.environ.get("CONNECTION_STRING", CONNECTION_STRING)
cli = connect(connection)
db = cli[DATABASE]
if args.github_repo:
db[COLLECTION_GITHUB_STATS].insert_one(git_doc)
if args.event:
db[COLLECTION_EVENTS].insert_one(event_doc)
if __name__ == "__main__":
log.info("Starting routine")
args = parse_args()
try:
log.info("Arguments: {}".format(args))
tracker(args)
except Exception as e:
trace = traceback.format_exc()
log.error("Traceback: {}".format(trace))
log.error("Exception: {}".format(e))
finally:
log.info("Routine finished")