Skip to content

Commit 9e5a925

Browse files
committed
Add log endpoint and refactor old functions
Add the new log endpoint and also refactor old manual WOQL queries to just use the log function. Closes #321
1 parent ccc3b00 commit 9e5a925

2 files changed

Lines changed: 70 additions & 61 deletions

File tree

terminusdb_client/client/Client.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,53 @@ def _check_connection(self, check_db=True) -> None:
391391
"No database is connected. Please either connect to a database or create a new database."
392392
)
393393

394+
def log(self,
395+
team: Optional[str] = None,
396+
db: Optional[str] = None,
397+
start: int = 0,
398+
count: int = -1):
399+
"""Get commit history of a database
400+
Parameters
401+
----------
402+
team: str, optional
403+
The team from which the database is. Defaults to the class property.
404+
db: str, optional
405+
The database. Defaults to the class property.
406+
start: int, optional
407+
Commit index to start from. Defaults to 0.
408+
count: int, optional
409+
Amount of commits to get. Defaults to -1 which gets all.
410+
411+
Returns
412+
-------
413+
list
414+
415+
List of the following commit objects:
416+
{
417+
"@id":"InitialCommit/hpl18q42dbnab4vzq8me4bg1xn8p2a0",
418+
"@type":"InitialCommit",
419+
"author":"system",
420+
"identifier":"hpl18q42dbnab4vzq8me4bg1xn8p2a0",
421+
"message":"create initial schema",
422+
"schema":"layer_data:Layer_4234adfe377fa9563a17ad764ac37f5dcb14de13668ea725ef0748248229a91b",
423+
"timestamp":1660919664.9129035
424+
}
425+
"""
426+
self._check_connection(check_db=(not team or not db))
427+
team = team if team else self.team
428+
db = db if db else self.db
429+
result = requests.get(
430+
f"{self.api}/log/{team}/{db}",
431+
params={'start': start, 'count': count},
432+
headers=self._default_headers,
433+
auth=self._auth(),
434+
)
435+
commits = json.loads(_finish_response(result))
436+
for commit in commits:
437+
commit['timestamp'] = datetime.fromtimestamp(commit['timestamp'])
438+
commit['commit'] = commit['identifier'] # For backwards compat.
439+
return commits
440+
394441
def get_commit_history(self, max_history: int = 500) -> list:
395442
"""Get the whole commit history.
396443
Commit history - Commit id, author of the commit, commit message and the commit time, in the current branch from the current commit, ordered backwards in time, will be returned in a dictionary in the follow format:
@@ -403,7 +450,7 @@ def get_commit_history(self, max_history: int = 500) -> list:
403450
Parameters
404451
----------
405452
max_history: int, optional
406-
maximum number of commit that would return, counting backwards from your current commit. Default is set to 500. It need to be nop-negitive, if input is 0 it will still give the last commit.
453+
maximum number of commit that would return, counting backwards from your current commit. Default is set to 500. It needs to be nop-negative, if input is 0 it will still give the last commit.
407454
408455
Example
409456
-------
@@ -429,70 +476,21 @@ def get_commit_history(self, max_history: int = 500) -> list:
429476
"""
430477
if max_history < 0:
431478
raise ValueError("max_history needs to be non-negative.")
432-
if max_history > 1:
433-
limit_history = max_history - 1
434-
else:
435-
limit_history = 1
436-
woql_query = (
437-
WOQLQuery()
438-
.using("_commits")
439-
.limit(limit_history)
440-
.triple("v:branch", "name", WOQLQuery().string(self.branch))
441-
.triple("v:branch", "head", "v:commit")
442-
.path("v:commit", "parent*", "v:target_commit")
443-
.triple("v:target_commit", "identifier", "v:cid")
444-
.triple("v:target_commit", "author", "v:author")
445-
.triple("v:target_commit", "message", "v:message")
446-
.triple("v:target_commit", "timestamp", "v:timestamp")
447-
)
448-
result = self.query(woql_query).get("bindings")
449-
if not result:
450-
return result
451-
else:
452-
result_list = []
453-
for result_item in result:
454-
result_list.append(
455-
{
456-
"commit": result_item["cid"]["@value"],
457-
"author": result_item["author"]["@value"],
458-
"message": result_item["message"]["@value"],
459-
"timestamp": datetime.fromtimestamp(
460-
int(result_item["timestamp"]["@value"])
461-
),
462-
}
463-
)
464-
return result_list
479+
return self.log(count=max_history)
465480

466481
def _get_current_commit(self):
467-
woql_query = (
468-
WOQLQuery()
469-
.using("_commits")
470-
.triple("v:branch", "name", WOQLQuery().string(self.branch))
471-
.triple("v:branch", "head", "v:commit")
472-
.triple("v:commit", "identifier", "v:cid")
473-
)
474-
result = self.query(woql_query)
475-
if not result:
476-
return None
477-
current_commit = result.get("bindings")[0].get("cid").get("@value")
478-
return current_commit
482+
descriptor = self.db
483+
if self.branch:
484+
descriptor = f'{descriptor}/local/branch/{self.branch}'
485+
commit = self.log(team=self.team, db=descriptor, count=1)[0]
486+
return commit['identifier']
479487

480488
def _get_target_commit(self, step):
481-
woql_query = (
482-
WOQLQuery()
483-
.using("_commits")
484-
.path(
485-
"v:commit",
486-
f"parent{{{step},{step}}}",
487-
"v:target_commit",
488-
)
489-
.triple("v:branch", "name", WOQLQuery().string(self.branch))
490-
.triple("v:branch", "head", "v:commit")
491-
.triple("v:target_commit", "identifier", "v:cid")
492-
)
493-
result = self.query(woql_query)
494-
target_commit = result.get("bindings")[0].get("cid").get("@value")
495-
return target_commit
489+
descriptor = self.db
490+
if self.branch:
491+
descriptor = f'{descriptor}/local/branch/{self.branch}'
492+
commit = self.log(team=self.team, db=descriptor, count=1, start=step)[0]
493+
return commit['identifier']
496494

497495
def get_all_branches(self, get_data_version=False):
498496
"""Get all the branches available in the database."""

terminusdb_client/tests/integration_tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ def test_add_get_remove_org(docker_url):
110110
client.get_organization("testOrg")
111111

112112

113+
def test_log(docker_url):
114+
# create client
115+
client = Client(docker_url, user_agent=test_user_agent)
116+
# test create db
117+
client.connect()
118+
db_name = "testDB" + str(random())
119+
client.create_database(db_name, team="admin")
120+
log = client.log(team="admin", db=db_name)
121+
assert log[0]['@type'] == 'InitialCommit'
122+
123+
113124
def test_get_database(docker_url):
114125
client = Client(docker_url, user_agent=test_user_agent, team="admin")
115126
client.connect()

0 commit comments

Comments
 (0)