blob: 8e995c17eaeed59a02cf1ad5f54454109b940696 [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
gstepanovb10fa5b2015-02-04 15:55:15 +02005from gspread import login
gstepanov7cc6ba02015-02-03 17:18:42 +02006from config import ROW_COUNT, DOCUMENT_ID, WORK_SHEET
gstepanove0f26ab2015-02-02 18:09:37 +02007
8
9def get_work_sheet(sheet, name, column_names):
10 try:
11 work_sheet = sheet.worksheet(name)
12 except WorksheetNotFound:
13 work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT,
14 cols=max(40, len(column_names)))
15
16 for i in range(1, len(column_names) + 1):
17 work_sheet.update_cell(1, i, column_names[i - 1])
18
19 return work_sheet
20
21
22def get_row_number(work_sheet):
23 num = 2
24
25 while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "":
26 num += 1
27
28 if num == work_sheet.row_count:
29 work_sheet.append_row(["" for x in range(work_sheet.col_count)])
30
31 return num
32
33
34def append_row(work_sheet, row):
35 row_number = get_row_number(work_sheet)
36
37 i = 1
38 for k in row.keys():
39 work_sheet.update_cell(row_number, i, row[k])
40 i += 1
gstepanov71764e92015-02-02 16:33:11 +020041
42
43class Measurement(object):
44 def __init__(self):
45 self.build = ""
46 self.build_type = 0 # GA/Master/Other
47 self.md5 = ""
48 self.results = {
49 "": (float, float)
50 }
51
gstepanovb10fa5b2015-02-04 15:55:15 +020052 def __str__(self):
53 return self.build + " " + self.build_type + " " + \
54 self.md5 + " " + str(self.results)
55
gstepanov71764e92015-02-02 16:33:11 +020056
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020057def create_storage(url, email=None, password=None):
58 u = urlparse(url)
gstepanov7cc6ba02015-02-03 17:18:42 +020059
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020060 if u.scheme == 'file':
61 storage = DiskStorage(u.path)
62 else:
63 storage = GoogleDocsStorage(DOCUMENT_ID, WORK_SHEET, email, password)
gstepanov7cc6ba02015-02-03 17:18:42 +020064
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020065 return storage
gstepanovbd7c3292015-02-03 15:23:41 +020066
67
gstepanov71764e92015-02-02 16:33:11 +020068class Storage(object):
gstepanovbd7c3292015-02-03 15:23:41 +020069
gstepanove0f26ab2015-02-02 18:09:37 +020070 def store(self, data):
71 pass
gstepanov71764e92015-02-02 16:33:11 +020072
gstepanove0f26ab2015-02-02 18:09:37 +020073 def retrieve(self, id):
74 pass
75
76
77class GoogleDocsStorage(Storage):
78
79 def __init__(self, doc_id, work_sheet_name, email=None, password=None):
80 self.gc = login(email, password)
gstepanov71764e92015-02-02 16:33:11 +020081 self.sh = self.gc.open_by_key(doc_id)
82 self.work_sheet = get_work_sheet(self.sh, work_sheet_name, 40)
83
84 def store(self, data):
85 append_row(self.work_sheet, data)
86
87 def retrieve(self, id):
88 row_number = self.find_by_id(id)
89
90 if row_number != -1:
91 vals = self.work_sheet.row_values(row_number)
92 m = Measurement()
gstepanove0f26ab2015-02-02 18:09:37 +020093 m.build = vals.pop("build_id")
94 m.build_type = vals.pop("type")
95 m.md5 = vals.pop("iso_md5")
gstepanov71764e92015-02-02 16:33:11 +020096 m.results = {k: vals[k] for k in vals.keys()}
97 else:
98 return None
99
100 def find_by_id(self, row_id):
101 for i in range(1, self.work_sheet):
102 if self.work_sheet.cell(i, 1) == row_id:
103 return i
104
105 return -1
106
gstepanov82d2a942015-02-03 15:10:11 +0200107 def recent_builds(self):
108 i = self.work_sheet.row_count - 1
109 d = {}
110 result = []
111
112 while i > 0:
113 vals = self.work_sheet.row_values(i)
114
115 if vals["type"] not in d:
116 d[vals["type"]] = vals
117 m = Measurement()
118 m.build = vals.pop("build_id")
119 m.build_type = vals.pop("type")
120 m.md5 = vals.pop("iso_md5")
121 m.results = {k: vals[k] for k in vals.keys()}
122 result.append(m)
123
124 i -= 1
125
126 return result
127
gstepanov71764e92015-02-02 16:33:11 +0200128
gstepanove0f26ab2015-02-02 18:09:37 +0200129class DiskStorage(Storage):
130 def __init__(self, file_name):
131 self.file_name = file_name
132
133 if not os.path.exists(file_name):
134 with open(file_name, "w+") as f:
135 f.write(json.dumps([]))
136
137 def store(self, data):
138 with open(self.file_name, "rt") as f:
139 raw_data = f.read()
140 document = json.loads(raw_data)
141 document.append(data)
142
143 with open(self.file_name, "w+") as f:
144 f.write(json.dumps(document))
145
146 def retrieve(self, id):
147 with open(self.file_name, "rt") as f:
148 raw_data = f.read()
149 document = json.loads(raw_data)
150
151 for row in document:
152 if row["build_id"] == id:
153 m = Measurement()
154 m.build = row.pop("build_id")
155 m.build_type = row.pop("type")
156 m.md5 = row.pop("iso_md5")
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +0200157 m.results = {k.split(" "): row[k] for k in row.keys()}
gstepanove0f26ab2015-02-02 18:09:37 +0200158
159 return m
160 return None
161
gstepanov82d2a942015-02-03 15:10:11 +0200162 def recent_builds(self):
163 with open(self.file_name, "rt") as f:
164 raw_data = f.read()
165 document = json.loads(raw_data)
166 d = {}
gstepanov9a9c54e2015-02-04 17:01:04 +0200167 result = {}
gstepanov3ab691b2015-02-04 16:19:15 +0200168 build_types = {"GA", "master"}
gstepanov82d2a942015-02-03 15:10:11 +0200169
170 for i in range(len(document) - 1, -1, - 1):
gstepanov3ab691b2015-02-04 16:19:15 +0200171 if document[i]["type"] in build_types:
172 if document[i]["type"] not in d:
173 d[document[i]["type"]] = document[i]
174 elif "other" not in d:
175 d["other"] = document[i]
gstepanov82d2a942015-02-03 15:10:11 +0200176
177 for k in d.keys():
178 m = Measurement()
179 m.build = d[k].pop("build_id")
180 m.build_type = d[k].pop("type")
181 m.md5 = d[k].pop("iso_md5")
gstepanovb10fa5b2015-02-04 15:55:15 +0200182 m.results = {k: v for k, v in d[k].items()}
gstepanov9a9c54e2015-02-04 17:01:04 +0200183 result[m.build_type] = m
gstepanov82d2a942015-02-03 15:10:11 +0200184
gstepanovb10fa5b2015-02-04 15:55:15 +0200185 return result
gstepanov82d2a942015-02-03 15:10:11 +0200186
gstepanov2164d822015-02-04 19:49:40 +0200187
gstepanov7cc6ba02015-02-03 17:18:42 +0200188if __name__ == "__main__":
gstepanovb10fa5b2015-02-04 15:55:15 +0200189 storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "")
190 print storage.recent_builds()