Skip to content

Instantly share code, notes, and snippets.

@ov7a
Created Apr 23, 2018
Embed
What would you like to do?
From Kibana to Redmine
(function logs_data_main(){
const ui_id = "logs_ui";
const data_id = "logs_data_ui";
function addLogsData(){
let text = document.getElementById(data_id).value;
let lines = text.split('\n');
let urlsAndNames = lines
.filter(x => x.length > 2)
.map(line => line.split(/\s+/, 2));
downloadUrlsToFiles(urlsAndNames);
}
function uploadFiles(files, urlsAndNames){
pseudoUpload(files);
changeDescription(urlsAndNames);
removeSelf();
}
const kibana_pattern = /https?:\/\/([^:]*):\d+\/app\/kibana#\/doc\/[^\/]*\/([^\/]*)\/([^\/]*)\/?\?id=(.*?)(&.*)?$/;
const es_pattern = 'https://$1:9243/$2/$3/$4';
function transformUrl(kibanaUrl){
return kibanaUrl.replace(kibana_pattern, es_pattern);
}
function checkError(response){
if (response.error !== undefined){
alert(responseJs.error);
throw responseJs.error;
}
}
function downloadUrlsToFiles(urlsAndNames){
let requests = urlsAndNames.map((splitted) => {
let url = transformUrl(splitted[0]);
return $.ajax({
url: url,
dataType: 'json'
});
});
$.when(...requests).done(function(){
let responses;
if (requests.length == 1){
responses = [arguments];
} else {
responses = Array.from(arguments);
}
let files = responses.map((responseRaw, index) => {
let response = responseRaw[0];
checkError(response);
let fileName = urlsAndNames[index][1];
return createFile(JSON.stringify(response._source), fileName + '.json');
});
uploadFiles(files, urlsAndNames);
}).fail((error) => {
let errorString = JSON.stringify(error);
alert(errorString);
throw errorString;
});
}
function removeSelf(){
let old = document.getElementById(ui_id);
if (old != null) old.remove();
}
function createUi(){
removeSelf();
let ui = document.createElement('p');
ui.id = ui_id;
let label = document.createElement('label');
label.innerHTML = "Logs data:";
ui.appendChild(label);
let textarea = document.createElement('textarea');
textarea.id = data_id;
textarea.cols = 60;
textarea.rows = 10;
textarea.name = "issue[logs_data]";
ui.appendChild(textarea);
let button = document.createElement('button');
button.type = "button";
button.onclick = addLogsData;
button.innerHTML = "Add logs data";
ui.appendChild(button);
let attributesBlock = document.querySelector("#attributes");
attributesBlock.parentNode.insertBefore(ui, attributesBlock);
}
function formatDecriptionLine(urlAndfileName){
let url = urlAndfileName[0];
let fileName = urlAndfileName[1];
return `${url} => ${fileName}`;
}
function changeDescription(urlsAndNames){
let descriptionArea = document.getElementById("issue_description");
descriptionArea.value += '\n' + urlsAndNames.map(formatDecriptionLine).join('\n');
}
function createFile(text, fileName){
let blob = new Blob([text], {type: 'text/plain'});
let file = new File([blob], fileName);
return file;
}
function createFileList(files){
const dt = new ClipboardEvent("").clipboardData || new DataTransfer();
for (let file of files) {
dt.items.add(file);
}
return dt.files;
}
function pseudoUpload(files){
let fileInput = document.querySelector(".file_selector");
fileInput.files = createFileList(files);
addInputFiles(fileInput);
}
function installReplaceHook(){
let original = window.replaceIssueFormWith;
window.replaceIssueFormWith = function(html){
let logs_data = document.getElementById(data_id).value;
let ret = original(html);
createUi();
document.getElementById(data_id).value = logs_data;
return ret;
};
}
installReplaceHook();
createUi();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment