Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
Read_from_Kairos_Component
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dasharatha.vamshi
Read_from_Kairos_Component
Commits
84e3ccd7
Commit
84e3ccd7
authored
Feb 16, 2021
by
Akshay G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Kairos Aggregation functionality
parent
f0fd658e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
63 additions
and
10 deletions
+63
-10
Dockerfile
Dockerfile
+2
-3
Dockerfile_bkp
Dockerfile_bkp
+5
-0
main.py
main.py
+8
-5
scripts/common/config_parser.py
scripts/common/config_parser.py
+7
-1
scripts/common/constants.py
scripts/common/constants.py
+14
-1
scripts/utils.py
scripts/utils.py
+27
-0
No files found.
Dockerfile
View file @
84e3ccd7
FROM
python:3.7-slim
FROM
azacrknowledgelens.azurecr.io/ai-forecasting/batch-multi-karios-read-component:v0.6
ADD
. /opt
ADD
. /opt
WORKDIR
/opt
WORKDIR
/opt
RUN
pip
install
-r
requirements.txt
CMD
python main.py
CMD
python main.py
\ No newline at end of file
Dockerfile_bkp
0 → 100755
View file @
84e3ccd7
FROM
python:3.7-slim
ADD
. /opt
WORKDIR
/opt
RUN
pip
install
-r
requirements.txt
CMD
python main.py
\ No newline at end of file
main.py
View file @
84e3ccd7
import
requests
import
requests
from
pandas
import
DataFrame
from
pandas
import
DataFrame
from
datetime
import
datetime
from
datetime
import
datetime
from
scripts.common.config_parser
import
*
from
scripts.common.config_parser
import
*
from
scripts.common.logsetup
import
logger
from
scripts.common.logsetup
import
logger
from
scripts.utils
import
get_aggregation_query
from
scripts.common.constants
import
KariosConstants
,
ComponentExceptions
from
scripts.common.constants
import
KariosConstants
,
ComponentExceptions
...
@@ -66,7 +66,9 @@ def get_data(query):
...
@@ -66,7 +66,9 @@ def get_data(query):
query
.
pop
(
KariosConstants
.
END_ABSOLUTE_KEY
)
query
.
pop
(
KariosConstants
.
END_ABSOLUTE_KEY
)
query
.
pop
(
KariosConstants
.
END_RELATIVE_KEY
)
query
.
pop
(
KariosConstants
.
END_RELATIVE_KEY
)
if
KariosConstants
.
AGGREGATORS_KEY
not
in
config
.
keys
():
if
config
.
get
(
KariosConstants
.
AGGREGATION_OPS
)
!=
"None"
or
config
.
get
(
KariosConstants
.
AGGREGATION_OPS
)
is
not
None
:
query
[
KariosConstants
.
METRICS_KEY
][
0
][
KariosConstants
.
AGGREGATORS_KEY
]
.
append
(
get_aggregation_query
(
config
))
else
:
query
[
KariosConstants
.
METRICS_KEY
][
0
]
.
pop
(
KariosConstants
.
AGGREGATORS_KEY
)
query
[
KariosConstants
.
METRICS_KEY
][
0
]
.
pop
(
KariosConstants
.
AGGREGATORS_KEY
)
if
KariosConstants
.
GROUPBY_KEY
not
in
config
.
keys
():
if
KariosConstants
.
GROUPBY_KEY
not
in
config
.
keys
():
query
[
KariosConstants
.
METRICS_KEY
][
0
]
.
pop
(
KariosConstants
.
GROUPBY_KEY
)
query
[
KariosConstants
.
METRICS_KEY
][
0
]
.
pop
(
KariosConstants
.
GROUPBY_KEY
)
...
@@ -77,14 +79,13 @@ def get_data(query):
...
@@ -77,14 +79,13 @@ def get_data(query):
raise
Exception
(
ComponentExceptions
.
MISSING_TAG_HIERARCHY_EXCEPTION
)
raise
Exception
(
ComponentExceptions
.
MISSING_TAG_HIERARCHY_EXCEPTION
)
logger
.
info
(
"Querying Karios DB..."
)
logger
.
info
(
"Querying Karios DB..."
)
logger
.
debug
(
"Query --> {}"
.
format
(
query
))
response
=
requests
.
post
(
kairosdb_server
+
KariosConstants
.
KARIOS_API
,
data
=
json
.
dumps
(
query
))
response
=
requests
.
post
(
kairosdb_server
+
KariosConstants
.
KARIOS_API
,
data
=
json
.
dumps
(
query
))
if
response
.
status_code
==
KariosConstants
.
REQUEST_SUCCESS_CODE
:
if
response
.
status_code
==
KariosConstants
.
REQUEST_SUCCESS_CODE
:
logger
.
info
(
"Receiving data..."
)
logger
.
info
(
"Receiving data..."
)
data
=
response
.
json
()[
KariosConstants
.
QUERIES_KEY
][
0
][
KariosConstants
.
RESULTS_KEY
][
0
]
\
data
=
response
.
json
()[
KariosConstants
.
QUERIES_KEY
][
0
][
KariosConstants
.
RESULTS_KEY
][
0
]
\
[
KariosConstants
.
VALUES_KEY
]
[
KariosConstants
.
VALUES_KEY
]
df
=
DataFrame
.
from_dict
(
data
)
df
=
DataFrame
.
from_dict
(
data
)
logger
.
info
(
"Dataframe --> {}"
.
format
(
df
.
head
()))
if
len
(
df
)
>
0
:
if
len
(
df
)
>
0
:
df
.
columns
=
[
KariosConstants
.
TIMESTAMP_COLUMN_NAME
,
list
(
config
[
KariosConstants
.
TAG_HIERARCHY_KEY
]
.
keys
())
df
.
columns
=
[
KariosConstants
.
TIMESTAMP_COLUMN_NAME
,
list
(
config
[
KariosConstants
.
TAG_HIERARCHY_KEY
]
.
keys
())
[
0
]]
[
0
]]
...
@@ -92,13 +93,15 @@ def get_data(query):
...
@@ -92,13 +93,15 @@ def get_data(query):
logger
.
warn
(
"The dataframe is empty!"
)
logger
.
warn
(
"The dataframe is empty!"
)
df
=
DataFrame
(
columns
=
[
KariosConstants
.
TIMESTAMP_COLUMN_NAME
,
list
(
config
[
KariosConstants
.
df
=
DataFrame
(
columns
=
[
KariosConstants
.
TIMESTAMP_COLUMN_NAME
,
list
(
config
[
KariosConstants
.
TAG_HIERARCHY_KEY
]
.
keys
())[
0
]])
TAG_HIERARCHY_KEY
]
.
keys
())[
0
]])
logger
.
debug
(
"Dataframe -->
\n
{}"
.
format
(
df
.
head
()))
return
df
return
df
else
:
else
:
raise
Exception
(
response
.
json
()[
KariosConstants
.
ERRORS_KEY
])
raise
Exception
(
response
.
json
()[
KariosConstants
.
ERRORS_KEY
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
logger
.
debug
(
"Config --> {}"
.
format
(
config
))
logger
.
debug
(
KariosConstants
.
LOG_VAR_MESSAGE
.
format
(
"COMPONENT CONFIG"
,
json
.
dumps
(
config
,
indent
=
1
)
))
data
=
get_data
(
KariosConstants
.
QUERY
)
data
=
get_data
(
KariosConstants
.
QUERY
)
data
.
to_csv
(
os
.
path
.
join
(
config
[
'shared_volume'
],
'data.csv'
),
index
=
False
)
data
.
to_csv
(
os
.
path
.
join
(
config
[
'shared_volume'
],
'data.csv'
),
index
=
False
)
logger
.
info
(
"Data successfully written to --> {}"
.
format
(
os
.
path
.
join
(
config
[
'shared_volume'
],
'data.csv'
)))
logger
.
info
(
"Data successfully written to --> {}"
.
format
(
os
.
path
.
join
(
config
[
'shared_volume'
],
'data.csv'
)))
scripts/common/config_parser.py
View file @
84e3ccd7
...
@@ -4,6 +4,7 @@ import sys
...
@@ -4,6 +4,7 @@ import sys
import
yaml
import
yaml
import
json
import
json
from
scripts.utils
import
str2bool
# os.environ['config'] = '{"kairosdb_url": "http://192.168.0.207:8080",' \
# os.environ['config'] = '{"kairosdb_url": "http://192.168.0.207:8080",' \
# '"metric_name": "ilens.live_data.raw",' \
# '"metric_name": "ilens.live_data.raw",' \
# '"tag_hierarchy": "site_153$dept_1006$line_220$equipment_1258$tag_5790",' \
# '"tag_hierarchy": "site_153$dept_1006$line_220$equipment_1258$tag_5790",' \
...
@@ -37,9 +38,14 @@ config = {
...
@@ -37,9 +38,14 @@ config = {
"start_absolute"
:
os
.
environ
.
get
(
"start_absolute"
),
"start_absolute"
:
os
.
environ
.
get
(
"start_absolute"
),
"tag_hierarchy"
:
json
.
loads
(
os
.
environ
.
get
(
"tag_hierarchy"
)),
"tag_hierarchy"
:
json
.
loads
(
os
.
environ
.
get
(
"tag_hierarchy"
)),
"metric_name"
:
os
.
environ
.
get
(
"metric_name"
),
"metric_name"
:
os
.
environ
.
get
(
"metric_name"
),
"kairosdb_url"
:
os
.
environ
.
get
(
"kairosdb_url"
)
"kairosdb_url"
:
os
.
environ
.
get
(
"kairosdb_url"
),
"aggregation_ops"
:
os
.
environ
.
get
(
"aggregation_ops"
),
"aggregation_sampling_value"
:
os
.
environ
.
get
(
"aggregation_sampling_value"
),
"aggregation_sampling_unit"
:
os
.
environ
.
get
(
"aggregation_sampling_unit"
),
"aggregation_alignment"
:
os
.
environ
.
get
(
"aggregation_alignment"
)
}
}
if
not
os
.
path
.
exists
(
config
[
'shared_volume'
]):
if
not
os
.
path
.
exists
(
config
[
'shared_volume'
]):
sys
.
stderr
.
write
(
"Shared path does not exist!"
)
sys
.
stderr
.
write
(
"Shared path does not exist!"
)
sys
.
stderr
.
write
(
"Creating path --> {}"
.
format
(
config
[
'shared_volume'
]))
sys
.
stderr
.
write
(
"Creating path --> {}"
.
format
(
config
[
'shared_volume'
]))
os
.
makedirs
(
config
[
'shared_volume'
])
os
.
makedirs
(
config
[
'shared_volume'
])
scripts/common/constants.py
View file @
84e3ccd7
...
@@ -9,6 +9,11 @@ class KariosConstants:
...
@@ -9,6 +9,11 @@ class KariosConstants:
START_RELATIVE_KEY
=
"start_relative"
START_RELATIVE_KEY
=
"start_relative"
END_ABSOLUTE_KEY
=
"end_absolute"
END_ABSOLUTE_KEY
=
"end_absolute"
END_RELATIVE_KEY
=
"end_relative"
END_RELATIVE_KEY
=
"end_relative"
APPLY_AGGREGATION_KEY
=
"apply_aggregation"
AGGREGATION_OPS
=
"aggregation_ops"
AGGREGATION_SAMPLING_VALUE
=
"aggregation_sampling_value"
AGGREGATION_SAMPLING_UNIT
=
"aggregation_sampling_unit"
AGGREGATION_ALIGNMENT
=
"aggregation_alignment"
VALUE_KEY
=
"value"
VALUE_KEY
=
"value"
VALUES_KEY
=
"values"
VALUES_KEY
=
"values"
METRICS_KEY
=
"metrics"
METRICS_KEY
=
"metrics"
...
@@ -27,6 +32,7 @@ class KariosConstants:
...
@@ -27,6 +32,7 @@ class KariosConstants:
PLUGINS_KEY
=
"plugins"
PLUGINS_KEY
=
"plugins"
CACHE_TIME_KEY
=
"cache_time"
CACHE_TIME_KEY
=
"cache_time"
TIMESTAMP_COLUMN_NAME
=
"timestamp"
TIMESTAMP_COLUMN_NAME
=
"timestamp"
SAMPLING_KEY
=
"sampling"
QUERY
=
{
QUERY
=
{
METRICS_KEY
:
[
METRICS_KEY
:
[
{
{
...
@@ -34,7 +40,7 @@ class KariosConstants:
...
@@ -34,7 +40,7 @@ class KariosConstants:
},
},
NAME_KEY
:
None
,
NAME_KEY
:
None
,
AGGREGATORS_KEY
:
None
,
AGGREGATORS_KEY
:
list
()
,
GROUPBY_KEY
:
None
GROUPBY_KEY
:
None
}
}
...
@@ -52,6 +58,13 @@ class KariosConstants:
...
@@ -52,6 +58,13 @@ class KariosConstants:
UNIT_KEY
:
None
UNIT_KEY
:
None
}
}
}
}
ALIGNMENT_MAPPING
=
{
"None"
:
None
,
"Sample"
:
"align_sampling"
,
"Start Time"
:
"align_start_time"
,
"End Time"
:
"align_end_time"
}
LOG_VAR_MESSAGE
=
"
\n
"
+
"#"
*
25
+
"
\n
"
+
"{}"
+
"
\n
"
+
"#"
*
25
+
"
\n
"
+
"{}
\n
"
class
ComponentExceptions
:
class
ComponentExceptions
:
...
...
scripts/utils.py
0 → 100644
View file @
84e3ccd7
from
scripts.common.constants
import
KariosConstants
def
str2bool
(
txt
):
if
str
(
txt
)
.
lower
()
in
[
'True'
,
'true'
,
1
,
'1'
,
True
,
'yes'
]:
return
True
return
False
def
get_value_or_raise_exception
(
dictionary
,
key
):
value
=
dictionary
.
get
(
key
)
if
value
is
None
:
raise
Exception
(
"Invalid/Missing value for the key {} in {}"
.
format
(
key
,
dictionary
))
return
value
def
get_aggregation_query
(
config
):
agg_dict
=
{
KariosConstants
.
NAME_KEY
:
get_value_or_raise_exception
(
config
,
KariosConstants
.
AGGREGATION_OPS
)
.
lower
(),
KariosConstants
.
SAMPLING_KEY
:
{
KariosConstants
.
VALUE_KEY
:
get_value_or_raise_exception
(
config
,
KariosConstants
.
AGGREGATION_SAMPLING_VALUE
),
KariosConstants
.
UNIT_KEY
:
get_value_or_raise_exception
(
config
,
KariosConstants
.
AGGREGATION_SAMPLING_UNIT
)
.
lower
()
}
}
if
KariosConstants
.
ALIGNMENT_MAPPING
.
get
(
KariosConstants
.
AGGREGATION_ALIGNMENT
)
is
not
None
:
agg_dict
[
KariosConstants
.
ALIGNMENT_MAPPING
.
get
(
KariosConstants
.
AGGREGATION_ALIGNMENT
)]
=
True
return
agg_dict
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment