mirror of
https://github.com/getredash/redash.git
synced 2025-12-25 01:03:20 -05:00
Fix: cohort visulization had infinte digest loop
This commit is contained in:
@@ -1,82 +1,87 @@
|
||||
(function () {
|
||||
var cohortVisualization = angular.module('redash.visualization');
|
||||
var cohortVisualization = angular.module('redash.visualization');
|
||||
|
||||
cohortVisualization.config(['VisualizationProvider', function(VisualizationProvider) {
|
||||
cohortVisualization.config(['VisualizationProvider', function (VisualizationProvider) {
|
||||
|
||||
var editTemplate = '<cohort-editor></cohort-editor>';
|
||||
var defaultOptions = {
|
||||
'timeInterval': 'daily'
|
||||
};
|
||||
var editTemplate = '<cohort-editor></cohort-editor>';
|
||||
var defaultOptions = {
|
||||
'timeInterval': 'daily'
|
||||
};
|
||||
|
||||
VisualizationProvider.registerVisualization({
|
||||
type: 'COHORT',
|
||||
name: 'Cohort',
|
||||
renderTemplate: '<cohort-renderer options="visualization.options" query-result="queryResult"></cohort-renderer>',
|
||||
editorTemplate: editTemplate,
|
||||
defaultOptions: defaultOptions
|
||||
});
|
||||
}]);
|
||||
VisualizationProvider.registerVisualization({
|
||||
type: 'COHORT',
|
||||
name: 'Cohort',
|
||||
renderTemplate: '<cohort-renderer options="visualization.options" query-result="queryResult"></cohort-renderer>',
|
||||
editorTemplate: editTemplate,
|
||||
defaultOptions: defaultOptions
|
||||
});
|
||||
}]);
|
||||
|
||||
cohortVisualization.directive('cohortRenderer', function() {
|
||||
return {
|
||||
restrict: 'E',
|
||||
scope: {
|
||||
queryResult: '='
|
||||
},
|
||||
template: "",
|
||||
replace: false,
|
||||
link: function($scope, element, attrs) {
|
||||
$scope.$watch('[queryResult && queryResult.getData(), visualization.options.timeInterval ]', function () {
|
||||
if ($scope.queryResult.getData() == null) {
|
||||
cohortVisualization.directive('cohortRenderer', function () {
|
||||
return {
|
||||
restrict: 'E',
|
||||
scope: {
|
||||
queryResult: '=',
|
||||
options: '='
|
||||
},
|
||||
template: "",
|
||||
replace: false,
|
||||
link: function ($scope, element, attrs) {
|
||||
$scope.options.timeInterval = $scope.options.timeInterval || 'daily';
|
||||
|
||||
} else {
|
||||
var sortedData = _.sortBy($scope.queryResult.getData(),function(r) {
|
||||
return r['date'] + r['day_number'] ;
|
||||
});
|
||||
|
||||
var grouped = _.groupBy(sortedData, "date");
|
||||
var maxColumns = _.reduce(grouped, function(memo, data){
|
||||
return (data.length > memo)? data.length : memo;
|
||||
}, 0);
|
||||
var data = _.map(grouped, function(values, date) {
|
||||
var row = [values[0].total];
|
||||
_.each(values, function(value) { row.push(value.value); });
|
||||
_.each(_.range(values.length, maxColumns), function() { row.push(null); });
|
||||
return row;
|
||||
});
|
||||
var updateCohort = function () {
|
||||
if ($scope.queryResult.getData() === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var initialDate = moment(sortedData[0].date).toDate(),
|
||||
container = angular.element(element)[0];
|
||||
var sortedData = _.sortBy($scope.queryResult.getData(), function (r) {
|
||||
return r['date'] + r['day_number'];
|
||||
});
|
||||
|
||||
$scope.visualization.options.timeInterval = $scope.visualization.options.timeInterval || 'daily';
|
||||
var grouped = _.groupBy(sortedData, "date");
|
||||
|
||||
var maxColumns = _.reduce(grouped, function (memo, data) {
|
||||
return (data.length > memo) ? data.length : memo;
|
||||
}, 0);
|
||||
|
||||
var timeLabels = {'daily': 'Day', 'weekly': 'Week', 'monthly': 'Month'};
|
||||
var timeLabel = timeLabels[$scope.visualization.options.timeInterval];
|
||||
var data = _.map(grouped, function (values, date) {
|
||||
var row = [values[0].total];
|
||||
_.each(values, function (value) {
|
||||
row.push(value.value);
|
||||
});
|
||||
_.each(_.range(values.length, maxColumns), function () {
|
||||
row.push(null);
|
||||
});
|
||||
return row;
|
||||
});
|
||||
|
||||
Cornelius.draw({
|
||||
initialDate: initialDate,
|
||||
container: container,
|
||||
cohort: data,
|
||||
title: null,
|
||||
timeInterval: $scope.visualization.options.timeInterval,
|
||||
labels: {
|
||||
time: 'Activation ' + timeLabel,
|
||||
people: 'Users'
|
||||
},
|
||||
formatHeaderLabel: function (i) {
|
||||
return timeLabel + (i - 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
var initialDate = moment(sortedData[0].date).toDate(),
|
||||
container = angular.element(element)[0];
|
||||
|
||||
Cornelius.draw({
|
||||
initialDate: initialDate,
|
||||
container: container,
|
||||
cohort: data,
|
||||
title: null,
|
||||
timeInterval: $scope.options.timeInterval,
|
||||
labels: {
|
||||
time: 'Time',
|
||||
people: 'Users',
|
||||
weekOf: 'Week of'
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
cohortVisualization.directive('cohortEditor', function() {
|
||||
return {
|
||||
restrict: 'E',
|
||||
templateUrl: '/views/visualizations/cohort_editor.html'
|
||||
}
|
||||
});
|
||||
|
||||
$scope.$watch('queryResult && queryResult.getData()', updateCohort);
|
||||
$scope.$watch('options.timeInterval', updateCohort);
|
||||
}
|
||||
}
|
||||
});
|
||||
cohortVisualization.directive('cohortEditor', function () {
|
||||
return {
|
||||
restrict: 'E',
|
||||
templateUrl: '/views/visualizations/cohort_editor.html'
|
||||
}
|
||||
});
|
||||
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user