Fleet-state

Presentation

There are 3 main componets

1. app – main application.

Uses technologies:

  1. buf – for generating models and server (grpc, http).

    Do not require have grpc, but easy to work with generates model. Protobuf helpful for that.

  2. clickhouse – column db, use for saving time series data.

    Clickhouse was chosen because we need store time series data – vehicle coordinates can be helpful for monitoring.

  3. docker-compose – for build and run app.

API

  1. Add coordinates for vehicle (POST /v1/vehicle/{vin}/coordinates)
  2. Get speed for vehicle (GET /v1/vehicle/{vin}/speed)
  3. Stream speed (GET /vehicle/{vin}/stream)

Actually, I found that I required to have streaming right before I almost submitted task. So I did it easy way, and didn’t use grpc for it, since I was not sure that grpc streams could be transformed to http streaming.

Calculating speed

I select last coordinates withing 5 minutes, sorted by time of capture. Then I calculate speed between each consecutive point and find average.

To find speed between two points I used formula:

D = R * arccos[(sin(lat1) * sin(lat2)) + cos(lat1) * cos(lat2) * cos(long2 – long1)]

Where R = 6371 - is Earth radius

5 minutes time window was chosen almost randomly. This should probably be changed according to possible usage of this value. If we interest in average speed of vehicle – it should be increased, if we interested in current state – we should reduce this value. Given small time window we probably will get less accurate results, since real-life GPS receivers can get not very precise coordinates, some updates may be lost, timestamps of coordinate updates mey be shifted due to network instability and so on.

To improve results we probably should just get speed from GPS receivers, but also we may:

  • get timestamps from vehicles, not timestamps from server
  • try to detect “bad” results and exclude them from calculations. For example results that differ from other more than some percentage

simulator

Emulates sending a data vehicle coordinates This is a simple golang application consisting of a single file

client

Emulates receiving a data about speed one vehicle by stream Bash script

Build

docker-compose up --build
  • Launches application
  • Run database, executes migrations
  • Launches simulator
  • Launches client

Api

  • GRPC API
    • localhost:12202
  • HTTP API
    • localhost:8082

Endpoint description:

./app/api/fleet-state.proto

Storage

jdbc:clickhouse://localhost:8124/fleet_state

Add new endpoints

  1. Add new endpoint to ./app/api/fleet-state.proto
  2. Run buf generate
  3. Add handler to server to main.go

GitHub

View Github