Skip to content

Commit

Permalink
Refactored CloudWatch dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
nicka committed Aug 2, 2017
1 parent 4feab41 commit fa2f426
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 125 deletions.
44 changes: 44 additions & 0 deletions cloudformation/__snapshots__/dashboard.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Dashboard returns cloudformation dashboard body 1`] = `
Object {
"Fn::Join": Array [
"",
Array [
"{\\"widgets\\":[{\\"type\\":\\"metric\\",\\"x\\":0,\\"y\\":1,\\"width\\":6,\\"height\\":3,\\"properties\\":{\\"view\\":\\"singleValue\\",\\"metrics\\":[[\\"\${self:custom.cloudWatchNamespace}\\",\\"BuyPrice\\",\\"CryptoCurrency\\",\\"\${self:provider.environment.PREFERRED_CRYPTO_CURRENCY}\\",\\"Stage\\",\\"\${self:custom.stage}\\",\\"LocalCurrency\\",\\"\${self:provider.environment.PREFERRED_LOCAL_CURRENCY}\\",{\\"label\\":\\"Buy\\"}],[\\".\\",\\"SellPrice\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",{\\"label\\":\\"Sell\\"}]],\\"region\\":\\"\${self:provider.region}\\",\\"title\\":\\"Current price in \${self:provider.environment.PREFERRED_LOCAL_CURRENCY}\\"}},{\\"type\\":\\"metric\\",\\"x\\":0,\\"y\\":4,\\"width\\":24,\\"height\\":9,\\"properties\\":{\\"view\\":\\"timeSeries\\",\\"stacked\\":false,\\"metrics\\":[[\\"\${self:custom.cloudWatchNamespace}\\",\\"BuyPrice\\",\\"CryptoCurrency\\",\\"\${self:provider.environment.PREFERRED_CRYPTO_CURRENCY}\\",\\"Stage\\",\\"\${self:custom.stage}\\",\\"LocalCurrency\\",\\"\${self:provider.environment.PREFERRED_LOCAL_CURRENCY}\\",{\\"color\\":\\"#aec7e8\\",\\"label\\":\\"Buy actual\\"}],[\\".\\",\\"SellPrice\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",{\\"color\\":\\"#ffbb78\\",\\"label\\":\\"Sell actual\\"}],[\\".\\",\\"BuyPrice\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",{\\"period\\":3600,\\"color\\":\\"#1f77b4\\",\\"label\\":\\"Buy hourly average\\"}],[\\".\\",\\"SellPrice\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",{\\"period\\":3600,\\"color\\":\\"#ff7f0e\\",\\"label\\":\\"Sell hourly average\\"}],[\\".\\",\\"BuyPrice\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",{\\"period\\":86400,\\"label\\":\\"Buy daily average\\",\\"color\\":\\"#ff9896\\"}],[\\".\\",\\"SellPrice\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",\\".\\",{\\"period\\":86400,\\"label\\":\\"Sell daily average\\",\\"color\\":\\"#98df8a\\"}]],\\"region\\":\\"\${self:provider.region}\\",\\"title\\":\\"Buy / Sell \${self:provider.environment.PREFERRED_CRYPTO_CURRENCY}-\${self:provider.environment.PREFERRED_LOCAL_CURRENCY}\\"}},{\\"type\\":\\"metric\\",\\"x\\":0,\\"y\\":13,\\"width\\":6,\\"height\\":6,\\"properties\\":{\\"title\\":\\"Low Buy Price\\",\\"annotations\\":{\\"alarms\\":[\\"arn:aws:cloudwatch:\${self:provider.region}:",
Object {
"Ref": "AWS::AccountId",
},
":alarm:",
Object {
"Ref": "LowBuyPriceAlarm",
},
"\\"]},\\"view\\":\\"timeSeries\\",\\"stacked\\":false}},{\\"type\\":\\"metric\\",\\"x\\":6,\\"y\\":13,\\"width\\":6,\\"height\\":6,\\"properties\\":{\\"title\\":\\"Low Sell Price\\",\\"annotations\\":{\\"alarms\\":[\\"arn:aws:cloudwatch:\${self:provider.region}:",
Object {
"Ref": "AWS::AccountId",
},
":alarm:",
Object {
"Ref": "LowSellPriceAlarm",
},
"\\"]},\\"view\\":\\"timeSeries\\",\\"stacked\\":false}},{\\"type\\":\\"metric\\",\\"x\\":12,\\"y\\":13,\\"width\\":6,\\"height\\":6,\\"properties\\":{\\"title\\":\\"High Buy Price\\",\\"annotations\\":{\\"alarms\\":[\\"arn:aws:cloudwatch:\${self:provider.region}:",
Object {
"Ref": "AWS::AccountId",
},
":alarm:",
Object {
"Ref": "HighBuyPriceAlarm",
},
"\\"]},\\"view\\":\\"timeSeries\\",\\"stacked\\":false}},{\\"type\\":\\"metric\\",\\"x\\":18,\\"y\\":13,\\"width\\":6,\\"height\\":6,\\"properties\\":{\\"title\\":\\"High Sell Price\\",\\"annotations\\":{\\"alarms\\":[\\"arn:aws:cloudwatch:\${self:provider.region}:",
Object {
"Ref": "AWS::AccountId",
},
":alarm:",
Object {
"Ref": "HighSellPriceAlarm",
},
"\\"]},\\"view\\":\\"timeSeries\\",\\"stacked\\":false}}]}",
],
],
}
`;
179 changes: 54 additions & 125 deletions cloudformation/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
/* eslint no-template-curly-in-string: "off" */
/* eslint-disable no-template-curly-in-string */

'use strict';

const label = (title, props) => ['.', title, '.', '.', '.', '.', '.', '.', props];
const alarmArn = cfAlarmResource => [
'arn:aws:cloudwatch:${self:provider.region}',
'-SPLIT-Ref:AWS::AccountId-SPLIT-',
'alarm',
`-SPLIT-Ref:${cfAlarmResource}-SPLIT-`,
].join(':');

module.exports.dashboard = () => {
let dashboardTemplate = JSON.stringify({
const json = JSON.stringify({
widgets: [
{
type: 'text',
x: 0,
y: 0,
width: 24,
height: 1,
properties: {
markdown: '## ${self:service}',
},
},
{
type: 'metric',
x: 0,
Expand All @@ -25,7 +23,7 @@ module.exports.dashboard = () => {
view: 'singleValue',
metrics: [
[
'${self:service}',
'${self:custom.cloudWatchNamespace}',
'BuyPrice',
'CryptoCurrency',
'${self:provider.environment.PREFERRED_CRYPTO_CURRENCY}',
Expand All @@ -37,19 +35,9 @@ module.exports.dashboard = () => {
label: 'Buy',
},
],
[
'.',
'SellPrice',
'.',
'.',
'.',
'.',
'.',
'.',
{
label: 'Sell',
},
],
label('SellPrice', {
label: 'Sell',
}),
],
region: '${self:provider.region}',
title: 'Current price in ${self:provider.environment.PREFERRED_LOCAL_CURRENCY}',
Expand All @@ -66,7 +54,7 @@ module.exports.dashboard = () => {
stacked: false,
metrics: [
[
'${self:service}',
'${self:custom.cloudWatchNamespace}',
'BuyPrice',
'CryptoCurrency',
'${self:provider.environment.PREFERRED_CRYPTO_CURRENCY}',
Expand All @@ -79,80 +67,30 @@ module.exports.dashboard = () => {
label: 'Buy actual',
},
],
[
'.',
'SellPrice',
'.',
'.',
'.',
'.',
'.',
'.',
{
color: '#ffbb78',
label: 'Sell actual',
},
],
[
'.',
'BuyPrice',
'.',
'.',
'.',
'.',
'.',
'.',
{
period: 3600,
color: '#1f77b4',
label: 'Buy hourly average',
},
],
[
'.',
'SellPrice',
'.',
'.',
'.',
'.',
'.',
'.',
{
period: 3600,
color: '#ff7f0e',
label: 'Sell hourly average',
},
],
[
'.',
'BuyPrice',
'.',
'.',
'.',
'.',
'.',
'.',
{
period: 86400,
label: 'Buy daily average',
color: '#ff9896',
},
],
[
'.',
'SellPrice',
'.',
'.',
'.',
'.',
'.',
'.',
{
period: 86400,
label: 'Sell daily average',
color: '#98df8a',
},
],
label('SellPrice', {
color: '#ffbb78',
label: 'Sell actual',
}),
label('BuyPrice', {
period: 3600,
color: '#1f77b4',
label: 'Buy hourly average',
}),
label('SellPrice', {
period: 3600,
color: '#ff7f0e',
label: 'Sell hourly average',
}),
label('BuyPrice', {
period: 86400,
label: 'Buy daily average',
color: '#ff9896',
}),
label('SellPrice', {
period: 86400,
label: 'Sell daily average',
color: '#98df8a',
}),
],
region: '${self:provider.region}',
title: 'Buy / Sell ${self:provider.environment.PREFERRED_CRYPTO_CURRENCY}-${self:provider.environment.PREFERRED_LOCAL_CURRENCY}',
Expand All @@ -167,9 +105,7 @@ module.exports.dashboard = () => {
properties: {
title: 'Low Buy Price',
annotations: {
alarms: [
'arn:aws:cloudwatch:${self:provider.region}:JOINREF:AWS::AccountIdJOIN:alarm:JOINREF:LowBuyPriceAlarmJOIN',
],
alarms: [alarmArn('LowBuyPriceAlarm')],
},
view: 'timeSeries',
stacked: false,
Expand All @@ -184,9 +120,7 @@ module.exports.dashboard = () => {
properties: {
title: 'Low Sell Price',
annotations: {
alarms: [
'arn:aws:cloudwatch:${self:provider.region}:JOINREF:AWS::AccountIdJOIN:alarm:JOINREF:LowSellPriceAlarmJOIN',
],
alarms: [alarmArn('LowSellPriceAlarm')],
},
view: 'timeSeries',
stacked: false,
Expand All @@ -201,9 +135,7 @@ module.exports.dashboard = () => {
properties: {
title: 'High Buy Price',
annotations: {
alarms: [
'arn:aws:cloudwatch:${self:provider.region}:JOINREF:AWS::AccountIdJOIN:alarm:JOINREF:HighBuyPriceAlarmJOIN',
],
alarms: [alarmArn('HighBuyPriceAlarm')],
},
view: 'timeSeries',
stacked: false,
Expand All @@ -218,9 +150,7 @@ module.exports.dashboard = () => {
properties: {
title: 'High Sell Price',
annotations: {
alarms: [
'arn:aws:cloudwatch:${self:provider.region}:JOINREF:AWS::AccountIdJOIN:alarm:JOINREF:HighSellPriceAlarmJOIN',
],
alarms: [alarmArn('HighSellPriceAlarm')],
},
view: 'timeSeries',
stacked: false,
Expand All @@ -229,20 +159,19 @@ module.exports.dashboard = () => {
],
});

// because of variable collisions with serverless, we can't use fn:sub
// so we need to split the template to use fn:join to ref the alarms
dashboardTemplate = dashboardTemplate.split('JOIN');
const lines = dashboardTemplate.map((line) => {
if (line.indexOf('REF:') > -1) {
return { Ref: line.replace('REF:', '') };
}
return line;
});
return {
'Fn::Join':
[
'Fn::Join': [
'',
lines,
/*
Because of variable collisions with serverless, we can't use Fn::Sub so we need to split the
template to use Fn::Join to Ref the alarms.
*/
json.split('-SPLIT-').map((line) => {
if (line.includes('Ref:')) {
return { Ref: line.replace('Ref:', '') };
}
return line;
}) // eslint-disable-line
],
};
};
7 changes: 7 additions & 0 deletions cloudformation/dashboard.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { dashboard } from './dashboard';

describe('Dashboard', () => {
test('returns cloudformation dashboard body', () => {
expect(dashboard()).toMatchSnapshot();
});
});

0 comments on commit fa2f426

Please sign in to comment.