######## ncctrllatency v8 templates ########
# ncctrllatency_controller_latency_v8_tmpl


resource "elasticstack_elasticsearch_script" "ncctrllatency_controller_latency_v8_tmpl" {
    script_id = "ncctrllatency_controller_latency_v8_tmpl"
    lang      = "mustache"
    source    = jsonencode({
    "_source": {
        "excludes": []
    },
    "stored_fields": [
        "*"
    ],
    "script_fields": {},
    "docvalue_fields": [
        {
            "field": "timestamp",
            "format": "date_time"
        }
    ],
    "query": {
        "bool": {
            "must": [
            
                 {
                        "match_phrase": {
                            "network_group_id": {
                                "query": "{{networkGroupId}}"
                            }
                        }
                },
                {
                    "match_phrase": {
                        "network_id": {
                            "query": "{{networkId}}"
                        }
                    }
                },
                {
                    "range": {
                        "timestamp": {
                            "gte": "{{gte}}",
                            "lte": "{{lte}}",
                            "format": "strict_date_optional_time"
                        }
                    }
                }
            ],
            "must_not": [
                {
                    "match_phrase": {
                        "max": 88888888888
                    }
                }
            ]
        }
    },
    "aggs": {
        "time_buckets": {
            "date_histogram": {
                "field": "timestamp",
                "fixed_interval": "{{interval}}",
                "min_doc_count": 1
            },
            "aggs": {
                "routers": {
                    "terms": {
                        "field": "edge_router_name.keyword",
                        "size": 2000,
                        "order": {
                            "_key": "desc"
                        }
                    },
                    "aggs": {
                        "avg_of": {
                            "avg": {
                                "field": "{{latencyType}}"
                            }
                        }
                    }
                }
            }
        }
    },
    "size": 0
})
}