DrMemCS / drmem

Full source tree for the DrMem control system
MIT License
3 stars 4 forks source link

Add fields to GraphQL's `deviceInfo` query response #30

Closed rneswold closed 1 year ago

rneswold commented 1 year ago

While playing with redis, I found the XINFO command which returns information about a redis stream. Since our device history is stored in streams, this could be a useful command to return history information. Among several interesting pieces of information, it returns the number of points of history and the first and last data points along with the timestamps.

This issue proposes to add several fields to the reply of a deviceInfo query.

field type description
total_points integer total number of point in the history
first_point Reading? the earliest reading in the history
last_point Reading? the most recent reading in the history

The reading fields are nullable, if there isn't any history. The Reading type is the same used when monitoring a device. In fact, reading the last_point field is a way to poll a device (monitoring a device is better, though, since you can't miss any intermediate values.)

For the simple backend, total_points will be 0 or 1 and the first_point and last_point fields will be null or have the same value since its history is only one point deep.

rneswold commented 1 year ago

GraphQL resolvers are only called if the field is specified in the reply. Unfortunately, it's not easy to save the results of one field's resolver to share with others. If the user wants all three fields returned, I don't see how I can save the results of the XINFO command to be used when each resolver is called. So it seems I need to run XINFO 3 times.

Instead, I created another type DeviceHistory that contains the three fields and made one field in DeviceInfo. Now if the user asks for the history, we grab it once with XINFO and return the structure. The reply may not return all three fields, but they're there.