Skip to content

Commit

Permalink
Merge pull request #77 from visualize-admin/feat/municipalities-info
Browse files Browse the repository at this point in the history
New columns in CSV download
  • Loading branch information
ptbrowne authored Jul 31, 2024
2 parents c5f2d03 + 21aa0fe commit a3520b1
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/pages/api/data-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { csvFormat } from "d3";
import { csvFormat, format } from "d3";
import { NextApiRequest, NextApiResponse } from "next";

import buildEnv from "src/env/build";
Expand All @@ -10,6 +10,21 @@ import {
getView,
} from "../../rdf/queries";

const formatters = {
gridusagebeforediscount: format(".5f"),
gridusagediscount: format(".5f"),
gridpowerprice: format(".5f"),
gridworkingprice: format(".5f"),
energybeforediscount: format(".5f"),
energydiscount: format(".5f"),
energyfixcost: format(".5f"),
energypowerprice: format(".5f"),
energyworkingprice: format(".5f"),
gridusage: format(".5f"),
energy: format(".5f"),
total: format(".5f"),
};

export default async (req: NextApiRequest, res: NextApiResponse) => {
const locale = parseLocaleString(req.query.locale?.toString());
const period = req.query.period?.toString() ?? buildEnv.CURRENT_PERIOD!;
Expand All @@ -20,18 +35,26 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {

const dimensions = [
"period",
// "municipality",
// "municipalityLabel",
"operator",
"operatorIdentifier",
"operatorLabel",
"category",
"product",
"aidfee",
"category",
"gridusagename",
"gridusagebeforediscount",
"gridusagediscount",
"gridpowerprice",
"gridworkingprice",
"energyname",
"energybeforediscount",
"energydiscount",
"energyfixcost",
"energypowerprice",
"energyworkingprice",
"charge",
"aidfee",
"gridusage",
"energy",
// "fixcostspercent",
"total",
"fixcosts",
];
Expand All @@ -46,6 +69,19 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
}
);

observations.map((observation) => {
for (const key_ in observation) {
if (!(key_ in formatters)) {
continue;
}
const key = key_ as keyof typeof formatters;
observation[key] = formatters[key](
observation[key as keyof typeof observation] as number
);
}
return observations;
});

const csv = csvFormat(
observations,
dimensions.filter((d) => d !== "operator")
Expand Down

0 comments on commit a3520b1

Please sign in to comment.