Skip to main content

Data Aggregation API

The JSON aggregation specification is designed to provide a structured way to aggregate JSON data streams. It offers several operators to aid in the summarization and grouping of data streams without the need for extensive code.

Aggregation Operators

Our specification currently supports the following operators:

  • $max: Determines the maximum value for the specified field(s).
  • $min: Determines the minimum value for the specified field(s).
  • $avg: Computes the average value for the specified field(s).
  • $sum: Sums up the values for the specified field(s).
  • $group: Group by specified field and perform nested aggregations.

Each operator can take either a string denoting the field name or an object with additional properties such as $field (for specifying the field name) and $label (for specifying a custom label for the result).

Aggregation data from a NoSQL query

JSON data aggregation is a standard library that is imported in your Codehooks app.

The example below shows how to use the library agains a salesData NoSQL data collection.

import { app, datastore, aggregation } from 'codehooks-js';

app.post('/agg', async (req, res) => {
try {
const spec = {
$max: "sales",
$avg: "sales",
};

const db = await datastore.open();
const dbstream = db.getMany('salesData', {active: true});
const result = await aggregation(dbstream, spec)
console.log(result)
res.json(result)
} catch (error) {
console.log(error.message)
res.status(400).end(error)
}
});

export default app.init(); // Bind functions to the serverless runtime

Example output

{
"count": 120500,
"sales": {
"max": 1234.00,
"avg": 123.00,
"sum_total": 123456.00
}
}

Operators

$max

  • Description: Determines the maximum value for the specified field(s).
  • Usage:
    • $max: fieldName for a simple field.
    • $max: { $field: fieldName, $label: customLabel } for specifying a custom label.
  • Example:
{ "$max": { "$field": "sales", "$label": "Max Sales" } }

$min

  • Description: Determines the minimum value for the specified field(s).
  • Usage:
    • $min: fieldName for a simple field.
    • $min: { $field: fieldName, $label: customLabel } for specifying a custom label.
  • Example:
{ "$min": { "$field": "sales", "$label": "Min Sales" } }

$avg

  • Description: Computes the average value for the specified field(s).
  • Usage:
    • $avg: fieldName for a simple field.
    • $avg: { $field: fieldName, $label: customLabel } for specifying a custom label.
  • Example:
{ "$avg": { "$field": "sales", "$label": "Average Sales" } }

$sum

  • Description: Sums up the values for the specified field(s).
  • Usage:
    • $sum: fieldName for a simple field.
    • $sum: { $field: fieldName, $label: customLabel } for specifying a custom label.
  • Example:
{ "$sum": { "$field": "sales", "$label": "Total Sales" } }

$group

  • Description: Group by specified field and perform nested aggregations.
  • Usage:
    • $group: { $field: fieldName, ...nestedAggregations } to group by a field and apply further aggregations.
  • Example:
{
"$group": {
"$field": "customerType",
"$label": "Customer Segment",
"$max": "sales"
}
}

Example: Complex grouping and aggregation

const spec = {
$max: { $field: "sales", $label: "All time top sales" },
$group: [
{
$field: "customerType",
$label: "Segment",
$max: "sales",
"$min": { $field: "sales", $label: "Low" },
$group: {
$field: "year",
$group: {
$field: "month",
$max: ["sales", { $field: "volume", $label: "Sales volume" }]
}
}
},
{
$field: "day", "$max": "sales"
}
]
}

Our JSON aggregation specification provides a robust tool for developers seeking powerful and streamlined data processing capabilities. Harnessing these operators allows for comprehensive insights into your data streams.