1# Copyright 2019 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from github import Github, Label 16from datetime import datetime, timedelta 17from time import time 18from google.cloud import bigquery 19 20ACCESS_TOKEN = "" 21 22 23def get_stats_from_github(): 24 # Please set the access token properly before deploying. 25 assert ACCESS_TOKEN 26 g = Github(ACCESS_TOKEN) 27 print g.rate_limiting 28 repo = g.get_repo('grpc/grpc') 29 30 LABEL_LANG = set(label for label in repo.get_labels() 31 if label.name.split('/')[0] == 'lang') 32 LABEL_KIND_BUG = repo.get_label('kind/bug') 33 LABEL_PRIORITY_P0 = repo.get_label('priority/P0') 34 LABEL_PRIORITY_P1 = repo.get_label('priority/P1') 35 LABEL_PRIORITY_P2 = repo.get_label('priority/P2') 36 37 def is_untriaged(issue): 38 key_labels = set() 39 for label in issue.labels: 40 label_kind = label.name.split('/')[0] 41 if label_kind in ('lang', 'kind', 'priority'): 42 key_labels.add(label_kind) 43 return len(key_labels) < 3 44 45 untriaged_open_issues = [ 46 issue for issue in repo.get_issues(state='open') 47 if issue.pull_request is None and is_untriaged(issue) 48 ] 49 total_bugs = [ 50 issue for issue in repo.get_issues(state='all', labels=[LABEL_KIND_BUG]) 51 if issue.pull_request is None 52 ] 53 54 lang_to_stats = {} 55 for lang in LABEL_LANG: 56 lang_bugs = filter(lambda bug: lang in bug.labels, total_bugs) 57 closed_bugs = filter(lambda bug: bug.state == 'closed', lang_bugs) 58 open_bugs = filter(lambda bug: bug.state == 'open', lang_bugs) 59 open_p0_bugs = filter(lambda bug: LABEL_PRIORITY_P0 in bug.labels, 60 open_bugs) 61 open_p1_bugs = filter(lambda bug: LABEL_PRIORITY_P1 in bug.labels, 62 open_bugs) 63 open_p2_bugs = filter(lambda bug: LABEL_PRIORITY_P2 in bug.labels, 64 open_bugs) 65 lang_to_stats[lang] = [ 66 len(lang_bugs), 67 len(closed_bugs), 68 len(open_bugs), 69 len(open_p0_bugs), 70 len(open_p1_bugs), 71 len(open_p2_bugs) 72 ] 73 return len(untriaged_open_issues), lang_to_stats 74 75 76def insert_stats_to_db(untriaged_open_issues, lang_to_stats): 77 timestamp = time() 78 client = bigquery.Client() 79 dataset_ref = client.dataset('github_issues') 80 table_ref = dataset_ref.table('untriaged_issues') 81 table = client.get_table(table_ref) 82 errors = client.insert_rows(table, [(timestamp, untriaged_open_issues)]) 83 table_ref = dataset_ref.table('bug_stats') 84 table = client.get_table(table_ref) 85 rows = [] 86 for lang, stats in lang_to_stats.iteritems(): 87 rows.append((timestamp, lang.name[5:]) + tuple(stats)) 88 errors = client.insert_rows(table, rows) 89 90 91def fetch(): 92 untriaged_open_issues, lang_to_stats = get_stats_from_github() 93 insert_stats_to_db(untriaged_open_issues, lang_to_stats) 94