|
1 |
| -# How to Find an Object State |
2 |
| - |
3 |
| -> **On this page:** A how-to guide to find an object state at a given time in Python. |
4 |
| -
|
5 |
| -## Steps |
6 |
| - |
7 |
| -[Step 1. Connect to a database](#step-1-connect-to-a-server) |
8 |
| - |
9 |
| -[Step 2. Get the intial commit](#step-2-get-the-initial-commit) |
10 |
| - |
11 |
| -[Step 3. Get the subsequent commit](#step-3-get-the-subsequent-commit) |
12 |
| - |
13 |
| -[Step 4. Get the commit identifier](#step-4-get-the-commit-identifier) |
14 |
| - |
15 |
| -[Step 5. Get the commit object state](#step-5-get-the-commit-object) |
16 |
| - |
17 |
| -### Step 1. Connect to a database |
18 |
| - |
19 |
| -<!-- doc-issue |
20 |
| -Connect to a server |
21 |
| -Given you have a database, (try downloading Seshat from TerminusdbHub) you can find the object state at a time in the following way: --> |
22 |
| - |
23 |
| -Connect to an existing database with multiple commit activity. Refer to the [Create a simple database](/how-to/how-to-create-simple-database) How-to guide for creating a database containing multiple commits if required. |
24 |
| - |
25 |
| -#### Code: Connect to an existing database |
26 |
| - |
27 |
| -```python |
28 |
| -import os |
29 |
| -import time |
30 |
| -import datetime |
31 |
| -from terminusdb_client import WOQLClient |
32 |
| -from terminusdb_client import WOQLQuery as WOQL |
33 |
| - |
34 |
| -server_url = "https://127.0.0.1:6363" |
35 |
| -db = "seshat" |
36 |
| -user = "admin" |
37 |
| -account = "admin" |
38 |
| -key = "root" |
39 |
| -client = WOQLClient(server_url) |
40 |
| - |
41 |
| -client.connect(user=user, account=account, key=key, db=db) |
42 |
| -``` |
43 |
| - |
44 |
| -### Step 2. Get the initial commit |
45 |
| - |
46 |
| -Get the `timestamp` of the last commit **before** the date specified in `date_string`, using the `timetuple()` method. |
47 |
| - |
48 |
| -#### Code: Get the timestamp of a commit |
49 |
| - |
50 |
| -```python |
51 |
| -date_string = "14/10/2020" |
52 |
| -timestamp = time.mktime(datetime.datetime.strptime(date_string, "%d/%m/%Y").timetuple()) |
53 |
| -``` |
54 |
| - |
55 |
| -### Step 3. Get the subsequent commit |
56 |
| - |
57 |
| -Get the **next** commit after the `timestamp` retrieved in [step 2](#step-2-get-the-initial-commit) and store commit data in `commit_query`. |
58 |
| - |
59 |
| -#### Code: Get the details of the subsequent commit |
60 |
| - |
61 |
| -```python |
62 |
| -commit_query = WOQL().using("admin/seshat/local/_commits", |
63 |
| - WOQL().limit(1, |
64 |
| - WOQL.woql_and( |
65 |
| - WOQL().triple("v:Branch", "ref:branch_name", "v2"), |
66 |
| - WOQL().triple("v:Branch", "ref:ref_commit", " v:Head"), |
67 |
| - WOQL().path("v:Head", "ref:commit_parent+", "v:Tail", "v:Path"), |
68 |
| - WOQL().triple("v:Tail", "ref:commit_id", "v:TailID"), |
69 |
| - WOQL().triple("v:Tail", "ref:commit_timestamp", "v:TimeStamp"), |
70 |
| - WOQL().greater(timestamp, "v:TimeStamp"), |
71 |
| -))) |
72 |
| -``` |
73 |
| - |
74 |
| -### Step 4. Get the commit identifier |
75 |
| - |
76 |
| -Get the commit identifier into variable `commit_id` after running the `commit_query` defined in [step 3](#step-3-get-the-subsequent-commit). |
77 |
| - |
78 |
| -<!-- docs-issue: Is TailID the same as the commit_id? --> |
79 |
| - |
80 |
| -#### Code: Get the commit id |
81 |
| - |
82 |
| -```python |
83 |
| -results = client.query(commit_query) |
84 |
| -commit_id = results['bindings'][0]['TailID']['@value'] |
85 |
| -``` |
86 |
| - |
87 |
| -### Step 5. Get the commit object state |
88 |
| - |
89 |
| -Get the commit object associated with the `commit_id` obtained in [step 4](#get-the-commit-identifier). Note the full `path` of the `commit_id`. |
90 |
| - |
91 |
| -#### Code: Get the commit object state |
92 |
| - |
93 |
| -```python |
94 |
| -path = f"admin/seshat/local/commit/{commit_id}" |
95 |
| - |
96 |
| -object_query = WOQL().using(path, |
97 |
| - WOQL().read_object("terminusdb:///data/afghazn", "v:Document")) |
98 |
| - |
99 |
| -results = client.query(object_query) |
100 |
| - |
101 |
| -print(results) |
102 |
| -``` |
| 1 | +# Find an Object State |
| 2 | + |
| 3 | +> **On this page:** A how-to guide to find an object state at a given time in Python. |
| 4 | +
|
| 5 | +## Steps |
| 6 | + |
| 7 | +[Step 1. Connect to a database](find-object-state.md#step-1-connect-to-a-server) |
| 8 | + |
| 9 | +[Step 2. Get the initial commit](find-object-state.md#step-2-get-the-initial-commit) |
| 10 | + |
| 11 | +[Step 3. Get the subsequent commit](find-object-state.md#step-3-get-the-subsequent-commit) |
| 12 | + |
| 13 | +[Step 4. Get the commit identifier](find-object-state.md#step-4-get-the-commit-identifier) |
| 14 | + |
| 15 | +[Step 5. Get the commit object state](find-object-state.md#step-5-get-the-commit-object) |
| 16 | + |
| 17 | +### Step 1. Connect to a database |
| 18 | + |
| 19 | +Connect to an existing database with multiple commit activities. Refer to the [Create a simple database](../../../how-to/how-to-create-simple-database/) How-to guide for creating a database containing multiple commits if required. |
| 20 | + |
| 21 | +#### Code: Connect to an existing database |
| 22 | + |
| 23 | +```python |
| 24 | +import os |
| 25 | +import time |
| 26 | +import datetime |
| 27 | +from terminusdb_client import WOQLClient |
| 28 | +from terminusdb_client import WOQLQuery as WOQL |
| 29 | + |
| 30 | +server_url = "https://127.0.0.1:6363" |
| 31 | +db = "seshat" |
| 32 | +user = "admin" |
| 33 | +account = "admin" |
| 34 | +key = "root" |
| 35 | +client = WOQLClient(server_url) |
| 36 | + |
| 37 | +client.connect(user=user, account=account, key=key, db=db) |
| 38 | +``` |
| 39 | + |
| 40 | +### Step 2. Get the initial commit |
| 41 | + |
| 42 | +Get the `timestamp` of the last commit **before** the date specified in `date_string`, using the `timetuple()` method. |
| 43 | + |
| 44 | +#### Code: Get the timestamp of a commit |
| 45 | + |
| 46 | +```python |
| 47 | +date_string = "14/10/2020" |
| 48 | +timestamp = time.mktime(datetime.datetime.strptime(date_string, "%d/%m/%Y").timetuple()) |
| 49 | +``` |
| 50 | + |
| 51 | +### Step 3. Get the subsequent commit |
| 52 | + |
| 53 | +Get the **next** commit after the `timestamp` retrieved in [step 2](find-object-state.md#step-2-get-the-initial-commit) and store commit data in `commit_query`. |
| 54 | + |
| 55 | +#### Code: Get the details of the subsequent commit |
| 56 | + |
| 57 | +```python |
| 58 | +commit_query = WOQL().using("admin/seshat/local/_commits", |
| 59 | + WOQL().limit(1, |
| 60 | + WOQL.woql_and( |
| 61 | + WOQL().triple("v:Branch", "ref:branch_name", "v2"), |
| 62 | + WOQL().triple("v:Branch", "ref:ref_commit", " v:Head"), |
| 63 | + WOQL().path("v:Head", "ref:commit_parent+", "v:Tail", "v:Path"), |
| 64 | + WOQL().triple("v:Tail", "ref:commit_id", "v:TailID"), |
| 65 | + WOQL().triple("v:Tail", "ref:commit_timestamp", "v:TimeStamp"), |
| 66 | + WOQL().greater(timestamp, "v:TimeStamp"), |
| 67 | +))) |
| 68 | +``` |
| 69 | + |
| 70 | +### Step 4. Get the commit identifier |
| 71 | + |
| 72 | +Get the commit identifier into variable `commit_id` after running the `commit_query` defined in [step 3](find-object-state.md#step-3-get-the-subsequent-commit). |
| 73 | + |
| 74 | +#### Code: Get the commit id |
| 75 | + |
| 76 | +```python |
| 77 | +results = client.query(commit_query) |
| 78 | +commit_id = results['bindings'][0]['TailID']['@value'] |
| 79 | +``` |
| 80 | + |
| 81 | +### Step 5. Get the commit object state |
| 82 | + |
| 83 | +Get the commit object associated with the `commit_id` obtained in [step 4](find-object-state.md#get-the-commit-identifier). Note the full `path` of the `commit_id`. |
| 84 | + |
| 85 | +#### Code: Get the commit object state |
| 86 | + |
| 87 | +```python |
| 88 | +path = f"admin/seshat/local/commit/{commit_id}" |
| 89 | + |
| 90 | +object_query = WOQL().using(path, |
| 91 | + WOQL().read_object("terminusdb:///data/afghazn", "v:Document")) |
| 92 | + |
| 93 | +results = client.query(object_query) |
| 94 | + |
| 95 | +print(results) |
| 96 | +``` |
0 commit comments