-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
DatapageHelpers.ts
115 lines (108 loc) · 4.69 KB
/
DatapageHelpers.ts
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
import "dayjs"
import { getLinkType, getUrlTarget } from "@ourworldindata/components"
import {
GdocsContentSource,
DataPageDataV2,
OwidVariableWithSource,
gdocIdRegex,
getAttributionFragmentsFromVariable,
getLastUpdatedFromVariable,
getNextUpdateFromVariable,
omitUndefinedValues,
} from "@ourworldindata/utils"
import {
getGdocBaseObjectById,
loadGdocFromGdocBase,
} from "../db/model/Gdoc/GdocFactory.js"
import { OwidGoogleAuth } from "../db/OwidGoogleAuth.js"
import { GrapherInterface, OwidGdocBaseInterface } from "@ourworldindata/types"
import { KnexReadWriteTransaction } from "../db/db.js"
export const getDatapageDataV2 = async (
variableMetadata: OwidVariableWithSource,
partialGrapherConfig: GrapherInterface
): Promise<DataPageDataV2> => {
{
const lastUpdated = getLastUpdatedFromVariable(variableMetadata) ?? ""
const nextUpdate = getNextUpdateFromVariable(variableMetadata)
const datapageJson: DataPageDataV2 = {
status: "draft",
title: variableMetadata.presentation?.titlePublic
? omitUndefinedValues({
title: variableMetadata.presentation?.titlePublic,
attributionShort:
variableMetadata.presentation?.attributionShort,
titleVariant: variableMetadata.presentation?.titleVariant,
})
: {
title:
partialGrapherConfig.title ??
variableMetadata.display?.name ??
variableMetadata.name ??
"",
},
description: variableMetadata.description,
descriptionShort: variableMetadata.descriptionShort,
descriptionFromProducer: variableMetadata.descriptionFromProducer,
attributionShort: variableMetadata.presentation?.attributionShort,
titleVariant: variableMetadata.presentation?.titleVariant,
topicTagsLinks: variableMetadata.presentation?.topicTagsLinks ?? [],
attributions: getAttributionFragmentsFromVariable(variableMetadata),
faqs: [],
descriptionKey: variableMetadata.descriptionKey ?? [],
descriptionProcessing: variableMetadata.descriptionProcessing,
owidProcessingLevel: variableMetadata.processingLevel,
dateRange: variableMetadata.timespan ?? "",
lastUpdated: lastUpdated,
nextUpdate: nextUpdate,
relatedData: [],
allCharts: [],
relatedResearch: [],
source: variableMetadata.source,
origins: variableMetadata.origins ?? [],
chartConfig: partialGrapherConfig as Record<string, unknown>,
unit: variableMetadata.display?.unit ?? variableMetadata.unit,
unitConversionFactor: variableMetadata.display?.conversionFactor,
}
return datapageJson
}
}
/**
* Get the datapage companion gdoc, if any.
*
* When previewing, we want to render the datapage from the live gdoc.
* Otherwise, we're baking from the gdoc parsed and saved in the database
* following a visit to /admin/gdocs/[googleDocId]/preview
*
* see https://github.com/owid/owid-grapher/issues/2121#issue-1676097164
*/
export const getDatapageGdoc = async (
// TODO: this transaction is only RW because somewhere inside it we fetch images
knex: KnexReadWriteTransaction,
googleDocEditLinkOrId: string,
isPreviewing: boolean
): Promise<OwidGdocBaseInterface | null> => {
// Get the google doc id from the datapage JSON file and return early if
// none found
const isPlainGoogleId = gdocIdRegex.exec(googleDocEditLinkOrId)
const googleDocId = isPlainGoogleId
? googleDocEditLinkOrId
: getLinkType(googleDocEditLinkOrId) === "gdoc"
? getUrlTarget(googleDocEditLinkOrId)
: null
if (!googleDocId) return null
// When previewing, we want to render the datapage from the live gdoc, but
// only if the user has set up the necessary auth keys to access the Google
// Doc API. This won't be the case for external contributors or possibly
// data engineers focusing on the data pipeline. In those cases, we grab the
// gdoc found in the database, if any. This use case doesn't currently
// support images (imageMetadata won't be set).
let datapageGdoc =
(await getGdocBaseObjectById(knex, googleDocId, true)) ?? null
if (datapageGdoc && isPreviewing && OwidGoogleAuth.areGdocAuthKeysSet())
datapageGdoc = await loadGdocFromGdocBase(
knex,
datapageGdoc,
GdocsContentSource.Gdocs
)
return datapageGdoc
}