Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to generate a map of US states in Datadog
#!/usr/bin/env node
const fs = require('fs');
// Creates a datadog square states map based on a metric
DASHBOARD_TITLE='New US States Screenboard';
// -- METRIC CONFIGURATION AND LAYOUT --
AGGREGATOR='sum';
METRIC='your_metric_here';
STATE_TAG='your_tag_here';
OVERVIEW_GROUP='your_group_here';
WIDTH=8;
HEIGHT=7;
function query(state) {
const groups = TEMPLATE_GROUPS.concat(
state == '00' ? [`${STATE_TAG}:`] // no state
: state == '99' ? [] // all states
: [`${STATE_TAG}:${state.toLowerCase()}`] // single state
);
return `default_zero(${AGGREGATOR}:${METRIC}{ ${ groups.join(',') } }.as_count())`;
}
STATE_GRID = read_state_grid(`
__ __ __ __ __ __ __ 00 99 __ __ __
AK __ __ __ __ __ __ __ __ __ __ ME
__ __ __ __ __ __ WI __ __ __ VT NH
__ WA ID MT ND MN IL MI __ NY MA
__ OR NV WY SD IA IN OH PA NJ CT RI
__ CA UT CO NE MO KY WV VA MD DE
__ __ AZ NM KS AR TN NC SC DC
HI __ __ __ OK LA MS AL GA
__ __ __ __ TX __ __ __ __ FL
`);
CONDITIONAL_FORMATS = [
// Gradient from https://colorbrewer2.org/#type=sequential&scheme=BuPu&n=5
{ "comparator": ">", "value": 999999, "palette":"custom_bg", "custom_bg_color":"#810f7c" },
{ "comparator": ">", "value": 99999, "palette":"custom_bg", "custom_bg_color":"#8856a7" },
{ "comparator": ">", "value": 9999, "palette":"custom_bg", "custom_bg_color":"#8c96c6" },
{ "comparator": ">", "value": 999, "palette":"custom_bg", "custom_bg_color":"#b3cde3" },
{ "comparator": ">", "value": 99, "palette":"custom_bg", "custom_bg_color":"#edf8fb" },
{ "comparator": ">", "value": 0, "palette":"custom_bg", "custom_bg_color":"#ffffff" },
// Gray out 0 values
{ "comparator": ">=", "value":0, "palette":"custom_bg", "custom_bg_color":"#F6F5F6" },
// The text color thing doesn't seem to work
// { "comparator": ">=", "value":0, "palette":"custom_text", "custom_fg_color":"#dddddd" },
]
// -- TEMPLATE VARIABLES --
TEMPLATE_VARIABLES = [
{ "name": "your_name_here", "prefix": "your_tag_here", "default": "*" },
]
TEMPLATE_GROUPS = TEMPLATE_VARIABLES.map(t => '$' + t.name);
TEMPLATE_VARIABLE_PRESETS = [
{
"name": "Production",
"template_variables": []
},
]
// -- GENERATOR --
OVERVIEW_WIDGET = {
"layout": {
"x": 2 * WIDTH,
"y": 0,
"width": 5 * WIDTH,
"height": HEIGHT + 2 // to account for no title
},
"definition": {
"type": "timeseries",
"requests": [
{
"q": ( `${AGGREGATOR}:${METRIC}{ ${ TEMPLATE_GROUPS.join(',') } }`
+ (OVERVIEW_GROUP ? `by { ${OVERVIEW_GROUP} }` : '')
+ '.as_count()' ),
"display_type": "bars"
}
],
}
}
DASHBOARD_TEMPLATE = {
"title": DASHBOARD_TITLE,
"description": `${ AGGREGATOR } of ${ METRIC } by ${ STATE_TAG }`,
"widgets": [ OVERVIEW_WIDGET ],
"template_variables": TEMPLATE_VARIABLES,
"template_variable_presets": TEMPLATE_VARIABLE_PRESETS,
"layout_type": "free",
}
function read_state_grid(g) {
const m = {};
g.split('\n')
.filter(x => /\S/.test(x))
.forEach((line, r) => {
line.split(' ').forEach((state, c) => {
m[state] = [c, r];
})
});
delete m['__'];
return m;
}
function state_widget(state) {
return {
layout: {
x: STATE_GRID[state][0] * WIDTH,
y: STATE_GRID[state][1] * HEIGHT,
width: WIDTH,
height: HEIGHT
},
definition: {
title: state == '00' ? 'No State' : state == '99' ? 'Total' : state,
title_size: "13",
title_align: "center",
type: "query_value",
requests: [{
"q": query(state),
"aggregator": AGGREGATOR,
"conditional_formats": CONDITIONAL_FORMATS
}],
autoscale: true,
precision: 0
}
}
}
function main() {
const layout = DASHBOARD_TEMPLATE;
layout.widgets.push(...Object.keys(STATE_GRID).map(state_widget));
// stick a copy of this script in the description
layout.description += ( "\n\n"
+ "---\n"
+ "[template](https://gist.github.com/mrichards42/eb0e64c917aa511f80b761d94379acba)\n\n"
+ "(full source below)\n\n"
+ " \n\n" + " \n\n" + " \n\n" + " \n\n" + " \n\n"
+ " \n\n" + " \n\n" + " \n\n" + " \n\n" + " \n\n"
+ "```\n" + fs.readFileSync(__filename, 'utf8') + "```" );
console.log(JSON.stringify(layout))
}
if (require.main === module) {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment