Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Button to download admin server info #16059

Merged
merged 2 commits into from
Feb 4, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 16 additions & 34 deletions app/api/server/v1/stats.js
@@ -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
@@ -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
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
@@ -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
@@ -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
@@ -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';