All files / owid-grapher/baker countryProfiles.tsx

18.33% Statements 33/180
100% Branches 0/0
0% Functions 0/7
18.33% Lines 33/180

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 2141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x               1x 1x 1x 1x                           1x 1x                 1x 1x                                                                                                         1x 1x                                               1x 1x                                                                                                                                                        
import React from "react"
import * as db from "../db/db"
import { CountriesIndexPage } from "../site/CountriesIndexPage"
import { GrapherInterface } from "../grapher/core/GrapherInterface"
import * as lodash from "lodash"
import {
    CountryProfileIndicator,
    CountryProfilePage,
} from "../site/CountryProfilePage"
import { Variable } from "../db/model/Variable"
import { SiteBaker } from "./SiteBaker"
import { countries, getCountry } from "../clientUtils/countries"
import { OwidTable } from "../coreTable/OwidTable"
import { JsonError } from "../clientUtils/owidTypes"
import { renderToHtmlPage } from "./siteRenderers"
 
export const countriesIndexPage = (baseUrl: string) =>
    renderToHtmlPage(
        <CountriesIndexPage countries={countries} baseUrl={baseUrl} />
    )
 
const cache = new Map()
// Cache the result of an operation by a key for the duration of the process
function bakeCache<T>(cacheKey: any, retriever: () => T): T {
    if (cache.has(cacheKey)) return cache.get(cacheKey)

    const result = retriever()
    cache.set(cacheKey, result)
    return result
}
 
// Find the charts that will be shown on the country profile page (if they have that country)
// TODO: make this page per variable instead
const countryIndicatorGraphers = async (): Promise<GrapherInterface[]> =>
    bakeCache(countryIndicatorGraphers, async () => {
        const graphers = (
            await db
                .knexTable("charts")
                .whereRaw("publishedAt is not null and is_indexable is true")
        ).map((c: any) => JSON.parse(c.config)) as GrapherInterface[]
        return graphers.filter(
            (grapher) =>
                grapher.hasChartTab &&
                grapher.type === "LineChart" &&
                grapher.dimensions?.length === 1
        )
    })
 
const countryIndicatorVariables = async (): Promise<Variable.Row[]> =>
    bakeCache(countryIndicatorVariables, async () => {
        const variableIds = (await countryIndicatorGraphers()).map(
            (c) => c.dimensions![0]!.variableId
        )
        return Variable.rows(
            await db.knexTable(Variable.table).whereIn("id", variableIds)
        )
    })
 
export const denormalizeLatestCountryData = async (variableIds?: number[]) => {
    const entities = (await db
        .knexTable("entities")
        .select("id", "code")
        .whereRaw("validated is true and code is not null")) as {
        id: number
        code: string
    }[]

    const entitiesByCode = lodash.keyBy(entities, (e) => e.code)
    const entitiesById = lodash.keyBy(entities, (e) => e.id)
    const entityIds = countries.map((c) => entitiesByCode[c.code].id)

    if (!variableIds)
        variableIds = (await countryIndicatorVariables()).map((v) => v.id)

    const dataValuesQuery = db
        .knexTable("data_values")
        .select("variableId", "entityId", "value", "year")
        .whereIn("variableId", variableIds)
        .whereRaw(`entityId in (?)`, [entityIds])
        .andWhere("year", ">", 2010)
        .andWhere("year", "<", 2020)
        .orderBy("year", "DESC")

    let dataValues = (await dataValuesQuery) as {
        variableId: number
        entityId: number
        value: string
        year: number
    }[]
    dataValues = lodash.uniqBy(
        dataValues,
        (dv) => `${dv.variableId}-${dv.entityId}`
    )
    const rows = dataValues.map((dv) => ({
        variable_id: dv.variableId,
        country_code: entitiesById[dv.entityId].code,
        year: dv.year,
        value: dv.value,
    }))

    db.knexInstance().transaction(async (t) => {
        // Remove existing values
        await t
            .table("country_latest_data")
            .whereIn("variable_id", variableIds as number[])
            .delete()

        // Insert new ones
        if (rows.length) await t.table("country_latest_data").insert(rows)
    })
}
 
const countryIndicatorLatestData = async (countryCode: string) => {
    const dataValuesByEntityId = await bakeCache(
        countryIndicatorLatestData,
        async () => {
            const dataValues = (await db
                .knexTable("country_latest_data")
                .select(
                    "variable_id AS variableId",
                    "country_code AS code",
                    "year",
                    "value"
                )) as {
                variableId: number
                code: string
                year: number
                value: string
            }[]

            return lodash.groupBy(dataValues, (dv) => dv.code)
        }
    )

    return dataValuesByEntityId[countryCode]
}
 
export const countryProfilePage = async (
    countrySlug: string,
    baseUrl: string
) => {
    const country = getCountry(countrySlug)
    if (!country) throw new JsonError(`No such country ${countrySlug}`, 404)

    const graphers = await countryIndicatorGraphers()
    const variables = await countryIndicatorVariables()
    const variablesById = lodash.keyBy(variables, (v) => v.id)
    const dataValues = await countryIndicatorLatestData(country.code)

    const valuesByVariableId = lodash.groupBy(dataValues, (v) => v.variableId)

    let indicators: CountryProfileIndicator[] = []
    for (const grapher of graphers) {
        const firstDimension = grapher.dimensions![0]
        const vid = firstDimension && firstDimension.variableId
        const values = valuesByVariableId[vid]

        if (values && values.length) {
            const latestValue = values[0]
            const variable = variablesById[vid]

            // todo: this is a lot of setup to get formatValueShort. Maybe cleanup?
            const table = new OwidTable(
                [],
                [
                    {
                        slug: vid.toString(),
                        unit: variable.unit,
                        display: variable.display,
                    },
                ]
            )
            const column = table.get(vid.toString())

            let value: string | number
            value = parseFloat(latestValue.value)
            if (isNaN(value)) value = latestValue.value
            else if (variable.display.conversionFactor)
                value *= variable.display.conversionFactor

            indicators.push({
                year: latestValue.year,
                name: grapher.title as string,
                slug: `/grapher/${grapher.slug}?tab=chart&country=${country.code}`,
                variantName: grapher.variantName,
            })
        }
    }
 
    indicators = lodash.sortBy(indicators, (i) => i.name.trim())
 
    return renderToHtmlPage(
        <CountryProfilePage
            indicators={indicators}
            country={country}
            baseUrl={baseUrl}
        />
    )
}
 
export const bakeCountries = async (baker: SiteBaker) => {
    const html = await countriesIndexPage(baker.baseUrl)
    await baker.writeFile("/countries.html", html)
 
    await baker.ensureDir("/country")
    for (const country of countries) {
        const path = `/country/${country.slug}.html`
        const html = await countryProfilePage(country.slug, baker.baseUrl)
        await baker.writeFile(path, html)
    }
 
    baker.progressBar.tick({ name: "✅ baked countries" })
}