blob: 55789848b905a22bb25428c32b3edb56b631d1fd [file] [log] [blame]
gstepanov7cc6ba02015-02-03 17:18:42 +02001from urlparse import urlparse
gstepanov725cc302015-02-09 15:08:06 +02002
gstepanove0f26ab2015-02-02 18:09:37 +02003import json
4import os
gstepanov71764e92015-02-02 16:33:11 +02005
6
7class Measurement(object):
8 def __init__(self):
9 self.build = ""
10 self.build_type = 0 # GA/Master/Other
11 self.md5 = ""
12 self.results = {
13 "": (float, float)
14 }
15
gstepanovb10fa5b2015-02-04 15:55:15 +020016 def __str__(self):
17 return self.build + " " + self.build_type + " " + \
18 self.md5 + " " + str(self.results)
19
gstepanov71764e92015-02-02 16:33:11 +020020
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020021def create_storage(url, email=None, password=None):
22 u = urlparse(url)
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020023 if u.scheme == 'file':
24 storage = DiskStorage(u.path)
gstepanov7cc6ba02015-02-03 17:18:42 +020025
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020026 return storage
gstepanovbd7c3292015-02-03 15:23:41 +020027
28
gstepanov71764e92015-02-02 16:33:11 +020029class Storage(object):
gstepanovbd7c3292015-02-03 15:23:41 +020030
gstepanove0f26ab2015-02-02 18:09:37 +020031 def store(self, data):
32 pass
gstepanov71764e92015-02-02 16:33:11 +020033
gstepanove0f26ab2015-02-02 18:09:37 +020034 def retrieve(self, id):
35 pass
36
37
gstepanove0f26ab2015-02-02 18:09:37 +020038class DiskStorage(Storage):
39 def __init__(self, file_name):
40 self.file_name = file_name
41
42 if not os.path.exists(file_name):
43 with open(file_name, "w+") as f:
44 f.write(json.dumps([]))
45
46 def store(self, data):
47 with open(self.file_name, "rt") as f:
48 raw_data = f.read()
49 document = json.loads(raw_data)
50 document.append(data)
51
52 with open(self.file_name, "w+") as f:
53 f.write(json.dumps(document))
54
55 def retrieve(self, id):
56 with open(self.file_name, "rt") as f:
57 raw_data = f.read()
58 document = json.loads(raw_data)
59
60 for row in document:
61 if row["build_id"] == id:
62 m = Measurement()
63 m.build = row.pop("build_id")
64 m.build_type = row.pop("type")
65 m.md5 = row.pop("iso_md5")
koder aka kdanilov3cbbc9d2015-02-04 15:37:58 +020066 m.results = {k.split(" "): row[k] for k in row.keys()}
gstepanove0f26ab2015-02-02 18:09:37 +020067
68 return m
69 return None
70
gstepanov82d2a942015-02-03 15:10:11 +020071 def recent_builds(self):
72 with open(self.file_name, "rt") as f:
73 raw_data = f.read()
74 document = json.loads(raw_data)
75 d = {}
gstepanov9a9c54e2015-02-04 17:01:04 +020076 result = {}
gstepanov3ab691b2015-02-04 16:19:15 +020077 build_types = {"GA", "master"}
gstepanov82d2a942015-02-03 15:10:11 +020078
79 for i in range(len(document) - 1, -1, - 1):
gstepanov3ab691b2015-02-04 16:19:15 +020080 if document[i]["type"] in build_types:
81 if document[i]["type"] not in d:
82 d[document[i]["type"]] = document[i]
83 elif "other" not in d:
84 d["other"] = document[i]
gstepanov82d2a942015-02-03 15:10:11 +020085
86 for k in d.keys():
87 m = Measurement()
88 m.build = d[k].pop("build_id")
89 m.build_type = d[k].pop("type")
90 m.md5 = d[k].pop("iso_md5")
gstepanovb10fa5b2015-02-04 15:55:15 +020091 m.results = {k: v for k, v in d[k].items()}
gstepanov9a9c54e2015-02-04 17:01:04 +020092 result[m.build_type] = m
gstepanov82d2a942015-02-03 15:10:11 +020093
gstepanovb10fa5b2015-02-04 15:55:15 +020094 return result
gstepanov82d2a942015-02-03 15:10:11 +020095
gstepanov2164d822015-02-04 19:49:40 +020096
koder aka kdanilov4a72f122015-02-09 12:25:54 +020097#if __name__ == "__main__":
98# storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "")
99# print storage.recent_builds()