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 jezdez committed Sep 6, 2018
1 parent 05698fa commit 877eaa5
Show file tree
Hide file tree
Showing 30 changed files with 168 additions and 60 deletions.
2 changes: 0 additions & 2 deletions client/app/components/dashboards/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ function DashboardWidgetCtrl($location, $uibModal, $window, Events, currentUser)
return;
}

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

this.widget.delete().then(() => {
if (this.deleted) {
this.deleted({});
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
3 changes: 1 addition & 2 deletions client/app/pages/alerts-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const stateClass = {
};

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

this.showEmptyState = false;
this.showList = false;
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 @@ -179,7 +179,6 @@ function DashboardCtrl(
(dashboard) => {
this.dashboard = dashboard;
this.isDashboardOwner = currentUser.id === dashboard.user.id || currentUser.hasPermission('admin');
Events.record('view', 'dashboard', dashboard.id);
renderDashboard(dashboard, force);

if ($location.search().edit === true) {
Expand Down Expand Up @@ -229,7 +228,6 @@ function DashboardCtrl(

this.archiveDashboard = () => {
const archive = () => {
Events.record('archive', 'dashboard', this.dashboard.id);
this.dashboard.$delete();
};

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(Policy, Events, DataSource) {
Events.record('view', 'page', 'admin/data_sources');

function DataSourcesCtrl(Policy, DataSource) {
this.policy = Policy;
this.dataSources = DataSource.query();
}
Expand Down
9 changes: 1 addition & 8 deletions client/app/pages/data-sources/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ const logger = debug('redash:http');

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

$scope.dataSource = $route.current.locals.dataSource;
$scope.dataSourceId = $routeParams.dataSourceId;
Expand Down Expand Up @@ -45,8 +44,6 @@ function DataSourceCtrl(

function deleteDataSource(callback) {
const doDelete = () => {
Events.record('delete', 'datasource', $scope.dataSource.id);

$scope.dataSource.$delete(() => {
toastr.success('Data source deleted successfully.');
$location.path('/data_sources/');
Expand All @@ -64,8 +61,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 @@ -81,8 +76,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
5 changes: 1 addition & 4 deletions client/app/pages/destinations/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ const logger = debug('redash:http');

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

$scope.destination = $route.current.locals.destination;
$scope.destinationId = $routeParams.destinationId;
Expand All @@ -34,8 +33,6 @@ function DestinationCtrl(

$scope.delete = () => {
const doDelete = () => {
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 { includes } from 'lodash';
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 { includes } from 'lodash';
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
4 changes: 1 addition & 3 deletions client/app/pages/queries-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class QueriesListCtrl {
this.pageSize = parseInt($location.search().page_size || 20, 10);
this.pageSizeOptions = [5, 10, 20, 50, 100];

if (isString(this.term) && this.term !== '') {
Events.record('search', 'query', '', { term: this.term });
} else {
if (!isString(this.term)) {
this.term = '';
}

Expand Down
3 changes: 0 additions & 3 deletions client/app/pages/queries/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ function QueryViewCtrl(
KeyboardShortcuts.unbind(shortcuts);
});

Events.record('view', 'query', $scope.query.id);
if ($scope.query.hasResult() || $scope.query.paramsRequired()) {
getQueryResult();
}
Expand Down Expand Up @@ -178,8 +177,6 @@ function QueryViewCtrl(
};

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

Query.fork({ id: $scope.query.id }, (newQuery) => {
$location.url(newQuery.getSourceLink()).replace();
});
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, AlertDialog, 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 @@ -3,9 +3,7 @@ import settingsMenu from '@/lib/settings-menu';
import { LivePaginator } from '@/lib/pagination';
import template from './list.html';

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

function UsersCtrl(currentUser, Policy, User) {
this.currentUser = currentUser;
if ($location.path() === '/users/disabled') {
this.currentPage = 'disabled_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 @@ -6,7 +6,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 @@ -16,7 +16,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
7 changes: 7 additions & 0 deletions redash/handlers/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ 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 serialize_alert(alert)
return alert.to_dict()

def post(self, alert_id):
req = request.get_json(True)
Expand Down
16 changes: 16 additions & 0 deletions redash/handlers/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -177,6 +183,11 @@ def post(self, dashboard_slug):
abort(409)

result = serialize_dashboard(dashboard, 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 @@ -194,6 +205,11 @@ def delete(self, dashboard_slug):
models.db.session.add(dashboard)
d = serialize_dashboard(dashboard, 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 877eaa5

Please sign in to comment.