######## ncutilization templates ########
# ncutilization_active_endpoints_tmpl
# ncutilization_endpoint_visualization_tmpl
# ncutilization_metrics_usage_bytes_by_component_tmpl
# ncutilization_metrics_usage_sum_by_component_tmpl
# ncutilization_resource_tmpl
# ncutilization_rx_and_tx_tmpl
# ncutilization_top_components_tmpl
# ncutilization_usage_summary_tmpl
# nc_utilization_tmpl
# ncutilization_usage_report_tmpl


resource "elasticstack_elasticsearch_script" "ncutilization_active_endpoints_tmpl" {
    script_id = "ncutilization_active_endpoints_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "_source": {
            "includes": [
                "identity_id.keyword"
            ]
        },
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "timestamp": {
                                "gte": "{{gte}}",
                                "lte": "{{lte}}",
                                    "format": "strict_date_optional_time||epoch_millis"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_id.keyword": {
                                "query": "{{networkId}}"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_group_id.keyword": {
                                "query": "{{networkGroupId}}"
                            }
                        }
                    }
                ]
            }
        },
        "size": "{{size}}",
        "aggs": {
            "date_buckets": {
                "date_histogram": {
                    "field": "timestamp",
                    "fixed_interval": "{{interval}}",
                    "time_zone": "{{timeZone}}",
                    "min_doc_count": 1
                },
                "aggs": {
                    "unique_identities": {
                        "cardinality": {
                            "field": "identity_id.keyword"
                        }
                    }
                }
            }
        }
    })
}

