-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fa313e
commit 9eecc95
Showing
10 changed files
with
218 additions
and
12 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/server/common/controller/generic-page-controller/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { | ||
createMetricsLogger, | ||
Unit, | ||
StorageResolution | ||
} from 'aws-embedded-metrics' | ||
import { config } from '~/src/config/config.js' | ||
import { createLogger } from '../../helpers/logging/logger.js' | ||
|
||
/** | ||
* @import {Page} from '../../model/page/page-model.js' | ||
*/ | ||
|
||
/** | ||
* export @interface PageController { | ||
* handleGet: (req: Request, h: ResponseToolkit) => ResponseObject | ||
* } | ||
* | ||
* export @typedef {{ | ||
* request ?: boolean; | ||
* response ?: boolean; | ||
* }} Metrics | ||
* | ||
* export @typedef {{ | ||
* get ?: Metrics; | ||
* post ?: Metrics; | ||
* }} MetricReports | ||
*/ | ||
|
||
export default class GenericPageController { | ||
metrics = createMetricsLogger() | ||
logger = createLogger() | ||
|
||
/** | ||
* @param {Page} page | ||
*/ | ||
constructor(page) { | ||
this.page = page | ||
} | ||
|
||
getHandler(req, h) { | ||
let served = true | ||
|
||
try { | ||
this.sendMetric('get', 'request') | ||
|
||
return this.handleGet(req, h) | ||
} catch (e) { | ||
this.logger.error(e) | ||
served = false | ||
} finally { | ||
if (served) { | ||
this.sendMetric('get', 'response') | ||
} | ||
} | ||
} | ||
|
||
postHandler(req, h) { | ||
let served = true | ||
|
||
try { | ||
this.sendMetric('post', 'request') | ||
|
||
return this.handlePost(req, h) | ||
} catch (e) { | ||
this.logger.error(e) | ||
served = false | ||
} finally { | ||
if (served) { | ||
this.sendMetric('post', 'response') | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} method | ||
* @param {string} event | ||
*/ | ||
sendMetric(method, event) { | ||
if (!config.get('isProduction')) return | ||
|
||
const sendMetric = this.page.reportMetrics[method]?.[event] | ||
|
||
if (sendMetric) { | ||
this.metrics.putMetric( | ||
`${method}::${event}-${this.page.urlPath}`, | ||
1, | ||
Unit.Count, | ||
StorageResolution.Standard | ||
) | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
handleGet(req, h) { | ||
throw new Error('Method not implemented') | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
handlePost(req, h) { | ||
throw new Error('Method not implemented') | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/server/common/controller/generic-page-controller/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { config } from '~/src/config/config.js' | ||
import { Page } from '../../model/page/page-model.js' | ||
import GenericPageController from './index.js' | ||
|
||
class TestPage extends Page { | ||
reportMetrics = { | ||
get: { | ||
request: false, | ||
response: true | ||
} | ||
} | ||
} | ||
|
||
class TestGenericController extends GenericPageController { | ||
handleGet() { | ||
return 'get' | ||
} | ||
|
||
handlePost() { | ||
return 'post' | ||
} | ||
} | ||
|
||
describe('#GenricPageController', () => { | ||
let controller | ||
|
||
beforeEach(() => { | ||
config.set('isProduction', true) | ||
}) | ||
|
||
beforeEach(() => { | ||
const page = new TestPage() | ||
controller = new TestGenericController(page) | ||
}) | ||
|
||
it('should output error on getHandler', () => { | ||
jest.spyOn(controller, 'handleGet').mockImplementation(() => { | ||
throw new Error('test error') | ||
}) | ||
|
||
jest.spyOn(controller.logger, 'error').mockImplementation(() => { | ||
throw new Error('test error') | ||
}) | ||
expect(() => controller.getHandler()).toThrow() | ||
}) | ||
|
||
it('should output error on postHandler', () => { | ||
jest.spyOn(controller, 'handlePost').mockImplementation(() => { | ||
throw new Error('test error') | ||
}) | ||
|
||
jest.spyOn(controller.logger, 'error').mockImplementation(() => { | ||
throw new Error('test error') | ||
}) | ||
expect(() => controller.postHandler()).toThrow() | ||
}) | ||
|
||
it('should send metric on getHandler', () => { | ||
jest.spyOn(controller, 'sendMetric') | ||
jest.spyOn(controller, 'handleGet').mockImplementation(() => { | ||
return 'get success' | ||
}) | ||
const metricSpy = jest.spyOn(controller.metrics, 'putMetric') | ||
controller.getHandler() | ||
expect(controller.sendMetric).toHaveBeenCalledWith('get', 'response') | ||
expect(metricSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
config.set('isProduction', false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters