Page MenuHomec4science

esquery.py
No OneTemporary

File Metadata

Created
Sun, May 12, 14:13

esquery.py

# © All rights reserved. ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE,
# Switzerland
# SCITAS - Scientific IT and Application Support, 2021
# See the LICENSE.txt file for more details.
from datetime import date
from sausage.loadconf import LoadConf
class ESQuery(object):
def __init__(self, index, entity, start=None, end=None, item=None):
newconf = LoadConf()
self.factor = getattr(newconf, index)
self.clusters = newconf.clusters
if not all((start, end)):
self.start = str(date.today().replace(day=1))
self.end = str(date.today())
self.set_query(entity)
else:
if start <= end:
self.start = start
self.end = end
self.set_range(entity, item)
def set_query(self, entity):
if entity == "account":
self.query = {
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"@end": {
"gte": self.start + "T00:00:00",
"lte": self.end + "T23:59:59"
}
}
},
{
"terms": {
"cluster.keyword": self.clusters
}
}
],
"must_not": [
{
"terms": {
"partition.keyword": ["build", "debug"]
}
},
{
"term": {
"state.keyword": "NODE_FAIL"
}
}
]
}
},
"aggs": {
"cluster": {
"terms": {
"field": "cluster.keyword",
"size": 10
},
"aggs": {
"account": {
"terms": {
"field": "account.keyword",
"size": 1000
},
"aggs": {
"cost": {
"sum": {
"script": {
"id": "cost_calculation",
"params": {"factors": self.factor}
}
}
}
}
}
}
}
}
}
elif entity == "user":
self.query = {
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"@end": {
"gte": self.start + "T00:00:00",
"lte": self.end + "T23:59:59"
}
}
},
{
"terms": {
"cluster.keyword": self.clusters
}
}
],
"must_not": [
{
"terms": {
"partition.keyword": ["build", "debug"]
}
},
{
"term": {
"state.keyword": "NODE_FAIL"
}
}
]
}
},
"aggs": {
"cluster": {
"terms": {
"field": "cluster.keyword",
"size": 10
},
"aggs": {
"account": {
"terms": {
"field": "account.keyword",
"size": 1000
},
"aggs": {
"user": {
"terms": {
"field": "username.keyword",
"size": 1000
},
"aggs": {
"cost": {
"sum": {
"script": {
"id": "cost_calculation",
"params": {
"factors": self.factor
}
}
}
}
}
}
}
}
}
}
}
}
def set_range(self, entity, item):
if entity == "account":
esfield = "account.keyword"
self.query = {
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"@end": {
"gte": self.start + "T00:00:00",
"lte": self.end + "T23:59:59"
}
}
},
{
"terms": {
"cluster.keyword": self.clusters
}
},
{
"term": {
esfield: {
"value": item
}
}
}
],
"must_not": [
{
"terms": {
"partition.keyword": ["build", "debug"]
}
},
{
"term": {
"state.keyword": "NODE_FAIL"
}
}
]
}
},
"aggs": {
"cluster": {
"terms": {
"field": "cluster.keyword",
"size": 10
},
"aggs": {
"cost": {
"sum": {
"script": {
"id": "cost_calculation",
"params": {
"factors": self.factor
}
}
}
}
}
}
}
}
elif entity == "user":
esfield = "username.keyword"
self.query = {
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"@end": {
"gte": self.start + "T00:00:00",
"lte": self.end + "T23:59:59"
}
}
},
{
"terms": {
"cluster.keyword": self.clusters
}
},
{
"term": {
esfield: {
"value": item
}
}
}
],
"must_not": [
{
"terms": {
"partition.keyword": ["build", "debug"]
}
},
{
"term": {
"state.keyword": "NODE_FAIL"
}
}
]
}
},
"aggs": {
"account": {
"terms": {
"field": "account.keyword",
"size": 10
},
"aggs": {
"cluster": {
"terms": {
"field": "cluster.keyword",
"size": 10
},
"aggs": {
"cost": {
"sum": {
"script": {
"id": "cost_calculation",
"params": {
"factors": self.factor
}
}
}
}
}
}
}
}
}
}

Event Timeline