Skip to content

Commit

Permalink
Merge pull request #3236 from keeps/cafonso-dev-statistics
Browse files Browse the repository at this point in the history
Updated statistics to use new API
  • Loading branch information
hmiguim authored Jul 4, 2024
2 parents 8dae8e0 + 7401f41 commit 5f1b878
Showing 1 changed file with 144 additions and 44 deletions.
188 changes: 144 additions & 44 deletions roda-ui/roda-wui/src/main/resources/config/theme/js/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,55 +73,115 @@
buildDataUrl(element, false);
}

function buildDataUrl(element, noLimits) {
function buildDataUrl(element) {
var returnClass = $(element).data("source-class");

var filters = $(element).data("source-filters") ? $(element).data(
"source-filters").split(/\s*[ ,]\s*/) : [];
var filterParams = filters.map(function(filter) {
return "filter=" + filter
}).join("&");
var lang = document.locale;

var facets = $(element).data("source-facets") ? $(element).data(
"source-facets").split(/\s*[ ,]\s*/) : [];
var facetParams = facets.map(function(facet) {
return "facet=" + facet
}).join("&");
var pathname = window.location.pathname;
var url;
url = pathname + "api/v2/" + getRespectiveEndpoint(returnClass) + "/find?" + "lang=" + lang
return url;
}

function buildDownloadUrl(element) {
var returnClass = $(element).data("source-class");

var onlyActive = $(element).data("source-only-active") || "false";
var lang = document.locale;

var pathname = window.location.pathname;
var url;
if(noLimits) {
url = pathname + "api/v1/index?returnClass=" +
returnClass + "&" +
filterParams + "&" +
facetParams +
"&lang=" + lang +
"&onlyActive=" + onlyActive;
} else {
var start = $(element).data("source-start") || 0;
var limit = $(element).data("source-limit") || 0;
var facetLimit = $(element).data("view-limit") || 100;

url = pathname + "api/v1/index?returnClass=" +
returnClass + "&" +
filterParams + "&" +
facetParams +
"&start=" + start +
"&limit=" + limit +
"&facetLimit=" + facetLimit +
"&lang=" + lang +
"&onlyActive=" + onlyActive;
}
url = pathname + "api/v2/" + getRespectiveEndpoint(returnClass) + "/export/csv"
return url;
}


function buildBody(element) {
var jsonBody = {};

// Handle sorter
var sorter = $(element).data("source-sorter");
if (sorter) {
jsonBody.sorter = {
parameters: sorter.split(/\s*[ ,]\s*/).map(function(param) {
return { name: param, descending: false };
})
};
}

// Handle sublist
var start = $(element).data("source-start");
var limit = $(element).data("source-limit");
jsonBody.sublist = {
firstElementIndex: start !== undefined ? start : 0,
maximumElementCount: limit !== undefined ? limit : 20
};

// Handle facets
var facets = $(element).data("source-facets");
if (facets) {
jsonBody.facets = {
parameters: {}
};

facets.split(/\s*[ ,]\s*/).forEach(function(facet) {
jsonBody.facets.parameters[facet] = {
type: "SimpleFacetParameter",
limit: 100,
name: facet,
values: [],
minCount: 1,
sort: "INDEX"
};
});

jsonBody.facets.query = "";
}

// Handle exportFacets
var exportFacets = $(element).data("source-export-facets");
jsonBody.exportFacets = exportFacets !== undefined ? exportFacets : false;

// Handle filename
var filename = $(element).data("source-filename");
jsonBody.filename = filename || null;

// Handle fieldsToReturn (example values; adjust dynamically as needed)
var fieldsToReturn = $(element).data("source-fields-to-return");
jsonBody.fieldsToReturn = fieldsToReturn ? fieldsToReturn.split(/\s*[ ,]\s*/) : [];

// Handle collapse
var collapse = $(element).data("source-collapse");
jsonBody.collapse = collapse || null;

// Handle children
var children = $(element).data("source-children");
jsonBody.children = children !== undefined ? children : false;

// Handle filters
var filters = $(element).data("source-filters");

if (filters) {
jsonBody.filter = {
parameters: filters.split(/\s*[ ,]\s*/).map(function(filter) {
return createFilter(filter);
})
};
}

// Handle onlyActive
var onlyActive = $(element).data("source-only-active");
jsonBody.onlyActive = onlyActive !== undefined ? onlyActive === "true" : true;

return JSON.stringify(jsonBody);
}

function fetchIndexData(element, viewCallback) {
$.ajax({
url: buildDataUrl(element)
url: buildDataUrl(element),
type: 'POST',
contentType: 'application/json',
data: buildBody(element)
}).done(function(data) {
viewCallback(element, data);
});
Expand All @@ -136,14 +196,8 @@
}

function initViewDownload(element, dataFunction) {
var noLimits = true;
var url = buildDataUrl(element, noLimits);
if (element.data("view-field") == "facetResults") {
url = url + "&exportFacets=true";
var filename = element.data("view-filename");
if (filename) {
url = url + "&filename=" + filename;
}
}

element.click(function() {
Expand All @@ -152,9 +206,9 @@
accepts: {
text: type
},
url: url + "&acceptFormat=csv",
processData: false,
dataType: 'text',
url: buildDownloadUrl(element) + "?findRequest=" + encodeURIComponent(buildBody(element)),
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
success: function(data) {
saveAs(new Blob([data], {
type: type
Expand Down Expand Up @@ -503,6 +557,52 @@ function rgbaRandomOpaqueColorAsString() {
return rgbaRandomColorAsString(color);
}

function getRespectiveEndpoint(returnClass) {
switch (returnClass) {
case "org.roda.core.data.v2.ip.IndexedAIP":
return "aips"
case "org.roda.core.data.v2.log.LogEntry":
return "audit-logs"
case "org.roda.core.data.v2.ip.IndexedRepresentation":
return "representations"
case "org.roda.core.data.v2.ip.IndexedFile":
return "files"
case "org.roda.core.data.v2.jobs.Job":
return "jobs"
case "org.roda.core.data.v2.jobs.IndexedReport":
return "job-reports"
case "org.roda.core.data.v2.user.RODAMember":
return "members"
case "org.roda.core.data.v2.ip.TransferredResource":
return "transfers"
case "org.roda.core.data.v2.ip.metadata.IndexedPreservationEvent":
return "preservation/events"
case "org.roda.core.data.v2.risks.IndexedRisk":
return "risks"
case "org.roda.core.data.v2.risks.RiskIncidence":
return "incidences"
case "org.roda.core.data.v2.notifications.Notification":
return "notifications"
default:
break
}
}

function createFilter(filter) {
if (filter == "any") {
return {
"type": "AllFilterParameter"
};
} else {
var parts = filter.split('=');
return {
"type": "SimpleFilterParameter",
name: parts[0],
value: parts[1]
};
}
}

/*** START ARRAY.FROM workaround ***/

if (!Array.from) {
Expand Down

0 comments on commit 5f1b878

Please sign in to comment.