Skip to content

Commit

Permalink
🐛 fix chart and explorer test pages, unify query builder use
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Mar 26, 2024
1 parent 12d857b commit fa71b19
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
4 changes: 2 additions & 2 deletions adminSiteServer/adminRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ getPlainRouteWithROTransaction(
)
return `File not found`
const explorer = await explorerAdminServer.getExplorerFromFile(filename)
const explorerPage = renderExplorerPage(explorer, knex)
const explorerPage = await renderExplorerPage(explorer, knex)

return res.send(explorerPage)
}
Expand All @@ -327,7 +327,7 @@ getPlainRouteNonIdempotentWithRWTransaction(
if (!variableMetadata) throw new JsonError("No such variable", 404)

res.send(
renderDataPageV2(
await renderDataPageV2(
{
variableId,
variableMetadata,
Expand Down
42 changes: 24 additions & 18 deletions adminSiteServer/testPageRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,29 @@ async function propsFromQueryParams(

let query = knex
.table("charts")
.where("publishedAt IS NOT NULL")
.select("id", "slug")
.whereRaw("publishedAt IS NOT NULL")
.limit(perPage)
.offset(perPage * (page - 1))
.orderBy("id", "DESC")
console.error(query.toSQL())

let tab = params.tab

if (params.type) {
if (params.type === ChartTypeName.WorldMap) {
query = query.andWhere(`config->>"$.hasMapTab" = "true"`)
query = query.andWhereRaw(`config->>"$.hasMapTab" = "true"`)
tab = tab || GrapherTabOption.map
} else {
if (params.type === "LineChart") {
query = query.andWhere(
query = query.andWhereRaw(
`(
config->"$.type" = "LineChart"
OR config->"$.type" IS NULL
) AND COALESCE(config->>"$.hasChartTab", "true") = "true"`
)
} else {
query = query.andWhere(
query = query.andWhereRaw(
`config->"$.type" = :type AND COALESCE(config->>"$.hasChartTab", "true") = "true"`,
{ type: params.type }
)
Expand All @@ -164,41 +166,43 @@ async function propsFromQueryParams(
}

if (params.logLinear) {
query = query.andWhere(
query = query.andWhereRaw(
`config->>'$.yAxis.canChangeScaleType' = "true" OR config->>'$.xAxis.canChangeScaleType' = "true"`
)
tab = GrapherTabOption.chart
}

if (params.comparisonLines) {
query = query.andWhere(`config->'$.comparisonLines[0].yEquals' != ''`)
query = query.andWhereRaw(
`config->'$.comparisonLines[0].yEquals' != ''`
)
tab = GrapherTabOption.chart
}

if (params.stackMode) {
query = query.andWhere(`config->'$.stackMode' = :stackMode`, {
query = query.andWhereRaw(`config->'$.stackMode' = :stackMode`, {
stackMode: params.stackMode,
})
tab = GrapherTabOption.chart
}

if (params.relativeToggle) {
query = query.andWhere(`config->>'$.hideRelativeToggle' = "false"`)
query = query.andWhereRaw(`config->>'$.hideRelativeToggle' = "false"`)
tab = GrapherTabOption.chart
}

if (params.categoricalLegend) {
// This is more of a heuristic, since this query can potentially include charts that don't
// have a visible categorial legend, and can leave out some that have one.
// But in practice it seems to work reasonably well.
query = query.andWhere(
query = query.andWhereRaw(
`json_length(config->'$.map.colorScale.customCategoryColors') > 1`
)
tab = GrapherTabOption.map
}

if (params.mixedTimeTypes) {
query = query.andWhere(
query = query.andWhereRaw(
`
(
SELECT COUNT(DISTINCT CASE
Expand All @@ -217,34 +221,34 @@ async function propsFromQueryParams(
if (params.addCountryMode) {
const mode = params.addCountryMode
if (mode === EntitySelectionMode.MultipleEntities) {
query = query.andWhere(
query = query.andWhereRaw(
`config->'$.addCountryMode' IS NULL OR config->'$.addCountryMode' = :mode`,
{
mode: EntitySelectionMode.MultipleEntities,
}
)
} else {
query = query.andWhere(`config->'$.addCountryMode' = :mode`, {
query = query.andWhereRaw(`config->'$.addCountryMode' = :mode`, {
mode,
})
}
}

if (ids.length > 0) {
query = query.andWhere(`charts.id IN (${params.ids})`)
query = query.andWhereRaw(`charts.id IN (${params.ids})`)
}

if (tab === GrapherTabOption.map) {
query = query.andWhere(`config->>"$.hasMapTab" = "true"`)
query = query.andWhereRaw(`config->>"$.hasMapTab" = "true"`)
} else if (tab === GrapherTabOption.chart) {
query = query.andWhere(
query = query.andWhereRaw(
`COALESCE(config->>"$.hasChartTab", "true") = "true"`
)
}

if (datasetIds.length > 0) {
const datasetIds = params.datasetIds
query = query.andWhere(
query = query.andWhereRaw(
`
EXISTS(
SELECT *
Expand All @@ -259,7 +263,7 @@ async function propsFromQueryParams(
}

if (namespaces.length > 0) {
query = query.andWhere(
query = query.andWhereRaw(
`
EXISTS(
SELECT *
Expand All @@ -274,9 +278,11 @@ async function propsFromQueryParams(
)
}

console.error(query.toSQL())

const charts: ChartItem[] = (await query).map((c) => ({
id: c.id,
slug: c.config.slug ?? "",
slug: c.slug ?? "",
}))

if (tab) {
Expand Down
2 changes: 1 addition & 1 deletion baker/postUpdatedHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const syncPostToGrapher = async (
else if (existsInGrapher)
await knex
.table(postsTable)
.where("id", "=", rowForDb.id)
.where({ id: rowForDb.id })
.update(rowForDb)
}

Expand Down
2 changes: 1 addition & 1 deletion db/syncPostsToGrapher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ const syncPostsToGrapher = async (
if (doesExistInGrapher[row.id])
await knex
.update(rowForDb)
.where("id", "=", rowForDb.id)
.where({ id: rowForDb.id })
.into(postsTable)
else await knex.insert(rowForDb).into(postsTable)
}
Expand Down

0 comments on commit fa71b19

Please sign in to comment.