-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (62 loc) · 2.11 KB
/
main.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
from threading import Thread
import time
import sqlite3
import os
from typing import Dict, List, Tuple, Union
from flask import g, Flask, render_template
import numpy as np
app = Flask("stockhorn")
COSTS = [0]
PATH_DB = 'db.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(PATH_DB)
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
class ThreadUpdate(Thread):
def __init__(self, season: str, last=0):
self.updated = last
self.sname: str = season
super().__init__(name="thread-update")
def run(self) -> None:
global COSTS
while True:
time.sleep(10)
t = COSTS[-1] + np.random.normal(0, 5)
with open(f'seasons/{self.sname}', 'a', encoding='utf-8') as f:
f.write(str(t) + '\n')
COSTS.append(t)
self.updated += 1
@app.route("/public/cost/all/")
def public_cost_all():
return {"cost": COSTS, "updated": _ut.updated}, 200
@app.route("/public/cost/all/chart/")
def public_cost_all_chart():
return {"cost": [{"x": i, "y": v} for i, v in enumerate(COSTS)], "updated": _ut.updated}, 200
@app.route("/public/cost/now/")
def public_cost_now():
return {"cost_now": COSTS[-1], "updated": _ut.updated}, 200
@app.route("/public/cost/period/<int:start>/<int:end>/")
def public_cost_period(start: int, end: int):
if start <= end < len(COSTS):
return {"cost": COSTS[start:end + 1], "updated": _ut.updated}, 200
else:
return {"cost": [], "updated": _ut.updated}, 200
@app.route("/")
@app.route("/front/")
def public_front():
return render_template('front.html')
if __name__ == '__main__':
season_name = 'vc-rrt'
if os.path.exists(rf"seasons/{season_name}"):
COSTS += [float(line) for line in open(rf"seasons/{season_name}", 'r', encoding='utf-8')]
_ut = ThreadUpdate(season_name, len(COSTS) - 1)
else:
_ut = ThreadUpdate(season_name)
_ut.start()
app.run('192.168.0.4', port=54321)