blob: 7841297211eb1913c182fe54dd48072392e424e1 [file] [log] [blame]
gstepanov7cc6ba02015-02-03 17:18:42 +02001from urlparse import urlparse
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +02002# from gspread import WorksheetNotFound, login
koder aka kdanilov4a72f122015-02-09 12:25:54 +02003# from gspread import login
gstepanove0f26ab2015-02-02 18:09:37 +02004import json
5import os
koder aka kdanilov4a72f122015-02-09 12:25:54 +02006# from gspread import login, WorksheetNotFound
gstepanov7cc6ba02015-02-03 17:18:42 +02007from config import ROW_COUNT, DOCUMENT_ID, WORK_SHEET
gstepanove0f26ab2015-02-02 18:09:37 +02008
koder aka kdanilov4a72f122015-02-09 12:25:54 +02009# TEST_PATH = os.environ.get("TEST_PATH", os.path.dirname(__file__) + "/test_results")
gstepanovc3ffac72015-02-05 15:39:45 +020010
gstepanove0f26ab2015-02-02 18:09:37 +020011
12def get_work_sheet(sheet, name, column_names):
13 try:
14 work_sheet = sheet.worksheet(name)
15 except WorksheetNotFound:
16 work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT,
17 cols=max(40, len(column_names)))
18
19 for i in range(1, len(column_names) + 1):
20 work_sheet.update_cell(1, i, column_names[i - 1])
21
22 return work_sheet
23
24
25def get_row_number(work_sheet):
26 num = 2
27
28 while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "":
29 num += 1
30
31 if num == work_sheet.row_count:
32 work_sheet.append_row(["" for x in range(work_sheet.col_count)])
33
34 return num
35
36
37def append_row(work_sheet, row):
38 row_number = get_row_number(work_sheet)
39
40 i = 1
41 for k in row.keys():
42 work_sheet.update_cell(row_number, i, row[k])
43 i += 1
gstepanov71764e92015-02-02 16:33:11 +020044
45
46class Measurement(object):
47 def __init__(self):
48 self.build = ""
49 self.build_type = 0 # GA/Master/Other
50 self.md5 = ""
51 self.results = {
52 "": (float, float)
53 }
54
gstepanovb10fa5b2015-02-04 15:55:15 +020055 def __str__(self):
56 return self.build + " " + self.build_type + " " + \
57 self.md5 + " " + str(self.results)
58
gstepanov71764e92015-02-02 16:33:11 +020059
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020060def create_storage(url, email=None, password=None):
61 u = urlparse(url)
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020062 if u.scheme == 'file':
63 storage = DiskStorage(u.path)
64 else:
65 storage = GoogleDocsStorage(DOCUMENT_ID, WORK_SHEET, email, password)
gstepanov7cc6ba02015-02-03 17:18:42 +020066
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020067 return storage
gstepanovbd7c3292015-02-03 15:23:41 +020068
69
gstepanov71764e92015-02-02 16:33:11 +020070class Storage(object):
gstepanovbd7c3292015-02-03 15:23:41 +020071
gstepanove0f26ab2015-02-02 18:09:37 +020072 def store(self, data):
73 pass
gstepanov71764e92015-02-02 16:33:11 +020074
gstepanove0f26ab2015-02-02 18:09:37 +020075 def retrieve(self, id):
76 pass
77
78
79class GoogleDocsStorage(Storage):
80
81 def __init__(self, doc_id, work_sheet_name, email=None, password=None):
82 self.gc = login(email, password)
gstepanov71764e92015-02-02 16:33:11 +020083 self.sh = self.gc.open_by_key(doc_id)
84 self.work_sheet = get_work_sheet(self.sh, work_sheet_name, 40)
85
86 def store(self, data):
87 append_row(self.work_sheet, data)
88
89 def retrieve(self, id):
90 row_number = self.find_by_id(id)
91
92 if row_number != -1:
93 vals = self.work_sheet.row_values(row_number)
94 m = Measurement()
gstepanove0f26ab2015-02-02 18:09:37 +020095 m.build = vals.pop("build_id")
96 m.build_type = vals.pop("type")
97 m.md5 = vals.pop("iso_md5")
gstepanov71764e92015-02-02 16:33:11 +020098 m.results = {k: vals[k] for k in vals.keys()}
99 else:
100 return None
101
102 def find_by_id(self, row_id):
103 for i in range(1, self.work_sheet):
104 if self.work_sheet.cell(i, 1) == row_id:
105 return i
106
107 return -1
108
gstepanov82d2a942015-02-03 15:10:11 +0200109 def recent_builds(self):
110 i = self.work_sheet.row_count - 1
111 d = {}
112 result = []
113
114 while i > 0:
115 vals = self.work_sheet.row_values(i)
116
117 if vals["type"] not in d:
118 d[vals["type"]] = vals
119 m = Measurement()
120 m.build = vals.pop("build_id")
121 m.build_type = vals.pop("type")
122 m.md5 = vals.pop("iso_md5")
123 m.results = {k: vals[k] for k in vals.keys()}
124 result.append(m)
125
126 i -= 1
127
128 return result
129
gstepanov71764e92015-02-02 16:33:11 +0200130
gstepanove0f26ab2015-02-02 18:09:37 +0200131class DiskStorage(Storage):
132 def __init__(self, file_name):
133 self.file_name = file_name
134
135 if not os.path.exists(file_name):
136 with open(file_name, "w+") as f:
137 f.write(json.dumps([]))
138
139 def store(self, data):
140 with open(self.file_name, "rt") as f:
141 raw_data = f.read()
142 document = json.loads(raw_data)
143 document.append(data)
144
145 with open(self.file_name, "w+") as f:
146 f.write(json.dumps(document))
147
148 def retrieve(self, id):
149 with open(self.file_name, "rt") as f:
150 raw_data = f.read()
151 document = json.loads(raw_data)
152
153 for row in document:
154 if row["build_id"] == id:
155 m = Measurement()
156 m.build = row.pop("build_id")
157 m.build_type = row.pop("type")
158 m.md5 = row.pop("iso_md5")
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +0200159 m.results = {k.split(" "): row[k] for k in row.keys()}
gstepanove0f26ab2015-02-02 18:09:37 +0200160
161 return m
162 return None
163
gstepanov82d2a942015-02-03 15:10:11 +0200164 def recent_builds(self):
165 with open(self.file_name, "rt") as f:
166 raw_data = f.read()
167 document = json.loads(raw_data)
168 d = {}
gstepanov9a9c54e2015-02-04 17:01:04 +0200169 result = {}
gstepanov3ab691b2015-02-04 16:19:15 +0200170 build_types = {"GA", "master"}
gstepanov82d2a942015-02-03 15:10:11 +0200171
172 for i in range(len(document) - 1, -1, - 1):
gstepanov3ab691b2015-02-04 16:19:15 +0200173 if document[i]["type"] in build_types:
174 if document[i]["type"] not in d:
175 d[document[i]["type"]] = document[i]
176 elif "other" not in d:
177 d["other"] = document[i]
gstepanov82d2a942015-02-03 15:10:11 +0200178
179 for k in d.keys():
180 m = Measurement()
181 m.build = d[k].pop("build_id")
182 m.build_type = d[k].pop("type")
183 m.md5 = d[k].pop("iso_md5")
gstepanovb10fa5b2015-02-04 15:55:15 +0200184 m.results = {k: v for k, v in d[k].items()}
gstepanov9a9c54e2015-02-04 17:01:04 +0200185 result[m.build_type] = m
gstepanov82d2a942015-02-03 15:10:11 +0200186
gstepanovb10fa5b2015-02-04 15:55:15 +0200187 return result
gstepanov82d2a942015-02-03 15:10:11 +0200188
gstepanov2164d822015-02-04 19:49:40 +0200189
koder aka kdanilov4a72f122015-02-09 12:25:54 +0200190#if __name__ == "__main__":
191# storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "")
192# print storage.recent_builds()