-
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
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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,10 @@ | ||
import MetricService from '../../../src/shared/metric_service.js'; | ||
|
||
describe('Metric Service', () => { | ||
it('should throw if trying to emit an non-Metric', async () => { | ||
await expect(async () => { | ||
await MetricService.emit('my-made-up-metric'); | ||
}).rejects.toThrow(/Invalid metric/); | ||
//TODO https://github.com/lfilho/ddg-test-project/issues/46 | ||
}); | ||
}); |
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,51 @@ | ||
import Metric from './model/metric.js'; | ||
import fetch from './util/fake_fetch.js'; | ||
|
||
export default class MetricService { | ||
static async emit(metric) { | ||
if (!(metric instanceof Metric)) { | ||
throw new Error('Invalid metric.'); | ||
} | ||
|
||
const metricPayload = getMetricPayload(metric); | ||
const metricJson = getMetricJson(metricPayload); | ||
const metricString = getMetricString(metricPayload); | ||
|
||
// Here we would make a `fetch` request to our metric service | ||
// For now, let's just log mimic an async request | ||
return fetch(metricJson) | ||
.then((response) => { | ||
console.log(`Metric sent! Server returned status: ${response.status}`); | ||
console.info(metricString); | ||
}) | ||
.catch((error) => { | ||
console.error( | ||
'Error while sending metric. Did you pay your internet bill?', | ||
error | ||
); | ||
}); | ||
} | ||
} | ||
|
||
// when are private fields and methods coming? :-( | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields | ||
|
||
function getMetricPayload(metric) { | ||
const timestamp = new Date().toISOString(); | ||
|
||
const payload = { | ||
timestamp, | ||
dimension: metric.dimension, | ||
value: metric.value, | ||
}; | ||
|
||
return payload; | ||
} | ||
|
||
function getMetricJson(payload) { | ||
return JSON.stringify(payload); | ||
} | ||
|
||
function getMetricString(payload) { | ||
return `INFO ${payload.timestamp} ${payload.dimension}=${payload.value}`; | ||
} |
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,5 @@ | ||
// A simple `fetch` mock so we can unit test via node without extra dependencies | ||
|
||
export default async function fetch() { | ||
return Promise.resolve({ status: 200 }); | ||
} |