The Field Device Data Management API is a comprehensive solution for recording, storing, and retrieving data from field devices over the internet. This API provides seamless data management capabilities to efficiently manage and monitor field device readings.
- Elixir (v1.14 or later)
- Phoenix Framework (v1.6 or later)
- Clone the project: https://github.com/Thomasmclean993/device_manager.git
- Setup the project:
- cd
device_manager - Run
mix setupto install and setup dependencies - Start Phoenix endpoint with
mix phx.serveror inside IEx withiex -S mix phx.server
- Now you can visit
localhost:4000from your browser.
The API enables users to upload reading data collected from field devices, encompassing measurements like counts, timestamps, and pertinent parameters.
Users can retrieve the recorded data for a specific field device by passing its unique ID. The API responds with the complete set of readings associated with the device, offering insights into historical trends and performance metrics.
This endpoint accepts reading data from field devices and stores it. The data must include the device ID, counts, timestamps, and any additional relevant information.
- REQUIRED FIELDS: ID, READINGS, COUNT, TIMESTAMP
- Readings fields are required but can by out of order
- Duplicate data for a specific field device will not be recorded
- Incomplete or malformed data will result in an error
curl --location --request POST 'localhost:4000/api/store' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "36d5658a-6908-479e-887e-a949ec199272",
"readings": [{
"timestamp": "2021-09-29T16:08:15+01:00",
"count": 2
}, {
"timestamp": "2021-09-29T16:09:15+01:00",
"count": 15
}]
}'
This endpoint retrieves the stored readings for a specific device identified by its ID. The API responds with a collection of readings, including counts, timestamps, and other relevant information
- A device id that is not found in the system will result in a not found error
curl --location --request GET 'localhost:4000/api/show?id=36d5658a-6908-479e-887e-a949ec199272' \
--header 'Content-Type: application/json' \
--header 'content_type: application/json' \
--data '{
"id": "36d5658a-6908-479e-887e-a949ec199272",
"readings": [{
"timestamp": "2021-09-29T16:08:17+01:00",
"count": 15
},
{
"timestamp": "2021-09-29T16:08:15+01:00",
"count": 2
} ,{
"timestamp": "2021-09-29T16:09:15+01:00",
"count": 15
}]
}'
- Duplication Logic Issue: I faced an initial challenge with the GenServer not performing as expected while attempting to prevent the storage of devices with duplicate data. Gen servers handle concurrency, and when dealing with operations that involve checking and modifying state, you need to be careful with managing these operations to avoid race conditions and unexpected behavior. In my case, the reset state logic wasn't handling ongoing processes correctly, leading to tests failing. Once I fixed the runaway processes, this ensured the proper termination and cleanup of processes within the GenServer.
- Required Fields Solution: My initial implementation for the required fields was a massive file of function after function calls, pattern matching, and the works. Headache to test and took much time. In short, it was a smell. So I decided to leverage the changeset logic in the Phoenix framework. Relying on this reliable framework significantly simplified my codebase and improved maintainability.
I would refactor the Genserver:
- Load testing
- Resource management: State management, Long running processes, Better Messaging
- Utilize more async operations: Genstagers that create task pipelines, Async callbacks, and task supervisor, that could off load tasks that does need to be executed in a synchronous manner.
- Test Coverage can be improved
- Naming Conventions
- Better Error Messages (etc, request structure is malformed, sent a error message explaining that)
- Adding typespecs
- Official website: https://www.phoenixframework.org/
- Guides: https://hexdocs.pm/phoenix/overview.html
- Docs: https://hexdocs.pm/phoenix
- Forum: https://elixirforum.com/c/phoenix-forum
- Source: https://github.com/phoenixframework/phoenix