######## ncctrllatency templates ########
# ncctrllatency_filter_by_date_tmpl
# ncctrllatency_weekly_hourly_tmpl
# ncctrllatency_controller_latency_tmpl

resource "elasticstack_elasticsearch_script" "ncctrllatency_filter_by_date_tmpl" {
    script_id = "ncctrllatency_filter_by_date_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "_source": {
            "excludes": []
        },
        "stored_fields": [
            "*"
        ],
        "script_fields": {},
        "docvalue_fields": [
            {
                "field": "@timestamp",
                "format": "date_time"
            },
            {
                "field": "timestamp",
                "format": "date_time"
            }
        ],
        "query": {
            "bool": {
                "must": [
                    {
                        "match_all": {}
                    },
                    {
                        "match_phrase": {
                            "network_id": {
                                "query": "{{networkId}}"
                            }
                        }
                    },
                    {
                        "range": {
                            "@timestamp": {
                                "gte": "{{gte}}",
                                "lte": "{{lte}}",
                                "format": "yyyy-MM-dd"
                            }
                        }
                    }
                ],
                "filter": [],
                "should": [],
                "must_not": [
                    {
                        "match_phrase": {
                            "max": 88888888888
                        }
                    }
                ]
            }
        },
        "aggs": {
            "time_buckets": {
                "date_histogram": {
                    "field": "@timestamp",
                    "fixed_interval": "{{aggInterval}}",
                    "time_zone": "{{timeZone}}",
                    "min_doc_count": 1
                },
                "aggs": {
                    "routers": {
                        "terms": {
                            "field": "router_id.keyword",
                            "order": {
                                "_key": "desc"
                            }
                        },
                        "aggs": {
                            "avg_of": {
                                "avg": {
                                    "field": "{{latencyFilterType}}"
                                }
                            }
                        }
                    }
                }
            }
        },
        "size": "{{size}}"
    })
}


resource "elasticstack_elasticsearch_script" "ncctrllatency_weekly_hourly_tmpl" {
    script_id = "ncctrllatency_weekly_hourly_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "_source": {
            "excludes": []
        },
        "stored_fields": [
            "*"
        ],
        "script_fields": {},
        "docvalue_fields": [
            {
                "field": "@timestamp",
                "format": "date_time"
            },
            {
                "field": "timestamp",
                "format": "date_time"
            }
        ],
        "query": {
            "bool": {
                "must": [
                    {
                        "match_all": {}
                    },
                    {
                        "match_all": {}
                    },
                    {
                        "match_phrase": {
                            "networkId": {
                                "query": "{{networkId}}"
                            }
                        }
                    },
                    {
                        "range": {
                            "@timestamp": {
                                "gte": "{{gte}}",
                                "lte": "{{lte}}",
                                "format": "yyyy-MM-dd"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "networkId": {
                                "query": "{{networkId}}"
                            }
                        }
                    }
                ],
                "filter": [],
                "should": [],
                "must_not": [
                    {
                        "match_phrase": {
                            "max": 88888888888
                        }
                    }
                ]
            }
        },
        "size": "{{size}}",
        "aggs": {
            "routers_bucket": {
                "terms": {
                    "field": "router_id.keyword",
                    "size": 50,
                    "order": {
                        "_key": "desc"
                    }
                },
                "aggs": {
                    "days_bucket": {
                        "date_histogram": {
                            "field": "@timestamp",
                            "fixed_interval": "1d",
                            "time_zone": "UTC",
                            "min_doc_count": 1
                        },
                        "aggs": {
                            "hourly_bucket": {
                                "date_histogram": {
                                    "field": "@timestamp",
                                    "fixed_interval": "1h",
                                    "time_zone": "UTC",
                                    "min_doc_count": 1
                                },
                                "aggs": {
                                    "avg_max": {
                                        "avg": {
                                            "field": "max"
                                        }
                                    },
                                    "avg_mean": {
                                        "avg": {
                                            "field": "mean"
                                        }
                                    },
                                    "avg_p99": {
                                        "avg": {
                                            "field": "p99"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    })
}




resource "elasticstack_elasticsearch_script" "ncctrllatency_controller_latency_tmpl" {
    script_id = "ncctrllatency_controller_latency_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": "nf_edge_router_name.keyword",
                        "size": 2000,
                        "order": {
                            "_key": "desc"
                        }
                    },
                    "aggs": {
                        "avg_of": {
                            "avg": {
                                "field": "{{latencyType}}"
                            }
                        }
                    }
                }
            }
        }
    },
    "size": 0
})
}