Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F62348109
esquery.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, May 12, 14:13
Size
11 KB
Mime Type
text/x-python
Expires
Tue, May 14, 14:13 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17636078
Attached To
R11586 sausage-api
esquery.py
View Options
# © 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
Log In to Comment