gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 1 | from urlparse import urlparse |
gstepanov | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame^] | 2 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 3 | import json |
| 4 | import os |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 5 | |
| 6 | |
| 7 | class 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 | |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 16 | def __str__(self): |
| 17 | return self.build + " " + self.build_type + " " + \ |
| 18 | self.md5 + " " + str(self.results) |
| 19 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 20 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 21 | def create_storage(url, email=None, password=None): |
| 22 | u = urlparse(url) |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 23 | if u.scheme == 'file': |
| 24 | storage = DiskStorage(u.path) |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 25 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 26 | return storage |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 27 | |
| 28 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 29 | class Storage(object): |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 30 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 31 | def store(self, data): |
| 32 | pass |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 33 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 34 | def retrieve(self, id): |
| 35 | pass |
| 36 | |
| 37 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 38 | class 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 kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 66 | m.results = {k.split(" "): row[k] for k in row.keys()} |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 67 | |
| 68 | return m |
| 69 | return None |
| 70 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 71 | 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 = {} |
gstepanov | 9a9c54e | 2015-02-04 17:01:04 +0200 | [diff] [blame] | 76 | result = {} |
gstepanov | 3ab691b | 2015-02-04 16:19:15 +0200 | [diff] [blame] | 77 | build_types = {"GA", "master"} |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 78 | |
| 79 | for i in range(len(document) - 1, -1, - 1): |
gstepanov | 3ab691b | 2015-02-04 16:19:15 +0200 | [diff] [blame] | 80 | 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] |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 85 | |
| 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") |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 91 | m.results = {k: v for k, v in d[k].items()} |
gstepanov | 9a9c54e | 2015-02-04 17:01:04 +0200 | [diff] [blame] | 92 | result[m.build_type] = m |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 93 | |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 94 | return result |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 95 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 96 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 97 | #if __name__ == "__main__": |
| 98 | # storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "") |
| 99 | # print storage.recent_builds() |