mirror of https://gitee.com/bigwinds/arangodb
Further impl of arangoDocuments. Warning all tests broken, ongoing work
This commit is contained in:
parent
bc8716c059
commit
f3b267db95
|
@ -14,11 +14,48 @@
|
|||
},
|
||||
|
||||
setPage: function(counter) {
|
||||
this.page = counter-1;
|
||||
if (counter > this.getLastPageNumber()) {
|
||||
this.page = this.getLastPageNumber();
|
||||
return;
|
||||
}
|
||||
if (counter < 1) {
|
||||
this.page = 0;
|
||||
return;
|
||||
}
|
||||
this.page = counter - 1;
|
||||
},
|
||||
|
||||
getLastPageNumber: function() {
|
||||
return Math.ceil(this.totalAmount / this.pagesize);
|
||||
return Math.max(Math.ceil(this.totalAmount / this.pagesize), 1);
|
||||
},
|
||||
|
||||
getOffset: function() {
|
||||
return this.getPage() * this.pagesize;
|
||||
},
|
||||
|
||||
getPageSize: function() {
|
||||
return this.pagesize;
|
||||
},
|
||||
|
||||
setToFirst: function() {
|
||||
this.page = 0;
|
||||
},
|
||||
|
||||
setToLast: function() {
|
||||
this.setPage(this.getLastPageNumber());
|
||||
},
|
||||
|
||||
setToPrev: function() {
|
||||
this.setPage(this.getPage() - 1);
|
||||
|
||||
},
|
||||
|
||||
setToNext: function() {
|
||||
this.setPage(this.getPage() + 1);
|
||||
},
|
||||
|
||||
setTotal: function(total) {
|
||||
this.totalAmount = total;
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -3,248 +3,263 @@
|
|||
(function() {
|
||||
"use strict";
|
||||
|
||||
window.arangoDocuments = Backbone.Collection.extend({
|
||||
currentPage: 1,
|
||||
collectionID: 1,
|
||||
totalPages: 1,
|
||||
documentsPerPage: 10,
|
||||
documentsCount: 1,
|
||||
offset: 0,
|
||||
window.arangoDocuments = window.PaginatedCollection.extend({
|
||||
currentPage: 1,
|
||||
collectionID: 1,
|
||||
totalPages: 1,
|
||||
documentsPerPage: 10,
|
||||
documentsCount: 1,
|
||||
offset: 0,
|
||||
|
||||
url: '/_api/documents',
|
||||
model: window.arangoDocumentModel,
|
||||
getFirstDocuments: function () {
|
||||
if (this.currentPage !== 1) {
|
||||
var link = window.location.hash.split("/");
|
||||
window.location.hash = link[0] + "/" + link[1] + "/" + link[2] + "/1";
|
||||
}
|
||||
},
|
||||
getLastDocuments: function () {
|
||||
if (this.currentPage !== this.totalPages) {
|
||||
var link = window.location.hash.split("/");
|
||||
window.location.hash = link[0] + "/" + link[1] + "/" + link[2] + "/" + this.totalPages;
|
||||
}
|
||||
},
|
||||
getPrevDocuments: function () {
|
||||
if (this.currentPage !== 1) {
|
||||
var link = window.location.hash.split("/");
|
||||
var page = parseInt(this.currentPage, null) - 1;
|
||||
window.location.hash = link[0] + "/" + link[1] + "/" + link[2] + "/" + page;
|
||||
}
|
||||
},
|
||||
getNextDocuments: function () {
|
||||
if (this.currentPage !== this.totalPages) {
|
||||
var link = window.location.hash.split("/");
|
||||
var page = parseInt(this.currentPage, null) + 1;
|
||||
window.location.hash = link[0] + "/" + link[1] + "/" + link[2] + "/" + page;
|
||||
}
|
||||
},
|
||||
getDocuments: function (colid, currpage) {
|
||||
var self = this;
|
||||
this.collectionID = colid;
|
||||
this.currentPage = currpage;
|
||||
filters: [],
|
||||
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: "GET",
|
||||
url: "/_api/collection/" + this.collectionID + "/count",
|
||||
contentType: "application/json",
|
||||
processData: false,
|
||||
async: false,
|
||||
success: function(data) {
|
||||
self.totalPages = Math.ceil(data.count / self.documentsPerPage);
|
||||
self.documentsCount = data.count;
|
||||
},
|
||||
error: function(data) {
|
||||
}
|
||||
});
|
||||
url: '/_api/documents',
|
||||
model: window.arangoDocumentModel,
|
||||
|
||||
setCollection: function(id) {
|
||||
this.collectionID = id;
|
||||
},
|
||||
|
||||
// TODO Remove this block
|
||||
|
||||
getFirstDocuments: function () {
|
||||
this.setToFirst();
|
||||
},
|
||||
|
||||
getLastDocuments: function () {
|
||||
this.setToLast();
|
||||
},
|
||||
|
||||
getPrevDocuments: function () {
|
||||
this.setToPrev();
|
||||
},
|
||||
getNextDocuments: function () {
|
||||
this.setToNext();
|
||||
},
|
||||
|
||||
// TODO Endof Remove this block
|
||||
|
||||
getTotalDocuments: function() {
|
||||
var result;
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: "GET",
|
||||
url: "/_api/collection/" + this.collectionID + "/count",
|
||||
contentType: "application/json",
|
||||
processData: false,
|
||||
async: false,
|
||||
success: function(data) {
|
||||
result = data.count;
|
||||
},
|
||||
error: function() {
|
||||
}
|
||||
});
|
||||
this.setTotal(result);
|
||||
return result;
|
||||
},
|
||||
|
||||
|
||||
if (isNaN(this.currentPage) || this.currentPage === undefined || this.currentPage < 1) {
|
||||
addFilter: function(attr, op, val) {
|
||||
this.filters.push({
|
||||
attr: attr,
|
||||
op: op,
|
||||
val: val
|
||||
});
|
||||
},
|
||||
|
||||
setFiltersForQuery: function(bindVars) {
|
||||
if (this.filters.length === 0) {
|
||||
return;
|
||||
}
|
||||
var query = " FILTER",
|
||||
parts = _.map(this.filters, function(f, i) {
|
||||
var res = " x.`";
|
||||
res += f.attr;
|
||||
res += "` ";
|
||||
res += f.op;
|
||||
res += " @param";
|
||||
res += i;
|
||||
bindVars["param" + i] = this.val;
|
||||
return res;
|
||||
});
|
||||
return query + parts.join(" &&");
|
||||
},
|
||||
|
||||
resetFilter: function() {
|
||||
this.filters = [];
|
||||
},
|
||||
|
||||
getDocuments: function () {
|
||||
var self = this,
|
||||
query,
|
||||
bindVars;
|
||||
bindVars = {
|
||||
"@collection": this.collectionID,
|
||||
"offset": this.getOffset(),
|
||||
"count": this.getPageSize()
|
||||
};
|
||||
|
||||
query = "FOR x in @@collection";
|
||||
query += this.setFiltersForQuery(bindVars);
|
||||
// Sort result, only useful for a small number of docs
|
||||
if (this.getTotal() < 10000) {
|
||||
query += " SORT TO_NUMBER(x._key) == 0 ? x._key : TO_NUMBER(x._key)";
|
||||
}
|
||||
query += " LIMIT @offset, @count RETURN x";
|
||||
|
||||
var myQuery = {
|
||||
query: query,
|
||||
bindVars: bindVars
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: 'POST',
|
||||
async: false,
|
||||
url: '/_api/cursor',
|
||||
data: JSON.stringify(myQuery),
|
||||
contentType: "application/json",
|
||||
success: function(data) {
|
||||
self.clearDocuments();
|
||||
self.setTotal(data.extra.fullCount);
|
||||
if (self.documentsCount !== 0) {
|
||||
_.each(data.result, function(v) {
|
||||
self.add({
|
||||
"id": v._id,
|
||||
"rev": v._rev,
|
||||
"key": v._key,
|
||||
"content": v
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
getFilteredDocuments: function (colid, currpage, filter, bindValues) {
|
||||
var self = this;
|
||||
this.collectionID = colid;
|
||||
this.currentPage = currpage;
|
||||
this.currentFilterPage = currpage;
|
||||
var filterString;
|
||||
if(filter.length === 0){
|
||||
filterString ="";
|
||||
} else {
|
||||
filterString = ' FILTER' + filter.join(' && ');
|
||||
}
|
||||
|
||||
var sortCount = 10000;
|
||||
|
||||
var sortString = '';
|
||||
if (this.documentsCount <= sortCount) {
|
||||
//sorted
|
||||
sortString = " SORT TO_NUMBER(u._key) == 0 ? u._key : TO_NUMBER(u._key)";
|
||||
}
|
||||
|
||||
var myQueryVal = "FOR u in @@collection" + filterString + sortString +
|
||||
" LIMIT @offset, @count RETURN u";
|
||||
|
||||
this.offset = (this.currentPage - 1) * this.documentsPerPage;
|
||||
|
||||
var myQuery = {
|
||||
query: myQueryVal,
|
||||
bindVars: {
|
||||
"@collection": this.collectionID,
|
||||
"count": this.documentsPerPage,
|
||||
"offset": this.offset
|
||||
},
|
||||
options: {
|
||||
fullCount: true
|
||||
}
|
||||
};
|
||||
|
||||
$.each(bindValues, function(k,v) {
|
||||
myQuery.bindVars[k] = v;
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: 'POST',
|
||||
async: false,
|
||||
url: '/_api/cursor',
|
||||
data: JSON.stringify(myQuery),
|
||||
contentType: "application/json",
|
||||
success: function(data) {
|
||||
self.clearDocuments();
|
||||
self.documentsCount = data.extra.fullCount;
|
||||
self.totalPages = Math.ceil(self.documentsCount / self.documentsPerPage);
|
||||
if (
|
||||
isNaN(this.currentPage)
|
||||
|| this.currentPage === undefined
|
||||
|| this.currentPage < 1
|
||||
) {
|
||||
this.currentPage = 1;
|
||||
}
|
||||
if (this.totalPages === 0) {
|
||||
if (this.totalPages === 0 || this.totalPages === undefined) {
|
||||
this.totalPages = 1;
|
||||
}
|
||||
|
||||
this.offset = (this.currentPage - 1) * this.documentsPerPage;
|
||||
|
||||
var myQueryVal;
|
||||
var sortCount = 10000;
|
||||
|
||||
if (this.documentsCount <= sortCount) {
|
||||
//sorted
|
||||
myQueryVal = "FOR x in @@collection SORT TO_NUMBER(x._key) == 0 " +
|
||||
"? x._key : TO_NUMBER(x._key) LIMIT @offset, @count RETURN x";
|
||||
if (self.documentsCount !== 0) {
|
||||
$.each(data.result, function(k, v) {
|
||||
window.arangoDocumentsStore.add({
|
||||
"id": v._id,
|
||||
"rev": v._rev,
|
||||
"key": v._key,
|
||||
"content": v
|
||||
});
|
||||
});
|
||||
window.documentsView.drawTable();
|
||||
window.documentsView.renderPagination(self.totalPages, true);
|
||||
}
|
||||
else {
|
||||
//not sorted
|
||||
myQueryVal = "FOR x in @@collection LIMIT @offset, @count RETURN x";
|
||||
window.documentsView.initTable();
|
||||
window.documentsView.drawTable();
|
||||
}
|
||||
|
||||
var myQuery = {
|
||||
query: myQueryVal,
|
||||
bindVars: {
|
||||
"@collection": this.collectionID,
|
||||
"offset": this.offset,
|
||||
"count": this.documentsPerPage
|
||||
}
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: 'POST',
|
||||
async: false,
|
||||
url: '/_api/cursor',
|
||||
data: JSON.stringify(myQuery),
|
||||
contentType: "application/json",
|
||||
success: function(data) {
|
||||
self.clearDocuments();
|
||||
if (self.documentsCount !== 0) {
|
||||
$.each(data.result, function(k, v) {
|
||||
//ERROR HERE
|
||||
window.arangoDocumentsStore.add({
|
||||
"id": v._id,
|
||||
"rev": v._rev,
|
||||
"key": v._key,
|
||||
"content": v
|
||||
});
|
||||
});
|
||||
window.documentsView.drawTable();
|
||||
window.documentsView.renderPagination(self.totalPages);
|
||||
}
|
||||
else {
|
||||
window.documentsView.initTable();
|
||||
window.documentsView.drawTable();
|
||||
}
|
||||
},
|
||||
error: function(data) {
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
getFilteredDocuments: function (colid, currpage, filter, bindValues) {
|
||||
var self = this;
|
||||
this.collectionID = colid;
|
||||
this.currentPage = currpage;
|
||||
this.currentFilterPage = currpage;
|
||||
var filterString;
|
||||
if(filter.length === 0){
|
||||
filterString ="";
|
||||
} else {
|
||||
filterString = ' FILTER' + filter.join(' && ');
|
||||
}
|
||||
|
||||
var sortCount = 10000;
|
||||
|
||||
var sortString = '';
|
||||
if (this.documentsCount <= sortCount) {
|
||||
//sorted
|
||||
sortString = " SORT TO_NUMBER(u._key) == 0 ? u._key : TO_NUMBER(u._key)";
|
||||
}
|
||||
|
||||
var myQueryVal = "FOR u in @@collection" + filterString + sortString +
|
||||
" LIMIT @offset, @count RETURN u";
|
||||
|
||||
this.offset = (this.currentPage - 1) * this.documentsPerPage;
|
||||
|
||||
var myQuery = {
|
||||
query: myQueryVal,
|
||||
bindVars: {
|
||||
"@collection": this.collectionID,
|
||||
"count": this.documentsPerPage,
|
||||
"offset": this.offset
|
||||
},
|
||||
options: {
|
||||
fullCount: true
|
||||
}
|
||||
};
|
||||
|
||||
$.each(bindValues, function(k,v) {
|
||||
myQuery.bindVars[k] = v;
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: 'POST',
|
||||
async: false,
|
||||
url: '/_api/cursor',
|
||||
data: JSON.stringify(myQuery),
|
||||
contentType: "application/json",
|
||||
success: function(data) {
|
||||
self.clearDocuments();
|
||||
self.documentsCount = data.extra.fullCount;
|
||||
self.totalPages = Math.ceil(self.documentsCount / self.documentsPerPage);
|
||||
if (
|
||||
isNaN(this.currentPage)
|
||||
|| this.currentPage === undefined
|
||||
|| this.currentPage < 1
|
||||
) {
|
||||
this.currentPage = 1;
|
||||
}
|
||||
if (this.totalPages === 0 || this.totalPages === undefined) {
|
||||
this.totalPages = 1;
|
||||
}
|
||||
|
||||
this.offset = (this.currentPage - 1) * this.documentsPerPage;
|
||||
if (self.documentsCount !== 0) {
|
||||
$.each(data.result, function(k, v) {
|
||||
window.arangoDocumentsStore.add({
|
||||
"id": v._id,
|
||||
"rev": v._rev,
|
||||
"key": v._key,
|
||||
"content": v
|
||||
});
|
||||
});
|
||||
window.documentsView.drawTable();
|
||||
window.documentsView.renderPagination(self.totalPages, true);
|
||||
}
|
||||
else {
|
||||
window.documentsView.initTable();
|
||||
window.documentsView.drawTable();
|
||||
}
|
||||
},
|
||||
error: function(data) {
|
||||
}
|
||||
});
|
||||
},
|
||||
clearDocuments: function () {
|
||||
window.arangoDocumentsStore.reset();
|
||||
},
|
||||
getStatisticsHistory: function(params) {
|
||||
var self = this;
|
||||
var body = {
|
||||
startDate : params.startDate,
|
||||
endDate : params.endDate,
|
||||
figures : params.figures
|
||||
};
|
||||
var server = params.server;
|
||||
var addAuth = function(){};
|
||||
var url = "";
|
||||
if (server) {
|
||||
url = server.endpoint;
|
||||
url += "/_admin/history";
|
||||
if (server.isDBServer) {
|
||||
url += "?DBserver=" + server.target;
|
||||
}
|
||||
addAuth = server.addAuth;
|
||||
} else {
|
||||
url = "/_admin/history";
|
||||
}
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: 'POST',
|
||||
async: false,
|
||||
url: url,
|
||||
data: JSON.stringify(body),
|
||||
contentType: "application/json",
|
||||
beforeSend: addAuth,
|
||||
success: function(data) {
|
||||
self.history = data.result;
|
||||
},
|
||||
error: function(data) {
|
||||
}
|
||||
});
|
||||
error: function(data) {
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
clearDocuments: function () {
|
||||
this.reset();
|
||||
},
|
||||
|
||||
getStatisticsHistory: function(params) {
|
||||
var self = this;
|
||||
var body = {
|
||||
startDate : params.startDate,
|
||||
endDate : params.endDate,
|
||||
figures : params.figures
|
||||
};
|
||||
var server = params.server;
|
||||
var addAuth = function(){};
|
||||
var url = "";
|
||||
if (server) {
|
||||
url = server.endpoint;
|
||||
url += "/_admin/history";
|
||||
if (server.isDBServer) {
|
||||
url += "?DBserver=" + server.target;
|
||||
}
|
||||
addAuth = server.addAuth;
|
||||
} else {
|
||||
url = "/_admin/history";
|
||||
}
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type: 'POST',
|
||||
async: false,
|
||||
url: url,
|
||||
data: JSON.stringify(body),
|
||||
contentType: "application/json",
|
||||
beforeSend: addAuth,
|
||||
success: function(data) {
|
||||
self.history = data.result;
|
||||
},
|
||||
error: function(data) {
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}());
|
||||
|
|
Loading…
Reference in New Issue