Quellcode durchsuchen

refactor: cleanup

Alexandru Cambose vor 3 Monaten
Ursprung
Commit
9e29e54f3c

+ 6 - 3
apps/insights/src/app/api/pyth/get-feeds-for-publisher/[publisher]/route.ts

@@ -1,22 +1,25 @@
 import { NextResponse } from "next/server";
 import { stringify } from "superjson";
 
-import { getFeeds } from '../../../../../server/pyth/get-feeds';
+import { getFeedsCached } from '../../../../../server/pyth/get-feeds';
 import { Cluster } from "../../../../../services/pyth";
 
 export const GET = async (request: Request, { params }: { params: Promise<{ publisher: string }> }) => {
   const { publisher } = await params;
-  // get cluster from query params
   const { searchParams } = new URL(request.url);
   const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
+
   // check if cluster is valid
   if (cluster && !Object.values(Cluster).includes(cluster)) {
     return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
   }
+
   if (!publisher) {
     return NextResponse.json({ error: "Publisher is required" }, { status: 400 });
   } 
-  const feeds = await getFeeds(cluster);
+
+  const feeds = await getFeedsCached(cluster);
+
   const filteredFeeds = feeds.filter((feed) => feed.price.priceComponents.some((c) => c.publisher === publisher));
   
   return new Response(stringify(filteredFeeds), {

+ 3 - 0
apps/insights/src/app/api/pyth/get-feeds/[symbol]/route.ts

@@ -8,12 +8,15 @@ export const GET = async (request: Request, { params }: { params: Promise<{ symb
   const { symbol } = await params;
   const { searchParams } = new URL(request.url);
   const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
+
   // check if cluster is valid
   if (cluster && !Object.values(Cluster).includes(cluster)) {
     return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
   }
+  
   const feeds = await getFeedsCached(cluster);
   const feed = feeds.find((feed) => feed.symbol === symbol);
+  
   return new Response(stringify(feed), {
     headers: {
       'Content-Type': 'application/json',

+ 3 - 0
apps/insights/src/app/api/pyth/get-feeds/route.ts

@@ -10,18 +10,21 @@ export const GET = async (request: Request) => {
   const { searchParams } = new URL(request.url);
   const excludePriceComponents = searchParams.get("excludePriceComponents") === "true";
   const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
+
   // check if cluster is valid
   if (cluster && !Object.values(Cluster).includes(cluster)) {
     return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
   }
 
   let feeds = await getFeedsCached(cluster) as Omit<z.infer<typeof priceFeedsSchema>[number], 'price'>[];
+  
   if(excludePriceComponents) {
     feeds = feeds.map((feed) => ({
       ...feed,
       price: undefined,
     }));
   }
+  
   return new Response(stringify(feeds), {
     headers: {
       'Content-Type': 'application/json',

+ 4 - 0
apps/insights/src/app/api/pyth/get-publishers/[symbol]/route.ts

@@ -8,13 +8,17 @@ export const GET = async (request: Request, { params }: { params: Promise<{ symb
   // get cluster from query params
   const { searchParams } = new URL(request.url);
   const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
+
   // check if cluster is valid
   if (cluster && !Object.values(Cluster).includes(cluster)) {
     return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
   }
+
   if (!symbol) {
     return NextResponse.json({ error: "Symbol is required" }, { status: 400 });
   }
+
   const map = await getPublishersForCluster(cluster);
+  
   return NextResponse.json(map[symbol] ?? []);
 };

+ 2 - 2
apps/insights/src/app/price-feeds/[slug]/layout.ts

@@ -2,7 +2,7 @@ import type { Metadata } from "next";
 import { notFound } from "next/navigation";
 import type { ReactNode } from "react";
 
-import { getFeeds } from "../../../server/pyth/get-feeds";
+import { getFeedsCached } from "../../../server/pyth/get-feeds";
 import { Cluster } from "../../../services/pyth";
 
 export { PriceFeedLayout as default } from "../../../components/PriceFeed/layout";
@@ -20,7 +20,7 @@ export const generateMetadata = async ({
 }: Props): Promise<Metadata> => {
   const [{ slug }, feeds] = await Promise.all([
     params,
-    getFeeds(Cluster.Pythnet),
+    getFeedsCached(Cluster.Pythnet),
   ]);
   const symbol = decodeURIComponent(slug);
   const feed = feeds.find((item) => item.symbol === symbol);

+ 4 - 4
apps/insights/src/components/Overview/index.tsx

@@ -9,7 +9,7 @@ import PriceFeedsLight from "./price-feeds-light.svg";
 import PublishersDark from "./publishers-dark.svg";
 import PublishersLight from "./publishers-light.svg";
 import { TabList } from "./tab-list";
-import { getFeeds } from "../../server/pyth/get-feeds";
+import { getFeedsCached } from "../../server/pyth/get-feeds";
 import { Cluster } from "../../services/pyth";
 import {
   totalVolumeTraded,
@@ -24,10 +24,9 @@ import { FormattedDate } from "../FormattedDate";
 import { FormattedNumber } from "../FormattedNumber";
 
 export const Overview = async () => {
-  // eslint-disable-next-line no-console
-  console.log('overview');
-  const priceFeeds = await getFeeds(Cluster.Pythnet);
+  const priceFeeds = await getFeedsCached(Cluster.Pythnet);
   const today = new Date();
+
   const feedCounts = [
     ...activeFeeds.map(({ date, numFeeds }) => ({
       x: date,
@@ -40,6 +39,7 @@ export const Overview = async () => {
       y: priceFeeds.length,
     },
   ];
+
   return (
     <div className={styles.overview}>
       <h1 className={styles.header}>Overview</h1>

+ 1 - 4
apps/insights/src/components/PriceFeed/publishers.tsx

@@ -23,7 +23,6 @@ export const Publishers = async ({ params }: Props) => {
   const { slug } = await params;
   const symbol = decodeURIComponent(slug);
   
-  const start = Date.now();
   const [
     feed,
     testFeed,
@@ -35,9 +34,7 @@ export const Publishers = async ({ params }: Props) => {
     getPublishers(Cluster.Pythnet, symbol),
     getPublishers(Cluster.PythtestConformance, symbol),
   ]);
-  const end = Date.now();
-  // eslint-disable-next-line no-console
-  console.info(`Publishers took ${(end - start).toString()}ms`);
+
   const publishers = [...pythnetPublishers, ...pythtestConformancePublishers];
   const metricsTime = pythnetPublishers.find(
     (publisher) => publisher.ranking !== undefined,

+ 2 - 4
apps/insights/src/components/PriceFeeds/index.tsx

@@ -19,7 +19,7 @@ import { AssetClassTable } from "./asset-class-table";
 import { ComingSoonList } from "./coming-soon-list";
 import styles from "./index.module.scss";
 import { PriceFeedsCard } from "./price-feeds-card";
-import { getFeeds } from "../../server/pyth/get-feeds";
+import { getFeedsCached } from "../../server/pyth/get-feeds";
 import { Cluster } from "../../services/pyth";
 import { priceFeeds as priceFeedsStaticConfig } from "../../static-data/price-feeds";
 import { activeChains } from "../../static-data/stats";
@@ -290,9 +290,7 @@ const FeaturedFeedsCard = <T extends ElementType>({
 );
 
 const getPriceFeeds = async () => {
-  // eslint-disable-next-line no-console
-  console.log('getPriceFeeds');
-  const priceFeeds = await getFeeds(Cluster.Pythnet);
+  const priceFeeds = await getFeedsCached(Cluster.Pythnet);
   const activeFeeds = priceFeeds.filter((feed) => isActive(feed));
   const comingSoon = priceFeeds.filter((feed) => !isActive(feed));
   return { activeFeeds, comingSoon };

+ 2 - 4
apps/insights/src/components/Root/index.tsx

@@ -11,7 +11,7 @@ import {
 } from "../../config/server";
 import { LivePriceDataProvider } from "../../hooks/use-live-price-data";
 import { getPublishersCached } from '../../server/clickhouse';
-import { getFeeds } from '../../server/pyth/get-feeds';
+import { getFeedsCached } from '../../server/pyth/get-feeds';
 import { Cluster } from "../../services/pyth";
 import { PriceFeedIcon } from "../PriceFeedIcon";
 import { PublisherIcon } from "../PublisherIcon";
@@ -75,9 +75,7 @@ const getPublishersForSearchDialog = async (cluster: Cluster) => {
 };
 
 const getFeedsForSearchDialog = async (cluster: Cluster) => {
-  // eslint-disable-next-line no-console
-  console.log('getFeedsForSearchDialog');
-  const feeds = await getFeeds(cluster);
+  const feeds = await getFeedsCached(cluster);
 
   return feeds.map((feed) => ({
     symbol: feed.symbol,

+ 2 - 4
apps/insights/src/config/server.ts

@@ -1,6 +1,6 @@
 // Disable the following rule because this file is the intended place to declare
 // and load all env variables.
-/* eslint-disable n/no-process-env */
+/* eslint-disable n/no-process-env, @typescript-eslint/no-non-null-assertion, turbo/no-undeclared-env-vars */
 
 import { Redis } from 'ioredis';
 import "server-only";
@@ -83,15 +83,13 @@ export function getRedis(): Redis {
 
 export const PUBLIC_URL = (() => {
   if (IS_PRODUCTION_SERVER) {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, turbo/no-undeclared-env-vars
+    
     return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL!}`;
   } else if (IS_PREVIEW_SERVER) {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, turbo/no-undeclared-env-vars
     return `https://${process.env.VERCEL_URL!}`;
   } else {
     return `http://localhost:3003`;
   }
 })();
 
-// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, turbo/no-undeclared-env-vars
 export const VERCEL_AUTOMATION_BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET!;

+ 2 - 2
apps/insights/src/server/clickhouse.ts

@@ -20,13 +20,13 @@ export const getPublishersCached = redisCache.define(
 export const getPublisherAverageScoreHistoryCached = redisCache.define(
   "getPublisherAverageScoreHistory",
   // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-  // @ts-expect-error
+  // @ts-expect-error - The ACD cache lib does not have `define` correctly typed 
   getPublisherAverageScoreHistory,
 ).getPublisherAverageScoreHistory as typeof getPublisherAverageScoreHistory;
 
 export const getPublisherRankingHistoryCached = redisCache.define(
   "getPublisherRankingHistory",
   // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-  // @ts-expect-error
+  // @ts-expect-error - The ACD cache lib does not have `define` correctly typed 
   getPublisherRankingHistory,
 ).getPublisherRankingHistory as typeof getPublisherRankingHistory;

+ 2 - 1
apps/insights/src/server/pyth.ts

@@ -47,7 +47,7 @@ export const getFeedsCached = async (cluster: Cluster) => {
     },
   });
   const dataJson = await data.text();
-  const feeds: z.infer<typeof priceFeedsSchema> = parse(dataJson);
+  const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson);
   return feeds;
 }
 
@@ -64,6 +64,7 @@ export const getFeedForSymbolCached = async ({symbol, cluster = Cluster.Pythnet}
   if(!data.ok) {
     return undefined;
   }
+
   const dataJson = await data.text();
   const feed: z.infer<typeof priceFeedsSchema>[0] = parse(dataJson);
   return feed;

+ 0 - 6
apps/insights/src/server/pyth/get-feeds.ts

@@ -1,5 +1,3 @@
-import { z } from 'zod';
-
 import { getPythMetadata } from './get-metadata';
 import { Cluster, priceFeedsSchema } from "../../services/pyth";
 import { redisCache } from '../../utils/cache';
@@ -33,7 +31,3 @@ export const getFeedsCached = redisCache.define(
   "getFeeds",
   _getFeeds,
 ).getFeeds;
-
-export const getFeeds = async (cluster: Cluster): Promise<z.infer<typeof priceFeedsSchema>> => {
-  return getFeedsCached(cluster);
-};

+ 0 - 1
apps/insights/src/server/pyth/get-metadata.ts

@@ -3,7 +3,6 @@ import { clients, Cluster } from '../../services/pyth';
 import { memoryOnlyCache } from '../../utils/cache';
 
 const _getPythMetadata = async (cluster: Cluster) => {
-  // Fetch fresh data from Pyth client
   return clients[cluster].getData();
 };