-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (49 loc) · 1.61 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
import os
from datetime import datetime as dt
from dotenv import load_dotenv
from flask import Flask, render_template
from pymongo import MongoClient
from pytz import timezone, utc
from rq import Queue
from dbstats import globals
from dbstats.refresh import refresh
from worker import conn
if 'MONGODB_URI' not in os.environ:
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
mongo = MongoClient(os.environ.get('MONGODB_URI'))
globals.db = mongo.get_database()
q = Queue(connection=conn, default_timeout=600)
q.enqueue(refresh)
@app.route('/')
def main():
total_events = globals.db.general.find_one({'name': 'events'})['total']
total_users = globals.db.general.find_one({'name': 'users'})['total']
total_groups = globals.db.general.find_one({'name': 'groups'})['total']
last_refreshed = globals.db.general.find_one(
{'name': 'last_refreshed'}
)['time']
tz = timezone('Asia/Kolkata')
time_since = (dt.now() - last_refreshed)
hrs, remainder = divmod(time_since.total_seconds(), 3600)
mins, secs = divmod(remainder, 60)
last_updated = last_refreshed.replace(tzinfo=utc).astimezone(tz). \
strftime('%d %b %Y %I:%M:%S %p')
time_since = '%d hour(s) %d minute(s) and %d second(s)' % (hrs, mins, secs)
return render_template(
'index.html',
total_events=f'{total_events:,}',
total_users=f'{total_users:,}',
total_groups=total_groups,
last_updated=last_updated,
time_since=time_since,
refresh=len(q) != 0
)
@app.route('/refresh/')
def page_refresh():
if len(q) == 0:
q.enqueue(refresh)
return render_template('refresh.html')
if __name__ == '__main__':
app.run()