---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Grafana Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and Grafana (Example)

metadata:
  name: Grafana Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  grafanaHost:
    description: Hostname of your Grafana instance, without the scheme
    type: string
    default: example.grafana.net

  grafanaPort:
    description: HTTPS port of your Grafana instance
    type: number
    default: 443

  serviceAccountToken:
    description: Grafana service account token used to authenticate API requests
    type: string

  topicRoot:
    description: Root of the MQTT topic hierarchy
    type: string
    default: enterprise

resources:
  # Connection to the Grafana HTTP API using a service account token
  grafanaConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref grafanaHost
        port: !ref grafanaPort
        headers:
          Authorization: !sub 'Bearer ${serviceAccountToken}'
        # The health endpoint responds without authentication
        probePath: /api/health
        probeMethod: GET

  # Creates one Grafana annotation per POST request
  annotationEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref grafanaConnection
      write:
        path: /api/annotations

  # Turns shop floor events into annotations. The named wildcards provide
  # the topic levels in $context.vars, so the transforms tag each
  # annotation with the affected equipment.
  annotationMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/events/machine-stop'
          publish:
            endpoint: !ref annotationEndpoint
          rules:
            - transform:
                expression: >-
                  {
                    "body": {
                      "time": $millis(),
                      "tags": ["machine-stop", $context.vars.line, $context.vars.cell],
                      "text": "Machine stop on " & $context.vars.cell & ": " & reason
                    }
                  }
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/events/batch-change'
          publish:
            endpoint: !ref annotationEndpoint
          rules:
            - transform:
                expression: >-
                  {
                    "body": {
                      "time": $millis(),
                      "tags": ["batch-change", $context.vars.line, $context.vars.cell],
                      "text": "Batch change on " & $context.vars.cell & ": new batch " & batchId
                    }
                  }

  # HTTP route that the Grafana webhook contact point calls.
  # Served at https://<connectware>/data/grafana/alerts
  grafanaAlertServer:
    type: Cybus::Server::Http
    properties:
      basePath: /grafana

  grafanaAlertRoute:
    type: Cybus::Node::Http
    properties:
      parent: !ref grafanaAlertServer
      method: POST
      route: /alerts

  alertMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref grafanaAlertRoute
          publish:
            topic: !sub '${topicRoot}/grafana/alerts'
