-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: MetricPage wired up to /metrics/ (#2118)
* This is everything related to the routing via code. * Everything else should be able to be wired up via Plasmic
- Loading branch information
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use client"; | ||
|
||
import { redirect } from "next/navigation"; | ||
import { useEffect } from "react"; | ||
import { logger } from "../../../lib/logger"; | ||
|
||
const REDIRECT_URL = "/retry"; | ||
|
||
type ErrorPageProps = { | ||
error: Error & { digest?: string }; | ||
reset: () => void; | ||
}; | ||
|
||
export default function Error(props: ErrorPageProps) { | ||
const { error } = props; | ||
useEffect(() => { | ||
logger.error(error); | ||
}, [error]); | ||
|
||
redirect(REDIRECT_URL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { notFound } from "next/navigation"; | ||
import { cache } from "react"; | ||
import { PlasmicComponent } from "@plasmicapp/loader-nextjs"; | ||
import { PLASMIC } from "../../../plasmic-init"; | ||
import { PlasmicClientRootProvider } from "../../../plasmic-init-client"; | ||
import { cachedGetMetricByName } from "../../../lib/clickhouse/cached-queries"; | ||
import { logger } from "../../../lib/logger"; | ||
import { catchallPathToString } from "../../../lib/paths"; | ||
|
||
const METRIC_SOURCE = "OSO"; | ||
const METRIC_NAMESPACE = "oso"; | ||
const PLASMIC_COMPONENT = "MetricPage"; | ||
//export const dynamic = STATIC_EXPORT ? "force-static" : "force-dynamic"; | ||
//export const dynamic = "force-static"; | ||
export const dynamic = "force-dynamic"; | ||
export const dynamicParams = true; | ||
export const revalidate = false; // 3600 = 1 hour | ||
// TODO: This cannot be empty due to this bug | ||
// https://github.com/vercel/next.js/issues/61213 | ||
const STATIC_EXPORT_SLUGS: string[] = ["stars"]; | ||
export async function generateStaticParams() { | ||
return STATIC_EXPORT_SLUGS.map((s) => ({ | ||
name: [s], | ||
})); | ||
} | ||
|
||
const cachedFetchComponent = cache(async (componentName: string) => { | ||
try { | ||
const plasmicData = await PLASMIC.fetchComponentData(componentName); | ||
return plasmicData; | ||
} catch (e) { | ||
logger.warn(e); | ||
return null; | ||
} | ||
}); | ||
|
||
/** | ||
* This SSR route allows us to fetch the metric from the database | ||
* on the first HTTP request, which should be faster than fetching it client-side | ||
*/ | ||
|
||
type MetricPagePath = { | ||
name: string[]; | ||
}; | ||
|
||
type MetricPageProps = { | ||
params: MetricPagePath; | ||
}; | ||
|
||
export default async function MetricPage(props: MetricPageProps) { | ||
const { params } = props; | ||
if (!params.name || !Array.isArray(params.name) || params.name.length < 1) { | ||
logger.warn("Invalid metric page path", params); | ||
notFound(); | ||
} | ||
|
||
// Get metric metadata from the database | ||
const name = catchallPathToString(params.name); | ||
const metricArray = await cachedGetMetricByName({ | ||
metricSource: METRIC_SOURCE, | ||
metricNamespace: METRIC_NAMESPACE, | ||
metricName: name, | ||
}); | ||
if (!Array.isArray(metricArray) || metricArray.length < 1) { | ||
logger.warn(`Cannot find metric (name=${name})`); | ||
notFound(); | ||
} | ||
const metadata = metricArray[0]; | ||
//console.log("project", project); | ||
|
||
// Get Plasmic component | ||
const plasmicData = await cachedFetchComponent(PLASMIC_COMPONENT); | ||
if (!plasmicData) { | ||
logger.warn(`Unable to get componentName=${PLASMIC_COMPONENT}`); | ||
notFound(); | ||
} | ||
const compMeta = plasmicData.entryCompMetas[0]; | ||
|
||
return ( | ||
<PlasmicClientRootProvider | ||
prefetchedData={plasmicData} | ||
pageParams={compMeta.params} | ||
> | ||
<PlasmicComponent | ||
component={compMeta.displayName} | ||
componentProps={{ | ||
metadata, | ||
}} | ||
/> | ||
</PlasmicClientRootProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { notFound } from "next/navigation"; | ||
import { cache } from "react"; | ||
import { PlasmicComponent } from "@plasmicapp/loader-nextjs"; | ||
import { PLASMIC } from "../../plasmic-init"; | ||
import { PlasmicClientRootProvider } from "../../plasmic-init-client"; | ||
import { logger } from "../../lib/logger"; | ||
|
||
const PLASMIC_COMPONENT = "MetricPage"; | ||
export const dynamic = "force-static"; | ||
export const revalidate = false; // 3600 = 1 hour | ||
|
||
const cachedFetchComponent = cache(async (componentName: string) => { | ||
try { | ||
const plasmicData = await PLASMIC.fetchComponentData(componentName); | ||
return plasmicData; | ||
} catch (e) { | ||
logger.warn(e); | ||
return null; | ||
} | ||
}); | ||
|
||
export default async function MetricPage() { | ||
// Get Plasmic component | ||
const plasmicData = await cachedFetchComponent(PLASMIC_COMPONENT); | ||
if (!plasmicData) { | ||
logger.warn(`Unable to get componentName=${PLASMIC_COMPONENT}`); | ||
notFound(); | ||
} | ||
const compMeta = plasmicData.entryCompMetas[0]; | ||
|
||
return ( | ||
<PlasmicClientRootProvider | ||
prefetchedData={plasmicData} | ||
pageParams={compMeta.params} | ||
> | ||
<PlasmicComponent component={compMeta.displayName} /> | ||
</PlasmicClientRootProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters