######## nclinklatency v8 templates ########
# nclinklatency_fabric_latency_v8_tmpl
# nclinklatency_timeouts_date_filter_v8_tmpl
# nclinklatency_timeouts_data_query_v8_tmpl



resource "elasticstack_elasticsearch_script" "nclinklatency_fabric_latency_v8_tmpl" {
    script_id = "nclinklatency_fabric_latency_v8_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
         "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "metric": "link.latency"
                    }
                },
                {
                        "match_phrase": {
                            "network_group_id": {
                                "query": "{{networkGroupId}}"
                            }
                        }
                },
                {
                    "match": {
                        "network_id": "{{networkId}}"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "source_edge_router_id": "{{resourceId}}"
                    }
                },
                {
                    "match": {
                        "destination_edge_router_id": "{{resourceId}}"
                    }
                }
            ],
            "minimum_should_match": 1,
            "must_not": [
                {
                    "term": {
                        "source_edge_router_name.keyword": ""
                    }
                },
                {
                    "term": {
                        "destination_edge_router_name.keyword": ""
                    }
                }
            ],
            "filter": [
                {
                    "script": {
                        "script": "doc['source_edge_router_name.keyword'].value !=  doc['destination_edge_router_name.keyword'].value"
                    }
                },
                {
                    "match_phrase": {
                        "network_id": "{{networkId}}"
                    }
                },
                {
                    "range": {
                        "timestamp": {
                            "gte": "{{gte}}",
                            "lte": "{{lte}}",
                            "format": "strict_date_optional_time||epoch_millis"
                        }
                    }
                }
                {{#eventSourceId}},
				{
					"terms": {
						"event_src_id.keyword": {{#toJson}}eventSourceId{{/toJson}}
					}
				}
				{{/eventSourceId}}
            ]
        }
    },
    "aggs": {
        "links": {
            "terms": {
                "field": "link_id.keyword",
                "size": "{{aggTermSize}}",
                "order": {
                    "_key": "desc"
                },
                "min_doc_count": 1
            },
            "aggs": {
                "source_routers": {
                    "terms": {
                        "field": "source_edge_router_name.keyword",
                        "size": 100,
                        "order": {
                            "_key": "desc"
                        },
                        "min_doc_count": 1
                    },
                    "aggs": {
                        "destination_routers": {
                            "terms": {
                                "field": "destination_edge_router_name.keyword",
                                "size": 100,
                                "order": {
                                    "_key": "desc"
                                },
                                "min_doc_count": 1
                            },
                            "aggs": {
                                "intervals": {
                                    "date_histogram": {
                                        "field": "timestamp",
                                        "min_doc_count": 0,
                                        "extended_bounds": {
                                            "min": "{{dayFilterMin}}",
                                            "max": "{{dayFilterMax}}"
                                        },
                                        "format": "strict_date_optional_time",
                                        "fixed_interval": "{{interval}}"
                                    },
                                    "aggs": {
                                        "latency": {
                                            "max": {
                                                "field": "{{latencyType}}"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
     "size": 0
    }
    EOT
}

resource "elasticstack_elasticsearch_script" "nclinklatency_timeouts_date_filter_v8_tmpl" {
    script_id = "nclinklatency_timeouts_date_filter_v8_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "query": {
            "bool": {
                "must": [],
                "filter": [
                    {
                        "range": {
                            "timestamp": {
                                "format": "strict_date_optional_time||epoch_millis",
                                "gte": "{{gte}}",
                                "lte": "{{lte}}"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_id": {
                                "query": "{{networkId}}"
                            }
                        }
                    }
                ],
                "must_not": [
                    {
                        "match_phrase": {
                            "max": 88888888888
                        }
                    },
                    {
                        "term": {
                            "source_edge_router_name.keyword": ""
                        }
                    },
                    {
                        "term": {
                            "destination_edge_router_name.keyword": ""
                        }
                    }
                ]
            }
        },
        "aggs": {
            "time_buckets": {
                "date_histogram": {
                    "field": "timestamp",
                    "fixed_interval": "1h",
                    "time_zone": "{{timeZone}}",
                    "min_doc_count": 1
                },
                "aggs": {
                    "source_routers": {
                        "terms": {
                            "field": "source_edge_router_name.keyword",
                            "order": {
                                "_key": "desc"
                            },
                            "size": "{{aggTermSize}}"
                        },
                        "aggs": {
                            "destination_routers": {
                                "terms": {
                                    "field": "destination_edge_router_name.keyword",
                                    "order": {
                                        "_key": "desc"
                                    },
                                    "size": "{{aggTermSize}}"
                                },
                                "aggs": {
                                    "avg_of": {
                                        "avg": {
                                            "field": "max"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "size": "{{size}}"
    })
}

resource "elasticstack_elasticsearch_script" "nclinklatency_timeouts_data_query_v8_tmpl" {
    script_id = "nclinklatency_timeouts_data_query_v8_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "query": {
            "bool": {
                "must": [],
                "filter": [
                    {
                        "range": {
                            "timestamp": {
                                "format": "strict_date_optional_time||epoch_millis",
                                "gte": "{{gte}}",
                                "lte": "{{lte}}"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "max": 88888888888
                        }
                    },
                    {
                        "match_phrase": {
                            "network_id": "{{networkId}}"
                        }
                    }
                ],
                "should": [],
                "must_not": [
                    {
                        "match_phrase": {
                            "destination_edge_router_name": ""
                        }
                    },
                    {
                        "match_phrase": {
                            "source_edge_router_name": ""
                        }
                    }
                ]
            }
        },
        "track_total_hits": false,
        "sort": [
            {
                "source_edge_router_name.keyword": {
                    "order": "desc"
                }
            },
            {
                "destination_edge_router_name.keyword": {
                    "order": "desc"
                }
            },
            {
                "timestamp": {
                    "order": "desc",
                    "unmapped_type": "boolean"
                }
            }
        ],
        "fields": [
            {
                "field": "source_edge_router_name"
            },
            {
                "field": "destination_edge_router_name"
            },
            {
                "field": "timestamp"
            },
            {
                "field": "max"
            }
        ],
        "size": "{{size}}",
        "version": true,
        "script_fields": {},
        "stored_fields": [
            "*"
        ],
        "runtime_mappings": {},
        "_source": false
    })
}
