gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 1 | from urlparse import urlparse |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 2 | # from gspread import WorksheetNotFound, login |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 3 | import json |
| 4 | import os |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 5 | from gspread import login |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 6 | from config import ROW_COUNT, DOCUMENT_ID, WORK_SHEET |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 7 | |
| 8 | |
| 9 | def 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 | |
| 22 | def 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 | |
| 34 | def 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 |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 41 | |
| 42 | |
| 43 | class 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 | |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 52 | def __str__(self): |
| 53 | return self.build + " " + self.build_type + " " + \ |
| 54 | self.md5 + " " + str(self.results) |
| 55 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 56 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 57 | def create_storage(url, email=None, password=None): |
| 58 | u = urlparse(url) |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 59 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 60 | if u.scheme == 'file': |
| 61 | storage = DiskStorage(u.path) |
| 62 | else: |
| 63 | storage = GoogleDocsStorage(DOCUMENT_ID, WORK_SHEET, email, password) |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 64 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 65 | return storage |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 66 | |
| 67 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 68 | class Storage(object): |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 69 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 70 | def store(self, data): |
| 71 | pass |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 72 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 73 | def retrieve(self, id): |
| 74 | pass |
| 75 | |
| 76 | |
| 77 | class GoogleDocsStorage(Storage): |
| 78 | |
| 79 | def __init__(self, doc_id, work_sheet_name, email=None, password=None): |
| 80 | self.gc = login(email, password) |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 81 | 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() |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 93 | m.build = vals.pop("build_id") |
| 94 | m.build_type = vals.pop("type") |
| 95 | m.md5 = vals.pop("iso_md5") |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 96 | 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 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 107 | 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 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 128 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 129 | class 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 kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 157 | m.results = {k.split(" "): row[k] for k in row.keys()} |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 158 | |
| 159 | return m |
| 160 | return None |
| 161 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 162 | 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 = {} |
gstepanov | 9a9c54e | 2015-02-04 17:01:04 +0200 | [diff] [blame^] | 167 | result = {} |
gstepanov | 3ab691b | 2015-02-04 16:19:15 +0200 | [diff] [blame] | 168 | build_types = {"GA", "master"} |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 169 | |
| 170 | for i in range(len(document) - 1, -1, - 1): |
gstepanov | 3ab691b | 2015-02-04 16:19:15 +0200 | [diff] [blame] | 171 | 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] |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 176 | |
| 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") |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 182 | m.results = {k: v for k, v in d[k].items()} |
gstepanov | 9a9c54e | 2015-02-04 17:01:04 +0200 | [diff] [blame^] | 183 | result[m.build_type] = m |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 184 | |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 185 | return result |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 186 | |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 187 | if __name__ == "__main__": |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 188 | storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "") |
| 189 | print storage.recent_builds() |