Skip to content

Commit

Permalink
ajax status working, added service online indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hanke committed Jan 17, 2025
1 parent 28f0e1c commit 4388128
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
7 changes: 6 additions & 1 deletion ckanext/csvwmapandtransform/assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ ckan.module('csvwmapandtransform', function (jQuery) {
var p;
p = this.options.parameters.html;
console.log("Initialized csvwmapandtransform for element: ", this.el);
const apiUrl = this.el.get(0).getAttribute("data-api-url"); // Fetch the API URL from the parent element
if (!apiUrl) {
console.error("No API URL specified for counter:", this.el);
return;
}
var log_length;
log_length = 0;
var update = function () { // define the update function
jQuery.ajax({
url: "/status",
url: apiUrl,
type: 'GET',
contentType: p.contentType,
dataType: p.dataType,
Expand Down
46 changes: 33 additions & 13 deletions ckanext/csvwmapandtransform/helpers.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@

# encoding: utf-8

from typing import Any
import ckan.plugins.toolkit as toolkit
import requests
import re, os

import re

def csvwmapandtransform__status_description(status: dict[str, Any]):
_ = toolkit._

if status.get('status'):
if status.get("status"):
captions = {
'complete': _('Complete'),
'pending': _('Pending'),
'submitting': _('Submitting'),
'error': _('Error'),
"complete": _("Complete"),
"pending": _("Pending"),
"submitting": _("Submitting"),
"error": _("Error"),
}

return captions.get(status['status'], status['status'].capitalize())
return captions.get(status["status"], status["status"].capitalize())
else:
return _('Not Uploaded Yet')
return _("Not Uploaded Yet")


def common_member(a, b):
return any(i in b for i in a)


def csvwmapandtransform_show_tools(resource):
from ckanext.csvwmapandtransform.plugin import DEFAULT_FORMATS
format_parts=re.split('/|;', resource['format'].lower().replace(' ',''))
if common_member(format_parts,DEFAULT_FORMATS):

format_parts = re.split("/|;", resource["format"].lower().replace(" ", ""))
if common_member(format_parts, DEFAULT_FORMATS):
return True
else:
False


def csvwmapandtransform_service_available():
extract_url = os.environ.get("CKAN_RDFCONVERTER_URL", "")
if not extract_url:
return False # If EXTRACT_URL is not set, return False
try:
# Perform a HEAD request (lightweight check) to see if the service responds
response = requests.head(extract_url, timeout=5)
if (200 <= response.status_code < 400) or response.status_code == 405:
return True # URL is reachable and returns a valid status code
else:
return False # URL is reachable but response status is not valid
except requests.RequestException as e:
# If there's any issue (timeout, connection error, etc.)
return False


def get_helpers():
return {
"csvwmapandtransform__status_description": csvwmapandtransform__status_description,
"csvwmapandtransform_show_tools": csvwmapandtransform_show_tools
"csvwmapandtransform_show_tools": csvwmapandtransform_show_tools,
"csvwmapandtransform_service_available": csvwmapandtransform_service_available,
}

6 changes: 0 additions & 6 deletions ckanext/csvwmapandtransform/templates/base.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{% block primary_content_inner %}
<h1>{% block form_title %}{{ _('Transform Status') }}{% endblock %}</h1>
{% if status %}
<div id="ajax-status" data-module="aiextract">
<div id="ajax-status" data-module="csvwmapandtransform" data-api-url="{{ status_url }}">
{% block form %}
<form class="add-to-group" method="post">
{{ h.csrf_input() }}
Expand All @@ -45,9 +45,9 @@ <h1>{% block form_title %}{{ _('Transform Status') }}{% endblock %}</h1>
service_status %}disabled{% endif %}" name="extract/update" type="submit">
<i class="fa fa-refresh"></i> {{ _('Run Transformation') }}
</button>
<button class="btn btn-danger invisible" name="delete" type="submit">
<!-- <button class="btn btn-danger invisible" name="delete" type="submit">
<i class="fa fa-trash"></i> {{ _('Delete') }}
</button>
</button> -->
</div>
<!-- Status Indicator Section -->
<style>
Expand Down
8 changes: 8 additions & 0 deletions ckanext/csvwmapandtransform/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import requests
import json
from distutils.util import strtobool
from .helpers import csvwmapandtransform_service_available

CSVWMAPANDTRANSFORM_TOKEN = os.environ.get("CSVWMAPANDTRANSFORM_TOKEN", "")
MAPTOMETHOD_URL = os.environ.get("CKAN_MAPTOMETHOD_URL")
Expand Down Expand Up @@ -52,6 +53,13 @@ def get(self, id: str, resource_id: str):
"pkg_dict": pkg_dict,
"resource": resource,
"status": status,
"status_url": toolkit.url_for(
"csvwmapandtransform.status",
id=id,
resource_id=resource_id,
qualified=True,
),
"service_status": csvwmapandtransform_service_available(),
"refresh_rate": 10,
},
)
Expand Down

0 comments on commit 4388128

Please sign in to comment.