Skip to content

Instantly share code, notes, and snippets.

@koladilip
Last active December 30, 2022 06:25
Show Gist options
  • Save koladilip/12f55118d50ded2ba944ebf13a1d4dbb to your computer and use it in GitHub Desktop.
Save koladilip/12f55118d50ded2ba944ebf13a1d4dbb to your computer and use it in GitHub Desktop.
Squad cast json export to csv with tags extracted from description
const fs = require("fs");
const jsonFilePath = process.argv[2] || "incidents.json";
const isDir = fs.lstatSync(jsonFilePath).isDirectory();
if(!isDir) {
const csv = getCSV(jsonFilePath);
csv.unshift("Title,DestType,Priority,Service,Namespace,WorkspaceId,CreatedAt");
fs.writeFileSync("incidents.csv", csv.join("\n"));
} else {
const files = fs.readdirSync(jsonFilePath);
const csv = files.map(file => {
const csv = getCSV(`${jsonFilePath}/${file}`);
return csv.join("\n");
});
csv.unshift("Title,DestType,Priority,Service,Namespace,WorkspaceId,CreatedAt");
fs.writeFileSync("incidents.csv", csv.join("\n"));
}
function getCSV(jsonFilePath) {
const incidentsData = JSON.parse(
fs.readFileSync(jsonFilePath, { encoding: "utf-8" })
);
const incidents = incidentsData.incidents;
function getTags(incident) {
const tags = {};
if (incident.tags) {
Object.entries(incident.tags).forEach((tag) => {
const key = tag[0] === "Destination" ? "DestType" : tag[0];
tags[key] = tag[1].value;
});
}
if (incident.description.includes("Labels")) {
const labels = incident.description.match(/ [^:]+:[^\n]+/g);
labels.forEach((label) => {
const [key, value] = label.trim().split(":");
if (key !== "Group Key") {
tags[key] = value.trim();
}
});
}
return tags;
}
incidents.forEach((incident) => {
Object.assign(incident, getTags(incident));
});
const csv = incidents.map((incident) => {
const {
title,
DestType,
Priority,
service,
Namespace,
WorkspaceId,
created_at,
} = incident;
return [
title.replace(/,/g, " "),
DestType,
Priority,
service,
Namespace,
WorkspaceId,
created_at,
].join(",");
});
return csv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment