srinetwork commited on
Commit
5ea2cf1
·
verified ·
1 Parent(s): c19338a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -35
app.py CHANGED
@@ -1,6 +1,5 @@
1
  from flask import Flask, render_template, request, jsonify
2
  import requests, time, json, os, threading
3
- from urllib.parse import urlparse
4
 
5
  app = Flask(__name__)
6
 
@@ -11,7 +10,6 @@ if not ADMIN_PASSWORD:
11
  raise RuntimeError("ADMIN_PASSWORD not set")
12
 
13
  # ---------------- STORAGE ----------------
14
-
15
  if not os.path.exists(DATA_FILE):
16
  with open(DATA_FILE, "w") as f:
17
  json.dump([], f)
@@ -25,14 +23,6 @@ def save_urls(data):
25
  json.dump(data, f, indent=2)
26
 
27
  # ---------------- CHECKER ----------------
28
-
29
- def get_favicon(url):
30
- try:
31
- parsed = urlparse(url)
32
- return f"{parsed.scheme}://{parsed.netloc}/favicon.ico"
33
- except:
34
- return None
35
-
36
  def check_sites():
37
  while True:
38
  urls = load_urls()
@@ -43,7 +33,6 @@ def check_sites():
43
  site["total_checks"] += 1
44
  ping = None
45
  up = False
46
-
47
  try:
48
  r = requests.get(site["url"], timeout=10)
49
  ping = round(r.elapsed.total_seconds() * 1000, 2)
@@ -58,7 +47,7 @@ def check_sites():
58
  if up:
59
  site["up_checks"] += 1
60
 
61
- # update history
62
  site["history"].append({
63
  "time": int(now),
64
  "uptime": 100 if up else 0,
@@ -66,10 +55,6 @@ def check_sites():
66
  })
67
  site["history"] = site["history"][-50:]
68
 
69
- # fetch favicon if not exist
70
- if not site.get("favicon"):
71
- site["favicon"] = get_favicon(site["url"])
72
-
73
  site["last_check"] = now
74
  site["next_check"] = now + site["interval"]
75
 
@@ -77,7 +62,6 @@ def check_sites():
77
  time.sleep(5)
78
 
79
  # ---------------- ROUTES ----------------
80
-
81
  @app.route("/")
82
  def index():
83
  return render_template("index.html")
@@ -86,22 +70,19 @@ def index():
86
  def data():
87
  urls = load_urls()
88
  now = time.time()
