From fee2ce8624a374ed2573cf9260a2940611a63421 Mon Sep 17 00:00:00 2001 From: Paddle CI Date: Wed, 30 May 2018 23:25:26 +0800 Subject: [PATCH 01/10] adjust db struct to make faster and clearer --- db.py | 4 ++ web/api.py | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ web/main.py | 26 ++++++-- web/view.py | 113 ++-------------------------------- 4 files changed, 204 insertions(+), 112 deletions(-) create mode 100644 web/api.py diff --git a/db.py b/db.py index ae2ed19..daff073 100644 --- a/db.py +++ b/db.py @@ -31,6 +31,10 @@ def find_one(self, table, cond): ''' return self.table(table).find_one(cond) + def find_sections(self, table, cond, sections): + + return self.table(table).find(cond, sections) + def find(self, table, cond): ''' Find records. diff --git a/web/api.py b/web/api.py new file mode 100644 index 0000000..d29becb --- /dev/null +++ b/web/api.py @@ -0,0 +1,173 @@ +__all__ = [ + "CommitRecord", + "TaskRecord", + "KpiRecord", +] + +import sys +sys.path.append('pypage') +sys.path.append('..') +import config +import json +from db import MongoDB +from datetime import datetime, timedelta +from kpi import Kpi + +db = MongoDB(config.db_name) + + +class objdict(dict): + def __setattr__(self, key, value): + self[key] = value + + def __getattr__(self, item): + return self[item] + + +class CommitRecord: + def __init__(self, commit=''): + self.commit = commit + self.short_commit = "" + self.date = None # datetime + self.info = "" + + @staticmethod + def get_all_commitids(): + ''' Get all commit records, and sort by latest to oldest. + returns: list of CommitRecord + ''' + commits = db.find_sections(config.table_name, {'type': 'kpi'}, + {'commitid': 1, + "_id": 0}) + commit_ids = [] + for commit in commits: + if commit['commitid'] not in commit_ids: + commit_ids.append(commit['commitid']) + return commit_ids + + def query_a_commit_info(self): + ''' get the corresponding TaskRecords. + returns: list of TaskRecord + ''' + return db.finds(config.table_name, + {'type': 'kpi', + 'commitid': self.commit}) + + @staticmethod + def query_all_commit_infos(): + '''Get all commit records, returns: list of Commit object''' + commits = CommitRecord.get_all_commitids() + + records = [] + for commit in commits: + commitobj = CommitRecord(commit) + tasks = commitobj.query_a_commit_info() + commitobj.commit = commit + commitobj.shortcommit = commit[:7] + commitobj.date = datetime.utcfromtimestamp(int(tasks[0]['date'])) + \ + timedelta(hours=8) + + commitobj.passed = tasks_success(tasks) + records.append(commitobj) + + return records + + +class TaskRecord(objdict): + def __init__(self, commit, name, infos, passed): + self.name = name + self.task = name + # dict of KpiRecord + self.kpis = [] + self.infos = infos + self.passed = passed + self.commitid = commit + + def query_a_task_info(self): + ''' get the corresponding TaskRecord. + returns: dict of TaskRecord''' + return db.find_one(config.table_name, {'type': 'kpi', \ + 'commitid': self.commitid, 'task': self.name}) + + @staticmethod + def get_tasks_from_details(commit): + ''' Get the task details belong to a commit from the database. ''' + record = CommitRecord(commit) + tasks = record.query_a_commit_info() + res = objdict() + for task in tasks: + taskobj = TaskRecord(commit, task['task'], task['infos'], + task['passed']) + taskobj.kpis = taskobj.get_kpi_details() + res[taskobj.name] = taskobj + return res + + def get_kpi_details(self): + '''Transfrom a mongodb kpi record from lists to a python dict.''' + task_info = self.query_a_task_info() + kpi_infos = {} + for kpi in task_info['kpis-keys']: + kpiobj = KpiRecord(kpi) + kpi_infos[kpi] = kpiobj.get_kpi_info_by_key(task_info) + return kpi_infos + + +class KpiRecord: + def __init__(self, name): + self.name = name + # list of list of float + self.values = [] + self.type = "" + self.avg = 0 + self.activeds = False + self.unit = "" + self.desc = "" + + def get_kpi_info_by_key(self, task_info): + '''Get the kpi infos according to the kpi key''' + for i in range(len(task_info['kpis-keys'])): + if self.name == task_info['kpis-keys'][i]: + break + #To keep the kpi datas in order, we should process the data one by one. + kpi_vals = json.loads(task_info['kpis-values']) + self.values = kpi_vals[i] + self.type = task_info['kpi-types'][i] + self.avg = '%.4f' % Kpi.dic.get(self.type).cal_kpi(data=kpi_vals[i]) + infos = parse_infos(task_info['infos']) + self.info = infos[self.name] + self.activeds = task_info['kpi-activeds'][i] + self.unit = task_info['kpi-unit-reprs'][i] + self.desc = task_info['kpi-descs'][i] + + return (self.values, self.type, self.avg, self.info, self.activeds, + self.unit, self.desc) + + +class objdict(dict): + def __setattr__(self, key, value): + self[key] = value + + def __getattr__(self, item): + return self[item] + + +def parse_infos(infos): + ''' + input format: [kpi0] xxxx [kpi1] xxx + + return dic of (kpi, info) + ''' + res = {} + for info in infos: + lb = info.find('[') + 1 + rb = info.find(']', lb) + kpi = info[lb:rb] + info = info[rb + 2:] + res[kpi] = info + return res + + +def tasks_success(tasks): + for task in tasks: + if not task['passed']: return False + return True diff --git a/web/main.py b/web/main.py index 8df7bb6..086f74e 100644 --- a/web/main.py +++ b/web/main.py @@ -10,6 +10,7 @@ import pprint from kpi import Kpi from view import * +from api import * SERVER_PATH = os.path.abspath(os.path.dirname(sys.argv[0])) STATIC_DIR = os.path.join(SERVER_PATH, "static") @@ -32,7 +33,7 @@ def index(): a list of commitids and their status(passed or not, the info) ''' page, snips = build_index_page() - commits = get_commits() + commits = CommitRecord.query_all_commit_infos() latest_commit = commits[-1].commit logics = merge_logics(snips[0].logic(), snips[1].logic(latest_commit)) print('commits', snips[0].logic()) @@ -54,7 +55,7 @@ def commit_details(): @cache.cached(timeout=120) def commit_compare(): if 'cur' not in request.args: - commits = get_commits() + commits = CommitRecord.query_all_commit_infos() latest_commit = commits[-1] success_commits = [v for v in filter(lambda r: r.passed, commits)] latest_success_commit = success_commits[ @@ -72,6 +73,21 @@ def commit_compare(): if __name__ == '__main__': - host = '0.0.0.0' - port = 80 - app.run(debug=False, host=host, port=port, threaded=True) + import argparse + + parser = argparse.ArgumentParser(description='CE Web') + parser.add_argument( + '--port', + type=int, + default=80, + required=False, + help='web service port') + + parser.add_argument( + '--host', + type=str, + default='0.0.0.0', + required=False, + help='web service host') + args = parser.parse_args() + app.run(debug=False, host=args.host, port=args.port, threaded=True) diff --git a/web/view.py b/web/view.py index db7727c..27974ea 100644 --- a/web/view.py +++ b/web/view.py @@ -8,6 +8,7 @@ from pypage import layout as lyt from datetime import datetime, timedelta from kpi import Kpi +from api import * status_page = Page( "Evaluation Status", filename="pypage-status.html").enable_bootstrap() @@ -149,7 +150,7 @@ def html(self): alert(c=VAL('kpi[3]')).set_danger() def logic(self, commitid): - task_kpis = query_commit_from_db(commitid) + task_kpis = TaskRecord.get_tasks_from_details(commitid) res = objdict(version=dict( commit=commitid, passed=tasks_success(task_kpis), @@ -188,7 +189,7 @@ def html(self): RawHtml('
') def logic(self): - records_ = get_commits() + records_ = CommitRecord.query_all_commit_infos() return {self.KEY('records'): records_} @@ -220,7 +221,7 @@ def html(self): Tag('span', VAL('commit.date')) def logic(self): - commits = get_commits() + commits = CommitRecord.query_all_commit_infos() return {self.KEY('commits'): [v for v in reversed(commits)], } @@ -275,8 +276,8 @@ def logic(self, cur_commit, base_commit): print('cur', cur_commit) print('base', base_commit) - cur_rcds = query_commit_from_db(cur_commit) - base_rcds = query_commit_from_db(base_commit) + cur_rcds = TaskRecord.get_tasks_from_details(cur_commit) + base_rcds = TaskRecord.get_tasks_from_details(base_commit) res = [] for name in cur_rcds.keys(): @@ -311,66 +312,6 @@ def passed_commits(): pass -def get_commit_py_records(commit): - ''' Get python records belonging to commit. ''' - records = query_commit_from_db(commit) - print('records.values', records.values()) - return [db_task_record_to_py(r) for r in records.values()] - - -def query_commit_from_db(commit): - ''' Get the task details belong to a commit from the database. ''' - tasks = db.finds(config.table_name, {'type': 'kpi', 'commitid': commit}) - res = objdict() - for task in tasks: - task = db_task_record_to_py(task) - task['task'] = task.name - res[task.name] = task - return res - - -def db_task_record_to_py(task_rcd): - ''' Transfrom a mongodb task record to python record. All the fields should be - transformed.''' - task = objdict( - name=task_rcd['task'], - passed=task_rcd['passed'], - commitid=task_rcd['commitid'], ) - - def safe_get_fields(field): - if field in task_rcd: - return json.loads(task_rcd[field]) - return None - - kpi_vals = json.loads(task_rcd['kpis-values']) - task.kpis = {} - infos = parse_infos(task_rcd['infos']) - activeds = safe_get_fields('kpi-activeds') - unit_reprs = safe_get_fields('kpi-unit-reprs') - descs = safe_get_fields('kpi-descs') - - for i in range(len(task_rcd['kpis-keys'])): - kpi_type = Kpi.dic.get(task_rcd['kpi-types'][i]) - kpi = task_rcd['kpis-keys'][i] - task.kpis[kpi] = ( - # kpi details - kpi_vals[i], - # type - task_rcd['kpi-types'][i], - # kpi - '%.4f' % kpi_type.cal_kpi(data=kpi_vals[i]), - # info - infos[kpi], - # actived - activeds[i] if activeds else True, - # unit repr - "(%s)" % unit_reprs[i] if unit_reprs else "", - # desc - descs[i] if descs else "", ) - task.infos = task_rcd['infos'] - return task - - class objdict(dict): def __setattr__(self, key, value): self[key] = value @@ -379,49 +320,7 @@ def __getattr__(self, item): return self[item] -def parse_infos(infos): - ''' - input format: [kpi0] xxxx [kpi1] xxx - - return dic of (kpi, info) - ''' - res = {} - for info in infos: - lb = info.find('[') + 1 - rb = info.find(']', lb) - kpi = info[lb:rb] - info = info[rb + 2:] - res[kpi] = info - return res - - def tasks_success(tasks): for task in tasks.values(): if not task['passed']: return False return True - - -def get_commits(): - ''' get all the commits ''' - records = db.finds(config.table_name, {'type': 'kpi'}) - - # detact whether the task is passed. - commits = {} - for task in records: - rcd = db_task_record_to_py(task) - commits.setdefault(rcd.commitid, {})[rcd.name] = rcd - - records_ = [] - commit_set = set() - for rcd in records: - if rcd['commitid'] not in commit_set: - commit_set.add(rcd['commitid']) - rcd_ = objdict() - rcd_.commit = rcd['commitid'] - rcd_.shortcommit = rcd['commitid'][:7] - rcd_.date = datetime.utcfromtimestamp(int(rcd['date'])) + \ - timedelta(hours=8) - rcd_.passed = tasks_success(commits[rcd_.commit]) - records_.append(rcd_) - - return records_ From 94cee6b32b9d7f8712d115b724a67207979ed545 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Thu, 7 Jun 2018 20:36:46 +0800 Subject: [PATCH 02/10] Update api.py --- web/api.py | 60 +++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/web/api.py b/web/api.py index d29becb..2750e10 100644 --- a/web/api.py +++ b/web/api.py @@ -32,36 +32,21 @@ def __init__(self, commit=''): self.info = "" @staticmethod - def get_all_commitids(): + def get_all(): ''' Get all commit records, and sort by latest to oldest. returns: list of CommitRecord ''' - commits = db.find_sections(config.table_name, {'type': 'kpi'}, - {'commitid': 1, - "_id": 0}) + commits = db.find_sections(config.table_name, + {'type': 'kpi'}, {'commitid': 1, "_id": 0}, "date") commit_ids = [] for commit in commits: if commit['commitid'] not in commit_ids: commit_ids.append(commit['commitid']) - return commit_ids - - def query_a_commit_info(self): - ''' get the corresponding TaskRecords. - returns: list of TaskRecord - ''' - return db.finds(config.table_name, - {'type': 'kpi', - 'commitid': self.commit}) - - @staticmethod - def query_all_commit_infos(): - '''Get all commit records, returns: list of Commit object''' - commits = CommitRecord.get_all_commitids() - + records = [] - for commit in commits: + for commit in commit_ids: commitobj = CommitRecord(commit) - tasks = commitobj.query_a_commit_info() + tasks = commitobj.get_commit_record() commitobj.commit = commit commitobj.shortcommit = commit[:7] commitobj.date = datetime.utcfromtimestamp(int(tasks[0]['date'])) + \ @@ -72,6 +57,14 @@ def query_all_commit_infos(): return records + def get_commit_record(self): + ''' get the corresponding TaskRecords. + returns: list of TaskRecord + ''' + return db.finds(config.table_name, + {'type': 'kpi', + 'commitid': self.commit}) + class TaskRecord(objdict): def __init__(self, commit, name, infos, passed): @@ -83,17 +76,18 @@ def __init__(self, commit, name, infos, passed): self.passed = passed self.commitid = commit - def query_a_task_info(self): + def get_task_info(self): ''' get the corresponding TaskRecord. returns: dict of TaskRecord''' return db.find_one(config.table_name, {'type': 'kpi', \ 'commitid': self.commitid, 'task': self.name}) @staticmethod - def get_tasks_from_details(commit): + def get_tasks(commit): ''' Get the task details belong to a commit from the database. ''' record = CommitRecord(commit) - tasks = record.query_a_commit_info() + tasks = record.get_commit_record() + print (tasks) res = objdict() for task in tasks: taskobj = TaskRecord(commit, task['task'], task['infos'], @@ -104,7 +98,7 @@ def get_tasks_from_details(commit): def get_kpi_details(self): '''Transfrom a mongodb kpi record from lists to a python dict.''' - task_info = self.query_a_task_info() + task_info = self.get_task_info() kpi_infos = {} for kpi in task_info['kpis-keys']: kpiobj = KpiRecord(kpi) @@ -128,6 +122,10 @@ def get_kpi_info_by_key(self, task_info): for i in range(len(task_info['kpis-keys'])): if self.name == task_info['kpis-keys'][i]: break + def safe_get_fields(field): + if field in task_info: + return task_info[field] + return None #To keep the kpi datas in order, we should process the data one by one. kpi_vals = json.loads(task_info['kpis-values']) self.values = kpi_vals[i] @@ -135,9 +133,15 @@ def get_kpi_info_by_key(self, task_info): self.avg = '%.4f' % Kpi.dic.get(self.type).cal_kpi(data=kpi_vals[i]) infos = parse_infos(task_info['infos']) self.info = infos[self.name] - self.activeds = task_info['kpi-activeds'][i] - self.unit = task_info['kpi-unit-reprs'][i] - self.desc = task_info['kpi-descs'][i] + + activeds = safe_get_fields('kpi-activeds') + self.activeds = activeds[i] if activeds else True + + unit_reprs = safe_get_fields('kpi-unit-reprs') + self.unit = "(%s)" % unit_reprs[i] if unit_reprs else "" + + descs = safe_get_fields('kpi-descs') + self.desc = descs[i] if descs else "" return (self.values, self.type, self.avg, self.info, self.activeds, self.unit, self.desc) From 177c2b76007b43afe98ab10662f1e6ef04921159 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Thu, 7 Jun 2018 20:42:18 +0800 Subject: [PATCH 03/10] Update db.py --- db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index daff073..05850a0 100644 --- a/db.py +++ b/db.py @@ -31,9 +31,9 @@ def find_one(self, table, cond): ''' return self.table(table).find_one(cond) - def find_sections(self, table, cond, sections): + def find_sections(self, table, cond, sections, key): - return self.table(table).find(cond, sections) + return self.table(table).find(cond, sections).sort(key) def find(self, table, cond): ''' From 2e75c1720be6e40b8fc402bbf1e78f7988c3b649 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Thu, 7 Jun 2018 20:45:25 +0800 Subject: [PATCH 04/10] Update main.py --- web/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web/main.py b/web/main.py index 6f0372e..d0d725f 100644 --- a/web/main.py +++ b/web/main.py @@ -34,7 +34,7 @@ def index(): a list of commitids and their status(passed or not, the info) ''' page, snips = build_index_page() - commits = CommitRecord.query_all_commit_infos() + commits = CommitRecord.get_all() latest_commit = commits[-1].commit logics = merge_logics(snips[0].logic(), snips[1].logic(latest_commit)) print('commits', snips[0].logic()) @@ -42,7 +42,7 @@ def index(): @app.route('/commit/details', methods=["GET"]) -@cache.cached(timeout=120) +#@cache.cached(timeout=5) def commit_details(): commit = request.args.get('commit') @@ -53,10 +53,10 @@ def commit_details(): @app.route('/commit/compare', methods=["GET"]) -@cache.cached(timeout=120) +#@cache.cached(timeout=5) def commit_compare(): if 'cur' not in request.args: - commits = CommitRecord.query_all_commit_infos() + commits = CommitRecord.get_all() latest_commit = commits[-1] success_commits = [v for v in filter(lambda r: r.passed, commits)] latest_success_commit = success_commits[ From 98f23aa1b547c6e18de3a386d380da2ec151e717 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Thu, 7 Jun 2018 20:48:01 +0800 Subject: [PATCH 05/10] Update view.py --- web/view.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/web/view.py b/web/view.py index b17860d..83adbd4 100644 --- a/web/view.py +++ b/web/view.py @@ -166,7 +166,7 @@ def html(self): alert(c=VAL('kpi[3]')).set_danger() def logic(self, commitid): - task_kpis = TaskRecord.get_tasks_from_details(commitid) + task_kpis = TaskRecord.get_tasks(commitid) res = objdict(version=dict( commit=commitid, passed=tasks_success(task_kpis), @@ -205,7 +205,7 @@ def html(self): RawHtml('
') def logic(self): - records_ = CommitRecord.query_all_commit_infos() + records_ = CommitRecord.get_all() return {self.KEY('records'): records_} @@ -237,7 +237,7 @@ def html(self): Tag('span', VAL('commit.date')) def logic(self): - commits = CommitRecord.query_all_commit_infos() + commits = CommitRecord.get_all() return {self.KEY('commits'): [v for v in reversed(commits)], } @@ -292,15 +292,15 @@ def logic(self, cur_commit, base_commit): print('cur', cur_commit) print('base', base_commit) - cur_rcds = TaskRecord.get_tasks_from_details(cur_commit) - base_rcds = TaskRecord.get_tasks_from_details(base_commit) - + cur_rcds = TaskRecord.get_tasks(cur_commit) + base_rcds = TaskRecord.get_tasks(base_commit) res = [] for name in cur_rcds.keys(): cur_task = cur_rcds.get(name, None) + base_task = base_rcds.get(name, None) # if eithor do not have some task, skip it. - if not (cur_task or base_task): continue + if not (cur_task and base_task): continue record = objdict() res.append(record) @@ -309,7 +309,7 @@ def logic(self, cur_commit, base_commit): for kpi in cur_task.kpis.keys(): cur_kpi = cur_task.kpis.get(kpi, None) base_kpi = base_task.kpis.get(kpi, None) - if not (cur_kpi or base_kpi): continue + if not (cur_kpi and base_kpi): continue kpi_ = objdict() kpi_type = Kpi.dic.get(cur_kpi[1]) @@ -348,10 +348,11 @@ def html(self): def logic(self): # should be sorted by freshness - commits = CommitRecord.query_all_commit_infos() + commits = CommitRecord.get_all() kpis = {} - for commit in commits: - rcd = TaskRecord.get_tasks_from_details(commit.commit) + last_N_commit = commits[-20:-1] + [commits[-1]] + for commit in last_N_commit: + rcd = TaskRecord.get_tasks(commit.commit) if self.task_name not in rcd: continue for (kpi,val) in rcd[self.task_name].kpis.items(): kpis.setdefault(kpi+'--x', []).append(commit.shortcommit) @@ -387,4 +388,3 @@ def tasks_success(tasks): for task in tasks.values(): if not task['passed']: return False return True - From b1ad43a8bcc6ed67cdedf32a18043b9c5ab6b99e Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Thu, 7 Jun 2018 20:55:04 +0800 Subject: [PATCH 06/10] Update view.py --- web/view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/view.py b/web/view.py index 83adbd4..bac284b 100644 --- a/web/view.py +++ b/web/view.py @@ -350,7 +350,7 @@ def logic(self): # should be sorted by freshness commits = CommitRecord.get_all() kpis = {} - last_N_commit = commits[-20:-1] + [commits[-1]] + last_N_commit = commits[-self.N:-1] + [commits[-1]] for commit in last_N_commit: rcd = TaskRecord.get_tasks(commit.commit) if self.task_name not in rcd: continue From f6f2e2f15f8fe0c0dba8bccfad7b204a94614175 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Fri, 8 Jun 2018 09:10:32 +0800 Subject: [PATCH 07/10] Update api.py --- web/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/web/api.py b/web/api.py index 2750e10..e7a0bab 100644 --- a/web/api.py +++ b/web/api.py @@ -36,6 +36,7 @@ def get_all(): ''' Get all commit records, and sort by latest to oldest. returns: list of CommitRecord ''' + # sort by 'date' in ascending order commits = db.find_sections(config.table_name, {'type': 'kpi'}, {'commitid': 1, "_id": 0}, "date") commit_ids = [] From d96bb5c220f1572650a456a6428b8bc563e18d47 Mon Sep 17 00:00:00 2001 From: Paddle CI Date: Tue, 12 Jun 2018 16:30:29 +0800 Subject: [PATCH 08/10] adjust api struct to looks more clear --- web/api.py | 62 +++++++++++++++++++++++++++-------------------------- web/view.py | 8 +++---- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/web/api.py b/web/api.py index e7a0bab..e605098 100644 --- a/web/api.py +++ b/web/api.py @@ -47,7 +47,7 @@ def get_all(): records = [] for commit in commit_ids: commitobj = CommitRecord(commit) - tasks = commitobj.get_commit_record() + tasks = commitobj.__get_db_record() commitobj.commit = commit commitobj.shortcommit = commit[:7] commitobj.date = datetime.utcfromtimestamp(int(tasks[0]['date'])) + \ @@ -55,12 +55,27 @@ def get_all(): commitobj.passed = tasks_success(tasks) records.append(commitobj) - return records - def get_commit_record(self): - ''' get the corresponding TaskRecords. - returns: list of TaskRecord + @staticmethod + def get_tasks(commit): + ''' Get the task details belong to a commit. + returns: dict of TaskRecord + keys equal to task name, + values equal to TaskRecord ''' + record = CommitRecord(commit) + tasks = record.__get_db_record() + print (tasks) + res = objdict() + for task in tasks: + taskobj = TaskRecord(commit, task['task'], task['infos'], + task['passed']) + taskobj.kpis = taskobj.get_kpi_details() + res[taskobj.name] = taskobj + return res + + def __get_db_record(self): + ''' get the corresponding tasks from database. ''' return db.finds(config.table_name, {'type': 'kpi', @@ -77,35 +92,22 @@ def __init__(self, commit, name, infos, passed): self.passed = passed self.commitid = commit - def get_task_info(self): - ''' get the corresponding TaskRecord. - returns: dict of TaskRecord''' - return db.find_one(config.table_name, {'type': 'kpi', \ - 'commitid': self.commitid, 'task': self.name}) - - @staticmethod - def get_tasks(commit): - ''' Get the task details belong to a commit from the database. ''' - record = CommitRecord(commit) - tasks = record.get_commit_record() - print (tasks) - res = objdict() - for task in tasks: - taskobj = TaskRecord(commit, task['task'], task['infos'], - task['passed']) - taskobj.kpis = taskobj.get_kpi_details() - res[taskobj.name] = taskobj - return res - def get_kpi_details(self): - '''Transfrom a mongodb kpi record from lists to a python dict.''' - task_info = self.get_task_info() + '''Transfrom mongodb kpis record from lists to a python dict. + returns dict of KpiRecord + keys equal to kpi name, + values equal to KpiRecord''' + task_info = self.__get_db_record() kpi_infos = {} for kpi in task_info['kpis-keys']: kpiobj = KpiRecord(kpi) - kpi_infos[kpi] = kpiobj.get_kpi_info_by_key(task_info) + kpi_infos[kpi] = kpiobj.get_kpi_info(task_info) return kpi_infos + def __get_db_record(self): + ''' get the corresponding kpis from database''' + return db.find_one(config.table_name, {'type': 'kpi', \ + 'commitid': self.commitid, 'task': self.name}) class KpiRecord: def __init__(self, name): @@ -118,8 +120,8 @@ def __init__(self, name): self.unit = "" self.desc = "" - def get_kpi_info_by_key(self, task_info): - '''Get the kpi infos according to the kpi key''' + def get_kpi_info(self, task_info): + '''Get the kpi infos according to the kpi name''' for i in range(len(task_info['kpis-keys'])): if self.name == task_info['kpis-keys'][i]: break diff --git a/web/view.py b/web/view.py index bac284b..d5e5830 100644 --- a/web/view.py +++ b/web/view.py @@ -166,7 +166,7 @@ def html(self): alert(c=VAL('kpi[3]')).set_danger() def logic(self, commitid): - task_kpis = TaskRecord.get_tasks(commitid) + task_kpis = CommitRecord.get_tasks(commitid) res = objdict(version=dict( commit=commitid, passed=tasks_success(task_kpis), @@ -292,8 +292,8 @@ def logic(self, cur_commit, base_commit): print('cur', cur_commit) print('base', base_commit) - cur_rcds = TaskRecord.get_tasks(cur_commit) - base_rcds = TaskRecord.get_tasks(base_commit) + cur_rcds = CommitRecord.get_tasks(cur_commit) + base_rcds = CommitRecord.get_tasks(base_commit) res = [] for name in cur_rcds.keys(): cur_task = cur_rcds.get(name, None) @@ -352,7 +352,7 @@ def logic(self): kpis = {} last_N_commit = commits[-self.N:-1] + [commits[-1]] for commit in last_N_commit: - rcd = TaskRecord.get_tasks(commit.commit) + rcd = CommitRecord.get_tasks(commit.commit) if self.task_name not in rcd: continue for (kpi,val) in rcd[self.task_name].kpis.items(): kpis.setdefault(kpi+'--x', []).append(commit.shortcommit) From 933d2d6d47102ee440ba7003b47b0242cd052292 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Tue, 12 Jun 2018 16:45:35 +0800 Subject: [PATCH 09/10] Update api.py --- web/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web/api.py b/web/api.py index e605098..f1fdb04 100644 --- a/web/api.py +++ b/web/api.py @@ -61,8 +61,8 @@ def get_all(): def get_tasks(commit): ''' Get the task details belong to a commit. returns: dict of TaskRecord - keys equal to task name, - values equal to TaskRecord ''' + keys: task name, + values: TaskRecord ''' record = CommitRecord(commit) tasks = record.__get_db_record() print (tasks) @@ -95,8 +95,8 @@ def __init__(self, commit, name, infos, passed): def get_kpi_details(self): '''Transfrom mongodb kpis record from lists to a python dict. returns dict of KpiRecord - keys equal to kpi name, - values equal to KpiRecord''' + keys: kpi name, + values: KpiRecord''' task_info = self.__get_db_record() kpi_infos = {} for kpi in task_info['kpis-keys']: From d427f21f534ca92551869f10336ccec6d01687e8 Mon Sep 17 00:00:00 2001 From: guochaorong <32069604+guochaorong@users.noreply.github.com> Date: Tue, 12 Jun 2018 19:22:55 +0800 Subject: [PATCH 10/10] Update api.py --- web/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web/api.py b/web/api.py index f1fdb04..073c35e 100644 --- a/web/api.py +++ b/web/api.py @@ -70,7 +70,7 @@ def get_tasks(commit): for task in tasks: taskobj = TaskRecord(commit, task['task'], task['infos'], task['passed']) - taskobj.kpis = taskobj.get_kpi_details() + taskobj.kpis = taskobj.get_kpis() res[taskobj.name] = taskobj return res @@ -92,8 +92,8 @@ def __init__(self, commit, name, infos, passed): self.passed = passed self.commitid = commit - def get_kpi_details(self): - '''Transfrom mongodb kpis record from lists to a python dict. + def get_kpis(self): + '''Get kpis details belong to a task. returns dict of KpiRecord keys: kpi name, values: KpiRecord'''