From 1218d3b35bfd5ef98a03d27772f87b090277cc11 Mon Sep 17 00:00:00 2001 From: Heiko Kernbach Date: Fri, 22 Aug 2014 16:54:53 +0200 Subject: [PATCH] added export feature for documents and for query results --- js/apps/system/aardvark/aardvark.js | 17 ++++++++ .../js/collections/arangoDocuments.js | 31 ++++++++++++++- .../frontend/js/templates/documentsView.ejs | 12 ++++++ .../frontend/js/templates/queryView.ejs | 2 +- .../frontend/js/views/documentsView.js | 39 ++++++++++++++++++- .../aardvark/frontend/js/views/queryView.js | 12 ++++++ .../frontend/scss/_documentsView.scss | 6 +++ .../aardvark/frontend/scss/generated.css | 5 +++ 8 files changed, 121 insertions(+), 3 deletions(-) diff --git a/js/apps/system/aardvark/aardvark.js b/js/apps/system/aardvark/aardvark.js index a4863cb222..5b50dd42b0 100644 --- a/js/apps/system/aardvark/aardvark.js +++ b/js/apps/system/aardvark/aardvark.js @@ -391,6 +391,23 @@ controller.get("/query/download/:user", function(req, res) { }).summary("Download all user queries") .notes("This function downloads all user queries from the given user"); +/** Download a query result + * + * Download and export all queries from the given username. + * + */ + +controller.get("/query/result/download/:query", function(req, res) { + var query = req.params("query"); + + var result = db._query(query).toArray(); + res.set("Content-Type", "application/json"); + res.set("Content-Disposition", "attachment; filename=results.json"); + res.json(result); + +}).summary("Download the result of a query") + .notes("This function downloads the result of a user query."); + // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // ----------------------------------------------------------------------------- diff --git a/js/apps/system/aardvark/frontend/js/collections/arangoDocuments.js b/js/apps/system/aardvark/frontend/js/collections/arangoDocuments.js index c419910ea2..54eee33832 100644 --- a/js/apps/system/aardvark/frontend/js/collections/arangoDocuments.js +++ b/js/apps/system/aardvark/frontend/js/collections/arangoDocuments.js @@ -8,6 +8,10 @@ filters: [], + MAX_SORT: 12000, + + lastQuery: {}, + sortAttribute: "_key", url: '/_api/documents', @@ -157,7 +161,7 @@ query = "FOR x in @@collection"; query += this.setFiltersForQuery(bindVars); // Sort result, only useful for a small number of docs - if (this.getTotal() < 12000) { + if (this.getTotal() < this.MAX_SORT) { if (this.getSort() === '_key') { query += " SORT TO_NUMBER(x." + this.getSort() + ") == 0 ? x." + this.getSort() + " : TO_NUMBER(x." + this.getSort() + ")"; @@ -210,6 +214,7 @@ }); }); } + self.lastQuery = queryObj; callback(); window.progressView.hide(); }, @@ -224,6 +229,30 @@ this.reset(); }, + buildDownloadDocumentQuery: function() { + var self = this, query, queryObj, bindVars; + + bindVars = { + "@collection": this.collectionID + }; + + query = "FOR x in " + this.collectionID; + query += this.setFiltersForQuery(bindVars); + // Sort result, only useful for a small number of docs + if (this.getTotal() < this.MAX_SORT) { + query += " SORT x." + this.getSort(); + } + + query += " RETURN x"; + + queryObj = { + query: query, + bindVars: bindVars + }; + + return query; + }, + updloadDocuments : function (file) { var result; $.ajax({ diff --git a/js/apps/system/aardvark/frontend/js/templates/documentsView.ejs b/js/apps/system/aardvark/frontend/js/templates/documentsView.ejs index 5a6167fee1..acf9b0f962 100644 --- a/js/apps/system/aardvark/frontend/js/templates/documentsView.ejs +++ b/js/apps/system/aardvark/frontend/js/templates/documentsView.ejs @@ -25,6 +25,11 @@ +
  • + + + +
  • @@ -68,6 +73,13 @@ +
    +
    +
    Please be careful. If no filter is set, the whole collection will be downloaded.
    + +
    +
    + diff --git a/js/apps/system/aardvark/frontend/js/views/documentsView.js b/js/apps/system/aardvark/frontend/js/views/documentsView.js index a4f1ccdafd..1fec1e3323 100644 --- a/js/apps/system/aardvark/frontend/js/views/documentsView.js +++ b/js/apps/system/aardvark/frontend/js/views/documentsView.js @@ -63,6 +63,7 @@ "click #markDocuments" : "editDocuments", "click #indexCollection" : "indexCollection", "click #importCollection" : "importCollection", + "click #exportCollection" : "exportCollection", "click #filterSend" : "sendFilter", "click #addFilterItem" : "addFilterItem", "click .removeFilterItem" : "removeFilterItem", @@ -83,6 +84,7 @@ "click #importModal" : "showImportModal", "click #resetView" : "resetView", "click #confirmDocImport" : "startUpload", + "click #exportDocuments" : "startDownload", "change #newIndexType" : "selectIndexType", "click #createIndex" : "createIndex", "click .deleteIndex" : "prepDeleteIndex", @@ -166,6 +168,17 @@ this.collection.getDocuments(this.getDocsCallback.bind(this)); }, + startDownload: function() { + var query = this.collection.buildDownloadDocumentQuery(); + + if (query !== '' || query !== undefined || query !== null) { + window.open(encodeURI("query/result/download/" + query)); + } + else { + arangoHelper.arangoError("Document error", "could not download documents"); + } + }, + startUpload: function () { var result; if (this.allowUpload === true) { @@ -244,27 +257,33 @@ } }, + //need to make following functions automatically! + editDocuments: function () { $('#indexCollection').removeClass('activated'); $('#importCollection').removeClass('activated'); + $('#exportCollection').removeClass('activated'); this.markFilterToggle(); $('#markDocuments').toggleClass('activated'); this.changeEditMode(); $('#filterHeader').hide(); $('#importHeader').hide(); $('#indexHeader').hide(); $('#editHeader').slideToggle(200); + $('#exportHeader').hide(); }, filterCollection : function () { $('#indexCollection').removeClass('activated'); $('#importCollection').removeClass('activated'); + $('#exportCollection').removeClass('activated'); $('#markDocuments').removeClass('activated'); this.changeEditMode(false); this.markFilterToggle(); this.activeFilter = true; - $('#filterHeader').slideToggle(200); $('#importHeader').hide(); $('#indexHeader').hide(); $('#editHeader').hide(); + $('#exportHeader').hide(); + $('#filterHeader').slideToggle(200); var i; for (i in this.filters) { @@ -275,20 +294,37 @@ } }, + exportCollection: function () { + $('#indexCollection').removeClass('activated'); + $('#importCollection').removeClass('activated'); + $('#filterHeader').removeClass('activated'); + $('#markDocuments').removeClass('activated'); this.changeEditMode(false); + $('#exportCollection').addClass('activated'); + this.markFilterToggle(); + $('#exportHeader').slideToggle(200); + $('#importHeader').hide(); + $('#indexHeader').hide(); + $('#filterHeader').hide(); + $('#editHeader').hide(); + }, + importCollection: function () { this.markFilterToggle(); $('#indexCollection').removeClass('activated'); $('#markDocuments').removeClass('activated'); this.changeEditMode(false); $('#importCollection').toggleClass('activated'); + $('#exportCollection').removeClass('activated'); $('#importHeader').slideToggle(200); $('#filterHeader').hide(); $('#indexHeader').hide(); $('#editHeader').hide(); + $('#exportHeader').hide(); }, indexCollection: function () { this.markFilterToggle(); $('#importCollection').removeClass('activated'); + $('#exportCollection').removeClass('activated'); $('#markDocuments').removeClass('activated'); this.changeEditMode(false); $('#indexCollection').toggleClass('activated'); $('#newIndexView').hide(); @@ -297,6 +333,7 @@ $('#importHeader').hide(); $('#editHeader').hide(); $('#filterHeader').hide(); + $('#exportHeader').hide(); }, changeEditMode: function (enable) { diff --git a/js/apps/system/aardvark/frontend/js/views/queryView.js b/js/apps/system/aardvark/frontend/js/views/queryView.js index 98a828b3bb..304e9c2c4b 100644 --- a/js/apps/system/aardvark/frontend/js/views/queryView.js +++ b/js/apps/system/aardvark/frontend/js/views/queryView.js @@ -38,6 +38,7 @@ 'click #arangoQueryTable .table-cell2 a': 'deleteAQL', 'click #confirmQueryImport': 'importCustomQueries', 'click #confirmQueryExport': 'exportCustomQueries', + 'click #downloadQueryResult': 'downloadQueryResult', 'click #importQueriesToggle': 'showImportMenu' }, @@ -295,6 +296,17 @@ } }, + downloadQueryResult: function() { + var inputEditor = ace.edit("aqlEditor"); + var query = inputEditor.getValue(); + if (query !== '' || query !== undefined || query !== null) { + window.open(encodeURI("query/result/download/" + query)); + } + else { + arangoHelper.arangoError("Query error", "could not query result."); + } + }, + exportCustomQueries: function () { var name, toExport = {}, exportArray = []; _.each(this.customQueries, function(value, key) { diff --git a/js/apps/system/aardvark/frontend/scss/_documentsView.scss b/js/apps/system/aardvark/frontend/scss/_documentsView.scss index 9bfddc89d3..c5a885b241 100644 --- a/js/apps/system/aardvark/frontend/scss/_documentsView.scss +++ b/js/apps/system/aardvark/frontend/scss/_documentsView.scss @@ -125,3 +125,9 @@ .ace_error { background: none !important; } + +#exportHeader .fa-exclamation-circle { + color: $c-nav-bg; + font-size: 13pt; + margin-right: 10px; +} diff --git a/js/apps/system/aardvark/frontend/scss/generated.css b/js/apps/system/aardvark/frontend/scss/generated.css index b2d2d09e3a..847eee3950 100644 --- a/js/apps/system/aardvark/frontend/scss/generated.css +++ b/js/apps/system/aardvark/frontend/scss/generated.css @@ -6116,3 +6116,8 @@ table .sorting { .ace_error { background: none !important; } + +#exportHeader .fa-exclamation-circle { + color: #333232; + font-size: 13pt; + margin-right: 10px; }