# Transform raw data reports into indicators

Every raw data report belongs to a source, which is referenced in the
`sourceId` property of each raw data report. For each source, a transform
script can be defined in the [transform option](../api/kazana-manifest.md#transform).

Give there is a `bookkeeping` source with reports that have income and Expenses
as a multi line string in the format

```
+1800 salary
-900 rent
```

A transform script can turn these bookkeeping reports into separate indicators,
one per movement. The script would look like this

```js
transform: {
  bookkeeping: function (server, dataReport, callback) {
    var lines = dataReport.data.split(/\n/)
    var indicators = []
    var matches

    for (var i = 0; i < lines.length; i++) {
      matches = lines[i].match(/^([+-]?[\d\.]+)\s*(.*)$/)
      if (!matches) return callback(new Error('Data could not be parsed'))
      indicators.push({
        type: 'movement',
        amount: parseFloat(matches[1]),
        category: matches[2] || 'not set'
      })
    }
    callback(null, indicators)
  }
}
```

Each method gets called with three arguments

1. [server](../api/server.md)
2. [dataReport](../how-it-works.md#raw-data)
3. `callback`: must be called with an array of indicators that got extracted
   from the raw data report.

Every time a new data report with `sourceId: 'bookkeeping'` gets added, the
script gets run and transformed into `indicator` objects that are stored in
the `integrated-data` database. If the data report gets removed, all of its
indicators get removed, too. If a data report gets updated, all indicators get
removed and the transform scripts gets called again.

If data reports with `sourceId: 'bookkeeping'` already exist in the database,
they get picked up by the new transform script on server start.
