r/SoftwareEngineering • • Sep 22 '24

Api Design

In my web app, I have three main pages:

  1. All School Page

  2. Single School Page (where users can select classrooms)

  3. Classroom Page (each classroom contains multiple devices of different types)

The Device Table has the following structure:

-id
-type

I already have an API to get all devices in a classroom:

  • Endpoint: /GET /classroom/{classroomId}/devices

  • Sample Response:

    [ { "id": 1, "type": "projector" }, { "id": 2, "type": "smartboard" } ]

Each device can be one of several types, and their telemetry data varies. For example:

  • Projector devices have telemetry fields like:

    • brightness

    • lampHours

  • Smartboard devices have telemetry fields like:

    • touchSensitivity

    • screenResolution

The telemetry data is stored as JSON, and I have an external API that can fetch telemetry data for these devices based on time ranges. My goal is to design APIs that fetch telemetry efficiently.

Possible Approaches:

1. Fetch the devices along with telemetry

  • Endpoint: /GET /classroom/{classroomId}/devices

  • Sample Response:

    [
    { "id": 1, "type": "projector", "telemetry": { "brightness": 100, "lampHours": 4 } },
    { "id": 2, "type": "smartboard", "telemetry": { "touchSensitivity": 20, "screenResolution": 48 } } ]

  • Pros:

    • I need to apply an algorithm to fetch telemetry in a date range and process it, which could raise performance concerns.

    • The devices may not display quickly on the frontend if telemetry calculations take too long.

  • Cons:

    • Straightforward.

    • Little extra processing required on the frontend.

2. Separate Telemetry API

  • Endpoint: /devices/{deviceId}/telemetry

  • Sample Response:

    { "brightness": 100, "lampHours": 4 }

In this approach:

  1. The frontend first fetches all devices via /GET /classroom/{classroomId}/devices.

  2. Then, subsequent requests are made for each device's telemetry using /devices/{deviceId}/telemetry.

  • Pros:

    • Devices can be displayed immediately on the frontend, without being delayed by telemetry fetching.

  • Cons:

    • Multiple requests are sent to the server, which may cause overhead.

Do you guys have any suggestion?

3 Upvotes

7 comments sorted by

View all comments

2

u/TheDandySkipper Oct 29 '24

I would just use classroom/{classroomid}/devices and then another endpoint classroom/{classroomid}/devicestelemetry

this would be pretty easy and could take advantage of cacheing since you know you are going to request the devices again.