Skip to content

mdiener87/safely-you

Repository files navigation

SafelyYou Fleet Monitor

A lightweight Go API for monitoring device uptime and upload performance — built for the SafelyYou Fleet Monitoring coding challenge.


Overview

This service simulates how SafelyYou tracks device connectivity and upload latency at customer sites.
Each device periodically sends heartbeat and upload stats telemetry to the API, which then calculates:

  • Uptime (%) — percentage of minutes the device reported a heartbeat
  • Average Upload Time — mean duration for uploads, formatted as a time string (e.g. 3m21.8s)

The implementation adheres to the provided OpenAPI contract (openapi.json) and supports concurrent updates across multiple devices.


Project Structure

├── Dockerfile                       # Multi-stage build for Go binary
├── docker-compose.yml               # Compose config for local orchestration
├── Makefile                         # Developer convenience commands
├── main.go                          # App entrypoint + route handlers
├── device_test.go                   # Unit tests for uptime & averages
├── devices.csv                      # Device list loaded on startup
├── openapi.json                     # API spec provided by challenge
├── results.txt                      # Simulator output for verification
├── device-simulator-linux-amd64     # Provided test utility (excluded from build)
└── .dockerignore                    # Ignore files for faster builds

API Endpoints

Method Endpoint Description
POST /api/v1/devices/{device_id}/heartbeat Record a heartbeat timestamp
POST /api/v1/devices/{device_id}/stats Record upload stats (in ns)
GET /api/v1/devices/{device_id}/stats Retrieve uptime and average upload time

Running Locally

Option 1 — Using Make

make run

Option 2 — Native Go

go run main.go

Option 3 — Using Docker

docker build -t safelyyou-fleet .
docker run -p 6733:6733 safelyyou-fleet

Option 4 — Using Docker Compose

docker compose up

The service will be available at http://localhost:6733

Unit Tests

Unit tests are included for the uptime % and mean upload time calculation functions

Test Execution

make test

Device Simulator

To verify the service against the official simulator:

Simulator Execution

make simulate

Expected Output

############ RESULTS #################
[device-simulator] 2025/10/19 18:56:28
DeviceID: 38-4e-73-e0-33-59
        Uptime
                Expected: 99.79167
                Actual: 99.58420

        AvgUploadTime
                Expected: 3m29.226522788s
                Actual: 3m29.226522788s

Minor rounding differences are expected and acceptable per spec.

Implementation Notes

  • Uses Go 1.23’s standard library only (no external deps).
  • Thread-safe per-device state with sync.Mutex.
  • Graceful shutdown using context and os/signal.
  • Fast, stateless, in-memory computation.

Makefile Commands

  • make run Run app locally
  • make build Build binary into bin/
  • make test Run unit tests
  • make tidy Clean Go modules
  • make clean Remove build artifacts
  • make simulate Run device simulator

Docker Details

Dockerfile

  • Multi-stage build (builder → runtime)
  • Produces a minimal Alpine-based image
  • Runs as non-root appuser
  • Exposes port 6733

Docker Compose

  • Builds from local Dockerfile
  • Runs the service on port 6733
  • Automatically restarts unless stopped

Run it: docker compose up

.dockerignore

Optimized to exclude project artifacts, including:

  • device-simulator-linux-amd64
  • results.txt
  • bin/
  • .git/
  • .vscode/
  • README.md
  • Makefile

Extending & Productionizing

To move this prototype toward a production service:

  • Persistent Storage – Replace in-memory maps with a database (e.g., PostgreSQL or Redis) so data survives restarts and scales horizontally.
  • Monitoring & Alerts – Expose /healthz and /metrics endpoints for Prometheus/Grafana and trigger alerts when devices miss heartbeats or uploads slow down.
  • Scalability & Resilience – Run behind a load balancer with multiple stateless API instances; use queues or pub/sub to handle bursty telemetry.
  • Security & Configuration – Add HTTPS, API key authentication, and environment-based configuration management.

Author

Michael Diener
Software Engineer
Built with ❤️ and Go for the SafelyYou Fleet Monitoring challenge (2025)

About

SafelyYou Monitoring the Fleet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors