1
0
Fork 0
llvm-premerge-checks/scripts/metrics/repo.py

97 lines
3.2 KiB
Python
Raw Normal View History

2020-02-17 15:01:15 +01:00
#!/usr/bin/env python3
# Copyright 2019 Google LLC
#
# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://llvm.org/LICENSE.txt
#
# 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 datetime
from time import timezone
import git
from typing import Dict
from google.cloud import monitoring_v3
2020-02-17 16:18:58 +01:00
import re
2020-02-17 15:01:15 +01:00
from datetime import tzinfo
GCP_PROJECT_ID = 'llvm-premerge-checks'
class RepoStats:
def __init__(self):
2020-02-17 16:18:58 +01:00
self.commits = 0 # type: int
self.reverts = 0 # type: int
self.reviewed = 0 # type: int
2020-02-17 15:01:15 +01:00
@property
def percent_reverted(self) -> float:
return 100.0 * self.reverts / self.commits
2020-02-17 16:18:58 +01:00
@property
def percent_reviewed(self) -> float:
return 100.0 * self.reviewed / (self.commits - self.reverts)
2020-02-17 15:01:15 +01:00
def __str__(self):
return "\n".join([
2020-02-17 16:18:58 +01:00
"commits: {}".format(self.commits),
"reverts: {}".format(self.reverts),
"reviewed: {}".format(self.reviewed),
2020-02-17 15:01:15 +01:00
"percent reverted: {:0.1f}".format(self.percent_reverted),
2020-02-17 16:18:58 +01:00
"percent reviewed: {:0.1f}".format(self.percent_reviewed),
2020-02-17 15:01:15 +01:00
])
def get_reverts_per_day(repo_path: str, max_age: datetime.datetime) -> RepoStats:
stats = RepoStats()
repo = git.Repo(repo_path)
repo.git.fetch()
2020-02-17 16:18:58 +01:00
diff_regex = re.compile(r'^Differential Revision: https:\/\/reviews\.llvm\.org\/(.*)$', re.MULTILINE)
2020-02-17 15:01:15 +01:00
for commit in repo.iter_commits('master'):
if commit.committed_datetime < max_age:
break
stats.commits += 1
2020-02-17 16:18:58 +01:00
if commit.message.startswith('Revert'):
2020-02-17 15:01:15 +01:00
stats.reverts += 1
2020-02-17 16:18:58 +01:00
if diff_regex.search(commit.message) is not None:
stats.reviewed += 1
2020-02-17 15:01:15 +01:00
return stats
def gcp_write_data(project_id: str, stats: RepoStats, now:datetime.datetime):
"""Upload metrics to Stackdriver."""
client = monitoring_v3.MetricServiceClient()
project_name = client.project_path(project_id)
for desc_type, value in [
["reverts", stats.reverts],
["commits", stats.commits],
["percent_reverted", stats.percent_reverted],
2020-02-17 16:18:58 +01:00
["reviewed", stats.reviewed],
["percent_reviewed", stats.percent_reviewed],
2020-02-17 15:01:15 +01:00
]:
series = monitoring_v3.types.TimeSeries()
series.metric.type = 'custom.googleapis.com/repository_{}'.format(desc_type)
series.resource.type = 'global'
point = series.points.add()
point.value.double_value = value
point.interval.end_time.seconds = int(now.timestamp())
client.create_time_series(project_name, [series])
if __name__ == '__main__':
now = datetime.datetime.now(tz=datetime.timezone.utc)
max_age = now - datetime.timedelta(days=1)
# TODO: make path configurable
stats = get_reverts_per_day('~/git/llvm-project', max_age)
print(stats)
2020-02-17 16:18:58 +01:00
# TODO: add `dryrun` parameter
2020-02-17 15:01:15 +01:00
gcp_write_data(GCP_PROJECT_ID, stats, now)