resource "elasticstack_elasticsearch_script" "ncutilization_endpoint_visualization_tmpl" {
    script_id = "ncutilization_endpoint_visualization_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "_source": {
            "includes": [
                "usage_type.keyword",
                "nf_endpoint_name.keyword",
                "nf_service_name.keyword",
                "nf_host_name.keyword",
                "nf_edge_router_name.keyword",
                "usage"
            ]
        },
        "size": "{{size}}",
        "aggs": {
            "endpoint_bucket": {
                "terms": {
                    "field": "nf_endpoint_name.keyword",
                    "order": {
                        "1": "desc"
                    },
                    "size": "{{aggTermSize}}"
                },
                "aggs": {
                    "1": {
                        "sum": {
                            "field": "usage"
                        }
                    },
                    "services_bucket": {
                        "terms": {
                            "field": "nf_service_name.keyword",
                            "order": {
                                "1": "desc"
                            },
                            "size": 500
                        },
                        "aggs": {
                            "1": {
                                "sum": {
                                    "field": "usage"
                                }
                            },
                            "hosts_bucket": {
                                "terms": {
                                    "field": "nf_host_name.keyword",
                                    "order": {
                                        "1": "desc"
                                    },
                                    "size": 500
                                },
                                "aggs": {
                                    "1": {
                                        "sum": {
                                            "field": "usage"
                                        }
                                    },
                                    "routers_bucket": {
                                        "terms": {
                                            "field": "nf_edge_router_name.keyword",
                                            "order": {
                                                "1": "desc"
                                            },
                                            "size": 500
                                        },
                                        "aggs": {
                                            "1": {
                                                "sum": {
                                                    "field": "usage"
                                                }
                                            },
                                            "usage_type_bucket": {
                                                "terms": {
                                                    "field": "usage_type.keyword",
                                                    "order": {
                                                        "1": "desc"
                                                    },
                                                    "size": 5
                                                },
                                                "aggs": {
                                                    "1": {
                                                        "sum": {
                                                            "field": "usage"
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "script_fields": {},
        "stored_fields": [
            "*"
        ],
        "runtime_mappings": {},
        "query": {
            "bool": {
                "must": [
                    {
                        "match_phrase": {
                            "network_id.keyword": "{{networkId}}"
                        }
                    },
                    {
                        "match_phrase": {
                            "nf_endpoint_id.keyword": "{{endpointId}}"
                        }
                    },
                    {
                        "match_phrase": {
                            "nf_service_name.keyword": "{{serviceName}}"
                        }
                    }
                ],
                "filter": [
                    {
                        "range": {
                            "timestamp": {
                                    "format": "strict_date_optional_time||epoch_millis",
                                "gte": "{{gte}}",
                                "lte": "{{lte}}"
                            }
                        }
                    }
                ],
                "should": [],
                "must_not": [
                    {
                        "match_phrase": {
                            "usage_type.keyword": "usage.egress.rx"
                        }
                    },
                    {
                        "match_phrase": {
                            "usage_type.keyword": "usage.ingress.rx"
                        }
                    }
                ]
            }
        }
    })
}

resource "elasticstack_elasticsearch_script" "ncutilization_metrics_usage_bytes_by_component_tmpl" {
    script_id = "ncutilization_metrics_usage_bytes_by_component_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
			"track_total_hits": false,
			"query": {
				"bool": {
					"filter": [
						{
							"term": {
								"network_id.keyword": "{{networkId}}"
							}
						},
						{
							"term": {
								"network_group_id.keyword": "{{networkGroupId}}"
							}
						},
						{
							"terms": {
								"usage_type.keyword": ["usage.ingress.tx", "usage.egress.tx"]
							}
						},
						{
							"range": {
								"timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "strict_date_optional_time||epoch_millis"
								}
							}
						}
						{{#componentName}},
						{
							"term": {
								"{{componentName}}": "{{componentValue}}"
							}
						}
						{{/componentName}}
						{{#attributes}},
						{
							"terms": {
								"attributes.keyword": {{#toJson}}attributes{{/toJson}}
							}
						}
						{{/attributes}}
					]
				}
			},
			"size": "{{size}}",
			"aggs": {
				"items": {
					"terms": {
						"field": "{{aggComponentName}}",
						"size": "{{aggTermSize}}",
						"collect_mode": "breadth_first",
						"order": {
							"usage_bytes": "desc"
						}
					},
					"aggs": {
						"usage_bytes": {
							"sum": {
								"field": "usage"
							}
						},
						"timebuckets": {
							"date_histogram": {
								"field": "timestamp",
								"fixed_interval": "{{interval}}",
								"time_zone": "{{timeZone}}",
								"min_doc_count": 1
							},
							"aggs": {
								"usage_bytes": {
									"sum": {
										"field": "usage"
									}
								}
							}
						}
					}
				}
			}
		}
EOT
}

resource "elasticstack_elasticsearch_script" "ncutilization_metrics_usage_sum_by_component_tmpl" {
    script_id = "ncutilization_metrics_usage_sum_by_component_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
			"_source": {
				"includes": [
					"{{aggComponentName}}",
					"usage"
				]
			},
			"query": {
				"bool": {
					"must": [
						{
							"match_phrase": {
								"network_id.keyword": {
									"query": "{{networkId}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id.keyword": {
									"query": "{{networkGroupId}}"
								}
							}
						},
						{
							"bool": {
								"should": [
									{
										"match_phrase": {
											"usage_type.keyword": "usage.ingress.tx"
										}
									},
									{
										"match_phrase": {
											"usage_type.keyword": "usage.egress.tx"
										}
									}
								],
								"minimum_should_match": 1
							}
						},
						{
							"range": {
								"@timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "strict_date_optional_time||epoch_millis"
								}
							}
						}
						
						{{#componentName}},
						 
						  {
							"match_phrase": {
								"{{componentName}}": {
									"query": "{{componentValue}}"
								}
							}
					  	
						}
						{{/componentName}}
						
					],
					"filter": {
						"bool": {
							"must": [
								{
									"match_all": {}
								}
								
								{{#attributes}},
								{
									"terms": {
									"attributes.keyword": {{#toJson}}attributes{{/toJson}}
										
									}
								}
							 {{/attributes}}
								
								
							]
						}
					}
				}
			},
			"size": "{{size}}",
			"aggs": {
				"items": {
					"terms": {
						"field": "{{aggComponentName}}",
						"size": "{{aggTermSize}}",
						"order": {
							"item_sum": "desc"
						}
					},
					"aggs": {
						"item_sum": {
							"sum": {
								"field": "usage"
							}
						}
					}
				}
			}
		}
	EOT	
}

resource "elasticstack_elasticsearch_script" "ncutilization_resource_tmpl" {
    script_id = "ncutilization_resource_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
			"_source": {
				"includes": [
					"{{aggComponentName}}",
					"usage"
				]
			},
			"query": {
				"bool": {
					"must": [
						{
							"range": {
								"timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "strict_date_optional_time||epoch_millis"
								}
							}
						},
						{
							"match_phrase": {
								"network_id.keyword": {
									"query": "{{networkId}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id.keyword": {
									"query": "{{networkGroupId}}"
								}
							}
						}
						{{#componentName}},
						 
						  {
							"match_phrase": {
								"{{componentName}}": {
									"query": "{{componentValue}}"
								}
							}
					  	
						}
						{{/componentName}}
						,
						{
							"bool": {
								"should": [
									{
										"match_phrase": {
											"usage_type.keyword": "usage.ingress.tx"
										}
									},
									{
										"match_phrase": {
											"usage_type.keyword": "usage.egress.tx"
										}
									}
								],
								"minimum_should_match": 1
							}
						}
					]
				}
			},
			"size": "{{size}}",
			"aggs": {
				"unique_items": {
					"terms": {
						"field": "{{aggComponentName}}",
						"size": "{{aggTermSize}}",
						"order": {
							"item_sum": "desc"
						}
					},
					"aggs": {
						"item_sum": {
							"sum": {
								"field": "usage"
							}
						}
					}
				}
			}
		}
EOT
}

resource "elasticstack_elasticsearch_script" "ncutilization_rx_and_tx_tmpl" {
    script_id = "ncutilization_rx_and_tx_tmpl"
    lang      = "mustache"
    source    = <<-EOT
     {
			"_source": {
				"includes": [
					"usage_type.keyword",
					"usage",
					"timestamp"
				]
			},
			"query": {
				"bool": {
					"must": [
						{
							"match_phrase": {
								"network_id": {
									"query": "{{networkId}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id": {
									"query": "{{networkGroupId}}"
								}
							}
						},
						{
							"bool": {
								"should": [
									{
										"match_phrase": {
											"usage_type.keyword": "usage.ingress.tx"
										}
									},
									{
										"match_phrase": {
											"usage_type.keyword": "usage.egress.tx"
										}
									}
								],
								"minimum_should_match": 1
							}
						},
						{
							"range": {
								"@timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "strict_date_optional_time||epoch_millis"
								}
							}
						}
						
						{{#componentName}},
						{
							"match_phrase": {
								"{{componentName}}": {
									"query": "{{componentValue}}"
								}
							}
						}
						{{/componentName}}
					],
					"filter": {
						"bool": {
							"must": [
								{
									"match_all": {}
								}
								 {{#attributes}},
								{
									"terms": {
									"attributes.keyword": {{#toJson}}attributes{{/toJson}}
										
									}
								}
							 {{/attributes}}
							]
						}
					}
				}
			},
			"size": "{{size}}",
			"aggs": {
				"timebuckets": {
					"date_histogram": {
						"field": "timestamp",
						"fixed_interval": "{{interval}}",
						"time_zone": "{{timeZone}}",
						"min_doc_count": 1
					},
					"aggs": {
						"usage_sums": {
							"terms": {
								"field": "usage_type.keyword",
								"size": 2,
								"order": {
									"usage_bytes": "desc"
								}
							},
							"aggs": {
								"usage_bytes": {
									"sum": {
										"field": "usage"
									}
								}
							}
						}
					}
				}
			}
		}
	EOT	
}

resource "elasticstack_elasticsearch_script" "ncutilization_top_components_tmpl" {
    script_id = "ncutilization_top_components_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "_source": {
            "includes": [
                "{{aggComponentName}}",
                "usage"
            ]
        },
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "timestamp": {
                                "gte": "{{gte}}",
                                "lte": "{{lte}}",
                                    "format": "strict_date_optional_time||epoch_millis"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_id.keyword": {
                                "query": "{{networkId}}"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_group_id.keyword": {
                                "query": "{{networkGroupId}}"
                            }
                        }
                    },
                    {
                        "bool": {
                            "should": [
                                {
                                    "match_phrase": {
                                        "usage_type.keyword": "usage.ingress.tx"
                                    }
                                },
                                {
                                    "match_phrase": {
                                        "usage_type.keyword": "usage.egress.tx"
                                    }
                                }
                            ],
                            "minimum_should_match": 1
                        }
                    }
                ]
            }
        },
        "size": "{{size}}",
        "aggs": {
            "unique_items": {
                "terms": {
                    "field": "{{aggComponentName}}",
                    "size": "{{aggTermSize}}",
                    "order": {
                        "item_sum": "desc"
                    }
                },
                "aggs": {
                    "item_sum": {
                        "sum": {
                            "field": "usage"
                        }
                    }
                }
            }
        }
    })
}

resource "elasticstack_elasticsearch_script" "ncutilization_usage_summary_tmpl" {
    script_id = "ncutilization_usage_summary_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "aggs": {
            "usage_sums": {
                "terms": {
                    "field": "usage_type.keyword",
                    "size": "{{aggTermSize}}",
                    "order": {
                        "usage_bytes": "desc"
                    }
                },
                "aggs": {
                    "usage_bytes": {
                        "sum": {
                            "field": "usage"
                        }
                    }
                }
            }
        },
        "size": "{{size}}",
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "@timestamp": {
                                "gte": "{{gte}}",
                                "lte": "{{lte}}",
                                    "format": "strict_date_optional_time||epoch_millis"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_id": {
                                "query": "{{networkId}}"
                            }
                        }
                    },
                    {
                        "match_phrase": {
                            "network_group_id": {
                                "query": "{{networkGroupId}}"
                            }
                        }
                    },
                    {
                        "bool": {
                            "should": [
                                {
                                    "match_phrase": {
                                        "usage_type.keyword": "usage.ingress.tx"
                                    }
                                },
                                {
                                    "match_phrase": {
                                        "usage_type.keyword": "usage.egress.tx"
                                    }
                                }
                            ],
                            "minimum_should_match": 1
                        }
                    }
                ]
            }
        }
    })
}
resource "elasticstack_elasticsearch_script" "ncutilization_component_usage_tmpl" {
    script_id = "ncutilization_component_usage_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
			"query": {
			    "bool": {
					"must": [
					  	{
							"bool": {
								"should": [
									{
										"match_phrase": {
											"usage_type.keyword": "usage.ingress.tx"
										}
									},
									{
										"match_phrase": {
											"usage_type.keyword": "usage.egress.tx"
										}
									}
								],
								"minimum_should_match": 1
							}
						}
					  
					  ],
					"filter": [
						{
							"match_phrase": {
								"network_id.keyword": "{{networkId}}"
							}
						},
						{
							"match_phrase": {
								"network_group_id.keyword": {
									"query": "{{networkGroupId}}"
								}
							}
						}
							{{#aggFieldName}},
						 
						  {
							"match_phrase": {
							  "{{aggFieldName}}": "{{aggFieldValue}}"
							}
					  	
						}
						{{/aggFieldName}},
						{
							"range": {
								"timestamp": {
									"format": "strict_date_optional_time||epoch_millis",
									"gte": "{{gte}}",
									"lte": "{{lte}}"
								}
							}
						}
					]
				}
			},
			"size": "{{size}}",
			"aggs": {
				"MinuteBucket": {
						"date_histogram": {
							"field": "@timestamp",
							"calendar_interval": "{{interval}}",
							"time_zone": "{{timeZone}}",
							"min_doc_count": 1
						},
						"aggs": {
							"totalusage": {
								"sum": {
									"field": "usage"
								}
							}
						}
					}
				}
		}
	EOT
}



resource "elasticstack_elasticsearch_script" "nc_utilization_tmpl" {
    script_id = "nc_utilization_tmpl"
    lang      = "mustache"
    source    = <<-EOT
     {
    "size": 1,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "terms": {
                                    "network_id.keyword": {{#toJson}}network_List{{/toJson}}
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "minimum_should_match": "1",
                        "boost": 1.0
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "match_phrase": {
                                    "usage_type": {
                                        "query": "usage.egress.tx",
                                        "slop": 0,
                                        "zero_terms_query": "NONE",
                                        "boost": 1.0
                                    }
                                }
                            },
                            {
                                "match_phrase": {
                                    "usage_type": {
                                        "query": "usage.ingress.tx",
                                        "slop": 0,
                                        "zero_terms_query": "NONE",
                                        "boost": 1.0
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "minimum_should_match": "1",
                        "boost": 1.0
                    }
                },
                {
                    "range": {
                        "timestamp": {
                            "gte": "{{gte}}",
                            "lte": "{{lte}}"
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "aggregations": {
        "group_by_networkid": {
            "terms": {
                "field": "network_id.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                    {
                        "_count": "desc"
                    },
                    {
                        "_key": "asc"
                    }
                ]
            },
            "aggregations": {
                "sum_data_usage": {
                    "sum": {
                        "field": "usage"
                    }
                }
            }
        }
    }
}
	EOT
}


resource "elasticstack_elasticsearch_script" "ncutilization_usage_report_tmpl"  {
    script_id = "ncutilization_usage_report_tmpl"
    lang      = "mustache"
    source    = <<-EOT
     {
    "size": 1,
     "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "terms": {
                                    "network_group_id.keyword": {{#toJson}}network_group_list{{/toJson}}
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "minimum_should_match": "1",
                        "boost": 1.0
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "match_phrase": {
                                    "usage_type": {
                                        "query": "usage.egress.tx",
                                        "slop": 0,
                                        "zero_terms_query": "NONE",
                                        "boost": 1.0
                                    }
                                }
                            },
                            {
                                "match_phrase": {
                                    "usage_type": {
                                        "query": "usage.ingress.tx",
                                        "slop": 0,
                                        "zero_terms_query": "NONE",
                                        "boost": 1.0
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "minimum_should_match": "1",
                        "boost": 1.0
                    }
                },
                {
                    "range": {
                        "timestamp": {
                            "gte": "{{gte}}",
                            "lte": "{{lte}}"
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "aggregations": {
        "group_by_networkid": {
            "terms": {
                "field": "network_id.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                    {
                        "_count": "desc"
                    },
                    {
                        "_key": "asc"
                    }
                ]
            },
            "aggregations": {
                "sum_data_usage": {
                    "sum": {
                        "field": "usage"
                    }
                }
            }
        }
    }
}
	EOT
}