89
-
90
- # total uptime calculation (sum of uptime in seconds)
91
- total_seconds = sum([u["uptime_seconds"] if "uptime_seconds" in u else (now - u["first_added"]) for u in urls])
92
- total_uptime_str = str(int(total_seconds // 86400)) + "d " + \
93
- str(int((total_seconds % 86400) // 3600)) + "h " + \
94
- str(int((total_seconds % 3600) // 60)) + "m " + \
95
- str(int(total_seconds % 60)) + "s"
96
-
97
  for u in urls:
98
  u["remaining"] = max(0, int(u["next_check"] - now))
99
- u["uptime_seconds"] = int(now - u["first_added"])
 
 
 
 
 
 
100
  u["uptime_percent"] = round(
101
- (u["up_checks"] / u["total_checks"]) * 100, 2
102
  ) if u["total_checks"] else 0
103
-
104
- return jsonify({"total_uptime": total_uptime_str, "sites": urls})
105
 
106
  def auth(req):
107
  return req.form.get("password") == ADMIN_PASSWORD
@@ -113,11 +94,11 @@ def add():
113
 
114
  now = time.time()
115
  data = load_urls()
116
-
117
  data.append({
118
- "id": int(now * 1000),
119
  "name": request.form["name"],
120
  "url": request.form["url"],
 
121
  "interval": int(request.form["interval"]),
122
  "status": "UNKNOWN",
123
  "last_ping": None,
@@ -126,10 +107,8 @@ def add():
126
  "first_added": now,
127
  "total_checks": 0,
128
  "up_checks": 0,
129
- "favicon": None,
130
  "history": []
131
  })
132
-
133
  save_urls(data)
134
  return "OK"
135
 
@@ -143,14 +122,13 @@ def edit(id):
143
  if s["id"] == id:
144
  s["name"] = request.form["name"]
145
  s["url"] = request.form["url"]
 
146
  s["interval"] = int(request.form["interval"])
147
  break
148
-
149
  save_urls(data)
150
  return "OK"
151
 
152
  # ---------------- MAIN ----------------
153
-
154
  if __name__ == "__main__":
155
  threading.Thread(target=check_sites, daemon=True).start()
156
  app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, render_template, request, jsonify
2
  import requests, time, json, os, threading
 
3
 
4
  app = Flask(__name__)
5
 
 
10
  raise RuntimeError("ADMIN_PASSWORD not set")
11
 
12
  # ---------------- STORAGE ----------------
 
13
  if not os.path.exists(DATA_FILE):
14
  with open(DATA_FILE, "w") as f:
15
  json.dump([], f)
 
23
  json.dump(data, f, indent=2)
24
 
25
  # ---------------- CHECKER ----------------
 
 
 
 
 
 
 
 
26
  def check_sites():
27
  while True:
28
  urls = load_urls()
 
33
  site["total_checks"] += 1
34
  ping = None
35
  up = False
 
36
  try:
37
  r = requests.get(site["url"], timeout=10)
38
  ping = round(r.elapsed.total_seconds() * 1000, 2)
 
47
  if up:
48
  site["up_checks"] += 1
49
 
50
+ # history
51
  site["history"].append({
52
  "time": int(now),
53
  "uptime": 100 if up else 0,
 
55
  })
56
  site["history"] = site["history"][-50:]
57
 
 
 
 
 
58
  site["last_check"] = now
59
  site["next_check"] = now + site["interval"]
60
 
 
62
  time.sleep(5)
63
 
64
  # ---------------- ROUTES ----------------
 
65
  @app.route("/")
66
  def index():
67
  return render_template("index.html")
 
70
  def data():
71
  urls = load_urls()
72
  now = time.time()
 
 
 
 
 
 
 
 
73
  for u in urls:
74
  u["remaining"] = max(0, int(u["next_check"] - now))
75
+ # formatted uptime string
76
+ total_sec = int(now - u["first_added"])
77
+ d, rem = divmod(total_sec, 86400)
78
+ h, rem = divmod(rem, 3600)
79
+ m, s = divmod(rem, 60)
80
+ u["uptime_str"] = f"{d}d {h}h {m}m {s}s"
81
+
82
  u["uptime_percent"] = round(
83
+ (u["up_checks"] / u["total_checks"])*100,2
84
  ) if u["total_checks"] else 0
85
+ return jsonify(urls)
 
86
 
87
  def auth(req):
88
  return req.form.get("password") == ADMIN_PASSWORD
 
94
 
95
  now = time.time()
96
  data = load_urls()
 
97
  data.append({
98
+ "id": int(now*1000),
99
  "name": request.form["name"],
100
  "url": request.form["url"],
101
+ "img": request.form.get("img", ""), # optional image URL
102
  "interval": int(request.form["interval"]),
103
  "status": "UNKNOWN",
104
  "last_ping": None,
 
107
  "first_added": now,
108
  "total_checks": 0,
109
  "up_checks": 0,
 
110
  "history": []
111
  })
 
112
  save_urls(data)
113
  return "OK"
114
 
 
122
  if s["id"] == id:
123
  s["name"] = request.form["name"]
124
  s["url"] = request.form["url"]
125
+ s["img"] = request.form.get("img", "")
126
  s["interval"] = int(request.form["interval"])
127
  break
 
128
  save_urls(data)
129
  return "OK"
130
 
131
  # ---------------- MAIN ----------------
 
132
  if __name__ == "__main__":
133
  threading.Thread(target=check_sites, daemon=True).start()
134
  app.run(host="0.0.0.0", port=7860)