Skip to content

Latest commit

 

History

History
311 lines (229 loc) · 13.7 KB

backend.md

File metadata and controls

311 lines (229 loc) · 13.7 KB

Table of Contents

Backend

Requirements

  • Scalability
    • No. of messages the backend can handle per second
    • No. of dashboards the backend can serve simultaneously
  • Real-time updates
    • The backend should be able to publishe the location updates received from the drones to the dashboard in real time

Design

Responsibilities

  • Ingest - Ingesting position data sent by drones
  • Store - Storing position data
  • Process - Processing position data to get speeds for the respective drones and flagging stationary drones
  • Serve - API to serve real-time position (& speed) data to front-end

backend-components

Basis for the above Design

  1. The backend has been split into 3 components each handling one major responsibility. This is to achieve modularity and ease to extend the respective components based on future requirements

  2. The components are connected to each other via events. For instance the MemoryStore fires a drone-updated event when the location of a drone is updated. This is received by the Express API which updates the dashboard accordingly

  3. The separation of the UDP server (used to ingest data) from any kind of processing of the data allows to improve performance of data ingestion (number of messages received per second). This also helps reduce the number of lost data packets

  4. To serve real time updates to the dashboard socket.io module has been used which allows for real-time bidirectional communication

MemoryStore - Code Design and Implementation

memory-store

  1. The MemoryStore is divided into three key responsibilities

    1. Store position data
    2. Process position data to generate speed or flag stationary drones
    3. Interface to fetch data from MemoryStore in the format required by the Express API
  2. The sub-division across the three responsibilities allows to extend the code to satisfy future requirements with ease

    1. Other kinds of processing on the location data can be introduced by subscribing to the event location-updated
    2. The data from the MemoryStore can be formatted to serve other endpoints (in addition to the Dashboard). For this additional interfaces can be added by subscribing to the event drone-updated

API Documentation

MemoryStore

The MemoryStore is an in-memory storage

Kind: global class

new MemoryStore()

Create a MemoryStore

memoryStore.setPositionFromDroneMessage(message) ⇒ Object

This method takes a message from the UDP server and stores the position for that drone

Kind: instance method of MemoryStore
Returns: Object - Position object (with droneId)
Access: public

Param Type Description
message Buffer Message encapsulating location data received on the UDP server

memoryStore.getDataForDashboard() ⇒ Object

This method returns speed data for all the drones along with a flag to specify if they should be highlighted or not on the dashboard

Kind: instance method of MemoryStore
Returns: Object - Speed data for all the drones and their respective highlight flags
Access: public

memoryStore.getDataForDashboardById(droneId) ⇒ Object

This method returns the speed data and highlight flag for a specific drone given by the droneId

Kind: instance method of MemoryStore
Returns: Object - Speed data for a drone and its highlight flag
Access: public

Param Type
droneId String

memoryStore.getPositionById(droneID) ⇒ Array | null

Returns the position array for the provided droneId

Kind: instance method of MemoryStore
Returns: Array | null - An array of the droneId's positions or null if droneId is not found in MemoryStore
Access: private

Param Type
droneID String

memoryStore.getPosition() ⇒ Object | null

Returns the position data for all the droneIds

Kind: instance method of MemoryStore
Returns: Object | null - An object containing data for all the droneIds positions or null if MemoryStore is empty
Access: private

memoryStore.setPositionById(droneID, position) ℗

This method appends the position for the droneId in the MemoryStore

Kind: instance method of MemoryStore
Access: private

Param Type Description
droneID String DroneId
position Object Representing position data for the drone

memoryStore.validatePosition() ℗

To be implemented in the next version. This method is used to validate the position data - Latitude and Longitude

Kind: instance method of MemoryStore
Access: private

memoryStore.parsePosition(message) ⇒ Object

This method parses the message received on the UDP server into a JSON object representing position. If the received message does not fit the expected structure it is discarded

Kind: instance method of MemoryStore
Returns: Object - Position data
Access: private

Param Type Description
message String Concatenated string of droneId, latitude, longitude and timestamp

memoryStore.calculateSpeed(droneId) ⇒ Object

This method uses the two most recent positions of a drone to calculate its speed

Kind: instance method of MemoryStore
Returns: Object - Returns the speed for the drone
Access: private

Param Type Description
droneId String Unique identifier for drone

memoryStore.setSpeedById(droneId, speed) ℗

This method stores the speed for a drone in MemoryStore

Kind: instance method of MemoryStore
Access: private

Param Type
droneId String
speed Object

memoryStore.getSpeedById(droneId) ⇒ Object | null

This method is used to retrieve the speed for a drone

Kind: instance method of MemoryStore
Returns: Object | null - Speed data for a droneId
Access: private

Param Type
droneId String

memoryStore.getSpeed() ⇒ Object | null

This method is used to retrieve the speed data for all the drones

Kind: instance method of MemoryStore
Returns: Object | null - Speed data for all the drones
Access: private

memoryStore.onLocationUpdate(droneId) ℗

This method is triggered on event location-updated and calculates the speed for that drone using the two most recent positions and stores them in MemoryStore

Kind: instance method of MemoryStore
Access: private

Param Type
droneId String

memoryStore.flagStationaryDrones(droneId) ℗

This method is triggered on location-update event. It checks for and highlights stationary drones (Drones that have covered a displacement of less than 1m in 10 seconds). If a drone is highlighted the function triggers drone-updated event.

Kind: instance method of MemoryStore
Access: private

Param Type
droneId String

memoryStore.findPositionInInterval(droneId, timestamp, start, range) ⇒ Object

This method checks the position data for a drone and returns a position between an interval The interval is defined between (timestamp - start) and (timestamp - start- range)

Kind: instance method of MemoryStore
Returns: Object - Position data
Access: private

Param Type Description
droneId String
timestamp number In milliseconds
start number In milliseconds
range number In milliseconds

memoryStore.setDroneFlag(droneId) ℗

This method sets the highlight flag for the given droneId to true

Kind: instance method of MemoryStore
Access: private

Param Type
droneId String

memoryStore.unsetDroneFlag(droneId) ℗

This methods sets the highlight flag for the given droneId to false

Kind: instance method of MemoryStore
Access: private

Param Type
droneId String