The Custom Queries section enables you to perform direct queries against the database
in which the data model is stored.
The database used by Altair IoT Studio is ArangoDB, which allows you to
perform queries using the capabilities of a graphical database.
Thing Diagram
When accessing the Custom Queries panel, the first thing to look at in some detail is
the Thing Diagram. This diagram defines the structure that
the AnythingDB database follows internally. This structure must be followed to
perform the queries correctly.Figure 1.
Run a Custom Query
To perform a custom query, you must directly insert the query text into the "query"
field inside the box below the Execute button. Below are two sample queries using
the parking system example.
Check occupied parking slots.
To obtain a list with the identifiers of all the parking slots that are
currently occupied, look for the sensors type "Parking sensor" and
select the ones that have the Presence property
set to True, which means it is currently occupied
by a vehicle.
The following code can be used to get the desired
result:
{
"query": "FOR t IN things FILTER t.title == @thing_title AND t.status.@presence == @value RETURN t._key",
"count": true,
"batchSize": 100,
"bindVars": {
"thing_title": "Parking Sensor",
"presence": "Presence",
"value": true
}
}
Get the average "Power Consumption" of all the Traffic lights.
In this example you will launch a query to obtain the average value of the
property "Power Consumption" of all the things titled "Traffic Light" that
belong to the category
"TrafficManagement."
{
"query": "FOR c in categories FILTER c.name == @category_name FOR t IN things FILTER t.title == @thing_title FILTER c._id IN t.categories FILTER HAS(t.status, @property_name) COLLECT AGGREGATE average = AVG(t.status.@property_name) RETURN average",
"count": true,
"bindVars": {
"category_name": "TrafficManagement",
"thing_title":"Traffic Light",
"property_name": "Power Consumption"
}
}
The response returned by the platform will include the
result: