Skip to content

Commit e71c3af

Browse files
authored
feat: add dApp, explorer and deeploy in liveness API (#292)
* feat: add dApp, explorer and deeploy in liveness API * chore: inc ver
1 parent 27e3606 commit e71c3af

2 files changed

Lines changed: 70 additions & 21 deletions

File tree

extensions/business/liveness/liveness_api.py

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,41 @@
55
from naeural_core.business.default.web_app.supervisor_fast_api_web_app import SupervisorFastApiWebApp as BasePlugin
66
from ratio1.const.evm_net import EvmNetData
77

8-
__VER__ = '0.1.0'
8+
__VER__ = '0.2.0'
99

1010
_CONFIG = {
1111
**BasePlugin.CONFIG,
1212

1313
"RESPONSE_FORMAT" : "RAW",
1414

1515
"MONITORED_SERVICES" : {
16-
"DAUTH-API": EvmNetData.DAUTH_URL_KEY,
17-
"ORACLE-API": EvmNetData.EE_ORACLE_API_URL_KEY,
18-
"DEEPLOY-API": EvmNetData.EE_DEEPLOY_API_URL_KEY
16+
"DAUTH-API": {
17+
"url_key": EvmNetData.DAUTH_URL_KEY,
18+
"type": "api",
19+
},
20+
"ORACLE-API": {
21+
"url_key": EvmNetData.EE_ORACLE_API_URL_KEY,
22+
"type": "api",
23+
},
24+
"DEEPLOY-API": {
25+
"url_key": EvmNetData.EE_DEEPLOY_API_URL_KEY,
26+
"type": "api",
27+
},
28+
"DAPP-APP": {
29+
"url_key": EvmNetData.EE_DAPP_APP_URL_KEY,
30+
"type": "app",
31+
"expected_substring": "<title>Ratio1 App</title>",
32+
},
33+
"EXPLORER-APP": {
34+
"url_key": EvmNetData.EE_EXPLORER_APP_URL_KEY,
35+
"type": "app",
36+
"expected_substring": "<title>Ratio1 Explorer</title>",
37+
},
38+
"DEEPLOY-APP": {
39+
"url_key": EvmNetData.EE_DEEPLOY_APP_URL_KEY,
40+
"type": "app",
41+
"expected_substring": "<title>Ratio1 Deeploy</title>",
42+
},
1943
},
2044
'PORT': None,
2145
# 'ASSETS': 'plugins/business/fastapi/epoch_manager',
@@ -25,6 +49,17 @@
2549
}
2650

2751

52+
def build_service_url(service_cfg):
53+
url = service_cfg.get("url", None)
54+
if url is None:
55+
return None
56+
svc_type = service_cfg.get("type", "api")
57+
if svc_type == "api":
58+
full_url = to_docs_url(url)
59+
else:
60+
full_url = url
61+
return full_url
62+
2863

2964

3065
def to_docs_url(raw: str, default_scheme: str = "https") -> str:
@@ -121,31 +156,39 @@ def on_init(self):
121156
return
122157

123158

124-
def __get_service_url_mapping(self, service_key=''):
159+
def __get_service_config(self, service_key):
125160
dct_mapping = self.cfg_monitored_services
126-
url_key = dct_mapping.get(service_key.upper())
161+
service_config = dct_mapping.get(service_key.upper())
162+
if service_config is None:
163+
return None
164+
url_key = service_config.get("url_key", None)
127165
url = self.evm_net_data.get(url_key, None)
128166
if url is None:
129167
url = url_key # fallback to the key itself if not found
130-
return url
168+
service_config["url"] = url
169+
return service_config
131170

132171

133-
def __get_service_status(self, url=''):
172+
def __get_service_status(self, service_config):
134173
"""
135-
Uses self.requests to test if the url is a live swagger-based api
174+
Uses self.requests to test if the url is a live swagger-based api or frontend app.
136175
"""
137176
result = {}
177+
url = build_service_url(service_config)
138178
if url is None:
139179
return {"error": "Service URL is not available."}
140-
# now check that the url is in the format https://url/ and if it has any appended endpoints such as
141-
# https://url/endpoint then delete "endpoint" and replace with /docs
142-
url = to_docs_url(url)
180+
expected_marker = service_config.get("expected_substring")
143181

144182
self.P("Checking service status for URL: {}".format(url))
145183
response = self.requests.get(url, timeout=5)
146184
self.P("Received response {} for {}".format(response.status_code, url))
147185
result['status_code'] = response.status_code
186+
service_is_ok = False
148187
if response.status_code == 200:
188+
service_is_ok = True
189+
if expected_marker:
190+
service_is_ok = expected_marker.lower() in response.text.lower()
191+
if service_is_ok:
149192
result["status"] = "live"
150193
result["message"] = "Service is running smoothly."
151194
result["color"] = "#1B47F7"
@@ -159,8 +202,8 @@ def __get_service_status(self, url=''):
159202
def __get_all_services_statuses(self):
160203
statuses = {}
161204
for service in self.cfg_monitored_services.keys():
162-
url = self.__get_service_url_mapping(service)
163-
statuses[service] = self.__get_service_status(url)
205+
cfg = self.__get_service_config(service)
206+
statuses[service] = self.__get_service_status(cfg)
164207
return statuses
165208

166209
@BasePlugin.endpoint
@@ -176,18 +219,21 @@ def get_liveness_data(self, service=None):
176219
"DAUTH-API"
177220
"ORACLE-API"
178221
"DEEPLOY-API"
222+
"DAPP-APP"
223+
"EXPLORER-APP"
224+
"DEEPLOY-APP"
179225
180226
"""
181227
try:
182228
if service is None:
183229
data = self.__get_all_services_statuses()
184230
else:
185231
data = {}
186-
url = self.__get_service_url_mapping(service)
187-
if url is None:
232+
cfg = self.__get_service_config(service)
233+
if cfg is None:
188234
data['error'] = f"Service '{service}' is not recognized or not available."
189235
else:
190-
data[service.upper()] = self.__get_service_status(url)
236+
data[service.upper()] = self.__get_service_status(cfg)
191237
#endif
192238
except Exception as e:
193239
data = {'error' : str(e)}
@@ -212,17 +258,20 @@ def simple_liveness(self, service=None, extended_message : int = 0):
212258
"DAUTH-API"
213259
"ORACLE-API"
214260
"DEEPLOY-API"
261+
"DAPP-APP"
262+
"EXPLORER-APP"
263+
"DEEPLOY-APP"
215264
216265
"""
217266
try:
218267
if service is None:
219268
response = "please provide data"
220269
else:
221-
url = self.__get_service_url_mapping(service)
222-
if url is None:
270+
cfg = self.__get_service_config(service)
271+
if cfg is None:
223272
response = "please provide a valid service"
224273
else:
225-
data = self.__get_service_status(url)
274+
data = self.__get_service_status(cfg)
226275
if extended_message == 1:
227276
response = data["message"]
228277
elif extended_message == 2:

ver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__VER__ = '2.9.821'
1+
__VER__ = '2.9.822'

0 commit comments

Comments
 (0)