Querying an OBC for Telemetry Data ================================== At some point, users will want to fetch data which was written to the telemetry database (for an example of this, see the :doc:`mission application ` tutorial). This tutorial will walk the user through the process of communicating with the :doc:`telemetry database service <../services/telemetry-db>` in order to retrieve a desired set of telemetry information. Pre-Requisites -------------- - :doc:`Install the Kubos SDK <../installation-docs/sdk-installing>` - Have an OBC available with ethernet capabilities (preferably with an :doc:`installation of Kubos Linux <../installation-docs/index>`) - :ref:`Configuring Ethernet ` - Have the telemetry database service running on a target OBC (this happens by default when running KubOS) Setup ----- In order to have something to query, we'll need to seed the database. We'll use ``curl`` to create some HTTP requests which will add data points to the telemetry database. .. note:: For all commands, replace the ``10.0.2.20`` value with the IP address of your OBC. The example commands will also be referencing the telemetry database service's default port, 8006 From your instance of the SDK, run the following:: $ curl 10.0.2.20:8006 -H "Content-Type: application/json" --data "{\"query\":\"mutation {insert(subsystem:\\\"eps\\\",parameter:\\\"voltage\\\",value:\\\"5.0\\\"){success}}\"}" $ curl 10.0.2.20:8006 -H "Content-Type: application/json" --data "{\"query\":\"mutation {insert(subsystem:\\\"eps\\\",parameter:\\\"voltage\\\",value:\\\"5.0\\\"){success}}\"}" $ curl 10.0.2.20:8006 -H "Content-Type: application/json" --data "{\"query\":\"mutation {insert(subsystem:\\\"eps\\\",parameter:\\\"current\\\",value:\\\"0.1\\\"){success}}\"}" $ curl 10.0.2.20:8006 -H "Content-Type: application/json" --data "{\"query\":\"mutation {insert(subsystem:\\\"eps\\\",parameter:\\\"current\\\",value:\\\"0.1\\\"){success}}\"}" $ curl 10.0.2.20:8006 -H "Content-Type: application/json" --data "{\"query\":\"mutation {insert(subsystem:\\\"gps\\\",parameter:\\\"voltage\\\",value:\\\"3.3\\\"){success}}\"}" Each of these commands should return the following:: {"data":{"insert":{"success":true}}} GraphiQL -------- Please navigate to ``http://{ip}:8006/graphiql`` in order to communicate with the telemetry service for this tutorial. More information about the GraphiQL interface can be found :ref:`here `. Schema ------ The ``telemetry`` query can be used to fetch a certain selection of data from the telemetry database. It will return an array of database entries. The query has the following schema:: query { telemetry(timestampGe: Float, timestampLe: Float, subsystem: String, parameter: String, limit: Integer): [{ timestamp: Float! subsystem: String! parameter: String! value: String! }] } Each of the input arguments acts as a filter for the database query: - timestampGe - Return entries with timestamps occurring on or after the given value - timestampLe - Return entries with timestamps occurring on or before the given value - subsystem - Return entries which match the given subsystem name - parameter - Return entries which match the given parameter name - limit - Return only the first `n` entries found Using the ``telemetry`` query without specifying any of the input arguments will result in all entries in the telemetry database being returned. The query returns an array of entries, each of which has the following response fields: - timestamp - The system time when the entry was recorded, in milliseconds since UNIX epoch - subsystem - The subsystem which generated the entry data - parameter - The specific data point the entry corresponds to - value - The value of the data point Querying a Specific Subsystem ----------------------------- In order to return only telemetry generated by a specific subsystem, we'll send a query using the ``subsystem`` input parameter. For example, to get only telemetry from our (fake) EPS subsystem, we'll send the following query:: { telemetry(subsystem: "eps") { timestamp subsystem parameter value } } The request should return something like this:: { "data": { "telemetry": [ { "timestamp": 730.855591753, "subsystem": "eps", "parameter": "current", "value": "0.1" }, { "timestamp": 730.21144042, "subsystem": "eps", "parameter": "current", "value": "0.1" }, { "timestamp": 721.187618335, "subsystem": "eps", "parameter": "voltage", "value": "5.0" }, { "timestamp": 688.450861332, "subsystem": "eps", "parameter": "voltage", "value": "5.0" } ] } } Querying a Time Frame --------------------- If we want to fetch the telemetry from a particular time frame, for example all telemetry occurring between 1pm and 2pm, we'll use a combination of the ``timestampGe`` and ``timestampLe`` input arguments. .. note:: In order to select a timeframe, it is helpful to know the current system time. This can be found by running the following command on the OBC: ``date +%s``. The command gets the current number of seconds since UNIX epoch, which matches the telemetry timestamp units. Our request should look like this:: { telemetry(timestampGe: 730.0, timestampLe: 731.0) { timestamp subsystem parameter value } } This queries the system for telemetry in a 1 second window between 730 and 731, inclusive. You will need to update the query for your specific timestamp range. The response should look like this:: { "data": { "telemetry": [ { "timestamp": 730.855591753, "subsystem": "eps", "parameter": "current", "value": "0.1" }, { "timestamp": 730.21144042, "subsystem": "eps", "parameter": "current", "value": "0.1" } ] } } Limiting the Results -------------------- Let's say we only want to know the most recent voltage reading from our EPS subsystem. We'll specify the ``subsystem`` and ``parameter`` input argument to narrow the filters down to the subsystem and parameter we want. We'll then also use the ``limit`` input argument to tell the telemetry database service to only return the single most-recent entry. Our request should look like this:: { telemetry(subsystem: "eps", parameter: "voltage", limit: 1) { timestamp subsystem parameter value } } The response should look like this:: { telemetry(subsystem: "eps", parameter: "voltage", limit: 1) { timestamp subsystem parameter value } }