Skip to content

Commit

Permalink
staus is now passed as json instead of html
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hanke committed May 24, 2024
1 parent bfe9208 commit 944cf39
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 23 deletions.
52 changes: 40 additions & 12 deletions ckanext/fuseki/assets/fuseki.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ ckan.module('fuseki', function (jQuery) {
options: {
parameters: {
html: {
contentType: 'text/html', // change the content type to text/html
dataType: 'html', // change the data type to html
contentType: 'application/json', // change the content type to text/html
dataType: 'json', // change the data type to html
dataConverter: function (data) { return data; },
language: 'html'
language: 'json'
}
}
},
Expand All @@ -15,23 +15,51 @@ ckan.module('fuseki', function (jQuery) {
var p;
p = this.options.parameters.html;
console.log("Initialized Fuseki for element: ", this.el);
var html_length;
html_length = 0;
var log_length;
log_length = 0;
var update = function () { // define the update function
jQuery.ajax({
url: "fuseki/status",
type: 'GET',
contentType: p.contentType,
dataType: p.dataType,
success: function (response) {
var html = jQuery(response);
var length = html.text().length
// console.log('html:', html); // log the HTML to the console for debugging
data: { get_param: 'value' },
success: function (data) {
console.log(data);
// console.log(log_length, length);
const haslogs = 'logs' in data.status;
const hasgraph = 'graph' in data.status;
if (hasgraph) {
console.log(self.el.find('button[name="delete"]'));
self.el.find('button[name="delete"]').removeClass("invisible");
self.el.find('a[name="query"]').removeClass("invisible");
self.el.find('a[name="query"]').attr("href", data.status.queryurl);
self.el.find('div[name="status"]').removeClass("invisible");
};
console.log(haslogs, hasgraph);
if (!haslogs) return;
var length = Object.keys(data.status.logs).length;
if (length) {
if (length !== html_length) {
self.el.html(html); // update the HTML if there are changes
if (length !== log_length) {
// self.el.html(JSON.stringify(data, null, 2)); // update the HTML if there are changes
console.log(data.status.logs)
var logs_div = $(self.el).find('ul[name="log"]');
jQuery.each(data.status.logs, function (key, value) {
if (key + 1 < log_length) return;
logs_div.append("<li class='item "
+ value.class +
"'><i class='fa icon fa-"
+ value.icon +
"'></i><div class='alert alert-"
+ value.alertlevel +
" mb-0 mt-3' role='alert'>"
+ value.message +
"</div><span class='date' title='timestamp'>"
+ value.timestamp +
"</span></li>");
});
console.log("Fuseki: status updated");
html_length = length;
log_length = length;
}
} else {
console.log('Error: #ajax-status element not found');
Expand Down
6 changes: 4 additions & 2 deletions ckanext/fuseki/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, **kwargs):

from ckanext.fuseki import db, backend
from ckanext.fuseki.tasks import update, SPARQL_RES_NAME, resource_search

from ckanext.fuseki.helpers import fuseki_graph_exists
import sqlalchemy as sa

JOB_TIMEOUT = 180
Expand Down Expand Up @@ -72,7 +72,8 @@ def fuseki_delete(context: Context, data_dict: dict[str, Any]) -> dict[str, Any]
)
# delete SPARQL link
link_id = resource_search(graph_id, SPARQL_RES_NAME)
toolkit.get_action("resource_delete")({}, link_id)
if link_id:
toolkit.get_action("resource_delete")({}, {"id": link_id})
if existing_task:
toolkit.get_action("task_status_delete")(
context, {"id": existing_task["id"]}
Expand Down Expand Up @@ -366,6 +367,7 @@ def fuseki_update_status(context: Context, data_dict: dict[str, Any]) -> dict[st
# this happens occasionally, such as when the job times out
error = task["error"]
status = {
"graph": fuseki_graph_exists(pkg_id),
"status": task["state"],
"job_id": job_id,
"job_url": url,
Expand Down
73 changes: 71 additions & 2 deletions ckanext/fuseki/templates/fuseki/status.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
{% extends "package/read_base.html" %}
{% import 'macros/form.html' as form %}


{% block primary_content_inner %}
<h2 class="hide-heading">{{ _('Fuseki') }}</h2>
<div id="ajax-status" data-module="fuseki"></div>
<div id="ajax-status" data-module="fuseki">
<form class="add-to-group" method="post">
{{ h.csrf_input() }}
<button class="btn btn-secondary" name="create/update" type="submit">
<i class="fa fa-refresh"></i> {{ _('Create/Update') }}
</button>
<button class="btn btn-danger invisible" name="delete" type="submit">
<i class="fa fa-trash"></i> {{ _('Delete') }}
</button>
<a class="btn btn-primary mb-0 invisible" name="query" href="{{ h.fuseki_query_url(pkg_dict)|safe }}"
role="button">
<i class="fa fa-play"></i> {{ _('Query') }}
</a>
<hr class="mt-0">
<table class="table table-bordered table-sm m-0">
<thead class="">
<tr>
<th>{{ _('upload') }}</th>
<th>{{ _('name') }}</th>
<th>{{ _('format') }}</th>
<th>{{ _('mime type') }}</th>
<th>{{ _('size') }} [B]</th>
</tr>
</thead>
<tbody>
{% for resource in pkg_dict.resources %}
<tr>
<td>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name='resid' id="ckeck-{{resource.id}}"
value={{resource.id}} {% if status.metadata is defined %} {% if resource.id in
status.metadata.res_ids%}checked='true' {% endif %} {% elif
h.fuseki_show_tools(resource) %}checked='true' {% endif %}>
</div>
</td>
<td>{{resource.name}}</td>
<td>{{resource.format}}</td>
<td>{{resource.mimetype}}</td>
<td>{{resource.size}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
<hr class="mt-0">
<div class="invisible" name=status>
<table class="table table-bordered">
<colgroup>
<col width="150">
<col>
</colgroup>
<tr>
<th>{{ _('Status') }}</th>
<td>{{status.status}}</td>
</tr>
<tr>
<th>{{ _('Last updated') }}</th>
{% if status.status %}
<td><span class="date" title="{{ h.render_datetime(status.last_updated, with_hours=True) }}">{{
h.time_ago_from_timestamp(status.last_updated) }}</span></td>
{% else %}
<td>{{ _('Never') }}</td>
{% endif %}
</tr>
</table>

<h3>{{ _('Graph Update Log') }}</h3>
<ul class="activity" name="log">
</ul>
</div>
</div>
{% endblock %}

{% block scripts %}
Expand Down
36 changes: 29 additions & 7 deletions ckanext/fuseki/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import ckan.plugins.toolkit as toolkit
import ckan.lib.helpers as core_helpers
import ckan.lib.base as base

from flask import request
from ckanext.fuseki.helpers import fuseki_query_url

log = __import__("logging").getLogger(__name__)

Expand Down Expand Up @@ -32,7 +32,9 @@ def post(self, id: str):
"pkg_id": pkg_dict["id"],
},
)
return core_helpers.redirect_to("fuseki.fuseki", id=id)
log.debug(toolkit.redirect_to("fuseki.fuseki", id=id))
return toolkit.redirect_to("fuseki.fuseki", id=id)
# return core_helpers.redirect_to("fuseki.fuseki", id=id)

def get(self, id: str):
pkg_dict = {}
Expand All @@ -43,7 +45,6 @@ def get(self, id: str):
status = toolkit.get_action("fuseki_update_status")(
{}, {"pkg_id": pkg_dict["id"]}
)

return base.render(
"fuseki/status.html",
extra_vars={
Expand All @@ -62,13 +63,34 @@ def get(self, id: str):
pkg_dict = toolkit.get_action("package_show")({}, {"id": id})
except (toolkit.ObjectNotFound, toolkit.NotAuthorized):
base.abort(404, "Resource not found")

status = toolkit.get_action("fuseki_update_status")(
{}, {"pkg_id": pkg_dict["id"]}
)
return base.render(
"fuseki/logs.html",
extra_vars={"pkg_dict": pkg_dict, "status": status},
)
if "logs" in status.keys():
for index, item in enumerate(status["logs"]):
status["logs"][index]["timestamp"] = (
core_helpers.time_ago_from_timestamp(item["timestamp"])
)
if item["level"] == "DEBUG":
status["logs"][index]["alertlevel"] = "info"
status["logs"][index]["icon"] = "bug-slash"
status["logs"][index]["class"] = "success"
elif item["level"] == "INFO":
status["logs"][index]["alertlevel"] = "info"
status["logs"][index]["icon"] = "check"
status["logs"][index]["class"] = "success"
else:
status["logs"][index]["alertlevel"] = "error"
status["logs"][index]["icon"] = "exclamation"
status["logs"][index]["class"] = "failure"
if "graph" in status.keys():
status["queryurl"] = fuseki_query_url(pkg_dict)
return {"pkg_dict": pkg_dict, "status": status}
# return base.render(
# "fuseki/logs.html",
# extra_vars={"pkg_dict": pkg_dict, "status": status},
# )


blueprint.add_url_rule(
Expand Down

0 comments on commit 944cf39

Please sign in to comment.