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 |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame^] | 3 | # from gspread import login |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 4 | import json |
| 5 | import os |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame^] | 6 | # from gspread import login, WorksheetNotFound |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 7 | from config import ROW_COUNT, DOCUMENT_ID, WORK_SHEET |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 8 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame^] | 9 | # TEST_PATH = os.environ.get("TEST_PATH", os.path.dirname(__file__) + "/test_results") |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 10 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 11 | |
| 12 | def get_work_sheet(sheet, name, column_names): |
| 13 | try: |
| 14 | work_sheet = sheet.worksheet(name) |
| 15 | except WorksheetNotFound: |
| 16 | work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT, |
| 17 | cols=max(40, len(column_names))) |
| 18 | |
| 19 | for i in range(1, len(column_names) + 1): |
| 20 | work_sheet.update_cell(1, i, column_names[i - 1]) |
| 21 | |
| 22 | return work_sheet |
| 23 | |
| 24 | |
| 25 | def get_row_number(work_sheet): |
| 26 | num = 2 |
| 27 | |
| 28 | while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "": |
| 29 | num += 1 |
| 30 | |
| 31 | if num == work_sheet.row_count: |
| 32 | work_sheet.append_row(["" for x in range(work_sheet.col_count)]) |
| 33 | |
| 34 | return num |
| 35 | |
| 36 | |
| 37 | def append_row(work_sheet, row): |
| 38 | row_number = get_row_number(work_sheet) |
| 39 | |
| 40 | i = 1 |
| 41 | for k in row.keys(): |
| 42 | work_sheet.update_cell(row_number, i, row[k]) |
| 43 | i += 1 |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 44 | |
| 45 | |
| 46 | class Measurement(object): |
| 47 | def __init__(self): |
| 48 | self.build = "" |
| 49 | self.build_type = 0 # GA/Master/Other |
| 50 | self.md5 = "" |
| 51 | self.results = { |
| 52 | "": (float, float) |
| 53 | } |
| 54 | |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 55 | def __str__(self): |
| 56 | return self.build + " " + self.build_type + " " + \ |
| 57 | self.md5 + " " + str(self.results) |
| 58 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 59 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 60 | def create_storage(url, email=None, password=None): |
| 61 | u = urlparse(url) |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 62 | if u.scheme == 'file': |
| 63 | storage = DiskStorage(u.path) |
| 64 | else: |
| 65 | storage = GoogleDocsStorage(DOCUMENT_ID, WORK_SHEET, email, password) |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 66 | |
koder aka kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 67 | return storage |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 68 | |
| 69 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 70 | class Storage(object): |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 71 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 72 | def store(self, data): |
| 73 | pass |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 74 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 75 | def retrieve(self, id): |
| 76 | pass |
| 77 | |
| 78 | |
| 79 | class GoogleDocsStorage(Storage): |
| 80 | |
| 81 | def __init__(self, doc_id, work_sheet_name, email=None, password=None): |
| 82 | self.gc = login(email, password) |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 83 | 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() |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 95 | m.build = vals.pop("build_id") |
| 96 | m.build_type = vals.pop("type") |
| 97 | m.md5 = vals.pop("iso_md5") |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 98 | 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 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 109 | 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 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 130 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 131 | class 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 kdanilov | 3cbbc9d | 2015-02-04 15:37:58 +0200 | [diff] [blame] | 159 | m.results = {k.split(" "): row[k] for k in row.keys()} |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 160 | |
| 161 | return m |
| 162 | return None |
| 163 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 164 | 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 = {} |
gstepanov | 9a9c54e | 2015-02-04 17:01:04 +0200 | [diff] [blame] | 169 | result = {} |
gstepanov | 3ab691b | 2015-02-04 16:19:15 +0200 | [diff] [blame] | 170 | build_types = {"GA", "master"} |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 171 | |
| 172 | for i in range(len(document) - 1, -1, - 1): |
gstepanov | 3ab691b | 2015-02-04 16:19:15 +0200 | [diff] [blame] | 173 | 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] |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 178 | |
| 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") |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 184 | m.results = {k: v for k, v in d[k].items()} |
gstepanov | 9a9c54e | 2015-02-04 17:01:04 +0200 | [diff] [blame] | 185 | result[m.build_type] = m |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 186 | |
gstepanov | b10fa5b | 2015-02-04 15:55:15 +0200 | [diff] [blame] | 187 | return result |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 188 | |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 189 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame^] | 190 | #if __name__ == "__main__": |
| 191 | # storage = create_storage("file:///home/gstepanov/rally-results-processor/sample.json", "", "") |
| 192 | # print storage.recent_builds() |