Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oembed fix #853

Merged
merged 9 commits into from Mar 24, 2019
Merged

Oembed fix #853

merged 9 commits into from Mar 24, 2019

Conversation

kak-tus
Copy link
Contributor

@kak-tus kak-tus commented Feb 24, 2019

Oembed support seems to be broken.
May be in case of running alerta in docker+postgresql. May be entirelly.

As example: /api/oembed endpoint accessible as /api/oembed, but not as /api/oembed.json.
Also: qb.from_params forms incorrect query to postgresql.

I am not a python programmer and don't know flask framework, so I fixed this not ideally.

After this fixes I've got oembed work in grafana
image

Andrey Kuzmin added 8 commits February 23, 2019 00:02
Use usual json (not jsonp request) with Access-Control-Allow-Origin header.
Correct path to oembed location.
Removed width/height from parameters.
Allow to hide title.
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.
qb.from_params forms incorrect arguments for query.
Grafana won't load script on "load". So it's better to embed js.
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
Copy link

@vykulakov vykulakov Mar 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you changed // to http:// here and in other places of code? // looks like a more general approach that will work in a wider range of environments. For instance, I'm trying to access the oembed.html page by HTTPs in my experiments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get ability to user open this page locally and test with local instance of alerta.
Without this fix, user opens page with "file:///" protocol and external scripts tries to open with "file:///" protocol too.
If this page is not test page, but it is an example page - I will revert this fixes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, your reasons are clear. But it may not suit the original idea. Maybe @satterly will leave some comments here and explain the original idea. About my comment - feel free to ignore it now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please revert this change.

<script src="http://api.alerta.io/embed.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://localhost:8080/api/embed.js"></script>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the /api path prefix here is not correct because it will work only in your environment. Please, use more general URL like this: //localhost:8080/embed.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


// $.alerta.defaults.endpoint = 'http://localhost:8080';
// $.alerta.defaults.key = 'demo-key';
// or
$.alerta.defaults = {
endpoint: 'http://api.alerta.io', // oEmbed endpoint becomes http://api.alerta.io/oembed.json
endpoint: 'http://localhost:8080/api', // oEmbed endpoint becomes http://localhost:8080/api/oembed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment about /api above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

But leave a comment, how to use it with alerta, running in docker.
key: 'demo-key'
};

$('.mobile-alerts').alerta('http://api.alerta.io/alerts/count?service=Mobile&status=open', {title:'Mobile Service'});
function renew() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call this function refresh() instead of renew()? Thanks.

width = int(request.args['maxwidth'])
height = int(request.args['maxheight'])
title = request.args.get('title', 'Alerts')
title = request.args['title']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the default title? It will throw an exception here now if someone does not provide title as a URL param.

@satterly
Copy link
Member

Thanks for the PR. It looks good except for a few minor changes. I'll be happy to merge this once you've addressed the concerns I've raised above. 👍

renew();
});
});
</script>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this example.

Maybe, you can add some comments with instructions on how to use this HTML in Grafana.

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'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the /oembed.<format> endpoint as well because there is only one available format - json. We can return it back when we have more than one format.


@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):

if format != 'json':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove this validation, you should remove the /oembed.<format> endpoint. Or if you leave the endpoint in place, you should leave this check in place as well.

if format != 'json':
return jsonify(status='error', message='unsupported format: %s' % format), 400

if 'url' not in request.args or 'maxwidth' not in request.args \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'url' not in request.args is a good validation. Please, don't remove it.

if 'url' not in request.args or 'maxwidth' not in request.args \
or 'maxheight' not in request.args:
return jsonify(status='error', message='missing default parameters: url, maxwidth, maxheight'), 400

try:
url = request.args['url']
title = request.args['title']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title param is not mandatory now so this code can raise the KeyError: 'title' error. Using .get('title') here solves this problem.

<a href="http://localhost:8080/">Alerta</a>

<!-- Content from http://localhost:8080/api/embed.js -->
<script type="text/javascript">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading script should work fine. I use this <script src="/embed.js"></script> and it works.

@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, your reasons are clear. But it may not suit the original idea. Maybe @satterly will leave some comments here and explain the original idea. About my comment - feel free to ignore it now.

@vykulakov
Copy link

Looks like I've forgotten to submit my review earlier. My mistake, sorry.

@satterly satterly merged commit cc8f930 into alerta:master Mar 24, 2019
saivarunr pushed a commit to rudderlabs/alerta that referenced this pull request Nov 27, 2021
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants