Skip to content

Commit 4ee771e

Browse files
Boyuan DengBoyuan Deng
authored andcommitted
implementing history.py file
1 parent 73301e9 commit 4ee771e

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

mltrace/db/store.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,35 @@ def get_history(
536536

537537
return history
538538

539+
def get_component_runs_by_index(
540+
self,
541+
component_name: str,
542+
front_idx: int,
543+
last_idx: int,
544+
) -> typing.List[ComponentRun]:
545+
"""Gets lineage for the component, or a history of all its runs."""
546+
547+
if front_idx < 0 or last_idx < 0:
548+
total_length = self.session.query(ComponentRun).count()
549+
if front_idx < 0:
550+
front_idx = front_idx + total_length
551+
if last_idx < 0:
552+
last_idx = last_idx + total_length
553+
554+
history = (
555+
self.session.query(ComponentRun)
556+
.filter(ComponentRun.component_name == component_name)
557+
.order_by(ComponentRun.start_timestamp)
558+
.offset(front_idx)
559+
.limit(last_idx - front_idx)
560+
.all()
561+
)
562+
return history
563+
564+
def get_component_runs_count(self, component_name: str):
565+
return self.session.query(ComponentRun).count()
566+
567+
539568
def get_components(self, tag: str = "", owner: str = ""):
540569
"""Returns a list of all the components associated with the specified
541570
owner and/or tags."""

mltrace/entities/base_component.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import inspect
1616
import git
1717
import mlflow
18-
18+
import history
1919

2020
class Component(Base):
2121
def __init__(
@@ -36,6 +36,8 @@ def __init__(
3636
self._tags = tags
3737
self._beforeTests = beforeTests
3838
self._afterTests = afterTests
39+
self._history = history.History(name)
40+
3941

4042
def beforeRun(self, **kwargs) -> dict:
4143
"""Computation to execute before running a component.

mltrace/entities/history.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
3+
from datetime import datetime
4+
5+
from mltrace.db import Store
6+
from mltrace import utils as clientUtils
7+
8+
9+
class History(object):
10+
def __init__(
11+
self,
12+
componentName,
13+
):
14+
# {component_run_id: component_run} sort by time
15+
self.component_name = componentName
16+
17+
# should I choose a ordered orderDict / list ?
18+
# self.history_component_runs = {}
19+
20+
def get_runs_by_time(
21+
self,
22+
start_time: datetime,
23+
end_time: datetime,
24+
):
25+
store = Store(clientUtils.get_db_uri())
26+
history_runs = store.get_history(self.component_name, None, start_time, end_time)
27+
# TODO: Client.py - convert to client facing component run object
28+
return str(history_runs)
29+
30+
def get_runs_by_index(
31+
self,
32+
front_idx: int,
33+
last_idx: int,
34+
):
35+
store = Store(clientUtils.get_db_uri())
36+
history_runs = store.get_component_runs_by_index(self.component_name, front_idx, last_idx)
37+
return str(history_runs)
38+
39+
# all lazy loading functions
40+
41+
def __getitem__(self, index):
42+
store = Store(clientUtils.get_db_uri())
43+
history_run = store.get_component_runs_by_index(self.component_name, index, index + 1)
44+
return str(history_run)
45+
46+
def __len__(self):
47+
store = Store(clientUtils.get_db_uri())
48+
return store.get_component_runs_count(self.component_name)

0 commit comments

Comments
 (0)