6
6
import os
7
7
from typing import Any
8
8
from fastapi import FastAPI , HTTPException
9
- from jinja2 import Template
10
9
import pkg_resources
11
- import yaml
12
10
from fmatch .logrus import SingletonLogger
11
+ from pkg .config import load_config
13
12
import pkg .constants as cnsts
14
13
15
14
from . import runTest
@@ -37,7 +36,8 @@ async def daemon_changepoint( # pylint: disable = R0913
37
36
json: json object of the changepoints and metrics
38
37
"""
39
38
parameters = {"version" : version }
40
- config_file_name = test_name + ".yml"
39
+ config_file_name = f"{ test_name } .yml"
40
+ config_path = pkg_resources .resource_filename ("configs" , config_file_name )
41
41
option_arguments = {
42
42
"config" : config_file_name ,
43
43
"save_data_path" : "output.csv" ,
@@ -47,7 +47,7 @@ async def daemon_changepoint( # pylint: disable = R0913
47
47
"uuid" : uuid ,
48
48
"lookback" :lookback ,
49
49
"baseline" : baseline ,
50
- "configMap" : render_template ( config_file_name , parameters ),
50
+ "configMap" : load_config ( config_path , parameters ),
51
51
"convert_tinyurl" : convert_tinyurl .lower () not in "false" ,
52
52
}
53
53
filter_changepoints = (
@@ -90,7 +90,7 @@ async def get_options() -> Any:
90
90
raise HTTPException (status_code = 500 , detail = str (e )) from e
91
91
92
92
@app .get ("/daemon/anomaly" )
93
- async def daemon_anomaly ( # pylint: disable = R0913
93
+ async def daemon_anomaly ( # pylint: disable = R0913, R0914
94
94
version : str = "4.17" ,
95
95
uuid : str = "" ,
96
96
baseline : str = "" ,
@@ -111,6 +111,7 @@ async def daemon_anomaly( # pylint: disable = R0913
111
111
"""
112
112
parameters = {"version" : version }
113
113
config_file_name = test_name + ".yml"
114
+ config_path = pkg_resources .resource_filename ("configs" , config_file_name )
114
115
option_arguments = {
115
116
"config" : config_file_name ,
116
117
"save_data_path" : "output.csv" ,
@@ -120,7 +121,7 @@ async def daemon_anomaly( # pylint: disable = R0913
120
121
"uuid" : uuid ,
121
122
"lookback" :lookback ,
122
123
"baseline" : baseline ,
123
- "configMap" : render_template ( config_file_name , parameters ),
124
+ "configMap" : load_config ( config_path , parameters ),
124
125
"anomaly_window" : int (anomaly_window ),
125
126
"min_anomaly_percent" :int (min_anomaly_percent ),
126
127
"convert_tinyurl" : convert_tinyurl .lower () not in "false" ,
@@ -136,22 +137,3 @@ async def daemon_anomaly( # pylint: disable = R0913
136
137
for key , value in result .items ():
137
138
result [key ] = list (filter (lambda x : x .get ("is_changepoint" , False ), value ))
138
139
return result
139
-
140
-
141
- def render_template (test_name : str , parameters : dict [str ,Any ]) -> Any :
142
- """replace parameters in the config file
143
-
144
- Args:
145
- file_name (str): the config file
146
- parameters (dict): parameters to be replaces
147
-
148
- Returns:
149
- dict: configMap in dict
150
- """
151
- config_path = pkg_resources .resource_filename ("configs" , test_name )
152
- with open (config_path , "r" , encoding = "utf-8" ) as template_file :
153
- template_content = template_file .read ()
154
- template = Template (template_content )
155
- rendered_config_yaml = template .render (parameters )
156
- rendered_config = yaml .safe_load (rendered_config_yaml )
157
- return rendered_config
0 commit comments