-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (68 loc) · 2.67 KB
/
app.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
"""
A simple web application based on Flask.
"""
from threading import Thread
import time
from flask import Flask, render_template
from collector import StatsCollector
# >>>>>>>>>>>>>>>>>>>>>>>>>>> Custom Configs >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Collection interval, in seconds
COLLECTION_INTERVAL = 1
# Pause threshold for collection thread, in seconds.
# State collection will be paused if there is no client accessing latest states
# lasting for more than this threshold.
PAUSE_THRESHOLD = 30
# AJAX request interval of client, in milliseconds
AJAX_REQUEST_INTERVAL = 2000
# Page title displayed in webpage title and header
PAGE_TITLE = "PRIS-727 Server Monitor"
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
app = Flask(__name__)
# [only for dev]
# app.config["TEMPLATES_AUTO_RELOAD"] = True
# >>>>>>>>>>>>>>>>>>>>>>>>>>> Current Stats >>>>>>>>>>>>>>>>>>>>>>>>>>>
class CurrentStats:
def __init__(self):
self.prev_time = time.time()
self.stats = None
def set_stats(self, stats):
self.stats = stats
def get_stats(self):
self.prev_time = time.time() # update access time
while self.stats is None:
time.sleep(COLLECTION_INTERVAL)
return self.stats
# The global varible representing current stats
CURRENT_STATS = CurrentStats()
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# >>>>>>>>>>>>>>>>>>>>>>>>>>> Collection Thread >>>>>>>>>>>>>>>>>>>>>>>>>>>
class CollectionThread(Thread):
def __init__(self):
super().__init__()
self.collector = StatsCollector()
def run(self):
while True:
# If there is request within PAUSE_THRESHOLD, refresh `CURRENT_STATS`
if time.time() - CURRENT_STATS.prev_time < PAUSE_THRESHOLD:
CURRENT_STATS.set_stats(self.collector.get_stats())
# Otherwise, pause collecting latest stats and set stats as `None`
else:
CURRENT_STATS.set_stats(None)
time.sleep(COLLECTION_INTERVAL)
# Create and start the collection thread
collection_thread = CollectionThread()
collection_thread.start()
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# >>>>>>>>>>>>>>>>>>>>>>>>>>> Flask Route Functions >>>>>>>>>>>>>>>>>>>>>>>>>>>
@app.route("/")
def index():
return render_template("index.html",
ajax_request_interval=AJAX_REQUEST_INTERVAL,
page_title=PAGE_TITLE)
@app.route("/query")
def query():
"""
For AJAX requets. Return JSON.
"""
return CURRENT_STATS.get_stats()
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<