Skip to content

Commit

Permalink
Fixes handling cache and job queue when calling linkdraw api.
Browse files Browse the repository at this point in the history
Signed-off-by: Kouhei Maeda <mkouhei@palmtb.net>
  • Loading branch information
mkouhei committed Jun 18, 2015
1 parent a765128 commit a449d82
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 47 deletions.
76 changes: 65 additions & 11 deletions pgraph/templates/config.pt
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
$(function(){
$("#graph").linkDraw({
"configPath": "/api/linkdraw/${pkg_name}/${version}",
"positionPath": "positions/${pkg_name}.json",
"positionWriter": "api/positions",
"positionSave": false,
//"zoom": false,
//"drag": false,
"width": 800,
"height": 600,
"interval":0
});

var load_linkdraw = function(pkg_name, version, task_id, loading_msg) {
if (task_id == null) {
var url = "/api/linkdraw/" + pkg_name + "/" + version;
} else {
var url = "/api/linkdraw/" + pkg_name + "/" + version + "?task=" + task_id;
}
$.getJSON(url, function(json) {
switch(json.status) {
case 200:
console.log("drawing lindraw.")
draw(pkg_name, url);
break;
case 202:
show_msg(loading_msg, "warning");
console.log(json.descr);
setTimeout(function(name, version, task_id) {
loading_msg = loading_msg + ".";
load_linkdraw(name, version, task_id, loading_msg);
}, 3000, pkg_name, version, json.task);
break;
case 404:
console.log(json.descr);
show_msg(json.descr, "danger");
break;
default:
console.log('error: not handling case.');
break;
}
});
}

function show_msg(msg, alert_level) {
var alert_class = "alert alert-dismissible";
if (alert_level == "warning") {
alert_class = "alert alert-warning";
} else if (alert_level == "danger") {
alert_class = "alert alert-danger";
}
$("#message").css("text-align", "center");
$("#message strong")
.attr("class", alert_class)
.text(msg)
.css("font-size", "xx-large");
}

function draw(pkg_name, url) {
$("#message")
.removeAttr("class")
.text("");
$("#graph").linkDraw({
"configPath": url,
"positionPath": "positions/" + pkg_name + ".json",
"positionWriter": "api/positions",
"positionSave": false,
//"zoom": false,
//"drag": false,
"width": 800,
"height": 600,
"interval":0
});
};

var loading_msg = "loading.";
load_linkdraw("${pkg_name}", "${version}", null, loading_msg);
});
3 changes: 3 additions & 0 deletions pgraph/templates/graph.pt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<div class="col-md-12">
<div class="content">
<div id="graph"></div>
<div id="message">
<strong></strong>
</div>
<script src="/linkdraw/config/${pkg_name}/${base_pkg.version}"></script>
</div>
</div>
Expand Down
76 changes: 65 additions & 11 deletions pgraph/tests/data/config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
$(function(){
$("#graph").linkDraw({
"configPath": "/api/linkdraw/py-deps/0.2.0",
"positionPath": "positions/py-deps.json",
"positionWriter": "api/positions",
"positionSave": false,
//"zoom": false,
//"drag": false,
"width": 800,
"height": 600,
"interval":0
});

var load_linkdraw = function(pkg_name, version, task_id, loading_msg) {
if (task_id == null) {
var url = "/api/linkdraw/" + pkg_name + "/" + version;
} else {
var url = "/api/linkdraw/" + pkg_name + "/" + version + "?task=" + task_id;
}
$.getJSON(url, function(json) {
switch(json.status) {
case 200:
console.log("drawing lindraw.")
draw(pkg_name, url);
break;
case 202:
show_msg(loading_msg, "warning");
console.log(json.descr);
setTimeout(function(name, version, task_id) {
loading_msg = loading_msg + ".";
load_linkdraw(name, version, task_id, loading_msg);
}, 3000, pkg_name, version, json.task);
break;
case 404:
console.log(json.descr);
show_msg(json.descr, "danger");
break;
default:
console.log('error: not handling case.');
break;
}
});
}

function show_msg(msg, alert_level) {
var alert_class = "alert alert-dismissible";
if (alert_level == "warning") {
alert_class = "alert alert-warning";
} else if (alert_level == "danger") {
alert_class = "alert alert-danger";
}
$("#message").css("text-align", "center");
$("#message strong")
.attr("class", alert_class)
.text(msg)
.css("font-size", "xx-large");
}

function draw(pkg_name, url) {
$("#message")
.removeAttr("class")
.text("");
$("#graph").linkDraw({
"configPath": url,
"positionPath": "positions/" + pkg_name + ".json",
"positionWriter": "api/positions",
"positionSave": false,
//"zoom": false,
//"drag": false,
"width": 800,
"height": 600,
"interval":0
});
};

var loading_msg = "loading.";
load_linkdraw("py-deps", "0.2.0", null, loading_msg);
});
46 changes: 21 additions & 25 deletions pgraph/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""pgraph.views module."""
import time
from pyramid.renderers import get_renderer
from pyramid.view import view_config
from pgraph import __project__, __version__, __author__, __repo__, READTHEDOCS
Expand Down Expand Up @@ -42,22 +41,35 @@ def linkdraw(self):
"""Linkdraw data."""
pkg_name = self.request.matchdict['pkg']
version = self.request.matchdict['version']
task_id = self.request.GET.get('task')
self.meta['pkg_name'] = pkg_name
data = tasks.read_cache(pkg_name, version)
if data:
result = data.draw('linkdraw',
decode_type='json',
disable_time=True,
disable_descr=True)
result['status'] = 200
else:
job = tasks.gen_dependency.delay(pkg_name, version)
while job.ready() is False:
time.sleep(1)
if job.successful():
result = job.result.draw('linkdraw',
decode_type='json',
disable_time=True,
disable_descr=True)
if task_id:
job = tasks.result(task_id)
else:
job = tasks.gen_dependency.delay(pkg_name, version)
if job.ready() is False:
result = {'status': 202,
'descr': 'Accepted',
'task': job.task_id}
else:
if job.successful():
result = job.result.draw('linkdraw',
decode_type='json',
disable_time=True,
disable_descr=True)
result['status'] = 200
else:
result = {'status': 404,
'descr': 'Failed parsing dependencies.',
'task': job.task_id}
return result

@view_config(route_name='graph', renderer='templates/graph.pt')
Expand All @@ -78,19 +90,3 @@ def search(self):
self.meta['pkg_name'] = pkg_name
self.meta['results'] = tasks.search(pkg_name)
return self.meta

def _check_result(self, job):
"""Check job result."""
if job.ready() is False:
self.meta['ready'] = False
self.meta['successful'] = False
self.meta['task_id'] = job.task_id
else:
if job.successful():
self.meta['ready'] = True
self.meta['successful'] = True
self.meta['results'] = job.result
else:
self.meta['ready'] = True
self.meta['successful'] = False
self.meta['task_id'] = job.task_id

0 comments on commit a449d82

Please sign in to comment.