Skip to content

Commit

Permalink
feat: export source in csv files (#2) from StephaneBour/export_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneBour committed Nov 8, 2017
2 parents 368e412 + ad6b812 commit 6b4b7c1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ Have fun !
- Autocompletion on fields
- Add "filter" in autocomplete
- Add "Saved query"
- Export source data in CSV file

### Screenshots

Main screen
#### Main screen

![Main screen](https://github.com/StephaneBour/sense-chrome/raw/master/screenshots/main.jpg)

Autocomplete
#### Autocomplete

![Main screen](https://github.com/StephaneBour/sense-chrome/raw/master/screenshots/autocomplete.jpg)


Save your query
#### Save your query

![Save 1](https://github.com/StephaneBour/sense-chrome/raw/master/screenshots/saved-1.jpg)

Expand Down
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
</button>
<button id="saved_btn" class="btn pull-right-btn" data-toggle="modal" data-target="#saved_popup">Saved query
</button>
<button id="export_csv" class="btn btn-info hide pull-right-btn">Export source in CSV
</button>

</div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions lib/src-noconflict/ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4386,6 +4386,19 @@ var EditSession = function(text, mode) {
this.$deltasFold = [];
this.getUndoManager().reset();
};
this.setCsv = function(text) {
if(text != '' && text != null) {
this.$csv = text;
$('#export_csv').removeClass('hide');
} else {
$('#export_csv').addClass('hide');
}
};

this.getCsv = function () {
return this.$csv;
}

this.getValue =
this.toString = function() {
return this.doc.getValue();
Expand Down Expand Up @@ -7310,6 +7323,7 @@ var Anchor = require("./anchor").Anchor;

var Document = function(text) {
this.$lines = [];
this.$csv = '';
if (text.length == 0) {
this.$lines = [""];
} else if (Array.isArray(text)) {
Expand Down Expand Up @@ -10500,6 +10514,7 @@ var VirtualRenderer = function(container, theme) {

this.content = dom.createElement("div");
this.content.className = "ace_content";

this.scroller.appendChild(this.content);

this.setHighlightGutterLine(true);
Expand Down
70 changes: 66 additions & 4 deletions src/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (!sense)
sense = { };
sense = {};

sense.VERSION = "0.9.0";

Expand Down Expand Up @@ -106,19 +106,22 @@ function submitCurrentRequestToES() {
if (typeof xhr.status == "number" &&
((xhr.status >= 400 && xhr.status < 600) ||
(xhr.status >= 200 && xhr.status < 300)
)) {
)) {
// we have someone on the other side. Add to history
sense.history.addToHistory(es_server, es_url, es_method, es_data);


var value = xhr.responseText;
try {
var inJson = JSON.parse(value);
value = JSON.stringify(JSON.parse(value), null, 3);

}
catch (e) {

}
sense.output.getSession().setValue(value);
sense.output.getSession().setCsv(ConvertSourceToCSV(inJson.hits.hits));
}
else {
sense.output.getSession().setValue("Request failed to get to the server (status code: " + xhr.status + "):" + xhr.responseText);
Expand Down Expand Up @@ -148,7 +151,7 @@ function reformatData(data, indent) {
}
}

return { changed: changed, data: formatted_data}
return {changed: changed, data: formatted_data}
}


Expand Down Expand Up @@ -237,6 +240,40 @@ function handleCURLPaste(text) {
var CURRENT_REQ_RANGE = null;


function ConvertSourceToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var headers = [];

for (var i = 0; i < array.length; i++) {
var line = '';
var source = array[i]._source;

if (str == '') {
for (var index in source) {
str += index + ',';
headers.push(index);
}

str = str.slice(0, -1);
str += "\r\n";
}

for (var h = 0; h < headers.length; h++) {
if (typeof source[headers[h]] == 'undefined') {
source[headers[h]] = '';
}
line += source[headers[h]] + ',';
}

line = line.slice(0, -1);

str += line + '\r\n';
}

return str;
}

function saveEditorState() {
try {
var content = sense.editor.getValue();
Expand All @@ -248,6 +285,25 @@ function saveEditorState() {
}
}

function exportCsv() {
var csv = sense.output.getSession().getCsv();
saveAs(csv, "export-sense.csv");
}

var saveAs = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, fileName) {
var blob = new Blob([data], {type: "text/plain;charset=utf-8"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());

function updateEditorActionsBar() {
var editor_actions = $("#editor_actions");

Expand Down Expand Up @@ -290,7 +346,7 @@ function highlighCurrentRequestAndUpdateActionBar() {
if (new_current_req_range != null && CURRENT_REQ_RANGE != null &&
new_current_req_range.start.row == CURRENT_REQ_RANGE.start.row &&
new_current_req_range.end.row == CURRENT_REQ_RANGE.end.row
) {
) {
// same request, now see if we are on the first line and update the action bar
var cursorRow = sense.editor.getCursorPosition().row;
if (cursorRow == CURRENT_REQ_RANGE.start.row) {
Expand Down Expand Up @@ -467,6 +523,12 @@ function init() {
e.preventDefault();
});


$("#export_csv").click(function (e) {
exportCsv();
e.preventDefault();
});

var help_popup = $("#help_popup");

help_popup.on('shown', function () {
Expand Down

0 comments on commit 6b4b7c1

Please sign in to comment.