-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gsu-library/develop
Update script to download/queue LibInsight dataset
- Loading branch information
Showing
3 changed files
with
102 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ Code Repository: https://github.com/gsu-library/userscripts | |
Author: Matt Brooks <[email protected]> | ||
Date Created: 2021-05-19 | ||
License: [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html) | ||
Version: 3.0.0 | ||
Version: 4.0.0 | ||
|
||
## Description | ||
UserScripts used by GSU Library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
// @namespace https://github.com/gsu-library/ | ||
// @author [email protected] | ||
// @license GPLv3 | ||
// @version 2.0.0 | ||
// @version 3.0.0 | ||
// @description Adds a download all reports item to the admin menu on the datasets page in LibInsight Lite. | ||
// @match https://gsu.libinsight.com/admin/all | ||
// @grant none | ||
|
@@ -14,76 +14,114 @@ | |
// ==/UserScript== | ||
|
||
(() => { | ||
'use strict'; | ||
"use strict"; | ||
|
||
// Set this array to strings of the dataset IDs you do not want to download. | ||
const blacklist = [ | ||
// '4698', | ||
// '4878', | ||
// '5011', | ||
'7927', | ||
'22125', | ||
'22124', | ||
'22126', | ||
'22123', | ||
'22122' | ||
'4698', | ||
'4878', | ||
// '5011', | ||
'7927', | ||
]; | ||
const maxRecords = 10000; | ||
let datasetsRows; | ||
let datasetCounts = []; | ||
|
||
|
||
// Process dataset ids and counts. Add menu item to download reports. | ||
if(datasetsRows = document.querySelectorAll('#all-datasets tr')) { | ||
let dataRows = [].slice.call(datasetsRows, 2); | ||
if ((datasetsRows = document.querySelectorAll("#all-datasets tr"))) { | ||
let dataRows = [].slice.call(datasetsRows, 2); | ||
|
||
dataRows.forEach((row) => { | ||
datasetCounts.push({ | ||
id: row.childNodes[1].textContent, | ||
count: row.childNodes[7].textContent.replace(",", ""), | ||
}); | ||
}); | ||
|
||
dataRows.forEach(row => { | ||
datasetCounts.push({ | ||
id: row.childNodes[1].textContent, | ||
count: row.childNodes[7].textContent.replace(',', '') | ||
}); | ||
}); | ||
datasetCounts = datasetCounts.filter( | ||
(dataset) => !blacklist.includes(dataset.id) | ||
); | ||
|
||
datasetCounts = datasetCounts.filter(dataset => !blacklist.includes(dataset.id)); | ||
// Create menu item. | ||
let downloadLink = document.createElement("a"); | ||
downloadLink.setAttribute("class", "dropdown-item"); | ||
downloadLink.setAttribute("href", "#"); | ||
downloadLink.text = "Download All Reports"; | ||
let adminMenu = document | ||
.querySelector("#li-admin-navbar-manage") | ||
.parentNode.querySelector("div"); | ||
adminMenu.appendChild(downloadLink); | ||
|
||
// Download reports. | ||
downloadLink.onclick = (e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
// Create menu item. | ||
let downloadLink = document.createElement('a'); | ||
downloadLink.setAttribute('class', 'dropdown-item'); | ||
downloadLink.setAttribute('href', '#'); | ||
downloadLink.text = 'Download All Reports'; | ||
let adminMenu = document.querySelector('#li-admin-navbar-manage').parentNode.querySelector('div'); | ||
adminMenu.appendChild(downloadLink); | ||
datasetCounts.forEach((dataset) => { | ||
// Download report if number of records less than max records | ||
if (dataset.count < maxRecords) { | ||
downloadReport(dataset); | ||
} | ||
else // Queue report if number of records more than max records | ||
{ | ||
queueReport(dataset); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function downloadReport(dataset) | ||
{ | ||
let url = 'https://gsu.libinsight.com/admin/custom-dataset/' + dataset.id + '/export' + | ||
'?filters[page]=1' + | ||
'&iid=294' + | ||
'&dataset_id=' + dataset.id + | ||
'&dataset_type=1' + | ||
'&user_level=1' + | ||
'&total_count=' + dataset.count + | ||
'&from=Jan 01 0000' + | ||
'&to=Dec 31 9999' + | ||
'&filters[day_limit]=21' + | ||
'&filters[hr_start]=0' + | ||
'&filters[hr_end]=24' + | ||
'&filters[sort]=date-desc'; | ||
|
||
setTimeout(() => { | ||
let a = document.createElement('a'); | ||
a.href = url; | ||
document.body.appendChild(a); | ||
a.setAttribute('target', '_blank'); | ||
a.click(); | ||
a.remove(); | ||
}, 100); | ||
} | ||
|
||
// Download reports. | ||
downloadLink.onclick = e => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
function queueReport(dataset) | ||
{ | ||
let url = "https://gsu.libinsight.com/admin/reports/" + dataset.id + "/add"; | ||
let form = new FormData(); | ||
form.set("name", "Dataset-" + dataset.id + "-" + new Date().toISOString()); | ||
form.set("dataset_type", 1); | ||
form.set("record_count", dataset.count); | ||
form.set("date_range", "Custom Date Range"); | ||
|
||
datasetCounts.forEach(dataset => { | ||
let url = 'https://gsu.libinsight.com/admin/custom-dataset/' + dataset.id + '/export' + | ||
'?filters[page]=1' + | ||
'&iid=294' + | ||
'&dataset_id=' + dataset.id + | ||
'&dataset_type=1' + | ||
'&user_level=1' + | ||
'&total_count=' + dataset.count + | ||
'&from=Jan 01 0000' + | ||
'&to=Dec 31 9999' + | ||
'&filters[day_limit]=21' + | ||
'&filters[hr_start]=0' + | ||
'&filters[hr_end]=24' + | ||
'&filters[sort]=date-desc'; | ||
let options = '?filters[page]=1' + | ||
'&iid=294' + | ||
'&dataset_id=' + dataset.id + | ||
'&dataset_type=1' + | ||
'&user_level=1' + | ||
'&total_count=' + dataset.count + | ||
'&from=Jan 01 0000' + | ||
'&to=Dec 31 9999' + | ||
'&filters[day_limit]=21' + | ||
'&filters[hr_start]=0' + | ||
'&filters[hr_end]=24' + | ||
'&filters[sort]=date-desc'; | ||
form.set("options", options); | ||
|
||
setTimeout(() => { | ||
let a = document.createElement('a'); | ||
a.href = url; | ||
document.body.appendChild(a); | ||
a.setAttribute('target', '_blank'); | ||
a.click(); | ||
a.remove(); | ||
}, 100); | ||
}); | ||
} | ||
fetch(url, { | ||
method: "POST", | ||
body: form, | ||
}); | ||
} | ||
})(); | ||
})(); |