-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathtime.ts
More file actions
38 lines (35 loc) · 1.25 KB
/
time.ts
File metadata and controls
38 lines (35 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Filter for statistics.
* Record used time for each request.
*/
import { getLogger } from 'pinus-logger';
let conLogger = getLogger('con-log', __filename);
import * as utils from '../../util/utils';
import { IHandlerFilter } from '../../interfaces/IHandlerFilter';
import { RouteRecord } from '../../util/constants';
import { HandlerCallback } from '../../common/service/handlerService';
import { FrontendOrBackendSession } from '../../server/server';
export class TimeFilter implements IHandlerFilter
{
before(routeRecord : RouteRecord , msg : any, session : FrontendOrBackendSession, next : HandlerCallback)
{
(session as any).__startTime__ = Date.now();
next(null);
};
after(err : Error, routeRecord : RouteRecord , msg : any, session : FrontendOrBackendSession, resp : any, next : HandlerCallback)
{
let start = (session as any).__startTime__;
if (typeof start === 'number')
{
let timeUsed = Date.now() - start;
let log = {
route: routeRecord.route,
args: msg,
time: utils.format(new Date(start)),
timeUsed: timeUsed
};
conLogger.info(JSON.stringify(log));
}
next(err);
};
}