thing.aggregate

The thing.aggregate command is used to create bucket aggregations. Unlike metrics aggregations that calculates values (for example, sum, average) over a set of documents; bucket aggregations group documents into buckets. The bucket aggregation also returns the number of documents (for example, Things) that belong to each bucket.

A bucket (in the term bucket aggregations) means a range of potential values that are grouped together. For example in looking at the number of miles a vehicle has traveled, it is possible to arbitrarily construct the following ranges (the ‘bucket’ name in parentheses): 0-999 miles (new car), 1,000-5,000 miles (mildly used car), 5,001-99,999 miles (moderately used), 100,000+ miles (used car). Similarly, temperature ranges read by sensors can be described in buckets could be used: 23°C and under (cold), 24°C to 29°C (warm), 30°C and more (hot). Our use of bucket aggregation is counting the number of Things that have a value that fits into one of the buckets: 10 new cars, 5 mildly used car, 7 moderately used car, and 1 used car; 51 sensors are cold, 72 are warm, and 66 are hot.

The thing.aggregate is designed to provide counts of the number of Things in various states, whether it be based on the current attribute value (string), current property value (integer or float), or a value from the LWM2M data structure (which could be analogous to either an attribute or a property).

Common Request Parameters

Name Type Required Description
query String Yes Query to filter list of Things to be aggregated
objId+instId+resId Integer All 3 must be specified to aggregate a LWM2M resource value, this is expanded into field lwm2m.objects.<objId>.instances.<instId>.resources.<resId>.lastKnownValue for the aggregation.
property Integer Specifies a property key to aggregate, this field is expanded into properties.<property>.value
attribute Boolean Specifies an attribute key to aggregate, this field is expanded into attrs.<attribute>.value
field String   If you do not use any of the above 3 methods to define the field, you can specify a field. For example: lastSeen

TR50 Request

{ 
   "1": { 
     "command": "thing.aggregate", 
 	"params": { 
	   "field":"lastSeen", 
	   "aggregation":"date_range",
	   "ranges":["3M","1M","7D","1D","0D"]
	} 
     } 
  } 

Request Parameters

Name Type Required Description
Aggregation String Yes date_range - A range aggregation that is dedicated for date values
ranges Array An array of strings from oldest to newest with time intervals for the buckets to aggregate. In this request the field lastSeen is used for the aggregation. "M" indicates months, and "D" indicates days

TR50 Response

If the command is sent successfully a success message is returned. Otherwise, an error and error message will be returned.

{
  "1",
  "data": {
       "success": true,
	"params": {
	   "count": 6,
	   "result": [
		{
		  "to": "2018-11-14T00:00:00Z",
		  "count": 16
		},
		{
		  "from": "2018-11-14T00:00:00Z",
		  "to": "2019-01-14T00:00:00Z",
		  "count": 0
		},
		{
		  "from": "2019-01-14T00:00:00Z",
		  "to": "2019-02-07T00:00:00Z",
		  "count": 0
		},
		{
		  "from": "2019-02-07T00:00:00Z",
		  "to": "2019-02-13T00:00:00Z",
		  "count": 0
		},
		{
		  "from": "2019-02-13T00:00:00Z",
		  "to": "2019-02-14T00:00:00Z",
		  "count": 0
		},
		{
		  "from": "2019-02-14T00:00:00Z",
		  "count": 3
		}
	  ]
	} 
    }
}

Response Parameters

Name Type Description
count Integer The total number of buckets.
result Array

The result set of the returned buckets. In the above example 6 buckets are returned. Refer the diagram above to understand the buckets returned.

count(within results) Integer The counts within each buckets.