-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
120 lines (93 loc) · 2.94 KB
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import pandas as pd
import plotly.express as px
import requests
import streamlit as st
# Streamlit app configuration
st.set_page_config(
page_title="API Monitoring",
layout="wide",
page_icon="📊",
menu_items={
"Get help": "https://github.com/airfold/api-analytics-template",
"About": "https://www.airfold.co",
},
)
# Constants and environment variables
api_url = "https://api.airfold.co/v1"
api_token = os.getenv("AIRFOLD_API_KEY")
def pipe_to_df(pipe, params=None, api_token=None):
if not api_token:
api_token = os.getenv("AIRFOLD_API_KEY")
url = f"{api_url}/pipes/{pipe}.json"
headers = {"Authorization": f"Bearer {api_token}"}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()["data"]
df = pd.DataFrame(data)
return df
else:
response.raise_for_status()
def get_total_metrics(api_token=None):
return pipe_to_df("totals", api_token=api_token)
def get_metrics(
time_range=None,
api_token=None,
):
params = {}
if time_range:
params["time_range"] = time_range
df = pipe_to_df("metrics", params=params, api_token=api_token)
df["ts"] = pd.to_datetime(df["ts"])
# Convert relevant columns to floats
int_columns = [
"request_count",
"error_count",
]
df[int_columns] = df[int_columns].astype(float)
# fill NaNs in quantile/average with zeros
df = df.fillna(value=0)
return df
# Sidebar for filters
with st.sidebar:
st.image(
"https://i.gyazo.com/b8ea59576765a4b5065b8cf1ef9e701d.png",
width=200,
)
time_range = st.slider(label="Select range in minutes", min_value=5, max_value=60, step=1, value=30)
# Refresh button
refresh_button = st.button("Refresh")
df = get_metrics(api_token=api_token, time_range=time_range)
total_df = get_total_metrics(api_token=api_token)
print(df)
# Display high-level metrics
def display_metrics(df):
if not df.empty:
col1, col2, col3 = st.columns(3)
col1.metric("Total Requests", total_df["request_count"][0])
col2.metric("Total Errors", total_df["error_count"][0])
col3.metric("Average Latency", str(total_df["latency_p50"][0]) + "ms")
# Display charts
def display_charts(df):
if not df.empty:
col1, col2 = st.columns(2)
fig1 = px.line(
df,
x="ts",
y=["request_count", "error_count"],
title="Total Requests",
labels={"value": "Requests", "variable": "Metric"},
)
col1.plotly_chart(fig1)
fig2 = px.line(
df,
x="ts",
y=["latency_p50", "latency_p95"],
title="Latency",
labels={"value": "Latency", "variable": "Metric"},
)
col2.plotly_chart(fig2)
# Main content
with st.spinner("Loading data..."):
display_metrics(total_df)
display_charts(df)