Skip to content

Commit

Permalink
Move events server side (re #245)
Browse files Browse the repository at this point in the history
  • Loading branch information
alison985 authored and Allen Short committed Jan 17, 2018
1 parent dd65de6 commit 68c7bb8
Show file tree
Hide file tree
Showing 32 changed files with 169 additions and 63 deletions.
1 change: 0 additions & 1 deletion client/app/components/dashboards/edit-dashboard-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const EditDashboardDialog = {
'Please copy/backup your changes and reload this page.', { autoDismiss: false });
}
});
Events.record('edit', 'dashboard', this.dashboard.id);
} else {
$http.post('api/dashboards', {
name: this.dashboard.name,
Expand Down
2 changes: 0 additions & 2 deletions client/app/components/dashboards/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ function DashboardWidgetCtrl($location, $uibModal, $window, Events, currentUser)
return;
}

Events.record('delete', 'widget', this.widget.id);

this.widget.$delete((response) => {
this.dashboard.widgets = this.dashboard.widgets.filter(widget => widget.id !== undefined);
this.dashboard.version = response.version;
Expand Down
3 changes: 1 addition & 2 deletions client/app/pages/admin/outdated-queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import moment from 'moment';
import { Paginator } from '@/lib/pagination';
import template from './outdated-queries.html';

function OutdatedQueriesCtrl($scope, Events, $http, $timeout) {
Events.record('view', 'page', 'admin/outdated_queries');
function OutdatedQueriesCtrl($scope, $http, $timeout) {
$scope.autoUpdate = true;

this.queries = new Paginator([], { itemsPerPage: 50 });
Expand Down
3 changes: 1 addition & 2 deletions client/app/pages/admin/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import moment from 'moment';
import { Paginator } from '@/lib/pagination';
import template from './tasks.html';

function TasksCtrl($scope, $location, $http, $timeout, Events) {
Events.record('view', 'page', 'admin/tasks');
function TasksCtrl($scope, $location, $http, $timeout) {
$scope.autoUpdate = true;

$scope.selectedTab = 'in_progress';
Expand Down
2 changes: 0 additions & 2 deletions client/app/pages/alert/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ function AlertCtrl($routeParams, $location, $sce, toastr, currentUser, Query, Ev

if (this.alertId === 'new') {
Events.record('view', 'page', 'alerts/new');
} else {
Events.record('view', 'alert', this.alertId);
}

this.trustAsHtml = html => $sce.trustAsHtml(html);
Expand Down
4 changes: 1 addition & 3 deletions client/app/pages/alerts-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const stateClass = {
};

class AlertsListCtrl {
constructor(Events, Alert) {
Events.record('view', 'page', 'alerts');

constructor(Alert) {
this.alerts = new Paginator([], { itemsPerPage: 20 });
Alert.query((alerts) => {
this.alerts.updateRows(alerts.map(alert => ({
Expand Down
2 changes: 0 additions & 2 deletions client/app/pages/dashboards/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ function DashboardCtrl(

this.loadDashboard = _.throttle((force) => {
this.dashboard = Dashboard.get({ slug: $routeParams.dashboardSlug }, (dashboard) => {
Events.record('view', 'dashboard', dashboard.id);
renderDashboard(dashboard, force);
}, (rejection) => {
const statusGroup = Math.floor(rejection.status / 100);
Expand All @@ -188,7 +187,6 @@ function DashboardCtrl(

this.archiveDashboard = () => {
const archive = () => {
Events.record('archive', 'dashboard', this.dashboard.id);
this.dashboard.$delete(() => {
$rootScope.$broadcast('reloadDashboards');
});
Expand Down
4 changes: 1 addition & 3 deletions client/app/pages/data-sources/list.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import settingsMenu from '@/lib/settings-menu';
import template from './list.html';

function DataSourcesCtrl($scope, $location, currentUser, Events, DataSource) {
Events.record('view', 'page', 'admin/data_sources');

function DataSourcesCtrl($scope, $location, currentUser, DataSource) {
$scope.dataSources = DataSource.query();
}

Expand Down
10 changes: 1 addition & 9 deletions client/app/pages/data-sources/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ const logger = debug('redash:http');

function DataSourceCtrl(
$scope, $routeParams, $http, $location, toastr,
currentUser, Events, DataSource,
currentUser, DataSource,
) {
Events.record('view', 'page', 'admin/data_source');

$scope.dataSourceId = $routeParams.dataSourceId;

if ($scope.dataSourceId === 'new') {
Expand All @@ -24,8 +22,6 @@ function DataSourceCtrl(
});

function deleteDataSource() {
Events.record('delete', 'datasource', $scope.dataSource.id);

$scope.dataSource.$delete(() => {
toastr.success('Data source deleted successfully.');
$location.path('/data_sources/');
Expand All @@ -36,8 +32,6 @@ function DataSourceCtrl(
}

function testConnection(callback) {
Events.record('test', 'datasource', $scope.dataSource.id);

DataSource.test({ id: $scope.dataSource.id }, (httpResponse) => {
if (httpResponse.ok) {
toastr.success('Success');
Expand All @@ -53,8 +47,6 @@ function DataSourceCtrl(
}

function getDataSourceVersion(callback) {
Events.record('test', 'data_source_version', $scope.dataSource.id);

DataSource.version({ id: $scope.dataSource.id }, (httpResponse) => {
if (httpResponse.ok) {
const versionNumber = httpResponse.message;
Expand Down
4 changes: 1 addition & 3 deletions client/app/pages/destinations/list.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import settingsMenu from '@/lib/settings-menu';
import template from './list.html';

function DestinationsCtrl($scope, $location, toastr, currentUser, Events, Destination) {
Events.record('view', 'page', 'admin/destinations');

function DestinationsCtrl($scope, $location, toastr, currentUser, Destination) {
$scope.destinations = Destination.query();
}

Expand Down
6 changes: 1 addition & 5 deletions client/app/pages/destinations/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ const logger = debug('redash:http');

function DestinationCtrl(
$scope, $routeParams, $http, $location, toastr,
currentUser, Events, Destination,
currentUser, Destination,
) {
Events.record('view', 'page', 'admin/destination');

$scope.destinationId = $routeParams.destinationId;

if ($scope.destinationId === 'new') {
Expand All @@ -25,8 +23,6 @@ function DestinationCtrl(
});

$scope.delete = () => {
Events.record('delete', 'destination', $scope.destination.id);

$scope.destination.$delete(() => {
toastr.success('Destination deleted successfully.');
$location.path('/destinations/');
Expand Down
3 changes: 1 addition & 2 deletions client/app/pages/groups/data-sources.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { contains } from 'underscore';
import template from './data-sources.html';

function GroupDataSourcesCtrl($scope, $routeParams, $http, Events, Group, DataSource) {
Events.record('view', 'group_data_sources', $scope.groupId);
function GroupDataSourcesCtrl($scope, $routeParams, $http, Group, DataSource) {
$scope.group = Group.get({ id: $routeParams.groupId });
$scope.dataSources = Group.dataSources({ id: $routeParams.groupId });
$scope.newDataSource = {};
Expand Down
3 changes: 1 addition & 2 deletions client/app/pages/groups/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import settingsMenu from '@/lib/settings-menu';
import { Paginator } from '@/lib/pagination';
import template from './list.html';

function GroupsCtrl($scope, $uibModal, currentUser, Events, Group) {
Events.record('view', 'page', 'groups');
function GroupsCtrl($scope, $uibModal, currentUser, Group) {
$scope.currentUser = currentUser;
$scope.groups = new Paginator([], { itemsPerPage: 20 });
Group.query((groups) => {
Expand Down
4 changes: 1 addition & 3 deletions client/app/pages/groups/show.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { contains } from 'underscore';
import template from './show.html';

function GroupCtrl($scope, $routeParams, $http, currentUser, Events, Group, User) {
Events.record('view', 'group', $scope.groupId);

function GroupCtrl($scope, $routeParams, $http, currentUser, Group, User) {
$scope.currentUser = currentUser;
$scope.group = Group.get({ id: $routeParams.groupId });
$scope.members = Group.members({ id: $routeParams.groupId });
Expand Down
2 changes: 0 additions & 2 deletions client/app/pages/queries/queries-search-results-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ function QuerySearchCtrl($location, $filter, currentUser, Events, Query) {
$location.search({ q: this.term });
}
};

Events.record('search', 'query', '', { term: this.term });
}

export default function init(ngModule) {
Expand Down
4 changes: 0 additions & 4 deletions client/app/pages/queries/source-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ function QuerySourceCtrl(
};

$scope.duplicateQuery = () => {
Events.record('fork', 'query', $scope.query.id);

Query.fork({ id: $scope.query.id }, (newQuery) => {
$location.url(newQuery.getSourceLink()).replace();
});
Expand All @@ -87,8 +85,6 @@ function QuerySourceCtrl(
const confirm = { class: 'btn-danger', title: 'Delete' };

AlertDialog.open(title, message, confirm).then(() => {
Events.record('delete', 'visualization', vis.id);

Visualization.delete({ id: vis.id }, () => {
if ($scope.selectedTab === String(vis.id)) {
$scope.selectedTab = DEFAULT_TAB;
Expand Down
1 change: 0 additions & 1 deletion client/app/pages/queries/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function QueryViewCtrl(
KeyboardShortcuts.unbind(shortcuts);
});

Events.record('view', 'query', $scope.query.id);
if ($scope.query.hasResult() || $scope.query.paramsRequired()) {
getQueryResult();
}
Expand Down
1 change: 0 additions & 1 deletion client/app/pages/query-snippets/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import template from './edit.html';

function SnippetCtrl($routeParams, $http, $location, toastr, currentUser, Events, QuerySnippet) {
this.snippetId = $routeParams.snippetId;
Events.record('view', 'query_snippet', this.snippetId);

this.editorOptions = {
mode: 'snippets',
Expand Down
4 changes: 1 addition & 3 deletions client/app/pages/query-snippets/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import settingsMenu from '@/lib/settings-menu';
import { Paginator } from '@/lib/pagination';
import template from './list.html';

function SnippetsCtrl($location, currentUser, Events, QuerySnippet) {
Events.record('view', 'page', 'query_snippets');

function SnippetsCtrl($location, currentUser, QuerySnippet) {
this.snippets = new Paginator([], { itemsPerPage: 20 });
QuerySnippet.query((snippets) => {
this.snippets.updateRows(snippets);
Expand Down
4 changes: 1 addition & 3 deletions client/app/pages/users/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import settingsMenu from '@/lib/settings-menu';
import { Paginator } from '@/lib/pagination';
import template from './list.html';

function UsersCtrl(currentUser, Events, User) {
Events.record('view', 'page', 'users');

function UsersCtrl(currentUser, User) {
this.currentUser = currentUser;
this.users = new Paginator([], { itemsPerPage: 20 });
User.query((users) => {
Expand Down
3 changes: 1 addition & 2 deletions client/app/pages/users/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import './settings.less';

function UserCtrl(
$scope, $routeParams, $http, $location, toastr,
clientConfig, currentUser, Events, User,
clientConfig, currentUser, User,
) {
$scope.userId = $routeParams.userId;
$scope.currentUser = currentUser;
Expand All @@ -14,7 +14,6 @@ function UserCtrl(
$scope.userId = currentUser.id;
}

Events.record('view', 'user', $scope.userId);
$scope.canEdit = currentUser.hasPermission('admin') || currentUser.id === parseInt($scope.userId, 10);
$scope.showSettings = false;
$scope.showPasswordSettings = false;
Expand Down
19 changes: 17 additions & 2 deletions redash/handlers/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
import time

from flask import request
from flask_login import login_required
from flask_login import current_user, login_required
from redash import models, redis_connection
from redash.authentication import current_org
from redash.handlers import routes
from redash.handlers.base import json_response
from redash.handlers.base import json_response, record_event
from redash.permissions import require_super_admin
from redash.tasks.queries import QueryTaskTracker

Expand All @@ -23,6 +25,13 @@ def outdated_queries():
else:
outdated_queries = []

record_event(current_org, current_user, {
'action': 'view',
'object_type': 'api_call',
'object_id': 'admin/outdated_queries',
'timestamp': int(time.time()),
})

return json_response(
dict(queries=[q.to_dict(with_stats=True, with_last_modified_by=False)
for q in outdated_queries],
Expand All @@ -41,6 +50,12 @@ def queries_tasks():
waiting = QueryTaskTracker.all(QueryTaskTracker.WAITING_LIST, limit=waiting_limit)
in_progress = QueryTaskTracker.all(QueryTaskTracker.IN_PROGRESS_LIST, limit=progress_limit)
done = QueryTaskTracker.all(QueryTaskTracker.DONE_LIST, limit=done_limit)
record_event(current_org, current_user, {
'action': 'view',
'object_type': 'api_call',
'object_id': 'admin/tasks',
'timestamp': int(time.time()),
})

response = {
'waiting': [t.data for t in waiting if t is not None],
Expand Down
12 changes: 12 additions & 0 deletions redash/handlers/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class AlertResource(BaseResource):
def get(self, alert_id):
alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)
require_access(alert.groups, self.current_user, view_only)
self.record_event({
'action': 'view',
'timestamp': int(time.time()),
'object_id': alert.id,
'object_type': 'alert'
})
return alert.to_dict()

def post(self, alert_id):
Expand Down Expand Up @@ -73,6 +79,12 @@ def post(self):

@require_permission('list_alerts')
def get(self):
self.record_event({
'action': 'view',
'timestamp': int(time.time()),
'object_id': 'alerts',
'object_type': 'api_call'
})
return [alert.to_dict() for alert in models.Alert.all(group_ids=self.current_user.group_ids)]


Expand Down
16 changes: 16 additions & 0 deletions redash/handlers/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ def get(self, dashboard_slug=None):

response['can_edit'] = can_modify(dashboard, self.current_user)

self.record_event({
'action': 'view',
'object_id': dashboard.id,
'object_type': 'dashboard',
})

return response

@require_permission('edit_dashboard')
Expand Down Expand Up @@ -158,6 +164,11 @@ def post(self, dashboard_slug):
abort(400)

result = dashboard.to_dict(with_widgets=True, user=self.current_user)
self.record_event({
'action': 'edit',
'object_id': dashboard.id,
'object_type': 'dashboard',
})
return result

@require_permission('edit_dashboard')
Expand All @@ -175,6 +186,11 @@ def delete(self, dashboard_slug):
models.db.session.add(dashboard)
d = dashboard.to_dict(with_widgets=True, user=self.current_user)
models.db.session.commit()
self.record_event({
'action': 'archive',
'object_id': dashboard.id,
'object_type': 'dashboard',
})
return d


Expand Down
Loading

0 comments on commit 68c7bb8

Please sign in to comment.