Skip to content

Commit 6d887ea

Browse files
author
EliuX
committed
fix: Update README and test time_tracker_events #101
1 parent 2759f0b commit 6d887ea

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Be sure you have installed in your system
1616
- [Python version 3](https://www.python.org/download/releases/3.0/) in your path. It will install
1717
automatically [pip](https://pip.pypa.io/en/stable/) as well.
1818
- A virtual environment, namely [venv](https://docs.python.org/3/library/venv.html).
19+
- Optionally for running Azure functions locally: [Azure functions core tool](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash).
1920

2021
### Setup
2122
- Create and activate the environment,
@@ -38,7 +39,7 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
3839
python3 -m pip install -r requirements/<app>/<stage>.txt
3940
```
4041
41-
Where <app> is one of the executable app namespace, e.g. `time_tracker_api`.
42+
Where <app> is one of the executable app namespace, e.g. `time_tracker_api` or `time_tracker_events`.
4243
The `stage` can be
4344
4445
* `dev`: Used for working locally
@@ -66,6 +67,40 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
6667
- Open `http://127.0.0.1:5000/` in a browser. You will find in the presented UI
6768
a link to the swagger.json with the definition of the api.
6869
70+
#### Handling Cosmos DB triggers for creating events with time_tracker_events
71+
The project `time_tracker_events` is an Azure Function project. Its main responsibility is to respond to calls related to
72+
events, like those [triggered by Change Feed](https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed-functions).
73+
Every time a write action (`create`, `update`, `soft-delete`) is done by CosmosDB, thanks to [bindings](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb?toc=%2Fazure%2Fcosmos-db%2Ftoc.json&bc=%2Fazure%2Fcosmos-db%2Fbreadcrumb%2Ftoc.json&tabs=csharp)
74+
these functions will be called. You can also run them in your local machine:
75+
76+
- You must have the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest)
77+
and the [Azure Functions Core Tools](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash)
78+
installed in your local machine.
79+
- Be sure to [authenticate](https://docs.microsoft.com/en-us/cli/azure/authenticate-azure-cli?view=azure-cli-latest)
80+
with the Azure CLI if you are not.
81+
```bash
82+
az login
83+
```
84+
- Execute the project
85+
```bash
86+
cd time_tracker_events
87+
source run.sh
88+
```
89+
You will see that a large console log will appear ending with a message like
90+
```log
91+
Now listening on: http://0.0.0.0:7071
92+
Application started. Press Ctrl+C to shut down.
93+
```
94+
- Now you are ready to start generating events. Just execute any change in your API and you will see how logs are being
95+
generated by the console app you ran before. For instance, this is the log generated when I restarted a time entry:
96+
```log
97+
[04/30/2020 14:42:12] Executing 'Functions.handle_time_entry_events_trigger' (Reason='New changes on collection time_entry at 2020-04-30T14:42:12.1465310Z', Id=3da87e53-0434-4ff2-8db3-f7c051ccf9fd)
98+
[04/30/2020 14:42:12] INFO: Received FunctionInvocationRequest, request ID: 578e5067-b0c0-42b5-a1a4-aac858ea57c0, function ID: c8ac3c4c-fefd-4db9-921e-661b9010a4d9, invocation ID: 3da87e53-0434-4ff2-8db3-f7c051ccf9fd
99+
[04/30/2020 14:42:12] INFO: Successfully processed FunctionInvocationRequest, request ID: 578e5067-b0c0-42b5-a1a4-aac858ea57c0, function ID: c8ac3c4c-fefd-4db9-921e-661b9010a4d9, invocation ID: 3da87e53-0434-4ff2-8db3-f7c051ccf9fd
100+
[04/30/2020 14:42:12] {"id": "9ac108ff-c24d-481e-9c61-b8a3a0737ee8", "project_id": "c2e090fb-ae8b-4f33-a9b8-2052d67d916b", "start_date": "2020-04-28T15:20:36.006Z", "tenant_id": "cc925a5d-9644-4a4f-8d99-0bee49aadd05", "owner_id": "709715c1-6d96-4ecc-a951-b628f2e7d89c", "end_date": null, "_last_event_ctx": {"user_id": "709715c1-6d96-4ecc-a951-b628f2e7d89c", "tenant_id": "cc925a5d-9644-4a4f-8d99-0bee49aadd05", "action": "update", "description": "Restart time entry", "container_id": "time_entry", "session_id": null}, "description": "Changing my description for testing Change Feed", "_metadata": {}}
101+
[04/30/2020 14:42:12] Executed 'Functions.handle_time_entry_events_trigger' (Succeeded, Id=3da87e53-0434-4ff2-8db3-f7c051ccf9fd)
102+
```
103+
69104
### Security
70105
In this API we are requiring authenticated users using JWT. To do so, we are using the library
71106
[PyJWT](https://pypi.org/project/PyJWT/), so in every request to the API we expect a header `Authorization` with a format
@@ -254,6 +289,8 @@ the win.
254289
- [Swagger](https://swagger.io/) for documentation and standardization, taking into account the
255290
[API import restrictions and known issues](https://docs.microsoft.com/en-us/azure/api-management/api-management-api-import-restrictions)
256291
in Azure.
292+
- [Azure Functions bindings](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb?toc=%2Fazure%2Fcosmos-db%2Ftoc.json&bc=%2Fazure%2Fcosmos-db%2Fbreadcrumb%2Ftoc.json&tabs=csharp)
293+
for making `time_tracker_events` to handle the triggers [generated by our Cosmos DB database throw Change Feed](https://docs.microsoft.com/bs-latn-ba/azure/cosmos-db/change-feed-functions).
257294

258295
## License
259296

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from azure.cosmos import PartitionKey
22

3-
container_definition = {
3+
container_definition = { # pragma: no cover
44
'id': 'event',
55
'partition_key': PartitionKey(path='/tenant_id')
66
}

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ branch = True
1212
source =
1313
time_tracker_api
1414
commons
15+
time_tracker_events
16+
omit =
17+
time_tracker_events/handle_*_events_trigger/*
1518

1619
[report]
1720
exclude_lines =

tests/time_tracker_events/__init__.py

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import azure.functions as func
2+
from faker import Faker
3+
4+
from time_tracker_events.handle_events_trigger import main as main_handler
5+
6+
fake = Faker()
7+
8+
9+
class OutImpl(func.Out):
10+
def __init__(self):
11+
self.val = None
12+
13+
def set(self, val: func.DocumentList) -> None:
14+
self.val = val
15+
16+
def get(self) -> func.DocumentList:
17+
return self.val
18+
19+
20+
def generate_sample_document(has_event_ctx=True):
21+
result = {
22+
"id": fake.uuid4(),
23+
"tenant_id": fake.uuid4(),
24+
}
25+
26+
if has_event_ctx:
27+
result["_last_event_ctx"] = {
28+
"user_id": fake.name(),
29+
"action": "update",
30+
"description": fake.paragraph(),
31+
"container_id": fake.uuid4(),
32+
"session_id": fake.uuid4(),
33+
"tenant_id": result["tenant_id"],
34+
}
35+
36+
return result
37+
38+
39+
def test_main_handler_should_generate_events_if_hidden_attrib_is_found():
40+
out = OutImpl()
41+
documents = func.DocumentList()
42+
for i in range(10):
43+
documents.append(func.Document.from_dict(generate_sample_document()))
44+
for i in range(5):
45+
documents.append(func.Document.from_dict(generate_sample_document(False)))
46+
47+
main_handler(documents, out)
48+
49+
assert len(out.get()) == 10

0 commit comments

Comments
 (0)