Skip to content
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
Prev Previous commit
Next Next commit
fix: Update README and test time_tracker_events #101
  • Loading branch information
EliuX committed Apr 30, 2020
commit 6d887ea63d4d2ff21e02dfe196fc624e53a9e81d
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Be sure you have installed in your system
- [Python version 3](https://www.python.org/download/releases/3.0/) in your path. It will install
automatically [pip](https://pip.pypa.io/en/stable/) as well.
- A virtual environment, namely [venv](https://docs.python.org/3/library/venv.html).
- 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).

### Setup
- Create and activate the environment,
Expand All @@ -38,7 +39,7 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
python3 -m pip install -r requirements/<app>/<stage>.txt
```

Where <app> is one of the executable app namespace, e.g. `time_tracker_api`.
Where <app> is one of the executable app namespace, e.g. `time_tracker_api` or `time_tracker_events`.
The `stage` can be

* `dev`: Used for working locally
Expand Down Expand Up @@ -66,6 +67,40 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
- Open `http://127.0.0.1:5000/` in a browser. You will find in the presented UI
a link to the swagger.json with the definition of the api.

#### Handling Cosmos DB triggers for creating events with time_tracker_events
The project `time_tracker_events` is an Azure Function project. Its main responsibility is to respond to calls related to
events, like those [triggered by Change Feed](https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed-functions).
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)
these functions will be called. You can also run them in your local machine:

- You must have the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest)
and the [Azure Functions Core Tools](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash)
installed in your local machine.
- Be sure to [authenticate](https://docs.microsoft.com/en-us/cli/azure/authenticate-azure-cli?view=azure-cli-latest)
with the Azure CLI if you are not.
```bash
az login
```
- Execute the project
```bash
cd time_tracker_events
source run.sh
```
You will see that a large console log will appear ending with a message like
```log
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
```
- Now you are ready to start generating events. Just execute any change in your API and you will see how logs are being
generated by the console app you ran before. For instance, this is the log generated when I restarted a time entry:
```log
[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)
[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
[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
[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": {}}
[04/30/2020 14:42:12] Executed 'Functions.handle_time_entry_events_trigger' (Succeeded, Id=3da87e53-0434-4ff2-8db3-f7c051ccf9fd)
```

### Security
In this API we are requiring authenticated users using JWT. To do so, we are using the library
[PyJWT](https://pypi.org/project/PyJWT/), so in every request to the API we expect a header `Authorization` with a format
Expand Down Expand Up @@ -254,6 +289,8 @@ the win.
- [Swagger](https://swagger.io/) for documentation and standardization, taking into account the
[API import restrictions and known issues](https://docs.microsoft.com/en-us/azure/api-management/api-management-api-import-restrictions)
in Azure.
- [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)
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).

## License

Expand Down
2 changes: 1 addition & 1 deletion commons/data_access_layer/events_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from azure.cosmos import PartitionKey

container_definition = {
container_definition = { # pragma: no cover
'id': 'event',
'partition_key': PartitionKey(path='/tenant_id')
}
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ branch = True
source =
time_tracker_api
commons
time_tracker_events
omit =
time_tracker_events/handle_*_events_trigger/*

[report]
exclude_lines =
Expand Down
Empty file.
49 changes: 49 additions & 0 deletions tests/time_tracker_events/test_handle_events_trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import azure.functions as func
from faker import Faker

from time_tracker_events.handle_events_trigger import main as main_handler

fake = Faker()


class OutImpl(func.Out):
def __init__(self):
self.val = None

def set(self, val: func.DocumentList) -> None:
self.val = val

def get(self) -> func.DocumentList:
return self.val


def generate_sample_document(has_event_ctx=True):
result = {
"id": fake.uuid4(),
"tenant_id": fake.uuid4(),
}

if has_event_ctx:
result["_last_event_ctx"] = {
"user_id": fake.name(),
"action": "update",
"description": fake.paragraph(),
"container_id": fake.uuid4(),
"session_id": fake.uuid4(),
"tenant_id": result["tenant_id"],
}

return result


def test_main_handler_should_generate_events_if_hidden_attrib_is_found():
out = OutImpl()
documents = func.DocumentList()
for i in range(10):
documents.append(func.Document.from_dict(generate_sample_document()))
for i in range(5):
documents.append(func.Document.from_dict(generate_sample_document(False)))

main_handler(documents, out)

assert len(out.get()) == 10