Skip to content

Commit

Permalink
clean up Lens usage collector
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Jan 25, 2021
1 parent 2ff5235 commit 77a86a1
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions x-pack/plugins/lens/server/usage/collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export function registerLensUsageCollector(
usageCollection: UsageCollectionSetup,
taskManager: Promise<TaskManagerStartContract>
) {
let isCollectorReady = false;
taskManager.then(() => {
// mark lensUsageCollector as ready to collect when the TaskManager is ready
isCollectorReady = true;
});
const lensUsageCollector = usageCollection.makeUsageCollector<LensUsage>({
type: 'lens',
async fetch() {
Expand Down Expand Up @@ -55,7 +50,10 @@ export function registerLensUsageCollector(
};
}
},
isReady: () => isCollectorReady,
isReady: async () => {
await taskManager;
return true;
},
schema: lensUsageSchema,
});

Expand All @@ -69,24 +67,10 @@ function addEvents(prevEvents: Record<string, number>, newEvents: Record<string,
}

async function getLatestTaskState(taskManager: TaskManagerStartContract) {
try {
const result = await taskManager.fetch({
query: { bool: { filter: { term: { _id: `task:Lens-lens_telemetry` } } } },
});
return result.docs;
} catch (err) {
const errMessage = err && err.message ? err.message : err.toString();
/*
The usage service WILL to try to fetch from this collector before the task manager has been initialized, because the
task manager has to wait for all plugins to initialize first. It's fine to ignore it as next time around it will be
initialized (or it will throw a different type of error)
*/
if (!errMessage.includes('NotInitialized')) {
throw err;
}
}

return null;
const result = await taskManager.fetch({
query: { bool: { filter: { term: { _id: `task:Lens-lens_telemetry` } } } },
});
return result.docs;
}

function getDataByDate(dates: Record<string, Record<string, number>>) {
Expand Down

0 comments on commit 77a86a1

Please sign in to comment.