0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-02-05 05:59:43 +00:00
alerta_alerta/alerta/views/oembed.py
Andrey Kuzmin cc8f93000c Oembed fix (#853)
* Fix oembed.

Use usual json (not jsonp request) with Access-Control-Allow-Origin header.
Correct path to oembed location.

* Move styling to embedding page from embedded to simplify configure.

Removed width/height from parameters.
Allow to hide title.

* More powerfull oembed.

Auto-renew.
Ability to open page locally (we heed to set http/https prefixes).
Fix loading: we need to set alerta defaults only after script loading.

* oembed example for grafana.

* Remove unused width/height from js.

* Ability to set empty background in case of no alerts.

* Oembed work fix.

qb.from_params forms incorrect arguments for query.

* Grafana load fix.

Grafana won't load script on "load". So it's better to embed js.

* Revert api path to usual environment prefix.

But leave a comment, how to use it with alerta, running in docker.
2019-03-24 20:57:21 +01:00

78 lines
2.4 KiB
Python

from urllib.parse import urlparse
from flask import current_app, jsonify, render_template, request
from flask_cors import cross_origin
from alerta.app import db, qb
from alerta.auth.decorators import permission
from alerta.models.enums import Scope
from alerta.utils.response import jsonp
from collections import namedtuple
from . import api
Query = namedtuple('Query', ['where', 'vars', 'sort', 'group'])
@api.route('/oembed', defaults={'format': 'json'}, methods=['OPTIONS', 'GET'])
@api.route('/oembed.<format>', methods=['OPTIONS', 'GET'])
@cross_origin()
@permission(Scope.read_oembed)
@jsonp
def oembed(format):
try:
url = request.args['url']
title = request.args['title']
except Exception as e:
return jsonify(status='error', message=str(e)), 400
try:
o = urlparse(url)
query = qb.from_params(request.args)
except Exception as e:
return jsonify(status='error', message=str(e)), 500
if o.path.endswith('/alerts/count'):
try:
qvars = dict()
qvars['status'] = 'open'
query = Query(where='"status"=%(status)s', vars=qvars, sort='', group='')
severity_count = db.get_counts_by_severity(query)
except Exception as e:
return jsonify(status='error', message=str(e)), 500
max = 'none'
if severity_count.get('informational', 0) > 0:
max = 'informational'
if severity_count.get('warning', 0) > 0:
max = 'warning'
if severity_count.get('minor', 0) > 0:
max = 'minor'
if severity_count.get('major', 0) > 0:
max = 'major'
if severity_count.get('critical', 0) > 0:
max = 'critical'
html = render_template(
'oembed/counts.html',
title=title,
max=max,
counts=severity_count
)
headers = {
"Access-Control-Allow-Origin": "*"
}
return jsonify(version='1.0', type='rich', title=title, provider_name='Alerta', provider_url=request.url_root, html=html), 200, headers
elif o.path.endswith('/alerts/top10/count'):
# TODO: support top10 oembed widget
pass
else:
return jsonify(status='error', message='unsupported oEmbed URL scheme'), 400
@api.route('/embed.js', methods=['OPTIONS', 'GET'])
def embed_js():
return current_app.send_static_file('embed.js')