All files / owid-grapher/baker GrapherImageBaker.tsx

10.75% Statements 20/186
100% Branches 0/0
0% Functions 0/8
10.75% Lines 20/186

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                   1x                                 1x                                                   1x                                                                 1x                         1x                       1x                                         1x                                                                                                                    
import * as db from "../db/db"
import { getVariableData } from "../db/model/Variable"
import * as fs from "fs-extra"
import svgo from "svgo"
import sharp from "sharp"
import * as path from "path"
import { GrapherInterface } from "../grapher/core/GrapherInterface"
import { Grapher } from "../grapher/core/Grapher"
import {
    grapherSlugToExportFileKey,
    grapherUrlToSlugAndQueryStr,
} from "./GrapherBakingUtils"
 
export async function bakeGraphersToPngs(
    outDir: string,
    jsonConfig: GrapherInterface,
    vardata: any,
    optimizeSvgs = false
) {
    const grapher = new Grapher({ ...jsonConfig, manuallyProvideData: true })
    grapher.isExportingtoSvgOrPng = true
    grapher.receiveOwidData(vardata)
    const outPath = path.join(outDir, grapher.slug as string)

    let svgCode = grapher.staticSVG
    if (optimizeSvgs) svgCode = await optimizeSvg(svgCode)

    return Promise.all([
        fs
            .writeFile(`${outPath}.svg`, svgCode)
            .then(() => console.log(`${outPath}.svg`)),
        sharp(Buffer.from(grapher.staticSVG), { density: 144 })
            .png()
            .resize(grapher.idealBounds.width, grapher.idealBounds.height)
            .flatten({ background: "#ffffff" })
            .toFile(`${outPath}.png`),
    ])
}
 
export async function getGraphersAndRedirectsBySlug() {
    const { graphersBySlug, graphersById } = await getPublishedGraphersBySlug()

    const redirectQuery = db.queryMysql(
        `SELECT slug, chart_id FROM chart_slug_redirects`
    )

    for (const row of await redirectQuery) {
        const grapher = graphersById.get(row.chart_id)
        if (grapher) {
            graphersBySlug.set(row.slug, grapher)
        }
    }

    return graphersBySlug
}
 
export async function getPublishedGraphersBySlug(
    includePrivate: boolean = false
) {
    const graphersBySlug: Map<string, GrapherInterface> = new Map()
    const graphersById: Map<number, GrapherInterface> = new Map()

    // Select all graphers that are published and that do not have the tag Private
    const sql = includePrivate
        ? `SELECT * FROM charts WHERE JSON_EXTRACT(config, "$.isPublished") IS TRUE`
        : `SELECT charts.id as id, charts.config as config FROM charts
LEFT JOIN chart_tags on chart_tags.chartId = charts.id
LEFT JOIN tags on tags.id = chart_tags.tagid
WHERE JSON_EXTRACT(config, "$.isPublished") IS TRUE
AND (tags.name IS NULL OR tags.name != 'Private')`

    const query = db.queryMysql(sql)
    for (const row of await query) {
        const grapher = JSON.parse(row.config)

        grapher.id = row.id
        graphersBySlug.set(grapher.slug, grapher)
        graphersById.set(row.id, grapher)
    }
    return { graphersBySlug, graphersById }
}
 
export async function bakeGrapherToSvg(
    jsonConfig: GrapherInterface,
    outDir: string,
    slug: string,
    queryStr = "",
    optimizeSvgs = false,
    overwriteExisting = false,
    verbose = true
) {
    const grapher = initGrapherForSvgExport(jsonConfig, queryStr)
    const { width, height } = grapher.idealBounds
    const outPath = buildSvgOutFilepath(
        slug,
        outDir,
        jsonConfig.version,
        width,
        height,
        verbose,
        queryStr
    )

    if (fs.existsSync(outPath) && !overwriteExisting) return
    const variableIds = grapher.dimensions.map((d) => d.variableId)
    const vardata = await getVariableData(variableIds)
    grapher.receiveOwidData(vardata)

    let svgCode = grapher.staticSVG
    if (optimizeSvgs) svgCode = await optimizeSvg(svgCode)

    fs.writeFile(outPath, svgCode)
    return svgCode
}
 
export function initGrapherForSvgExport(
    jsonConfig: GrapherInterface,
    queryStr: string = ""
) {
    const grapher = new Grapher({
        ...jsonConfig,
        manuallyProvideData: true,
        queryStr,
    })
    grapher.isExportingtoSvgOrPng = true
    return grapher
}
 
export function buildSvgOutFilename(
    slug: string,
    version: number | undefined,
    width: number,
    height: number,
    queryStr: string = ""
) {
    const fileKey = grapherSlugToExportFileKey(slug, queryStr)
    const outFilename = `${fileKey}_v${version}_${width}x${height}.svg`
    return outFilename
}
 
export function buildSvgOutFilepath(
    slug: string,
    outDir: string,
    version: number | undefined,
    width: number,
    height: number,
    verbose: boolean,
    queryStr: string = ""
) {
    const outFilename = buildSvgOutFilename(
        slug,
        version,
        width,
        height,
        queryStr
    )
    const outPath = path.join(outDir, outFilename)
    if (verbose) console.log(outPath)
    return outPath
}
 
export async function bakeGraphersToSvgs(
    grapherUrls: string[],
    outDir: string,
    optimizeSvgs = false
) {
    await fs.mkdirp(outDir)
    const graphersBySlug = await getGraphersAndRedirectsBySlug()

    return Promise.all(
        Array.from(grapherUrls).map((grapherUrl) => {
            const { slug, queryStr } = grapherUrlToSlugAndQueryStr(grapherUrl)
            const jsonConfig = graphersBySlug.get(slug)
            if (jsonConfig) {
                return bakeGrapherToSvg(
                    jsonConfig,
                    outDir,
                    slug,
                    queryStr,
                    optimizeSvgs
                )
            }
            return undefined
        })
    )
}
 
const svgoConfig: svgo.OptimizeOptions = {
    floatPrecision: 2,
    plugins: [
        {
            name: "preset-default",
            params: {
                overrides: {
                    // disable certain plugins
                    collapseGroups: false, // breaks the "Our World in Data" logo in the upper right
                    removeUnknownsAndDefaults: false, // would remove hrefs from links (<a>)
                    removeViewBox: false,
                },
            },
        },
    ],
}
 
async function optimizeSvg(svgString: string): Promise<string> {
    const optimizedSvg = await svgo.optimize(svgString, svgoConfig)
    return optimizedSvg.data
}
 
export async function grapherToSVG(
    jsonConfig: GrapherInterface,
    vardata: any
): Promise<string> {
    const grapher = new Grapher({ ...jsonConfig, manuallyProvideData: true })
    grapher.isExportingtoSvgOrPng = true
    grapher.receiveOwidData(vardata)
    return grapher.staticSVG
}