######## ncutilization_v8 templates ########
# ncutilization_rx_and_tx_dashboard_v8_tmpl

# Viable RESOURCE_KEY Fields (dimensions):
# identity_id.keyword
# identity_name.keyword
# service_id.keyword
# service_name.keyword
# edge_router_id.keyword
# edge_router_name.keyword
# host_id.keyword
# host_name.keyword
# network_id.keyword

# ncutilization_rx_and_tx_dashboard_v8_tmpl
resource "elasticstack_elasticsearch_script" "ncutilization_rx_and_tx_dashboard_v8_tmpl" {
    script_id = "ncutilization_rx_and_tx_dashboard_v8_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}}"
								}
							}
						},
						{
							"range": {
								"timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "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": {
				"time_buckets": {
					"date_histogram": {
						"field": "timestamp",
						"calendar_interval": "{{calendar_interval}}",
						"time_zone": "{{timeZone}}",
						"min_doc_count": 1
					},
					"aggs": {
						"tx": {
							"sum": {
								"field": "usage.egress.tx"
							}
						},
						"rx": {
							"sum": {
								"field": "usage.ingress.tx"
							}
						}
					}
				}
			}
		}
    
    EOT
}

# ncutilization_top_components_v8_tmpl
resource "elasticstack_elasticsearch_script" "ncutilization_top_components_v8_tmpl" {
    script_id = "ncutilization_top_components_v8_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
  "query": {
 "bool": {
"must": [
    {
   "range": {
  "timestamp": {
 "gte": "{{gte}}",
 "lte": "{{lte}}",
 "format": "epoch_millis"
  }
   }
    },
    {
   "match_phrase": {
  "network_id.keyword": {
 "query": "{{networkId}}"
  }
   }
    },
    {
   "match_phrase": {
  "network_group_id.keyword": {
 "query": "{{networkGroupId}}"
  }
   }
    }
]
 }
  },
  "size": "{{size}}",
  "aggs": {
 "Top10Services": {
"terms": {
    "field": "{{aggComponentName}}",
    "order": {
   "tx": "desc"
    },
    "size": {{component_size}}{{^component_size}}10{{/component_size}}
},
"aggs": {
    "tx": {
   "sum": {
  "field": "usage.egress.tx"
   }
    },
    "rx": {
   "sum": {
  "field": "usage.ingress.tx"
   }
    },
    "total_sum": {
   "bucket_script": {
  "buckets_path": {
 "sumtx": "tx",
 "sumrx": "rx"
  },
  "script": "params.sumtx + params.sumrx"
   }
    },
     "sort_by_total_sum": {
                    "bucket_sort": {
                        "sort": [
                            {
                                "total_sum": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
}
 }
  }
   }
EOT
}

# ncutilization_metrics_usage_bytes_by_component_v8_tmpl

resource "elasticstack_elasticsearch_script" "ncutilization_metrics_usage_bytes_by_component_v8_tmpl" {
    script_id = "ncutilization_metrics_usage_bytes_by_component_v8_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    
    {
			"_source": {
			},
			"query": {
				"bool": {
					"must": [
						{
							"match_phrase": {
								"network_id": {
									"query": "{{networkId}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id": {
									"query": "{{networkGroupId}}"
								}
							}
						},
						
						{
							"range": {
								"timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "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": {
    "top10Services": {
      "terms": {
        "field": "{{aggComponentName}}",
        "order": {
          "_key": "desc"
        },
        "size": 10
      },
      "aggs": {
        "time_buckets": {
          "date_histogram": {
            "field": "timestamp",
            "fixed_interval": "{{interval}}",
            "time_zone": "{{timeZone}}",
            "min_doc_count": 1
          },
          "aggs": {
            "tx": {
              "sum": {
                "field": "usage.egress.tx"
              }
            },
            "rx": {
              "sum": {
                "field": "usage.ingress.tx"
              }
            },
            "total_sum": {
              "bucket_script": {
                "buckets_path": {
                  "sumtx": "tx",
                  "sumrx": "rx"
                },
                "script": "params.sumtx + params.sumrx"
              }
            }
          }
        }
      }
    }
  }
}
EOT
}

# ncutilization_metrics_usage_sum_by_component_v8_tmpl

resource "elasticstack_elasticsearch_script" "ncutilization_metrics_usage_sum_by_component_v8_tmpl" {
    script_id = "ncutilization_metrics_usage_sum_by_component_v8_tmpl"
    lang      = "mustache"
    source    = <<-EOT
    {
			"_source": {},
			"query": {
				"bool": {
					"must": [
						{
							"match_phrase": {
								"network_id.keyword": {
									"query": "{{networkId}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id.keyword": {
									"query": "{{networkGroupId}}"
								}
							}
						},
						{
							"range": {
								"timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "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": {
    "Top10Services": {
      "terms": {
        "field": "{{aggComponentName}}",
        "order": {
          "tx": "desc"
        },
        "size": 10
      },
      "aggs": {
        "tx": {
          "sum": {
            "field": "usage.egress.tx"
          }
        },
        "rx": {
          "sum": {
            "field": "usage.ingress.tx"
          }
        },
        "total_sum": {
          "bucket_script": {
            "buckets_path": {
              "sumtx": "tx",
              "sumrx": "rx"
            },
            "script": "params.sumtx + params.sumrx"
          }
        },
        "sort_by_total_sum": {
                    "bucket_sort": {
                        "sort": [
                            {
                                "total_sum": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
        
      }
    }
  }
}

EOT
}

# ncutilization_usage_summary_v8_tmpl
resource "elasticstack_elasticsearch_script" "ncutilization_usage_summary_v8_tmpl" {
    script_id = "ncutilization_usage_summary_v8_tmpl"
    lang      = "mustache"
    source    = jsonencode({
        "aggs" : {
            "tx" : {
                "sum" : {
                    "field" : "usage.egress.tx"
                }
            },
            "rx" : {
                "sum" : {
                    "field" : "usage.ingress.tx"
                }
            }
        },
        "size" : 0,
        "query" : {
            "bool" : {
                "filter" : [
                    {
                        "match_phrase" : {
                            "network_id.keyword" : "{{networkId}}"
                        }
                    },
                    {
                        "match_phrase" : {
                            "network_group_id.keyword" : "{{networkGroupId}}"
                        }
                    },
                    {
                        "range" : {
                            "timestamp" : {
                                "format" : "strict_date_optional_time",
                                "gte" : "{{gte}}",
                                "lte" : "{{lte}}"
                            }
                        }
                    }
                ]
            }
        }
    })
}
