Reduce

The reduce system takes key:value pairs as input from the Map operation. When the input meets defined conditions, the reduce produces a new set of key:value pairs using a defined operation.

The reduce operation can be min, max, count or sum. The output of the reduce operation is a summary of all processed inputs transformed into an array of all the reduce operations.

Reduce definition example

{
  "version": "1.7.0",
  "event": {
    "game": {
      "reductions": [
        {
          "id": "changeColor",
          "where": {"key": ["changeColor", "<name>", "<color>"]},
          "key": ["<name>", "<color>"],
          "value": ["$count"],
          "period": 250
        },
        {
          "id": "cheer",
          "where": {"key": ["cheer", "<name>"]},
          "key": ["<name>"],
          "value": ["$sum"],
          "period": 250
        }
      ]
    }
  }
}

Reduce schema

id

A unique string that refers to the reduce.

where

A schema defining the conditions that trigger producing a new key:value pair. See Where clause content for more information.

period

The frequency of the reduction in milliseconds (ms). For example, a 250ms period means that the game would receive the reduce output 4 times per second.

key

An array of strings representing the new key. Each string can be:

  • Alphanumeric string: A plain literal value.
  • <anyText>: A named value taken from the input key.

value

An array of operations which merges the inputs and generates the outputs. Note that even though an operation is specified, all operations are currently returned into each reduce.

Possible operations are:

  • $min: Returns the minimum value among all inputs.
  • $max: Returns the maximum value among all inputs.
  • $count: Returns the number of times the key was encountered.
  • $sum: Returns the sum of all input values added together.

Reduce result example

This is an example based on the reduce displayed above, here are the events received from the viewers (note that these were transformed by the map step before):

[
  {"key": ["changeColor", "Aramis", "red"], "value": 1},
  {"key": ["changeColor", "Porthos", "green"], "value": 1},
  {"key": ["changeColor", "Porthos", "green"], "value": 1},
  {"key": ["changeColor", "Athos", "blue"], "value": 1},
  {"key": ["cheer", "Aramis"], "value": 1},
  {"key": ["cheer", "Porthos"], "value": 1},
  {"key": ["cheer", "Porthos"], "value": 1},
  {"key": ["cheer", "Athos"], "value": 1}
]

The reduce function merges them into the following:

[
  {"key": ["Aramis", "red"], "value": [{"min": 1}, {"max": 1}, {"count": 1}, {"sum": 1}]},
  {"key": ["Porthos", "green"], "value": [{"min": 1}, {"max": 1}, {"count": 2}, {"sum": 2}]},
  {"key": ["Athos", "blue"], "value": [{"min": 1}, {"max": 1}, {"count": 1}, {"sum": 1}]},
  {"key": "Aramis", "value": [{"min": 1}, {"max": 1}, {"count": 1}, {"sum": 1}]},
  {"key": "Porthos", "value": [{"min": 1}, {"max": 1}, {"count": 2}, {"sum": 2}]},
  {"key": "Athos", "value": [{"min": 1}, {"max": 1}, {"count": 1}, {"sum": 1}]}
]