Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 16 additions & 34 deletions app/api/server/v1/stats.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,30 @@
import { Meteor } from 'meteor/meteor';

import { hasPermission } from '../../../authorization';
import { Statistics } from '../../../models';
import { API } from '../api';
import { getStatistics, getLastStatistics } from '../../../statistics/server';

API.v1.addRoute('statistics', { authRequired: true }, {
get() {
let refresh = false;
if (typeof this.queryParams.refresh !== 'undefined' && this.queryParams.refresh === 'true') {
refresh = true;
}

let stats;
Meteor.runAsUser(this.userId, () => {
stats = Meteor.call('getStatistics', refresh);
});

return API.v1.success({
statistics: stats,
});
const { refresh } = this.requestParams();
return API.v1.success(Promise.await(getLastStatistics({
userId: this.userId,
refresh: refresh && refresh === 'true',
})));
},
});

API.v1.addRoute('statistics.list', { authRequired: true }, {
get() {
if (!hasPermission(this.userId, 'view-statistics')) {
return API.v1.unauthorized();
}

const { offset, count } = this.getPaginationItems();
const { sort, fields, query } = this.parseJsonQuery();

const statistics = Statistics.find(query, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
fields,
}).fetch();

return API.v1.success({
statistics,
count: statistics.length,
offset,
total: Statistics.find(query).count(),
});
return API.v1.success(Promise.await(getStatistics({
userId: this.userId,
query,
pagination: {
offset,
count,
sort,
fields,
},
})));
},
});
14 changes: 14 additions & 0 deletions app/models/server/raw/Statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BaseRaw } from './BaseRaw';

export class StatisticsRaw extends BaseRaw {
async findLast() {
const options = {
sort: {
createdAt: -1,
},
limit: 1,
};
const records = await this.find({}, options).toArray();
return records && records[0];
}
}
3 changes: 3 additions & 0 deletions app/models/server/raw/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import CustomUserStatusModel from '../models/CustomUserStatus';
import { CustomUserStatusRaw } from './CustomUserStatus';
import LivechatAgentActivityModel from '../models/LivechatAgentActivity';
import { LivechatAgentActivityRaw } from './LivechatAgentActivity';
import StatisticsModel from '../models/Statistics';
import { StatisticsRaw } from './Statistics';

export const Permissions = new PermissionsRaw(PermissionsModel.model.rawCollection());
export const Roles = new RolesRaw(RolesModel.model.rawCollection());
Expand All @@ -68,3 +70,4 @@ export const OAuthApps = new OAuthAppsRaw(OAuthAppsModel.model.rawCollection());
export const CustomSounds = new CustomSoundsRaw(CustomSoundsModel.model.rawCollection());
export const CustomUserStatus = new CustomUserStatusRaw(CustomUserStatusModel.model.rawCollection());
export const LivechatAgentActivity = new LivechatAgentActivityRaw(LivechatAgentActivityModel.model.rawCollection());
export const Statistics = new StatisticsRaw(StatisticsModel.model.rawCollection());
165 changes: 0 additions & 165 deletions app/statistics/server/functions/get.js

This file was deleted.

14 changes: 14 additions & 0 deletions app/statistics/server/functions/getLastStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { statistics } from '../lib/statistics';
import { Statistics } from '../../../models/server/raw';

export async function getLastStatistics({ userId, refresh }) {
if (!await hasPermissionAsync(userId, 'view-statistics')) {
throw new Error('error-not-allowed');
}

if (refresh) {
return statistics.save();
}
return Statistics.findLast();
}
26 changes: 26 additions & 0 deletions app/statistics/server/functions/getStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { Statistics } from '../../../models/server/raw';

export async function getStatistics({ userId, query = {}, pagination: { offset, count, sort, fields } }) {
if (!await hasPermissionAsync(userId, 'view-statistics')) {
throw new Error('error-not-allowed');
}

const cursor = Statistics.find(query, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
fields,
});

const total = await cursor.count();

const statistics = await cursor.toArray();

return {
statistics,
count: statistics.length,
offset,
total,
};
}
9 changes: 0 additions & 9 deletions app/statistics/server/functions/save.js

This file was deleted.

6 changes: 3 additions & 3 deletions app/statistics/server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './functions/get';
import './functions/save';
import './methods/getStatistics';
import './startup/monitor';

export { statistics } from './statisticsNamespace';
export { statistics } from './lib/statistics';
export { getLastStatistics } from './functions/getLastStatistics';
export { getStatistics } from './functions/getStatistics';
Loading