Carries per-calculation state through a single call to QueryService.calcMetric, including cache and recursion controls and a scratch attribute bag that metric types can use to pass data to sub-calculations. A new instance is normally created per top level metric calculation, and nested calculations spawn named sub-contexts with newSubContext so a metric type can record intermediate values (for example, for use in a chart or breakdown) without polluting the parent context.
Properties
| Property | Returns | Description |
|---|---|---|
| attributes | Map<String,Object> | The scratch attribute bag for this context, including any sub-contexts stored under the "subContexts" key. Never null, but may be empty. |
| cacheEnabled | boolean | Whether metric results calculated in this context may be read from or written to the cache. False once noCache has been called on this context. |
| callDepth | int | How many nested calcMetric calls deep this context is, used to detect runaway recursion between metric types that reference each other. |
| forceRecalc | boolean | Whether a pre-calculated metric using this context must be recalculated rather than reading the existing cached value. |
Methods
isCacheEnabled() · isForceRecalc() · noCache() · forceRecalc() · getAttributes() · newSubContext(String name) · setAttribute(String key, Object val) · getCallDepth() · incrementAndCheckDepth() · asSerializableMap()
isCacheEnabled()
Returns: boolean
Whether metric results calculated in this context may be read from or written to the cache. False once noCache has been called on this context.
isForceRecalc()
Returns: boolean
Whether a pre-calculated metric using this context must be recalculated rather than reading the existing cached value.
noCache()
Returns: CalcContext
Disables caching for calculations that use this context.
forceRecalc()
Returns: CalcContext
Forces pre-calculated metrics that use this context to be recalculated instead of returning a cached value.
getAttributes()
Returns: Map<String,Object>
The scratch attribute bag for this context, including any sub-contexts stored under the "subContexts" key. Never null, but may be empty.
newSubContext(String name)
Returns: CalcContext
Creates a new child CalcContext, inheriting this context's call depth, and stores it under the given name in this context's attributes map, under the reserved key "subContexts". Metric types use this to keep per-item or per-series state separate while still nesting within the parent calculation.
| Parameter | Description |
|---|---|
name | the key to store the new sub-context under |
setAttribute(String key, Object val)
Returns: void
Stores a value in this context's attribute bag under the given key.
| Parameter | Description |
|---|---|
key | the attribute name |
val | the value to store |
getCallDepth()
Returns: int
How many nested calcMetric calls deep this context is, used to detect runaway recursion between metric types that reference each other.
incrementAndCheckDepth()
Returns: boolean
Increments the call depth counter and reports whether it has now exceeded MAX_CALL_DEPTH, so a caller can abort a recursive metric calculation before it overflows the stack.
asSerializableMap()
Returns: Map<String,Serializable>
Converts this context's attributes, and recursively any sub-contexts stored under "subContexts", into a plain map of serializable values suitable for including in an API response or event. Attributes in IGNORED_CALC_PROPS are skipped, and any value that is not a recognised serializable primitive is dropped rather than included.