-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_monitor.py
84 lines (71 loc) · 2.37 KB
/
url_monitor.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
import requests
import time
import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import pandas as pd
def read_urls(file_name):
"""Reads a file with some URLs."""
with open(file_name, "r") as f:
urls = f.readlines()
return urls
def check_urls(urls):
"""Checks the URLs and gets the HTTP Code response."""
response_data = []
for url in urls:
try:
response = requests.get(url)
except requests.exceptions.ConnectionError:
continue
response_data.append({
"url": url,
"http_code": response.status_code,
"response_time": response.elapsed.total_seconds(),
})
return response_data
def main():
"""The main function."""
file_name = "urls.txt"
urls = read_urls(file_name)
# Create a Dash app.
app = dash.Dash(__name__)
# Create a layout for the dashboard.
layout = html.Div([
html.H1("URL Monitor"),
dcc.Graph(id="url-graph", figure=figure),
dcc.Table(id="url-table", data=unique_line, style_cell_conditional=[
{'if': {'column_name': 'url'}, 'width': '100px'},
{'if': {'column_name': 'http_code'}, 'width': '200px'},
{'if': {'column_name': 'response_time'}, 'width': '300px'},
]),
])
# Update the dashboard every 60 seconds.
@app.callback(
Output("url-graph", "figure"),
[Input("interval", "interval")],
)
def update_graph(interval):
# Get the latest HTTP Code responses.
response_data = check_urls(urls)
# Create a figure for the dashboard.
figure = px.line(response_data, x="response_time", y="http_code", color="url")
# Create a unique line with all informations.
unique_line = []
for url in urls:
unique_line.append({
"url": url,
"http_code": response_data[url]["http_code"],
"response_time": response_data[url]["response_time"],
})
# Update the dashboard with the unique line.
app.layout = html.Div([
html.H1("URL Monitor"),
dcc.Graph(id="url-graph", figure=figure),
dcc.Table(id="url-table", data=unique_line),
])
return figure
# Run the app.
app.run_server(debug=True)
if __name__ == "__main__":
main()