Skip to content
This repository has been archived by the owner on Feb 2, 2020. It is now read-only.

Commit

Permalink
enh more status (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermr authored Feb 1, 2020
1 parent f45dcb8 commit ff8f905
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
96 changes: 96 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
import requests
import json

import lxml.html
import cachetools

from flask import (
Flask, request, make_response, jsonify, render_template)

Expand All @@ -24,9 +27,22 @@
}
}

STATUS_UPDATE_DELAY = 300
NOSTATUS = 'No Status Available'
STATUS_UPDATED = None
STATUS_DATA = {
'azure': {
'status': NOSTATUS,
},
'webservices': {
'status': NOSTATUS,
},
}

START_TIME = datetime.datetime.fromisoformat("2020-01-01T00:00:00+00:00")
TIME_INTERVAL = 60*5 # five minutes


app = Flask(__name__)


Expand Down Expand Up @@ -150,6 +166,86 @@ def report_name(name):
return resp


@app.route('/status', methods=['GET'])
def status():
global STATUS_DATA
global STATUS_UPDATED

do_update = False
if STATUS_UPDATED is None:
do_update = True
else:
now = datetime.datetime.now().astimezone(pytz.UTC)
dt = now - STATUS_UPDATED
# five minutes
if dt.total_seconds() >= STATUS_UPDATE_DELAY:
do_update = True

if do_update:
try:
r = requests.get('https://status.dev.azure.com')
if r.status_code != 200:
STATUS_DATA['azure'] = NOSTATUS
else:
s = json.loads(
lxml
.html
.fromstring(r.content)
.get_element_by_id('dataProviders')
.text
)

def _rec_search(d):
if isinstance(d, dict):
if 'health' in d and 'message' in d:
return d['message']
else:
for v in d.values():
if isinstance(v, dict):
val = _rec_search(v)
if val is not None:
return val
return None
else:
return None

stat = _rec_search(s)

if stat is None:
stat = NOSTATUS

STATUS_DATA['azure'] = stat
except requests.exceptions.RequestException:
STATUS_DATA['azure'] = NOSTATUS

try:
r = requests.post(
(
'https://conda-forge.herokuapp.com'
'/conda-webservice-update/hook'
),
headers={'X-GitHub-Event': 'ping'}
)

if (
r.status_code != 200 or
r.elapsed.total_seconds() > 1 or
r.text != 'pong'
):
STATUS_DATA['webservices'] = 'Degraded Performance'
else:
STATUS_DATA['webservices'] = 'All Systems Operational'
except requests.exceptions.RequestException:
STATUS_DATA['webservices'] = 'Degraded Performance'

STATUS_UPDATED = datetime.datetime.now().astimezone(pytz.UTC)
STATUS_DATA['updated_at'] = STATUS_UPDATED.isoformat()

resp = make_response(jsonify(STATUS_DATA))
resp.headers['Access-Control-Allow-Origin'] = "*"
return resp


@app.route('/payload', methods=['POST'])
def payload():
global APP_DATA
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pytz
pyyaml
ruamel.yaml
requests
lxml

0 comments on commit ff8f905

Please sign in to comment.