Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Adjust db struct to make faster and clearer #59

Merged
merged 11 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,35 @@ 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'])) + \
timedelta(hours=8)

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',
Expand All @@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数是不是private 就可以了,用户需要显式调用吗

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

脚本外目前没有调用的。就是拿1个task 的 kpi infos。
returns dict of KpiRecord
keys equal to kpi name,
values equal to KpiRecord'''

现在在commitRecord 类里面调用了这个函数, 改个名字 get_kpis? 是不是可以暴露出去

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个 TaskRecord 应该直接包含多个 KpiRecord,这个接口应该不需要额外调,所以不一定需要

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯啊 这是个现有鸡还是先有蛋的问题, 如果他仅仅初始化了一个TaskRecord 对象, 还没调用任何方法, 这个对象还是空的。

现在在CommitRecord 这个类里面用到了TaskRecord 里面的这个方法, 如果写成私有的CommitRecord类还能调用到它么

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉改个更general的名字, 比如get_kpis, 然后作为public 方法看着还行?

'''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):
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions web/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down