2021-03-12 11:37:13 +01:00
|
|
|
from app import app, db, imgproxy
|
2017-09-20 23:02:36 +02:00
|
|
|
from libforget.auth import require_auth_api, get_viewer
|
2019-03-15 17:18:43 +01:00
|
|
|
from flask import jsonify, redirect, make_response, request, Response
|
2021-11-10 01:26:36 +01:00
|
|
|
from model import Account, WorkerCheckin
|
2017-09-20 23:02:36 +02:00
|
|
|
import libforget.settings
|
|
|
|
import libforget.json
|
2019-03-15 18:29:55 +01:00
|
|
|
import random
|
2021-11-10 01:26:36 +01:00
|
|
|
from datetime import datetime, timedelta
|
2017-09-16 12:22:17 +02:00
|
|
|
|
2021-03-12 11:37:13 +01:00
|
|
|
@app.route('/api/health_check') # deprecated 2021-03-12
|
|
|
|
@app.route('/api/status_check')
|
|
|
|
def api_status_check():
|
2017-10-15 20:38:39 +02:00
|
|
|
try:
|
|
|
|
db.session.execute('SELECT 1')
|
|
|
|
except Exception:
|
2021-03-12 11:37:13 +01:00
|
|
|
return ('PostgreSQL bad', 500)
|
|
|
|
|
|
|
|
try:
|
|
|
|
imgproxy.redis.set('forget-status-check', 'howdy', ex=5)
|
|
|
|
except Exception:
|
|
|
|
return ('Redis bad', 500)
|
|
|
|
|
2021-11-10 01:26:36 +01:00
|
|
|
CHECKIN_EVENTS = 5
|
|
|
|
CHECKIN_PERIOD = timedelta(minutes=10)
|
|
|
|
# sorry about the obtuse variable names, this trips if the frequency is
|
|
|
|
# lower than events/period
|
|
|
|
checkin_count = db.session.query(WorkerCheckin)\
|
|
|
|
.filter(WorkerCheckin.created_at > db.func.now() - CHECKIN_PERIOD)\
|
|
|
|
.count()
|
|
|
|
if checkin_count < events:
|
|
|
|
return ('Celery slow, {} check-ins in {}'.format(
|
|
|
|
checkin_count, CHECKIN_PERIOD
|
|
|
|
), 500)
|
|
|
|
|
|
|
|
CHECKIN_LATENESS_THRESHOLD = timedelta(minutes=5)
|
|
|
|
checkin = db.session.query(WorkerCheckin.created_at)\
|
|
|
|
.order_by(db.desc(WorkerCheckin.created_at)).first()
|
|
|
|
if checkin + CHECKIN_LATENESS_THRESHOLD < datetime.utcnow():
|
|
|
|
return ('Celery late, last check-in was {}'.format(checkin), 500)
|
2021-03-13 01:46:27 +01:00
|
|
|
|
2021-03-12 11:37:13 +01:00
|
|
|
return 'OK'
|
2017-10-15 20:38:39 +02:00
|
|
|
|
2017-09-16 12:22:17 +02:00
|
|
|
|
|
|
|
@app.route('/api/settings', methods=('PUT',))
|
|
|
|
@require_auth_api
|
|
|
|
def api_settings_put():
|
|
|
|
viewer = get_viewer()
|
|
|
|
data = request.json
|
|
|
|
updated = dict()
|
2017-09-20 23:02:36 +02:00
|
|
|
for key in libforget.settings.attrs:
|
2017-09-16 12:22:17 +02:00
|
|
|
if key in data:
|
2018-01-03 17:58:13 +01:00
|
|
|
if (
|
|
|
|
isinstance(getattr(viewer, key), bool) and
|
|
|
|
isinstance(data[key], str)):
|
|
|
|
data[key] = data[key] == 'true'
|
2017-09-16 12:22:17 +02:00
|
|
|
setattr(viewer, key, data[key])
|
|
|
|
updated[key] = data[key]
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify(status='success', updated=updated)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/viewer')
|
|
|
|
@require_auth_api
|
|
|
|
def api_viewer():
|
|
|
|
viewer = get_viewer()
|
2017-09-20 23:02:36 +02:00
|
|
|
resp = make_response(libforget.json.account(viewer))
|
2017-09-16 12:22:17 +02:00
|
|
|
resp.headers.set('content-type', 'application/json')
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/reason', methods={'DELETE'})
|
|
|
|
@require_auth_api
|
|
|
|
def delete_reason():
|
|
|
|
get_viewer().reason = None
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify(status='success')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/badge/users')
|
|
|
|
def users_badge():
|
|
|
|
count = (
|
|
|
|
Account.query.filter(Account.policy_enabled)
|
|
|
|
.filter(~Account.dormant)
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
return redirect(
|
|
|
|
"https://img.shields.io/badge/active%20users-{}-blue.svg"
|
|
|
|
.format(count))
|