Skip to content

Commit

Permalink
from Promise to async / await; from es2017 to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
rstiller committed Nov 3, 2018
1 parent b87dd43 commit 3d96cbd
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 188 deletions.
6 changes: 2 additions & 4 deletions lib/metrics/reporter/logger-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,16 @@ export class LoggerReporter extends ScheduledMetricReporter<LoggerReporterOption
* @param {Date} date
* @param {MetricType} type
* @param {Array<ReportingResult<any, LogLine>>} results
* @returns {Promise<void>}
* @memberof LoggerReporter
*/
protected handleResults(
protected async handleResults(
registry: MetricRegistry,
date: Date,
type: MetricType,
results: Array<ReportingResult<any, LogLine>>): Promise<void> {
results: Array<ReportingResult<any, LogLine>>) {
for (const logLine of results) {
this.options.log.info(logLine.result.message, logLine.result.metadata);
}
return Promise.resolve();
}

/**
Expand Down
36 changes: 15 additions & 21 deletions lib/metrics/reporter/metric-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ export abstract class MetricReporter<O extends MetricReporterOptions, T> {
* @protected
* @memberof MetricReporter
*/
protected beforeReport(): Promise<any> {
return Promise.resolve();
protected async beforeReport() {
}

/**
Expand All @@ -228,8 +227,7 @@ export abstract class MetricReporter<O extends MetricReporterOptions, T> {
* @protected
* @memberof MetricReporter
*/
protected afterReport(): Promise<any> {
return Promise.resolve();
protected async afterReport() {
}

/**
Expand All @@ -240,16 +238,14 @@ export abstract class MetricReporter<O extends MetricReporterOptions, T> {
* @protected
* @memberof MetricReporter
*/
protected report(): Promise<any> {
protected async report() {
if (this.metricRegistries && this.metricRegistries.length > 0) {
return Promise.resolve()
.then(() => this.beforeReport())
.then(() => Promise.all(
this.metricRegistries.map((registry) => this.reportMetricRegistry(registry)),
))
.then(() => this.afterReport());
await this.beforeReport();
for (const registry of this.metricRegistries) {
await this.reportMetricRegistry(registry);
}
await this.afterReport();
}
return Promise.resolve();
}

/**
Expand All @@ -263,7 +259,7 @@ export abstract class MetricReporter<O extends MetricReporterOptions, T> {
* @param {MetricRegistry} registry
* @memberof MetricReporter
*/
protected reportMetricRegistry(registry: MetricRegistry): Promise<any> {
protected async reportMetricRegistry(registry: MetricRegistry) {
const date: Date = new Date(this.options.clock.time().milliseconds);
const counterCtx: ReportingContext<MonotoneCounter | Counter> = this.createReportingContext(
registry, date, "counter");
Expand Down Expand Up @@ -302,14 +298,12 @@ export abstract class MetricReporter<O extends MetricReporterOptions, T> {
(timer: Timer) => this.reportTimer(timer, timerCtx),
(timer: Timer) => timer.getCount());

return Promise.all([
this.handleResults(registry, date, "counter", monotoneCounterResults),
this.handleResults(registry, date, "counter", counterResults),
this.handleResults(registry, date, "gauge", gaugeResults),
this.handleResults(registry, date, "histogram", histogramResults),
this.handleResults(registry, date, "meter", meterResults),
this.handleResults(registry, date, "timer", timerResults),
]);
await this.handleResults(registry, date, "counter", monotoneCounterResults);
await this.handleResults(registry, date, "counter", counterResults);
await this.handleResults(registry, date, "gauge", gaugeResults);
await this.handleResults(registry, date, "histogram", histogramResults);
await this.handleResults(registry, date, "meter", meterResults);
await this.handleResults(registry, date, "timer", timerResults);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions lib/metrics/reporter/scheduled-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ export abstract class ScheduledMetricReporter<O extends ScheduledMetricReporterO
* Uses the scheduler function to call the {@link #report} function
* in the interval specified. The interval is converted into {@link MILLISECOND}s.
*
* @returns {Promise<void>}
* @memberof ScheduledMetricReporter
*/
public start(): Promise<void> {
public async start() {
const interval: number = this.options.unit.convertTo(this.options.reportInterval, MILLISECOND);
this.timer = this.options.scheduler(() => this.report(), interval);
return Promise.resolve();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "inspector-metrics",
"description": "monitoring / metric library similar to http://metrics.dropwizard.io",
"homepage": "https://rstiller.github.io/inspector-metrics/",
"version": "1.13.0-rc.2",
"version": "1.13.0-rc.3",
"main": "./build/lib/metrics/index.js",
"typings": "./build/lib/metrics/index.d.ts",
"bugs": {
Expand All @@ -25,7 +25,7 @@
"typescript"
],
"engines": {
"node": ">= 7",
"node": ">= 6",
"npm": ">= 3"
},
"devDependencies": {
Expand All @@ -46,7 +46,7 @@
"sinon-chai": "^3.2.0",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "3.1.5"
"typescript": "3.1.6"
},
"optionalDependencies": {
"native-hdr-histogram": "^0.5.0"
Expand Down
Loading

0 comments on commit 3d96cbd

Please sign in to comment.