blob: 026a46d16291ec577edb17fe2da43368ca724145 [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
gstepanove0f26ab2015-02-02 18:09:37 +02003import json
4import os
gstepanovc3ffac72015-02-05 15:39:45 +02005from gspread import login, WorksheetNotFound
gstepanov7cc6ba02015-02-03 17:18:42 +02006from config import ROW_COUNT, DOCUMENT_ID, WORK_SHEET
gstepanove0f26ab2015-02-02 18:09:37 +02007
gstepanovc3ffac72015-02-05 15:39:45 +02008TEST_PATH = os.environ.get("TEST_PATH", os.path.dirname(__file__) + "/test_results")
9
gstepanove0f26ab2015-02-02 18:09:37 +020010
11def get_work_sheet(sheet, name, column_names):
12 try:
13 work_sheet = sheet.worksheet(name)
14 except WorksheetNotFound:
15 work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT,
16 cols=max(40, len(column_names)))
17
18 for i in range(1, len(column_names) + 1):
19 work_sheet.update_cell(1, i, column_names[i - 1])
20
21 return work_sheet
22
23
24def get_row_number(work_sheet):
25 num = 2
26
27 while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "":
28 num += 1
29
30 if num == work_sheet.row_count:
31 work_sheet.append_row(["" for x in range(work_sheet.col_count)])
32
33 return num
34
35
36def append_row(work_sheet, row):
37 row_number = get_row_number(work_sheet)
38
39 i = 1
40 for k in row.keys():
41 work_sheet.update_cell(row_number, i, row[k])
42 i += 1
gstepanov71764e92015-02-02 16:33:11 +020043
44
45class Measurement(object):
46 def __init__(self):
47 self.build = ""
48 self.build_type = 0 # GA/Master/Other
49 self.md5 = ""
50 self.results = {
51 "": (float, float)
52 }
53
gstepanovb10fa5b2015-02-04 15:55:15 +020054 def __str__(self):
55 return self.build + " " + self.build_type + " " + \
56 self.md5 + " " + str(self.results)
57
gstepanov71764e92015-02-02 16:33:11 +020058
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020059def create_storage(url, email=None, password=None):
60 u = urlparse(url)
gstepanov7cc6ba02015-02-03 17:18:42 +020061
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
gstepanov7cc6ba02015-02-03 17:18:42 +0200190if __name__ == "__main__":
gstepanovb10fa5b2015-02-04 15:55:15 +0200191 storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "")
192 print storage.recent_builds()