# PIMP
## Backend
### Initialization
The code responsible for initializing the backend can be found in the [init.ts](backend/src/init.ts) and [api.ts](backend/src/api.ts) files.
### Endpoint Controllers
The code responsible for handling requests to the endpoints of the internal and external API can be found in the [node controller](backend/src/controller/node.controller.ts) and the [node-external controller](backend/src/controller/node-external.controller.ts) files, respectively.
### Database
The project uses a graph based database: [Neo4J](https://neo4j.com).
The choice of the graph database paradigm was motivated by the network-like structure of eletrical grid data and the need to have a flexible schema. Each node can store arbitrary information and may be connected to simpler aggretator nodes, called sinks.
To interact with the database, [neogma](https://github.com/themetalfleece/neogma) was used, although a very small amount of the package's functionality was used.
A singleton is used to connect to the database and the code can be found in the [conection.ts file](backend/src/connection.ts).
The database is also seeded using the code in the [setup.ts file](backend/src/setup.ts). The data entries for the assets come from the .csv file which was provided during an earlier phase of the project.
### Internal API
This API could be used for the communication between the front and back ends, e.g. fetch the information for the graphs in the frontend's dashboard.
SwaggerHub Documentation for this API can be found [here](https://app.swaggerhub.com/apis/zeus-gis/electrica-imperium-internal-api/1.0.0). The API routes are defined in the [node.ts file](./backend/src/routes/node.ts).
### External API
The purpose of this API is so external entities can insert data into de database (consequently, making it available to the frontend).
SwaggerHub Documentation for this API can be found [here](https://app.swaggerhub.com/apis/zeus-gis/DEUS.AI/1.0.0#/). The API routes are defined in the [node-external.ts file](./backend/src/routes/node-external.ts).
## Frontend
### Dashboard
The application has a dashboard graph that is able to display any type of time-series data. This is idealized to provide flexibility to the end user to be able to use what kind of data is more relevant to their use-case. For example, it may be useful to overlay a production dataset with a dataset containing weather information, to see influence over time.
The dashboard uses [echarts](https://echarts.apache.org/en/index.html) to draw the time-series graph. Since we're using react for the frontend, we used the [echarts-for-react project](https://echarts.apache.org/en/index.html).
The `Dashboard` component, defined in the [Dashboard.tsx file](./frontend/src/components/Dashboard.tsx), is responsible for holding, fetching, and passing the data to the dashboard graph. The `getData` method uses [axios-http](https://axios-http.com) to fetch information for the database (using the internal API). As of writing, this method only fetches energy production data (that is generated automatically). This data is used in the `render` method by the `Graph` component.
The [`Graph`](frontend/src/components/Graph.tsx) component is taken from [echarts](https://echarts.apache.org/en/index.html). It is configured to use and display time-series data. This function component takes the data to display as argument and the theme of the graph. More graph themes can be found [here](https://echarts.apache.org/en/download-theme.html). As of writing, the title text is hard-coded, but can easily be turned into an argument for the component.
The data is a 2D array of numbers.
### GIS
[deck.gl](https://deck.gl) and [nebula.gl](https://nebula.gl) were the libraries used for the GIS environment.
Mapbox was used as the map provider.
To use it, a key is needed.
### Mapbox + env file
There must be an **environemnt** file at `frontend/.env`. This file should follow the design of the [example env file](frontend/.env.example).
As of writing, the purpose of this file is solely to provide the token to access the [mapbox API](https://docs.mapbox.com/api/overview). The token is stored as the `REACT_APP_MAPBOX_ACCESS_TOKEN` variable in the frontend's environment. To obtain a token, one should register an account with [mapbox](https://www.mapbox.com) and follow the instructions. This API is pay-per-use after exausting the monthly free uses.