From 8239c68cf323e4bb20007d2b456336becead273d Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Thu, 25 Feb 2016 12:32:46 -0500 Subject: Refactor and modularize analytics on the client --- web/react/utils/async_client.jsx | 270 ++++++++++++++++++++++++++++++++++++++- web/react/utils/client.jsx | 23 ++-- web/react/utils/constants.jsx | 20 +++ 3 files changed, 294 insertions(+), 19 deletions(-) (limited to 'web/react/utils') diff --git a/web/react/utils/async_client.jsx b/web/react/utils/async_client.jsx index ca9d81865..7d5e1bd0f 100644 --- a/web/react/utils/async_client.jsx +++ b/web/react/utils/async_client.jsx @@ -11,16 +11,17 @@ import UserStore from '../stores/user_store.jsx'; import * as utils from './utils.jsx'; import Constants from './constants.jsx'; -var ActionTypes = Constants.ActionTypes; +const ActionTypes = Constants.ActionTypes; +const StatTypes = Constants.StatTypes; // Used to track in progress async calls -var callTracker = {}; +const callTracker = {}; export function dispatchError(err, method) { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_ERROR, - err: err, - method: method + err, + method }); } @@ -848,3 +849,264 @@ export function getFileInfo(filename) { } ); } + +export function getStandardAnalytics(teamId) { + const callName = 'getStandardAnaytics' + teamId; + + if (isCallInProgress(callName)) { + return; + } + + callTracker[callName] = utils.getTimestamp(); + + client.getAnalytics( + 'standard', + teamId, + (data) => { + callTracker[callName] = 0; + + const stats = {}; + + for (const index in data) { + if (data[index].name === 'channel_open_count') { + stats[StatTypes.TOTAL_PUBLIC_CHANNELS] = data[index].value; + } + + if (data[index].name === 'channel_private_count') { + stats[StatTypes.TOTAL_PRIVATE_GROUPS] = data[index].value; + } + + if (data[index].name === 'post_count') { + stats[StatTypes.TOTAL_POSTS] = data[index].value; + } + + if (data[index].name === 'unique_user_count') { + stats[StatTypes.TOTAL_USERS] = data[index].value; + } + + if (data[index].name === 'team_count' && teamId == null) { + stats[StatTypes.TOTAL_TEAMS] = data[index].value; + } + } + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_ANALYTICS, + teamId, + stats + }); + }, + (err) => { + callTracker[callName] = 0; + + dispatchError(err, 'getStandardAnalytics'); + } + ); +} + +export function getAdvancedAnalytics(teamId) { + const callName = 'getAdvancedAnalytics' + teamId; + + if (isCallInProgress(callName)) { + return; + } + + callTracker[callName] = utils.getTimestamp(); + + client.getAnalytics( + 'extra_counts', + teamId, + (data) => { + callTracker[callName] = 0; + + const stats = {}; + + for (const index in data) { + if (data[index].name === 'file_post_count') { + stats[StatTypes.TOTAL_FILE_POSTS] = data[index].value; + } + + if (data[index].name === 'hashtag_post_count') { + stats[StatTypes.TOTAL_HASHTAG_POSTS] = data[index].value; + } + + if (data[index].name === 'incoming_webhook_count') { + stats[StatTypes.TOTAL_IHOOKS] = data[index].value; + } + + if (data[index].name === 'outgoing_webhook_count') { + stats[StatTypes.TOTAL_OHOOKS] = data[index].value; + } + + if (data[index].name === 'command_count') { + stats[StatTypes.TOTAL_COMMANDS] = data[index].value; + } + + if (data[index].name === 'session_count') { + stats[StatTypes.TOTAL_SESSIONS] = data[index].value; + } + } + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_ANALYTICS, + teamId, + stats + }); + }, + (err) => { + callTracker[callName] = 0; + + dispatchError(err, 'getAdvancedAnalytics'); + } + ); +} + +export function getPostsPerDayAnalytics(teamId) { + const callName = 'getPostsPerDayAnalytics' + teamId; + + if (isCallInProgress(callName)) { + return; + } + + callTracker[callName] = utils.getTimestamp(); + + client.getAnalytics( + 'post_counts_day', + teamId, + (data) => { + callTracker[callName] = 0; + + data.reverse(); + + const stats = {}; + stats[StatTypes.POST_PER_DAY] = data; + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_ANALYTICS, + teamId, + stats + }); + }, + (err) => { + callTracker[callName] = 0; + + dispatchError(err, 'getPostsPerDayAnalytics'); + } + ); +} + +export function getUsersPerDayAnalytics(teamId) { + const callName = 'getUsersPerDayAnalytics' + teamId; + + if (isCallInProgress(callName)) { + return; + } + + callTracker[callName] = utils.getTimestamp(); + + client.getAnalytics( + 'user_counts_with_posts_day', + teamId, + (data) => { + callTracker[callName] = 0; + + data.reverse(); + + const stats = {}; + stats[StatTypes.USERS_WITH_POSTS_PER_DAY] = data; + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_ANALYTICS, + teamId, + stats + }); + }, + (err) => { + callTracker[callName] = 0; + + dispatchError(err, 'getUsersPerDayAnalytics'); + } + ); +} + +export function getRecentAndNewUsersAnalytics(teamId) { + const callName = 'getRecentAndNewUsersAnalytics' + teamId; + + if (isCallInProgress(callName)) { + return; + } + + callTracker[callName] = utils.getTimestamp(); + + client.getProfilesForTeam( + teamId, + (users) => { + const stats = {}; + + const usersList = []; + for (const id in users) { + if (users.hasOwnProperty(id)) { + usersList.push(users[id]); + } + } + + usersList.sort((a, b) => { + if (a.last_activity_at < b.last_activity_at) { + return 1; + } + + if (a.last_activity_at > b.last_activity_at) { + return -1; + } + + return 0; + }); + + const recentActive = []; + for (let i = 0; i < usersList.length; i++) { + if (usersList[i].last_activity_at == null) { + continue; + } + + recentActive.push(usersList[i]); + if (i >= Constants.STAT_MAX_ACTIVE_USERS) { + break; + } + } + + stats[StatTypes.RECENTLY_ACTIVE_USERS] = recentActive; + + usersList.sort((a, b) => { + if (a.create_at < b.create_at) { + return 1; + } + + if (a.create_at > b.create_at) { + return -1; + } + + return 0; + }); + + var newlyCreated = []; + for (let i = 0; i < usersList.length; i++) { + newlyCreated.push(usersList[i]); + if (i >= Constants.STAT_MAX_NEW_USERS) { + break; + } + } + + stats[StatTypes.NEWLY_CREATED_USERS] = newlyCreated; + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_ANALYTICS, + teamId, + stats + }); + }, + (err) => { + callTracker[callName] = 0; + + dispatchError(err, 'getRecentAndNewUsersAnalytics'); + } + ); +} diff --git a/web/react/utils/client.jsx b/web/react/utils/client.jsx index f647e2296..1a002bc8c 100644 --- a/web/react/utils/client.jsx +++ b/web/react/utils/client.jsx @@ -435,23 +435,16 @@ export function getConfig(success, error) { }); } -export function getTeamAnalytics(teamId, name, success, error) { - $.ajax({ - url: '/api/v1/admin/analytics/' + teamId + '/' + name, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('getTeamAnalytics', xhr, status, err); - error(e); - } - }); -} +export function getAnalytics(name, teamId, success, error) { + let url = '/api/v1/admin/analytics/'; + if (teamId == null) { + url += name; + } else { + url += teamId + '/' + name; + } -export function getSystemAnalytics(name, success, error) { $.ajax({ - url: '/api/v1/admin/analytics/' + name, + url, dataType: 'json', contentType: 'application/json', type: 'GET', diff --git a/web/react/utils/constants.jsx b/web/react/utils/constants.jsx index 0a4944708..88f01a475 100644 --- a/web/react/utils/constants.jsx +++ b/web/react/utils/constants.jsx @@ -71,6 +71,26 @@ export default { VIEW_ACTION: null }), + StatTypes: keyMirror({ + TOTAL_USERS: null, + TOTAL_PUBLIC_CHANNELS: null, + TOTAL_PRIVATE_GROUPS: null, + TOTAL_POSTS: null, + TOTAL_TEAMS: null, + TOTAL_FILE_POSTS: null, + TOTAL_HASHTAG_POSTS: null, + TOTAL_IHOOKS: null, + TOTAL_OHOOKS: null, + TOTAL_COMMANDS: null, + TOTAL_SESSIONS: null, + POST_PER_DAY: null, + USERS_WITH_POSTS_PER_DAY: null, + RECENTLY_ACTIVE_USERS: null, + NEWLY_CREATED_USERS: null + }), + STAT_MAX_ACTIVE_USERS: 20, + STAT_MAX_NEW_USERS: 20, + SocketEvents: { POSTED: 'posted', POST_EDITED: 'post_edited', -- cgit v1.2.3-1-g7c22