flespi Calculators API
API for [flespi analytics](https://flespi.com/kb/analytics-reports-and-events-engine) management. The API allows you to define the rules for how to split messages stored by devices into intervals and what to calculate inside generated intervals. Analytics is intended to build your own report engine on top of it or to be used as a source of events for your software - e.g. to generate and handle notifications from flespi about certain changes associated with the device. [Calculators](https://flespi.com/kb/analytics-reports-and-events-engine#calculators) are primary entity in flespi analytics. You define all the calculation configuration math inside calculator and than [assign devices to calculator](https://flespi.com/kb/analytics-reports-and-events-engine#assigned-devices) to apply this math to device. Once assigned to calculator device performs initial intervals calculation followed by automatical intervals update in the background as soon as new device messages are received. Intervals are defined by [**intervals selectors**](https://flespi.com/kb/analytics-reports-and-events-engine#interval-selectors) configuration. Then [**counters**](https://flespi.com/kb/analytics-reports-and-events-engine#interval-counters) can be applied to each interval to calculate user-defined metrics (e.g. mileage, engine hours, or any other custom calculations) over device messages. In general with calculators you can detect almost everything that is hapenning with device. Including geofence enter/exit, inactivity and GPS/connection problems, extract trips, parkings, stops, engine hours and any other user-defined events and calculations. Be aware that whenever the [calculator configuration](https://flespi.com/kb/analytics-reports-and-events-engine#calculators) changes - or the geofences, assets or device metadata it uses change - the system will recalculate intervals for assigned devices within 'update_period' time and thus may modify them. You control such behaviour with 'update_onchange' field in calculator properties. Each device assigned to the calculator has a dedicated intervals storage. You can [control](https://flespi.com/kb/how-to-manage-device-storage) the size and duration of this storage with 'intervals_ttl' and 'intervals_rotate' fields in calculator properties. One of the possibilities to optimize your flespi storage utilization is to set shorter **messages_ttl** for device messages, say 30-90 days, and set large **intervals_ttl** up to 10 years to store calculated intervals data for a much longer period. Intervals aggregate dozens of messages and thus take up much less space. ###### Calculator intervals The generated interval message contains timestamp of its generation, interval begin/end times, duration, id and all calculated counters as properties. Interval ID is generated automatically and you use ID if you want to attach own properties to the interval. To attach to the interval your custom properties you use **PUT /gw/calcs/{calcs.selector}/devices/{calc.devices.selector}/intervals/{calc.device.intervals.selector}** API call with corresponding interval ID in 'calc.device.intervals.selector'. However you can not attach own properties with same names as counters defined for calculator or reserved names such as id, timestamp, begin, end, duration. Calculated intervals from the corresponding intervals storage you may retrieve via REST API with **GET /gw/calcs/{calcs.selector}/devices/{calc.devices.selector}/intervals/{calc.device.intervals.selector}** call. Or via MQTT. MQTT has two sets of topics: * **flespi/state/gw/calcs/{calc_id}/devices/{device_id}/+** - retained topic from which you can retrieve and catch updates of last or active interval that was calculated; * **flespi/interval/gw/calcs/{calc_id}/devices/{device_id}/+** - real-time only topic to which any update with device intervals are published - create, delete, update events as well as last interval activated and deactivated events; Maximum size of data inside each counter can not exceed 8 MiB. If it will be larger during interval generation the counter will reset to null and corresponding log message will be added to calculator logs (event_code = 614). Maximum total JSON size for the interval message can not exceed 16 MiB. If it will be larger during generation - calculated device will be disabled with the reason indicated in calculator logs (event_code = 615). ###### Calculator logs All log messages in calculators related to intervals contain specific 'reason_code' field which clarifies why this or that interval event happened. Same 'reason_code' value is attached to MQTT messages as a user property to understand the reason of this or that interval event. If there are multiple reasons for the event the system will show the reason with bigger code value. List of possible reason codes: Reason code | Description -----|------ 2|timeout waiting for new data 5|referenced information has changed, e.g. intervals from referenced calculator 10|interval modified via REST API 20|received new message from device 30|asset interval update 47|calculator assigned assets change 48|calculator geofences change 49|device metadata change 50|calculator configuration change 90|automatic recalculation due to assigned device properties change 95|recalculation due to internal reason 100|initial automatic calculation 200|manual (forced) synchronization request via REST API 250|calculated device is disabled