# README #

### Netfoundry reporting service ###
## Create template
PUT _scripts/NW_UTILIZATION_BY_RX_AND_TX_TMPL
request body
## INQUIRY TEMPLATE
VERB GET ncutilization-*/_search/template
request body

Example 

GET ncutilization-*/_search/template
{
  "id": "NW_UTILIZATION_BY_RX_AND_TX_TMPL",
  "params": {
    "network_id_query_param": "955a7431-4930-4572-a952-16a83ae057ed",
    "organizationId_query_param": "93f9b94e-88dd-4ea3-beb1-cf7cc9476f1a",
    "timestamp_greater_than_equal_query_param":"now-30d",
    "timestamp_lesser_than_equal_query_param":"now",
    "usage_ingress_query_param":"usage.ingress.tx",
    "usage_egress_query_param":"usage.egress.tx",
    "timestamp_interval_query_param":"5m"
    
  }
}


GET _search
{
  "query": {
    "match_all": {}
  }
}

GET /_scripts/NW_UTILIZATION_BY_RX_AND_TX_TMPL

GET ncutilization-*/_search/template
{
	"id": "nw_utilization_by_rx_and_tx_tmpl",
	"params": {
		"network_id_query_param": "955a7431-4930-4572-a952-16a83ae057ed",
		"organizationId_query_param": "93f9b94e-88dd-4ea3-beb1-cf7cc9476f1a",
		"timestamp_greater_than_equal_query_param": "now-30d",
		"timestamp_lesser_than_equal_query_param": "now",
		"usage_ingress_query_param": "usage.ingress.tx",
		"usage_egress_query_param": "usage.egress.tx",
		"timestamp_interval_query_param": "5m",
		"size_query_param": "1",
		"timezone_query_param":"Asia/Calcutta"
	}
}



DELETE /_scripts/NW_UTILIZATION_BY_RX_AND_TX_TMPL
POST /_scripts/nw_utilization_by_rx_and_tx_tmpl
{
	"id": "nw_utilization_by_rx_and_tx_tmpl",
	"params": {
		"network_id_query_param": "955a7431-4930-4572-a952-16a83ae057ed",
		"organizationId_query_param": "93f9b94e-88dd-4ea3-beb1-cf7cc9476f1a",
		"timestamp_greater_than_equal_query_param": "now-30d",
		"timestamp_lesser_than_equal_query_param": "now",
		"usage_ingress_query_param": "usage.ingress.tx",
		"usage_egress_query_param": "usage.egress.tx",
		"timestamp_interval_query_param": "5m"
	}
}
PUT _scripts/nw_utilization_by_rx_and_tx_tmpl
{
	"script": {
		"lang": "mustache",
		"source": {
			"_source": {
				"includes": [
					"usage_type.keyword",
					"usage"
				]
			},
			"query": {
				"bool": {
					"must": [
						{
							"range": {
								"timestamp": {
									"gte": "{{timestamp_greater_than_equal_query_param}}",
									"lte": "{{timestamp_lesser_than_equal_query_param}}",
									"format": "epoch_millis"
								}
							}
						},
						{
							"match_phrase": {
								"network_id.keyword": {
									"query": "{{network_id_query_param}}"
								}
							}
						},
						{
							"match_phrase": {
								"organizationId.keyword": {
									"query": "{{organizationId_query_param}}"
								}
							}
						},
						{
							"bool": {
								"should": [
									{
										"match_phrase": {
											"usage_type.keyword": "usage.egress.rx"
										}
									},
									{
										"match_phrase": {
											"usage_type.keyword": "usage.ingress.rx"
										}
									}
								],
								"minimum_should_match": 1
							}
						}
					]
				}
			},
			"size": "{{size_query_param}}",
			"aggs": {
				"timebuckets": {
					"date_histogram": {
						"field": "timestamp",
						"interval": "{{timestamp_interval_query_param}}",
						"time_zone": "{{timezone_query_param}}",
						"min_doc_count": 1
					},
					"aggs": {
						"usage_sums": {
							"terms": {
								"field": "usage_type.keyword",
								"size": 2,
								"order": {
									"usage_bytes": "desc"
								}
							},
							"aggs": {
								"usage_bytes": {
									"sum": {
										"field": "usage"
									}
								}
							}
						}
					}
				}
			}
		}
	}
}