1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 17:06:37 -04:00

move some cruft from serverless API to client

This commit is contained in:
2021-07-09 14:40:23 -04:00
parent b411560a34
commit 523670fa22
9 changed files with 26 additions and 65 deletions

View File

@ -5,9 +5,6 @@ import * as Tracing from "@sentry/tracing"; // eslint-disable-line @typescript-e
import { VercelRequest, VercelResponse } from "@vercel/node";
import { Client, query as q } from "faunadb";
import fetch from "node-fetch";
import pluralize from "pluralize";
import numeral from "numeral";
import { DateTime } from "luxon";
import parser from "fast-xml-parser";
import { decode } from "html-entities";
@ -68,7 +65,7 @@ export default async (req: VercelRequest, res: VercelResponse) => {
}
};
const incrementPageHits = async (slug: string | string[], client: Client) => {
const incrementPageHits = async (slug: string | string[], client: Client): Promise<PageStats> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await client.query<any>(
q.Let(
@ -87,18 +84,11 @@ const incrementPageHits = async (slug: string | string[], client: Client) => {
)
);
// add formatted hits with comma and pluralized "hit(s)", simpler to do here than in browser
const hits: PageStats = {
...result.data,
pretty_hits: numeral(result.data.hits).format("0,0"),
pretty_unit: pluralize("hit", result.data.hits),
};
// send client the *new* hit count
return hits;
return result.data;
};
const getSiteStats = async (client: Client) => {
const getSiteStats = async (client: Client): Promise<OverallStats> => {
// get database and RSS results asynchronously
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const [feed, result] = await Promise.all<{ [key: string]: any }, any>([
@ -123,13 +113,9 @@ const getSiteStats = async (client: Client) => {
if (match) {
p.title = decode(match.title);
p.url = match.link;
p.date = DateTime.fromRFC2822(match.pubDate).toISO();
p.date = new Date(match.pubDate).toISOString();
}
// it's easier to add comma-separated numbers and proper pluralization here on the backend
p.pretty_hits = numeral(p.hits).format("0,0");
p.pretty_unit = pluralize("hit", p.hits);
// add these hits to running tally
stats.total.hits += p.hits;
@ -141,9 +127,5 @@ const getSiteStats = async (client: Client) => {
return a.hits > b.hits ? -1 : 1;
});
// do same prettification as above to totals
stats.total.pretty_hits = numeral(stats.total.hits).format("0,0");
stats.total.pretty_unit = pluralize("hit", stats.total.hits);
return stats;
};

View File

@ -4,8 +4,6 @@ import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { VercelRequest, VercelResponse } from "@vercel/node";
import { encode } from "html-entities";
import { DateTime } from "luxon";
import numeral from "numeral";
import { GraphQLClient } from "graphql-request";
import { gql } from "graphql-tag";
@ -101,9 +99,6 @@ const fetchRepos = async (sort: string, limit: number): Promise<Repository[]> =>
({ node: repo }: { [key: string]: Repository }) => ({
...repo,
description: encode(repo.description),
stargazerCount_pretty: numeral(repo.stargazerCount).format("0,0"),
forkCount_pretty: numeral(repo.forkCount).format("0,0"),
pushedAt_relative: DateTime.fromISO(repo.pushedAt).toRelative({ locale: "en" }),
})
);

4
api/types/hits.d.ts vendored
View File

@ -4,15 +4,11 @@ type PageStats = {
date?: string;
slug: string;
hits: number;
pretty_hits: string;
pretty_unit: string;
};
type OverallStats = {
total: {
hits: number;
pretty_hits?: string;
pretty_unit?: string;
};
pages: PageStats[];
};

View File

@ -7,9 +7,6 @@ type Repository = {
name: string;
};
stargazerCount: number;
stargazerCount_pretty?: string;
forkCount: number;
forkCount_pretty?: string;
pushedAt: string;
pushedAt_relative?: string;
};