def query(bot, chat_id, user_id=None, company=None): """Query to http://stackalytics.com/""" url = 'http://stackalytics.com/api/1.0/contribution?' if company: url += 'company={}' . format(company) if user_id: url += '&user_id={}' . format(user_id) LOG.debug('Query url: {}' . format(url)) try: stats = urllib.request.urlopen(url) stats = json.loads(stats.read().decode()) except ValueError as e: msg = 'Error with JSON format: {}' . format(e) utils.handle_error(LOG, bot, chat_id, msg) return None except urllib.error.HTTPError as e: msg = 'Error when query member {}\'s stats: {}' . format(user_id, e) utils.handle_error(LOG, bot, chat_id, msg) return None patches = stats['contribution']['patch_set_count'] commits = stats['contribution']['commit_count'] reviews = \ stats['contribution']['marks']['1'] + \ stats['contribution']['marks']['-1'] + \ stats['contribution']['marks']['2'] + \ stats['contribution']['marks']['-2'] return (patches, commits, reviews)
def do_report(bot, job): chat_id = job.context['chat_id'] try: config = json.load(open('config.json')) except FileNotFoundError: msg = 'Config file doesn\'t exist! Type /help stackalytics again to \ check usage!' utils.handle_error(LOG, bot, chat_id, msg) return except json.decoder.JSONDecodeError: msg = 'Wrong format! Type /help stackalytics again to \ check right format!' utils.handle_error(LOG, bot, chat_id, msg) return bot.send_message(chat_id=chat_id, text=emojies.no_speak + 'Report time, please wait for seconds...') text = '{} *Report:*\n' . format(emojies.point_right) targets = (int(config['review_target']), int(config['commit_target'])) report_file = 'FVL_UC_Contribution_Summarization.xlsx' workbook = xlsxwriter.Workbook(report_file) num_members = len(config['members']) team_stats_patches = 0 team_stats_commits = 0 team_stats_reviews = 0 worksheet = workbook.add_worksheet() for index, name in enumerate(config['members']): results = stackalytics.query(bot, chat_id, user_id=config['members'][name], company=config['company']) if not results: continue patches, commits, reviews = results team_stats_commits += commits team_stats_patches += patches team_stats_reviews += reviews text = stackalytics.get_report(workbook, worksheet, name, (reviews, commits), targets, num_members, index, text) text = stackalytics.get_report(workbook, worksheet, 'Team', (team_stats_reviews, team_stats_commits), targets, num_members, 0, text, summary=True) workbook.close() bot.send_message(chat_id=chat_id, text=text, parse_mode=ParseMode.MARKDOWN) bot.send_document(chat_id=chat_id, document=open(report_file, 'rb'), caption='Report') return
def handle(bot, update): chat_id = update.message.chat_id try: config = json.load(open('config.json')) except FileNotFoundError: msg = 'Config file doesn\' exist! Type /help stackalytics again to \ check usage!' utils.handle_error(LOG, bot, chat_id, msg) return except json.decoder.JSONDecodeError: msg = 'Wrong format! Type /help stackalytics again to \ check right format!' utils.handle_error(LOG, bot, chat_id, msg) return args = update.message.text.split(' ') report = False if len(args) > 2: update.message.reply_text('Wrong number of options! ' 'Please type /help stackalytics for usage.') return elif len(args) == 2 and args[1] != 'report': update.message.reply_text('Wrong option! ' 'Please type /help stackalytics for usage.') return else: report = True update.message.reply_text(emojies.no_speak + 'Please wait for few seconds! ' 'Query members stats from Stackalytics...') text = '{} *Member report:*\n' . format(emojies.point_right) targets = (int(config['review_target']), int(config['commit_target'])) report_file = \ 'FVL_UC_Contribution_Summarization.xlsx' workbook = xlsxwriter.Workbook(report_file) num_members = len(config['members']) team_stats_patches = 0 team_stats_commits = 0 team_stats_reviews = 0 worksheet = workbook.add_worksheet() for index, name in enumerate(config['members']): results = query(bot, chat_id, user_id=config['members'][name], company=config['company']) if not results: continue patches, commits, reviews = results team_stats_commits += commits team_stats_patches += patches team_stats_reviews += reviews # Bad performance, condition in loop (Fixme) if report: text = get_report(workbook, worksheet, name, (reviews, commits), targets, num_members, index, text) text = get_report(workbook, worksheet, 'Team', (team_stats_reviews, team_stats_commits), targets, num_members, 0, text, summary=True) workbook.close() bot.send_message(chat_id=chat_id, text=text, parse_mode=ParseMode.MARKDOWN) if report: bot.send_document(chat_id=chat_id, document=open(report_file, 'rb'), caption='Report') return