-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathDatapageHelpers.ts
224 lines (206 loc) · 7.81 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
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
219
220
221
222
223
224
import "dayjs"
import { getLinkType, getUrlTarget } from "@ourworldindata/components"
import {
GdocsContentSource,
DataPageDataV2,
OwidVariableWithSource,
gdocIdRegex,
getAttributionFragmentsFromVariable,
getLastUpdatedFromVariable,
getNextUpdateFromVariable,
omitUndefinedValues,
partition,
} from "@ourworldindata/utils"
import {
getGdocBaseObjectById,
getPublishedGdocBaseObjectBySlug,
loadGdocFromGdocBase,
} from "../db/model/Gdoc/GdocFactory.js"
import { OwidGoogleAuth } from "../db/OwidGoogleAuth.js"
import {
EnrichedFaq,
FaqDictionary,
GrapherInterface,
OwidGdocBaseInterface,
} from "@ourworldindata/types"
import { KnexReadonlyTransaction } from "../db/db.js"
import { parseFaqs } from "../db/model/Gdoc/rawToEnriched.js"
import { logErrorAndMaybeCaptureInSentry } from "../serverUtils/errorLog.js"
import { getSlugForTopicTag } from "./GrapherBakingUtils.js"
import { getShortPageCitation } from "../site/gdocs/utils.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,
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 (
knex: KnexReadonlyTransaction,
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
}
type EnrichedFaqLookupError = {
type: "error"
error: string
}
type EnrichedFaqLookupSuccess = {
type: "success"
enrichedFaq: EnrichedFaq
}
type EnrichedFaqLookupResult = EnrichedFaqLookupError | EnrichedFaqLookupSuccess
export const fetchAndParseFaqs = async (
knex: KnexReadonlyTransaction,
faqGdocIds: string[],
{ isPreviewing }: { isPreviewing: boolean }
) => {
const gdocFetchPromises = faqGdocIds.map((gdocId) =>
getDatapageGdoc(knex, gdocId, isPreviewing)
)
const gdocs = await Promise.all(gdocFetchPromises)
const gdocIdToFragmentIdToBlock: Record<string, FaqDictionary> = {}
gdocs.forEach((gdoc) => {
if (!gdoc) return
const faqs = parseFaqs(
("faqs" in gdoc.content && gdoc.content?.faqs) ?? [],
gdoc.id
)
gdocIdToFragmentIdToBlock[gdoc.id] = faqs.faqs
})
return gdocIdToFragmentIdToBlock
}
export const resolveFaqsForVariable = (
gdocIdToFragmentIdToBlock: Record<string, FaqDictionary>,
variableMetadata: OwidVariableWithSource
) => {
const resolvedFaqResults: EnrichedFaqLookupResult[] = variableMetadata
.presentation?.faqs
? variableMetadata.presentation.faqs.map((faq) => {
const enrichedFaq = gdocIdToFragmentIdToBlock[faq.gdocId]?.[
faq.fragmentId
] as EnrichedFaq | undefined
if (!enrichedFaq)
return {
type: "error",
error: `Could not find fragment ${faq.fragmentId} in gdoc ${faq.gdocId}`,
}
return {
type: "success",
enrichedFaq,
}
})
: []
const [resolvedFaqs, errors] = partition(
resolvedFaqResults,
(result) => result.type === "success"
) as [EnrichedFaqLookupSuccess[], EnrichedFaqLookupError[]]
return { resolvedFaqs, errors }
}
export const getPrimaryTopic = async (
knex: KnexReadonlyTransaction,
firstTopicTag: string | undefined
) => {
if (!firstTopicTag) return undefined
let topicSlug: string
try {
topicSlug = await getSlugForTopicTag(knex, firstTopicTag)
} catch {
await logErrorAndMaybeCaptureInSentry(
new Error(
`Data page is using "${firstTopicTag}" as its primary tag, which we are unable to resolve to a tag in the grapher DB`
)
)
return undefined
}
if (topicSlug) {
const gdoc = await getPublishedGdocBaseObjectBySlug(
knex,
topicSlug,
true
)
if (gdoc) {
const citation = getShortPageCitation(
gdoc.content.authors,
gdoc.content.title ?? "",
gdoc?.publishedAt
)
return { topicTag: firstTopicTag, citation }
}
}
return undefined
}