JSON formatting now allows specifying which time series to use.

This commit is contained in:
Tom Keffer
2021-03-08 12:26:08 -08:00
parent 0458b00e40
commit 84ea236a7e
2 changed files with 32 additions and 4 deletions

View File

@@ -1286,14 +1286,30 @@ class SeriesHelper(tuple):
def data(self):
return self[2]
def json(self, order_by='row'):
def json(self, order_by='row', time_series='both'):
import json
time_series = time_series.lower()
if time_series not in ['both', 'start', 'stop']:
raise ValueError("Unknown option '%s' for which time series to include" % time_series)
if order_by == 'row':
json_data = [[start_.raw, stop_.raw, data_.raw]
for start_, stop_, data_ in zip(self.start, self.stop, self.data)]
if time_series == 'both':
json_data = [[start_.raw, stop_.raw, data_.raw]
for start_, stop_, data_ in zip(self.start, self.stop, self.data)]
elif time_series == 'start':
json_data = [[start_.raw, data_.raw]
for start_, data_ in zip(self.start, self.data)]
else:
json_data = [[stop_.raw, data_.raw]
for stop_, data_ in zip(self.stop, self.data)]
elif order_by == 'column':
json_data = [self.start.raw, self.stop.raw, self.data.raw]
if time_series == 'both':
json_data = [self.start.raw, self.stop.raw, self.data.raw]
elif time_series == 'start':
json_data = [self.start.raw, self.data.raw]
else:
json_data = [self.stop.raw, self.data.raw]
else:
raise ValueError('Unknown order by option %s' % order_by)

View File

@@ -14,6 +14,18 @@ include the keyword `series`:
$period($data_binding=binding_name, $optional_ago=delta).obstype.series[.optional_unit_conversion][.optional_formatting]
```
## JSON formatting
By adding the suffix `.json()` to the tag, the results will be formatted as JSON. This option has
two optional parameters, `order_by` and `time_series`:
```
.json(order_by=['row'|'column'], time_series=['start'|'stop'|'both'])
```
`order_by`: The returned JSON can either be organized by rows, or by columns. The default is `row`.
`time_series`: This option controls which series are emitted for time. Option `start` selects the
start of each aggregation interval. Option `stop` selects the end of each interval. Option `both`
causes both to be emitted.
## Series with no aggregation
Here's an example of asking for a series with the temperature for all records in the day. We will
display it with an HTML `<pre>` tag so that the embedded newlines work.