Skip to content

Commit

Permalink
feat: Metrics - WorkClaim Counts (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwicks31 authored Oct 23, 2018
1 parent a3b0dcb commit 47b99f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/API/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class Router {
this.koaRouter.get('/works', RequestValidationMiddleware(getWorksSchema), this.getWorks)
this.koaRouter.post('/works', this.postWork)
this.koaRouter.get('/health', this.getHealth)
this.koaRouter.get('/metrics', this.getWorkCounts)

this.koa.use(helmet(SecurityHeaders))
this.koa.use(KoaCors({ expose: ['X-Total-Count'] }))
Expand Down Expand Up @@ -113,6 +114,14 @@ export class Router {
context.body = works
}

private getWorkCounts = async (context: KoaRouter.IRouterContext, next: () => Promise<any>) => {
this.logger.trace('/metrics')
const TotalWorkClaims = await this.workController.getWorksCountByFilters({
...context.query,
})
context.body = { TotalWorkClaims }
}

private postWork = async (context: KoaRouter.IRouterContext, next: () => Promise<any>) => {
const { body } = context.request

Expand Down
6 changes: 6 additions & 0 deletions src/API/WorkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export class WorkController {
return { count, works }
}

async getWorksCountByFilters(worksFilters: WorksFilters = {}): Promise<number> {
this.logger.trace({ method: 'getWorksCountByFilters', worksFilters }, 'Getting Work Counts by Filters from DB')
const count = await this.collection.find(worksFilters, { projection: { _id: false } }).count()
return count
}

async create(work: Work): Promise<void> {
this.logger.trace({ method: 'create', work }, 'Creating Work')
// TODO: verify id, publicKey, signature and createdDate
Expand Down

0 comments on commit 47b99f2

Please sign in to comment.