Skip to content

Instantly share code, notes, and snippets.

@JosephShering
Last active March 1, 2018 19:07
Show Gist options
  • Save JosephShering/fb43591e057b6a2a7e21e320346176df to your computer and use it in GitHub Desktop.
Save JosephShering/fb43591e057b6a2a7e21e320346176df to your computer and use it in GitHub Desktop.
datadog coding challenge
const get_tag_counts = function(stream) {
const unsortedTags = {};
const sortedTags = [];
stream.forEach(function(row) {
const splitRow = row.split('|');
const tags = splitRow[2] || '';
const splitTags = tags.split(',');
splitTags.forEach(function(tag) {
if(tag === '') {
return
}
if(unsortedTags[tag]) {
unsortedTags[tag] += 1
} else {
unsortedTags[tag] = 1
}
});
});
Object.keys(unsortedTags).forEach(function(tag) {
sortedTags.push([tag, unsortedTags[tag]]);
});
sortedTags.sort(function(a, b) {
if(b[1] === a[1]) {
return b[0] < a[0] ? 1 : -1;
} else {
return b[1] - a[1];
}
});
sortedTags.forEach(function(tagCount) {
console.log(tagCount[1] + ' ' + tagCount[0]);
});
};
const stream = [
'system.load.1|1|host:a,role:web,availability-zone:us-east-1a',
'system.load.15|1|host:b,role:web,availability-zone:us-east-1b',
'system.cpu.user|20|host:a,role:web,availability-zone:us-east-1a',
'postgresql.locks|12|host:c,role:db,db_role:master,availability-zone:us-east-1e',
'postgresql.db.count|2|host:d,role:db,db_role:replica,availability-zone:us-east-1a',
'kafka.consumer.lag|20000|host:e,role:intake,availability-zone:us-east-1a',
'kafka.consumer.offset'
];
get_tag_counts(stream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment