{{- with .Values.logging }}
{{- if .enabled }}
apiVersion: logging.banzaicloud.io/v1beta1
kind: Flow
metadata:
  name: {{ include "collabora-online.fullname" $ }}
spec:
  match:
    - select:
        labels:
          type: main
          {{- include "collabora-online.selectorLogLabels" $ | nindent 10 }}
        container_names:
          - "{{ $.Chart.Name }}"
  filters:
    - parser:
        hash_value_field: "collabora"
        remove_key_name_field: true
        reserve_data: true
        parse:
          type: "multi_format"
          patterns:
            - format: "regexp"
              # regex for wopihost and wopidoc
              # wsd-00001-00092 2023-04-20 15:34:56.729798 +0000 [ docbroker_001 ] ERR  Failed to add session to [https://integrator.example.org:443/collabora/lptFile123.odt] with URI [https://integrator.example.org/collabora/lptFile123.odt?access_token=1&access_token_ttl=0&permission=edit]: No acceptable WOPI hosts found matching the target host [integrator.example.org] in config.| wsd/DocumentBroker.cpp:2251
              # wsd-00001-00092 2023-04-20 15:34:56.729826 +0000 [ docbroker_001 ] ERR  Unauthorized Request while starting session on https://integrator.example.org:443/collabora/lptFile123.odt for socket #33. Terminating connection. Error: No acceptable WOPI hosts found matching the target host [integrator.example.org] in config.| wsd/COOLWSD.cpp:4727
              expression: '^(?<process>\w+)-(?<pid>\d+)-(?<thread>\d+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4})\s+\[\s+(?<compenent>\w+)\s+\]\s+(?<level>\w+)\s+(?<message>.*(session (to|on) \[?(?<wopidoc>[^\s\]]*)\]?.*)No acceptable WOPI hosts found matching the target host \[(?<wopihost>.+)\] in config.)\|\s(?<code>[^:]+):(?<codeline>[0-9]+)'
              types: 'pid:integer,thread:integer,codeline:integer'
              time_key: "time"
              time_type: "string"
              time_format: "%Y-%m-%d %H:%M:%S.%N %z"

            - format: "regexp"
              # regex for just wopihost
              # wsd-00001-00092 2023-04-20 15:34:56.729615 +0000 [ docbroker_001 ] ERR  No acceptable WOPI hosts found matching the target host [integrator.example.org] in config.| wsd/Storage.cpp:263
              # wsd-00001-00092 2023-04-20 15:34:56.729748 +0000 [ docbroker_001 ] ERR  loading document exception: No acceptable WOPI hosts found matching the target host [integrator.example.org] in config.| wsd/DocumentBroker.cpp:2289
              expression: '^(?<process>\w+)-(?<pid>\d+)-(?<thread>\d+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4})\s+\[\s+(?<compenent>\w+)\s+\]\s+(?<level>\w+)\s+(?<message>.*No acceptable WOPI hosts found matching the target host \[(?<wopihost>.+)\] in config.)\|\s(?<code>[^:]+):(?<codeline>[0-9]+)'
              types: 'pid:integer,thread:integer,codeline:integer'
              time_key: "time"
              time_type: "string"
              time_format: "%Y-%m-%d %H:%M:%S.%N %z"

            - format: "regexp"
              # regex for address denied with ip
              # wsd-00001-00056 2023-04-18 16:00:20.954860 +0000 [ websrv_poll ] WRN  convert-to: Requesting address is denied: 0.0.0.0| wsd/COOLWSD.cpp:3540
              expression: '^(?<process>\w+)-(?<pid>\d+)-(?<thread>\d+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4})\s+\[\s+(?<compenent>\w+)\s+\]\s+(?<level>\w+)\s+(?<message>.*address is denied: )(?<ip>\d+\.\d+\.\d+\.\d+)\|\s(?<code>[^:]+):(?<codeline>[0-9]+)'
              types: 'pid:integer,thread:integer,codeline:integer'
              time_key: "time"
              time_type: "string"
              time_format: "%Y-%m-%d %H:%M:%S.%N %z"

            - format: "regexp"
              # regex for response code with statuscode
              # wsd-00001-00040 2023-04-16 10:03:08.379757 +0000 [ remoteconfig_poll ] ERR  Remote config server has response status code: 503| wsd/COOLWSD.cpp:1163
              expression: '^(?<process>\w+)-(?<pid>\d+)-(?<thread>\d+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4})\s+\[\s+(?<compenent>\w+)\s+\]\s+(?<level>\w+)\s+(?<message>.*has response status code: (?<statuscode>\d+))\|\s(?<code>[^:]+):(?<codeline>[0-9]+)'
              types: 'pid:integer,thread:integer,codeline:integer,statuscode:integer'
              time_key: "time"
              time_type: "string"
              time_format: "%Y-%m-%d %H:%M:%S.%N %z"

            - format: "regexp"
              # by default parsed, this line
              # Adding trusted WOPI host: [cloud\.example\.de].
              expression: '^(?<process>\w+)-(?<pid>\d+)-(?<thread>\d+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4})\s+\[\s+(?<compenent>\w+)\s+\]\s+(?<level>\w+)\s+(?<message>Adding trusted WOPI host): \[(?<wopihost>[^\]]+)\]\|\s(?<code>[^:]+):(?<codeline>[0-9]+)'
              types: 'pid:integer,thread:integer,codeline:integer'
              time_key: "time"
              time_type: "string"
              time_format: "%Y-%m-%d %H:%M:%S.%N %z"

            - format: "regexp"
              # fallback to default message regex
              expression: '^(?<process>\w+)-(?<pid>\d+)-(?<thread>\d+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4})\s+\[\s+(?<compenent>\w+)\s+\]\s+(?<level>\w+)\s+(?<message>.+)\|\s(?<code>[^:]+):(?<codeline>[0-9]+)'
              types: 'pid:integer,thread:integer,codeline:integer'
              time_key: "time"
              time_type: "string"
              time_format: "%Y-%m-%d %H:%M:%S.%N %z"

            - format: "none"
    {{- if .ecs }}
    - record_transformer:
        enable_ruby: true
        records:
          - event.kind: "event"
            message: '${ record["collabora"]["message"] }'
            log.level: '${ record["collabora"]["level"].gsub("ERR", "error").gsub("WRN", "warn").gsub("INF","info").gsub("DBG","debug").gsub("TRC", "trace") }'
            related.ip: '${ record["collabora"]["ip"] }'
            client.ip: '${ record["collabora"]["ip"] }'
        remove_keys: "$['collabora']['message'],$['collabora']['level'],$['collabora']['ip']"
    {{- end }}
    {{- with .additionalFilters }}
    {{- toYaml . | nindent 4 }}
    {{- end }}

  {{- with .globalOutputRefs }}
  globalOutputRefs:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .localOutputRefs }}
  localOutputRefs:
    {{- toYaml . | nindent 4 }}
  {{- end }}

{{- end }}
{{- end }}
