Mercurial > roundup-cc
view collect_issues.py @ 20:3bb3d9a9f1b7
Filter by keywords and states.
Configuration via configuration file.
"No prio" grapf is displayed.
(./display_issues_demo.py is not yet dynamic, it needs all states)
author | Magnus Schieder <mschieder@intevation.de> |
---|---|
date | Mon, 22 Oct 2018 16:49:58 +0200 |
parents | adca5b3780d2 |
children | 10167e40a657 7161ce4e7ab1 |
line wrap: on
line source
#!/usr/bin/env python3 """ Fetch issues from a roundup-tracker and save them in a databse. author: Sascha L. Teichmann <sascha.teichmann@intevation.de> author: Bernhard Reiter <bernhard@intevation.de> author: Sean Engelhardt <sean.engelhardt@intevation.de> (c) 2010, 2015, 2018 by Intevation GmbH This is Free Software unter the terms of the GNU GENERAL PUBLIC LICENSE Version 3 or later. See http://www.gnu.org/licenses/gpl-3.0.txt for details For usage see examples/. """ import http.cookiejar import urllib.parse import urllib.request import csv import io import sqlite3 as db import os CHECK_ROUNDUP_ORDER = "priority?@action=export_csv&@columns=id,order" CHECK_KEYWORD_ORDER = "keyword?@action=export_csv&@columns=id,name" CHECK_ROUNDUP_SEARCH_VALUES = "status?@action=export_csv&@columns=id,name&@filter=open&open=1" SEARCH_ROUNDUP = "issue?@action=export_csv&@columns=priority&@filter=status,keyword&@pagesize=500&@startwith=0&status={search_values}&keyword={keyword_values}" def connect_to_server(params, baseurl): enc_data = urllib.parse.urlencode(params).encode() cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) req = urllib.request.Request(url=baseurl, data=enc_data) opener.open(req) return opener def get_csv_from_server(opener, roundup_url, sub_url): csv_req = urllib.request.Request(url=roundup_url+sub_url) f = opener.open(csv_req) csv_reader = csv.DictReader(io.TextIOWrapper(f)) return csv_reader def check_create_database(database_file, sql_create_db): if not os.path.isfile(database_file): con = None cur = None try: con = db.connect(database_file) cur = con.cursor() try: cur.execute(sql_create_db) con.commit() os.chmod(database_file, 0o644) except: con.rollback() raise finally: if cur: cur.close() if con: con.close() def get_keyword_ids(opener, baseurl, keywords): if keywords == [""]: return "" keywords_csv = get_csv_from_server(opener, baseurl, CHECK_KEYWORD_ORDER) keywords_dict = {} for x in keywords_csv: keywords_dict[x["name"]] = x["id"] keywords_ids = [] for x in keywords: keywords_ids.append(keywords_dict[x]) return ",".join(keywords_ids) def get_status_ids(opener, baseurl, status): status_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_SEARCH_VALUES) if status == [""]: return "" status_dict = {} for x in status_csv: status_dict[x["name"]] = x["id"] staus_ids = ["-1"] for x in status: staus_ids.append(status_dict[x]) return ",".join(staus_ids) def issues_to_quantities(issue_csv, columns, orders_csv): """Count issues per priority. Returns: a list of ints, containing how often a prio occurred [:-1] in order of the priorities, with the last being the "None" prio """ quantities = [0] * (len(columns) +1) order_dict = {} #convert the csv-dict reader to real dict for row in orders_csv: order_dict[row["id"]] = int(float(row["order"])) # int(float()) because the order-value is indeed "1.0, 2.0" etc for issue in issue_csv: priority = issue["priority"] if priority.isdigit() == True : quantities[order_dict[priority] -1 ] += 1 else: # no priority set quantities[-1] += 1 # print("quantities : " + str(quantities)) return quantities def save_issues_to_db(quantities, database_file, sql_create_db, sql_insert_in_db): check_create_database(database_file, sql_create_db) cur = None con = None try: con = db.connect(database_file) cur = con.cursor() try: cur.execute(sql_insert_in_db, quantities) con.commit() except: con.rollback() raise finally: if cur: cur.close() if con: con.close() def save_stats_in_db(login_parmeters, baseurl, db_file, columns, sql_create_db, sql_insert_in_db, keywords, status, include_no_prio=False): try: opener = connect_to_server(login_parmeters, baseurl) order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER) keywords_ids_url = get_keyword_ids(opener, baseurl, keywords) status_ids_url = get_status_ids(opener, baseurl, status) formated_search_url = SEARCH_ROUNDUP.format(search_values=status_ids_url, keyword_values=keywords_ids_url) current_issues_csv = get_csv_from_server(opener, baseurl, formated_search_url) opener.close() quantities = issues_to_quantities(current_issues_csv, columns, order_csv) if not include_no_prio: quantities = quantities[:-1] save_issues_to_db(quantities, db_file, sql_create_db, sql_insert_in_db) except urllib.error.URLError as e: print("No Valid Connection to server : " + baseurl + "\nerror: " + str(e))