{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "title": "User Metric",
  "description": "User metrics establish how aggregate functions can be used to update a custom user field with respect to an event.",
  "type": "object",
  "properties": {
    "customFieldKey": {
      "type": "string",
      "title": "Custom Field",
      "description": "The name of the custom field associated with this metric. "
    },
    "aggregateId": {
      "type": "string",
      "title": "Aggregate Identifier",
      "description": "An unique identifier for the aggregate operation to perform.",
      "enum": [
        "SUM",
        "COUNT",
        "FIRST_SEEN",
        "LAST_SEEN",
        "FIRST_VALUE",
        "LAST_VALUE",
        "THROTTLED_LAST_NUMERIC_VALUE"
      ]
    },
    "dateTriggeredWindow": {
      "type": "string",
      "title": "Date Triggered Window",
      "description": "An ISO 8601 interval. The aggregate operation will only be performed between this window."
    },
    "userEventKey": {
      "type": "string",
      "title": "User Event Key",
      "description": "The event that will be aggregated by this metric."
    },
    "filter": {
      "type": "string",
      "title": "Filter",
      "description": "A JSONata expression which will filter out non-matching events"
    }
  },
  "required": [
    "customFieldKey",
    "aggregateId",
    "userEventKey"
  ],
  "dependencies": {
    "aggregateId": {
      "oneOf": [
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "SUM"
              ]
            },
            "aggregateRules": {
              "$ref": "#/definitions/singleFieldWithWindowingAggregate"
            }
          }
        },
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "COUNT"
              ]
            },
            "aggregateRules": {
              "$ref": "#/definitions/windowingAggregate"
            }
          }
        },
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "FIRST_SEEN"
              ]
            }
          }
        },
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "LAST_SEEN"
              ]
            }
          }
        },
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "FIRST_VALUE"
              ]
            },
            "aggregateRules": {
              "$ref": "#/definitions/singleFieldAggregate"
            }
          }
        },
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "LAST_VALUE"
              ]
            },
            "aggregateRules": {
              "$ref": "#/definitions/singleFieldAggregate"
            }
          }
        },
        {
          "properties": {
            "aggregateId": {
              "enum": [
                "THROTTLED_LAST_NUMERIC_VALUE"
              ]
            },
            "aggregateRules": {
              "type": "object",
              "properties": {
                "fieldName": {
                  "$ref": "#/definitions/fieldName"
                },
                "diff": {
                  "type": "string",
                  "title": "Throttle Interval",
                  "description": "The minimum interval time that can elapse between recording the value of field name. Default is milliseconds per hour."
                }
              },
              "additionalProperties": false,
              "required": [
                "fieldName"
              ]
            }
          }
        }
      ]
    }
  },
  "definitions": {
    "singleFieldAggregate": {
      "type": "object",
      "properties": {
        "fieldName": {
          "$ref": "#/definitions/fieldName"
        }
      },
      "additionalProperties": false,
      "required": [
        "fieldName"
      ]
    },
    "singleFieldWithWindowingAggregate": {
      "type": "object",
      "properties": {
        "fieldName": {
          "$ref": "#/definitions/fieldName"
        },
        "windowing": {
          "$ref": "#/definitions/windowing"
        }
      },
      "additionalProperties": false,
      "required": [
        "fieldName"
      ]
    },
    "windowingAggregate": {
      "type": "object",
      "properties": {
        "windowing": {
          "$ref": "#/definitions/windowing"
        }
      },
      "additionalProperties": false
    },
    "fieldName": {
      "type": "string",
      "title": "Field Name",
      "description": "The field on which this aggregate will be performed."
    },
    "windowing": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "FIXED",
            "SLIDING"
          ]
        }
      },
      "required": [
        "type"
      ],
      "dependencies": {
        "type": {
          "oneOf": [
            {
              "properties": {
                "type": {
                  "enum": [
                    "FIXED"
                  ]
                },
                "unit": {
                  "type": "string",
                  "enum": [
                    "DAY",
                    "WEEK",
                    "MONTH",
                    "YEAR"
                  ]
                }
              },
              "required": [
                "type",
                "unit"
              ]
            },
            {
              "properties": {
                "type": {
                  "enum": [
                    "SLIDING"
                  ]
                },
                "duration": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 90
                },
                "unit": {
                  "type": "string",
                  "enum": [
                    "DAY",
                    "WEEK",
                    "MONTH",
                    "YEAR"
                  ]
                },
                "maxBucketValue": {
                  "type": "number"
                }
              },
              "required": [
                "type",
                "duration",
                "unit"
              ]
            }
          ]
        }
      }
    }
  }
}