# Define analytics

Pre-computed metrics generated by the [Metrics Engine](https://docs.fractalworks.io/joule/components/analytics/metrics-engine) can be used within a custom processing component. For example, events could be filtered by user-defined metrics using time intervals, scoring models use metrics as part of the input feature space or build KPIs that combine metrics with event data.

## Development steps

## API

The below API is provided for developers to implement.

```java
public abstract class AnalyticsFunction<T> {

    protected Logger logger;
    protected Properties parameters;

    public AnalyticsFunction() {
        logger = LoggerFactory.getLogger(this.getClass().getName());
    }

    /**
     * Set function parameters
     *
     * @param parameters
     */
    @JsonProperty(value = "parameters")
    public void setParameters(Properties parameters) {
        this.parameters = parameters;
    }


    /**
     * Referenceable function ID for use in projections and window processing
     *
     * @return name of function as String
     */
    public abstract String getVariablePostFixID();

    /**
     * AnalyticDefinition function to compute
     *
     * @param values to use in the computation
     * @param previousValue computed
     * @param context state                     
     * @return computed result
     */
    public T compute(Number[] values, Number previousValue, Context context) {
        return null;
    }

    /**
     * 
     * @param column raw values to use in the computation
     * @param previousValue computed
     * @param context state
     * @return
     */
    public T compute(NumberColumn column, Number previousValue, Context context) {
        return null;
    }
```

## Development steps

1. Implement AnalyticsFunction interface
2. Validate and test
3. Deploy
4. Add to a use case DSL

## Explaining each step <a href="#explaining-each-step" id="explaining-each-step"></a>

### Step 1: Implement AnalyticsFunction interface

See the [Bollinger bands example](https://docs.fractalworks.io/joule/use-case-examples/banking/bollinger-bands)

### Step 2: Build, test and package <a href="#step-1-create-the-destination-using-the-template" id="step-1-create-the-destination-using-the-template"></a>

The template project provides basic JUnit test to validate DSL. The project will execute these tests during the gradle build cycle and deploy to your local maven repository.&#x20;

```
gradle build publishToMavenLocal
```

### Step 3: Deploy <a href="#step-1-create-the-destination-using-the-template" id="step-1-create-the-destination-using-the-template"></a>

Once your package has been successfully created you are ready to deploy to a Joule project. The resulting jar artefact needs to be placed in to the `userlibs` directory in your Joule projects directory. See provided examples [documentation](https://docs.fractalworks.io/joule/use-case-examples/overview) for further directions.

```bash
cp build/libs/<your-connector>.jar <location>/userlibs
```

### Step 4: Add to a use case DSL

Now the jar is created and deployed the use case can use the implementation within the DSL definition file.

#### Example

```yaml
processing unit:
  pipeline:
    - filter:
      expression: "symbol == 'CVCO'"
    - sliding window analytics:
        function: com.fractalworks.examples.banking.analytics.BollingerBands
        windowSize: 5
        fields: [ ask ]
        parameters:
          deviations: 2
```
