Skip to content

Commit

Permalink
feat: update monitor interval & timeout workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
sahasourav123 committed Sep 23, 2024
1 parent 0e2b92f commit ce2ce2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def create_monitor(monitor_type: Literal["api", "website", "database", "server",
def update_monitor(monitor_id: int, monitor_data: dm.MonitorModel):
if monitor_data.interval:
sch.create_job(monitor_id, monitor_data.interval)
qe.update_monitor(monitor_id, monitor_data.model_dump(exclude_none=True))
return {"message": "Monitor updated successfully"}

# delete monitor
Expand Down
8 changes: 7 additions & 1 deletion backend/src/query_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ def insert_monitor(data: dict):

def update_monitor(monitor_id: int, data: dict):
logger.info(f"Updating monitor: {data}")
sql = f"""
UPDATE monitors SET
{','.join([f"{key}=%({key})s" for key in data.keys()])}
WHERE monitor_id = {monitor_id}
"""
print(sql)
db.update(sql, data)
logger.info(f"Updated Monitor with id {monitor_id}")
pass

def delete_monitor(monitor_id: int):
logger.info(f"Deleting monitor: #{monitor_id}")
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/Dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@

def _display_monitor(monitor):
_tags = ', '.join([f"`{tag}`" for tag in monitor.get('tags')]) if monitor['tags'] else '`-`'
_header = f"**{monitor['is_active']} [{monitor['monitor_type'].upper()}] {monitor['monitor_name']}** | _Tags:_ {_tags} | Last 20 Checks: {monitor['outcomes']}"
title = f"**{monitor['is_active']} [{monitor['monitor_type'].upper()}] {monitor['monitor_name']}**"
_header = f"{title} || {monitor['outcomes']}"
with st.expander(_header):
cc = st.columns([1, 2, 1])
with cc[0]:
st.text_input("Check Interval (sec)", value=monitor['interval'], key=f"{monitor['monitor_id']}_interval")
st.text_input("Timeout (sec)", value=monitor['timeout'], key=f"{monitor['monitor_id']}_timeout")
_interval = st.text_input("Check Interval (sec)", value=monitor['interval'], key=f"{monitor['monitor_id']}_interval")
if int(_interval) != monitor['interval']:
res = backend.update_monitor(monitor['monitor_id'], {'interval': int(_interval)})
st.toast("Monitor Interval updated successfully", icon='🟢')
_timeout = st.text_input("Timeout (sec)", value=monitor['timeout'], key=f"{monitor['monitor_id']}_timeout")
if int(_timeout) != monitor['timeout']:
res = backend.update_monitor(monitor['monitor_id'], {'timeout': int(_timeout)})
st.toast(f"Monitor Timeout updated successfully", icon='🟢')

with cc[1]:
st.write(f"Monitor Config")
Expand Down
33 changes: 18 additions & 15 deletions frontend/src/svc/svc_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,29 @@ def create_monitor(monitor_type, monitor_name, monitor_body, timeout, interval,

return res.json()

@st.cache_data(ttl=300)
def fetch_monitors(filters: dict):
url = f'{BACKEND_SERVICE}/fetch/monitor'
res = requests.get(url, params=filters)
if res.status_code != 200:
return []
def update_monitor(monitor_id, monitor_data):
url = f'{BACKEND_SERVICE}/update/monitor/{monitor_id}'
res = requests.put(url, data=json.dumps(monitor_data), headers={'Content-Type': 'application/json'})

data = res.json()['data']
if len(data) == 0:
return pd.DataFrame()
return pd.DataFrame(data)
# clear cache if successful
if 200 >= res.status_code >= 201:
fetch_monitors.clear()

def fetch_monitor_history(filters: dict):
url = f'{BACKEND_SERVICE}/fetch/recent/monitor'
res = requests.get(url, params=filters)
return res.json()

def _fetch_api_data(url, params) -> pd.DataFrame:
res = requests.get(url, params=params)
if res.status_code != 200:
return []
return pd.DataFrame()

data = res.json()['data']
print(data)
if len(data) == 0:
return pd.DataFrame()
return pd.DataFrame(data)

@st.cache_data(ttl=300)
def fetch_monitors(filters: dict):
return _fetch_api_data(url=f'{BACKEND_SERVICE}/fetch/monitor', params=filters)

def fetch_monitor_history(filters: dict):
return _fetch_api_data(url=f'{BACKEND_SERVICE}/fetch/recent/monitor', params=filters)

0 comments on commit ce2ce2e

Please sign in to comment.