Skip to content

Commit

Permalink
move events serverside
Browse files Browse the repository at this point in the history
addresses getredash#1501
  • Loading branch information
alison985 committed Jul 1, 2018
1 parent 5846a57 commit f31e346
Show file tree
Hide file tree
Showing 29 changed files with 173 additions and 56 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
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.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 @@ -177,7 +177,6 @@ function DashboardCtrl(
{ slug: $routeParams.dashboardSlug },
(dashboard) => {
this.dashboard = dashboard;
Events.record('view', 'dashboard', dashboard.id);
renderDashboard(dashboard, force);

if ($location.search().edit === true) {
Expand Down Expand Up @@ -227,7 +226,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(Policy, Events, DataSource) {
Events.record('view', 'page', 'admin/data_sources');

function DataSourcesCtrl(Policy, DataSource) {
this.policy = Policy;
this.dataSources = DataSource.query();
}
Expand Down
8 changes: 1 addition & 7 deletions client/app/pages/data-sources/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +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;
$scope.types = $route.current.locals.types;
Expand Down Expand Up @@ -45,8 +43,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 +60,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 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, $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;
$scope.types = $route.current.locals.types;
Expand All @@ -34,8 +32,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/queries-search-results-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isString } from 'lodash';
import { Paginator } from '@/lib/pagination';
import template from './queries-search-results-page.html';

function QuerySearchCtrl($location, $filter, currentUser, Events, Query) {
function QuerySearchCtrl($location, $filter, currentUser, Query) {
this.term = $location.search().q;
this.paginator = new Paginator([], { itemsPerPage: 20 });

Expand Down 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
5 changes: 0 additions & 5 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 Expand Up @@ -326,8 +323,6 @@ function QueryViewCtrl(
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
3 changes: 1 addition & 2 deletions client/app/pages/query-snippets/edit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'brace/mode/snippets';
import template from './edit.html';

function SnippetCtrl($routeParams, $http, $location, toastr, currentUser, AlertDialog, Events, QuerySnippet) {
function SnippetCtrl($routeParams, $http, $location, toastr, currentUser, AlertDialog, 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 { Paginator } from '@/lib/pagination';
import template from './list.html';

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

function UsersCtrl(currentUser, Policy, User) {
this.currentUser = currentUser;
this.policy = Policy;
this.users = new Paginator([], { itemsPerPage: 20 });
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
15 changes: 15 additions & 0 deletions redash/handlers/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import time

from flask import request
from flask_login import login_required
Expand All @@ -7,6 +8,7 @@
from redash.handlers.base import json_response
from redash.permissions import require_super_admin
from redash.tasks.queries import QueryTaskTracker
from redash.tasks import record_event


@routes.route('/api/admin/queries/outdated', methods=['GET'])
Expand All @@ -23,6 +25,13 @@ def outdated_queries():
else:
outdated_queries = []

record_event({
'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 @@ -33,6 +42,12 @@ def outdated_queries():
@require_super_admin
@login_required
def queries_tasks():
record_event({
'action': 'view',
'object_type': 'api_call',
'object_id': 'admin/tasks',
'timestamp': int(time.time()),
})
global_limit = int(request.args.get('limit', 50))
waiting_limit = int(request.args.get('waiting_limit', global_limit))
progress_limit = int(request.args.get('progress_limit', global_limit))
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 @@ -108,6 +108,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 @@ -147,6 +153,11 @@ def post(self, dashboard_slug):
abort(409)

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 @@ -164,6 +175,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 f31e346

Please sign in to comment.