annotate modules/roundup_cc/collect_issues.py @ 13:63b9f41c3008

made the charts a bit more modular - can now define the div_name and the script name in the .conf data
author sean
date Wed, 05 Aug 2015 12:37:33 +0200
parents 7a573ec679a6
children
rev   line source
11
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
1 #!/usr/bin/env python3
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
2
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
3 """ Fetch issues from a roundup-tracker and save them in a databse.
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
4
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
5 author: Sascha L. Teichmann <sascha.teichmann@intevation.de>
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
6 author: Bernhard Reiter <bernhard@intevation.de>
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
7 author: Sean Engelhardt <sean.engelhardt@intevation.de>
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
8
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
9 (c) 2010,2015 by Intevation GmbH
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
10
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
11 This is Free Software unter the terms of the
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
12 GNU GENERAL PUBLIC LICENSE Version 3 or later.
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
13 See http://www.gnu.org/licenses/gpl-3.0.txt for details
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
14
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
15
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
16 ##USAGE EXAMPLE: ##
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
17
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
18 BASE_URL_DEMO = "http://localhost:8917/demo/"
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
19 SEARCH_URL_DEMO = "issue?@action=export_csv&@columns=title,priority&@filter=status&@pagesize=50&@startwith=0&status=-1,1,2,3,4,5,6,7"
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
20
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
21 LOGIN_PARAMETERS_DEMO = (
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
22 ("__login_name", "demo"),
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
23 ("__login_password", "demo"),
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
24 ("@action", "Login"),
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
25 )
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
26
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
27 save_stats_in_db(LOGIN_PARAMETERS_DEMO, BASE_URL_DEMO, rcd.DATABASE_DEMO, rcd.COLUMNS, rcd.CREATE_DB, rcd.INSERT_NEW, SEARCH_URL_DEMO)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
28 """
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
29
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
30 import http.cookiejar
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
31 import urllib.parse
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
32 import urllib.request
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
33 import csv
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
34 import io
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
35 import sqlite3 as db
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
36 import os
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
37 import roundup_cc.roundup_content_data as rcd
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
38
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
39
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
40 CHECK_ROUNDUP_ORDER = "priority?@action=export_csv&@columns=id,order"
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
41 CHECK_ROUNDUP_SEARCH_VALUES = "status?@action=export_csv&@columns=id&@filter=open&open=1"
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
42 SEARCH_ROUNDUP = "issue?@action=export_csv&@columns=priority&@filter=status&@pagesize=500&@startwith=0&status=-1,{search_values}"
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
43
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
44
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
45 def connect_to_server(params, baseurl):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
46 enc_data = urllib.parse.urlencode(params).encode()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
47 cj = http.cookiejar.CookieJar()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
48 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
49 req = urllib.request.Request(url=baseurl, data=enc_data)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
50 opener.open(req)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
51 return opener
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
52
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
53
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
54 def get_csv_from_server(opener, roundup_url, sub_url):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
55 csv_req = urllib.request.Request(url=roundup_url+sub_url)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
56 f = opener.open(csv_req)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
57 csv_reader = csv.DictReader(io.TextIOWrapper(f))
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
58 return csv_reader
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
59
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
60
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
61 def set_search_paramters_on_URL(url, search_param_csv):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
62
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
63 id_list = []
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
64
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
65 for row in search_param_csv:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
66 id_list.append(row["id"])
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
67
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
68 new_url = url.format(search_values = ",".join(id_list))
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
69
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
70 return new_url
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
71
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
72
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
73 def check_create_database(database_file, sql_create_db):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
74 if not os.path.isfile(database_file):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
75 con = None
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
76 cur = None
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
77 try:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
78 con = db.connect(database_file)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
79 cur = con.cursor()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
80 try:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
81 cur.execute(sql_create_db)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
82 con.commit()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
83 os.chmod(database_file, 0o644)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
84 except:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
85 con.rollback()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
86 raise
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
87 finally:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
88 if cur:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
89 cur.close()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
90 if con:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
91 con.close()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
92
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
93
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
94 def issues_to_quantities(issue_csv, columns, orders_csv):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
95
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
96 quantities = [0] * len(columns)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
97 order_dict = {}
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
98
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
99 #convert the csv-dict reader to real dict
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
100 for row in orders_csv:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
101 order_dict[row["id"]] = int(float(row["order"])) # int(float()) because the order-value is indeed "1.0, 2.0" etc
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
102
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
103 for issue in issue_csv:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
104 priority = issue["priority"]
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
105
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
106 if priority.isdigit() == True :
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
107 quantities[order_dict[priority] -1 ] += 1
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
108
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
109 # print("quantities : " + str(quantities))
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
110
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
111 return quantities
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
112
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
113
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
114 def save_issues_to_db(quantities, database_file, sql_create_db, sql_insert_in_db):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
115 check_create_database(database_file, sql_create_db)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
116
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
117 cur = None
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
118 con = None
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
119
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
120 try:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
121 con = db.connect(database_file)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
122 cur = con.cursor()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
123 try:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
124 cur.execute(sql_insert_in_db, quantities)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
125 con.commit()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
126 except:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
127 con.rollback()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
128 raise
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
129 finally:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
130 if cur:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
131 cur.close()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
132 if con:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
133 con.close()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
134
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
135
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
136 def save_stats_in_db(login_parmeters, baseurl, db_file, columns, sql_create_db, sql_insert_in_db, searchurl=False):
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
137 try:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
138
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
139 opener = connect_to_server(login_parmeters, baseurl)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
140
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
141 search_operators_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_SEARCH_VALUES)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
142 order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
143
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
144 if searchurl == False:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
145 formated_search_url = set_search_paramters_on_URL(SEARCH_ROUNDUP, search_operators_csv)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
146 else:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
147 formated_search_url = searchurl
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
148
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
149 current_issues_csv = get_csv_from_server(opener, baseurl, formated_search_url)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
150
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
151 opener.close()
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
152
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
153 quantities = issues_to_quantities(current_issues_csv, columns, order_csv)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
154
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
155 save_issues_to_db(quantities, db_file, sql_create_db, sql_insert_in_db)
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
156
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
157 except urllib.error.URLError as e:
7a573ec679a6 added more files, can now display charts a bit
sean
parents:
diff changeset
158 print("No Valid Connection to server : " + baseurl + "\nerror: " + str(e))
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)