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

Stats API #11992

Merged
merged 4 commits into from
Jun 12, 2017
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
4 changes: 4 additions & 0 deletions src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import optimizeMixin from '../optimize';
import pluginsInitializeMixin from './plugins/initialize';
import { indexPatternsMixin } from './index_patterns';
import { savedObjectsMixin } from './saved_objects';
import { statsMixin } from './stats';

const rootDir = fromRoot('.');

Expand All @@ -40,6 +41,9 @@ export default class KbnServer {
warningsMixin,
statusMixin,

// set up stats route
statsMixin,

// writes pid file
pidMixin,

Expand Down
1 change: 1 addition & 0 deletions src/server/stats/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { statsMixin } from './stats_mixin';
21 changes: 21 additions & 0 deletions src/server/stats/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SavedObjectsClient } from '../saved_objects/client';

async function getStatsForType(savedObjectsClient, type) {
const { total } = await savedObjectsClient.find({ type, perPage: 0 });
return { total };
}

export async function getStats(kibanaIndex, callAdminCluster) {
const savedObjectsClient = new SavedObjectsClient(kibanaIndex, callAdminCluster);
const types = ['dashboard', 'visualization', 'search', 'index-pattern'];
const requests = types.map(type => getStatsForType(savedObjectsClient, type));
const results = await Promise.all(requests);
const stats = {};

results.forEach((statsForType, index) => {
stats[types[index]] = statsForType;
});

stats.index = kibanaIndex;
return stats;
}
19 changes: 19 additions & 0 deletions src/server/stats/stats_mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getStats } from './stats';

export function statsMixin(kbnServer, server) {
server.route({
method: 'GET',
path: '/api/stats',
handler: function (request, reply) {
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
const callAdminCluster = (...args) => callWithInternalUser(...args);

const stats = getStats(
server.config().get('kibana.index'),
callAdminCluster
);

return reply(stats);
Copy link
Contributor

@tylersmalley tylersmalley Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reply with the promise instead of awaiting it. Just a nitpick, doesn't need to be changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then the handler doesn't need to be marked as async.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat

Copy link
Member

@pickypg pickypg Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stacey-gammon

return Promise.resolve(getStats(server.config().get('kibana.index'), callAdminCluster));

Unless not modifying the return actually works? I would be surprised if that were the case, but JS has done crazier things.

Copy link
Contributor Author

@stacey-gammon stacey-gammon Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pickypg, It worked when I tested it, but using the above doesn't seem to work (I don't see an error, it just hangs). Do I need to wrap your return with the reply?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async / await is all just sugar on top of Promise, so the method is already returning a Promise that you choose to await against to get the value (or throw the exception). So I'm guessing if it worked, then you simply can stick with what you've got and can avoid my extra fluff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh doh! Yes, my example was missing reply wrapping the Promise.

Copy link
Contributor

@tylersmalley tylersmalley Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need Promise.resolve since getStats returns a promise. Just need to return the getStats call.

}
});
}