Store filters on QueryResult object and use them in getQueryData.

This commit is contained in:
Arik Fraimovich 2014-03-17 20:22:08 +02:00
parent 2b5291900d
commit a99e290bc5
2 changed files with 67 additions and 31 deletions

View File

@ -7,6 +7,7 @@
angular.extend(this, props);
if ('query_result' in props) {
this.status = "done";
this.filters = undefined;
_.each(this.query_result.data.rows, function (row) {
_.each(row, function (v, k) {
@ -26,6 +27,7 @@
this.job = {};
this.query_result = {};
this.status = "waiting";
this.filters = undefined;
this.updatedAt = moment();
@ -76,15 +78,51 @@
return this.query_result.runtime;
}
QueryResult.prototype.getData = function() {
QueryResult.prototype.getRawData = function() {
if (!this.query_result.data) {
return null;
}
var data = this.query_result.data.rows;
return data;
}
QueryResult.prototype.getData = function() {
if (!this.query_result.data) {
return null;
}
var filterValues = function(filters) {
if (!filters) {
return null;
}
return _.reduce(filters, function(str, filter) {
return str + filter.current;
}, "")
}
var filters = this.getFilters();
var filterFreeze = filterValues(filters);
if (this.filterFreeze != filterFreeze) {
this.filterFreeze = filterFreeze;
if (filters) {
this.filteredData = _.filter(this.query_result.data.rows, function (row) {
return _.reduce(filters, function (memo, filter) {
return (memo && row[filter.name] == filter.current);
}, true);
});
} else {
this.filteredData = this.query_result.data.rows;
}
}
return this.filteredData;
}
QueryResult.prototype.getChartData = function () {
var series = {};
@ -181,6 +219,14 @@
}
QueryResult.prototype.getFilters = function () {
if (!this.filters) {
this.prepareFilters();
}
return this.filters;
};
QueryResult.prototype.prepareFilters = function() {
var filterNames = [];
_.each(this.getColumns(), function (col) {
if (col.split('::')[1] == 'filter') {
@ -189,7 +235,7 @@
});
var filterValues = [];
_.each(this.getData(), function (row) {
_.each(this.getRawData(), function (row) {
_.each(filterNames, function (filter, i) {
if (filterValues[i] == undefined) {
filterValues[i] = [];
@ -198,7 +244,7 @@
})
});
var filters = _.map(filterNames, function (filter, i) {
this.filters = _.map(filterNames, function (filter, i) {
var f = {
name: filter,
friendlyName: this.getColumnFriendlyName(filter),
@ -208,9 +254,7 @@
f.current = f.values[0];
return f;
}, this);
return filters;
};
}
var refreshStatus = function(queryResult, query, ttl) {
Job.get({'id': queryResult.job.id}, function(response) {

View File

@ -38,17 +38,21 @@
$scope.gridData = [];
$scope.filters = [];
} else {
$scope.filters = $scope.queryResult.getFilters();
var gridData = _.map($scope.queryResult.getData(), function (row) {
var newRow = {};
_.each(row, function (val, key) {
newRow[$scope.queryResult.getColumnCleanName(key)] = val;
})
return newRow;
});
var prepareGridData = function(data) {
var gridData = _.map(data, function (row) {
var newRow = {};
_.each(row, function (val, key) {
newRow[$scope.queryResult.getColumnCleanName(key)] = val;
})
return newRow;
});
return gridData;
};
$scope.gridData = prepareGridData($scope.queryResult.getData());
$scope.gridColumns = _.map($scope.queryResult.getColumnCleanNames(), function (col, i) {
var columnDefinition = {
@ -56,8 +60,10 @@
'map': col
};
if (gridData.length > 0) {
var exampleData = gridData[0][col];
var rawData = $scope.queryResult.getRawData();
if (rawData.length > 0) {
var exampleData = rawData[0][col];
if (angular.isNumber(exampleData)) {
columnDefinition['formatFunction'] = 'number';
columnDefinition['formatParameter'] = 2;
@ -76,20 +82,6 @@
return columnDefinition;
});
$scope.gridData = _.clone(gridData);
$scope.$watch('filters', function (filters) {
$scope.gridData = _.filter(gridData, function (row) {
return _.reduce(filters, function (memo, filter) {
if (filter.current == 'All') {
return memo && true;
}
return (memo && row[$scope.queryResult.getColumnCleanName(filter.name)] == filter.current);
}, true);
});
}, true);
}
});
}]