Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add total counts to start page #215

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions web/app/data/author.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { Tables } from "./supabase.types.generated";
import TTLCache from "@isaacs/ttlcache";
import { format, hoursToMilliseconds } from "date-fns";

type CacheKey = "sitemap-items";
type CacheKey = "sitemap-items" | "count-authors";

type CacheValue = SitemapItem[];
type CacheValue = SitemapItem[] | number;

export class AuthorService {
private static cache = new TTLCache<CacheKey, CacheValue>({
Expand Down Expand Up @@ -154,7 +154,7 @@ export class AuthorService {
* @returns
*/
static async getAllSitemapAuthors(): Promise<SitemapItem[]> {
const cached = this.cache.get("sitemap-items");
const cached = this.cache.get("sitemap-items") as SitemapItem[] | undefined;
if (cached) {
return cached;
}
Expand Down Expand Up @@ -196,6 +196,25 @@ export class AuthorService {
return sitemapItems;
}

static async getTotalAuthorsCount(): Promise<number> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider consolidating this with the existing count utility in PackageService by creating a shared function that takes the table name as a parameter.

const cached = this.cache.get("count-authors") as number | undefined;
if (cached) {
return cached;
}

const { count, error } = await supabase
.from("authors")
.select("id", { count: "exact", head: true });

if (error) {
slog.error("Error in getAuthorsCount", error);
return 0;
}

this.cache.set("count-authors", count ?? 0);
return count ?? 0;
}

/**
*
* @param query
Expand Down
25 changes: 22 additions & 3 deletions web/app/data/package.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { embed, generateText } from "ai";

type Package = Tables<"cran_packages">;

type CacheKey = "sitemap-items";
type CacheKey = "sitemap-items" | "count-packages";

type CacheValue = SitemapItem[];
type CacheValue = SitemapItem[] | number;

type SearchResult = {
combined: Array<{
Expand Down Expand Up @@ -80,6 +80,25 @@ export class PackageService {
return data?.id || null;
}

static async getTotalPackagesCount(): Promise<number> {
const cached = this.cache.get("count-packages") as number | undefined;
if (cached) {
return cached;
}

const { count, error } = await supabase
.from("cran_packages")
.select("*", { count: "exact", head: true });

if (error) {
slog.error("Error in getTotalPackagesCount", error);
return 0;
}

this.cache.set("count-packages", count ?? 0);
return count ?? 0;
}

static async getPackageRelationsByPackageId(packageId: number) {
packageIdSchema.parse(packageId);

Expand Down Expand Up @@ -141,7 +160,7 @@ export class PackageService {
}

static async getAllSitemapPackages(): Promise<SitemapItem[]> {
const cached = this.cache.get("sitemap-items");
const cached = this.cache.get("sitemap-items") as SitemapItem[] | undefined;
if (cached) {
return cached;
}
Expand Down
26 changes: 23 additions & 3 deletions web/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { clsx } from "clsx";
import { Footer } from "../modules/footer";
import { ENV } from "../data/env";
import { ClientOnly } from "remix-utils/client-only";
import { PackageService } from "../data/package.service";
import { AuthorService } from "../data/author.service";

export const handle = {
hasFooter: false,
Expand All @@ -14,8 +16,24 @@ export const handle = {
export const loader = async () => {
const meshIndex = randomInt(0, 27);
const version = ENV.npm_package_version;

const [packageRes, authorRes] = await Promise.allSettled([
PackageService.getTotalPackagesCount(),
AuthorService.getTotalAuthorsCount(),
]);

const packageCount = packageRes.status === "fulfilled" ? packageRes.value : 0;
const authorCount = authorRes.status === "fulfilled" ? authorRes.value : 0;

return data(
{ meshIndex, version },
{
meshIndex,
version,
// We're using the server here for formatting numbers
// to avoid client-side rehydration issues.
packageCount: Intl.NumberFormat().format(packageCount),
authorCount: Intl.NumberFormat().format(authorCount),
},
{
headers: {
"Cache-Control": `public, s-maxage=10`,
Expand All @@ -25,7 +43,8 @@ export const loader = async () => {
};

export default function Index() {
const { meshIndex, version } = useLoaderData<typeof loader>();
const { meshIndex, version, packageCount, authorCount } =
useLoaderData<typeof loader>();

return (
<>
Expand All @@ -43,7 +62,8 @@ export default function Index() {
</h1>
</div>
<p className="text-gray-dim text-lg font-light md:text-xl xl:text-2xl">
Search for R packages and authors hosted on CRAN
Search for {packageCount} R packages and {authorCount} authors
hosted on CRAN
</p>
<div className="text-gray-dim mt-16 space-y-2">
<p className="animate-fade">
Expand Down