mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-03 17:26:37 -04:00
merge /api/hits and /api/stats
This commit is contained in:
174
api/hits.ts
174
api/hits.ts
@ -4,11 +4,29 @@ import { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
import { Client, query as q } from "faunadb";
|
||||
import numeral from "numeral";
|
||||
import pluralize from "pluralize";
|
||||
import rssParser from "rss-parser";
|
||||
|
||||
const baseUrl = "https://jarv.is/";
|
||||
type PageStats = {
|
||||
title?: string;
|
||||
url?: string;
|
||||
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[];
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
const { slug } = req.query;
|
||||
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (!process.env.FAUNADB_SERVER_SECRET) {
|
||||
@ -17,40 +35,36 @@ export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!slug || slug === "/") {
|
||||
throw new Error("Parameter `slug` is required.");
|
||||
}
|
||||
|
||||
const client = new Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
type PageHits = {
|
||||
slug: string;
|
||||
hits: number;
|
||||
pretty_hits?: string;
|
||||
pretty_unit?: string;
|
||||
};
|
||||
|
||||
// refer to snippet below for the `hit` function defined in the Fauna cloud
|
||||
const { slug } = req.query;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const result = await client.query<any>(q.Call(q.Function("hit"), slug));
|
||||
let result: any;
|
||||
|
||||
const hits: PageHits = {
|
||||
...result.data,
|
||||
pretty_hits: numeral(result.data.hits).format("0,0"),
|
||||
pretty_unit: pluralize("hit", result.data.hits),
|
||||
};
|
||||
if (!slug || slug === "/") {
|
||||
// return overall site stats if slug not specified
|
||||
result = await getSiteStats(client);
|
||||
|
||||
// let Vercel edge and browser cache results for 15 mins
|
||||
res.setHeader("Cache-Control", "public, max-age=900, s-maxage=900, stale-while-revalidate");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
} else {
|
||||
// increment this page's hits
|
||||
result = await incrementPageHits(slug, client);
|
||||
|
||||
// disable caching on both ends
|
||||
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
||||
res.setHeader("Expires", 0);
|
||||
res.setHeader("Pragma", "no-cache");
|
||||
}
|
||||
|
||||
// disable caching on both ends
|
||||
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
||||
res.setHeader("Expires", 0);
|
||||
res.setHeader("Pragma", "no-cache");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
// send client the *new* hit count
|
||||
res.status(200).json(hits);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
@ -58,34 +72,86 @@ export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
This is the FaunaDB function named `hit` defined in the cloud:
|
||||
https://dashboard.fauna.com/functions/hit/@db/global/jarv.is
|
||||
https://docs.fauna.com/fauna/current/api/fql/user_defined_functions
|
||||
|
||||
{
|
||||
name: "hit",
|
||||
role: null,
|
||||
body: Query(
|
||||
Lambda(
|
||||
"slug",
|
||||
Let(
|
||||
{ match: Match(Index("hits_by_slug"), Var("slug")) },
|
||||
If(
|
||||
Exists(Var("match")),
|
||||
Let(
|
||||
{
|
||||
ref: Select("ref", Get(Var("match"))),
|
||||
hits: ToInteger(Select("hits", Select("data", Get(Var("match")))))
|
||||
},
|
||||
Update(Var("ref"), { data: { hits: Add(Var("hits"), 1) } })
|
||||
),
|
||||
Create(Collection("hits"), { data: { slug: Var("slug"), hits: 1 } })
|
||||
)
|
||||
const incrementPageHits = async (slug: string | string[], client: Client) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const result = await client.query<any>(
|
||||
q.Let(
|
||||
{ match: q.Match(q.Index("hits_by_slug"), slug) },
|
||||
q.If(
|
||||
q.Exists(q.Var("match")),
|
||||
q.Let(
|
||||
{
|
||||
ref: q.Select("ref", q.Get(q.Var("match"))),
|
||||
hits: q.ToInteger(q.Select("hits", q.Select("data", q.Get(q.Var("match"))))),
|
||||
},
|
||||
q.Update(q.Var("ref"), { data: { hits: q.Add(q.Var("hits"), 1) } })
|
||||
),
|
||||
q.Create(q.Collection("hits"), { data: { slug: slug, hits: 1 } })
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
*/
|
||||
// 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;
|
||||
};
|
||||
|
||||
const getSiteStats = async (client: Client) => {
|
||||
const parser = new rssParser({
|
||||
timeout: 3000,
|
||||
});
|
||||
|
||||
// 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>([
|
||||
parser.parseURL(baseUrl + "feed.xml"),
|
||||
client.query(
|
||||
q.Map(
|
||||
q.Paginate(q.Documents(q.Collection("hits"))),
|
||||
q.Lambda((x) => q.Select("data", q.Get(x)))
|
||||
)
|
||||
),
|
||||
]);
|
||||
|
||||
const pages: PageStats[] = result.data;
|
||||
const stats: OverallStats = {
|
||||
total: { hits: 0 },
|
||||
pages,
|
||||
};
|
||||
|
||||
pages.map((p: PageStats) => {
|
||||
// match URLs from RSS feed with db to populate some metadata
|
||||
const match = feed.items.find((x: { link: string }) => x.link === baseUrl + p.slug + "/");
|
||||
if (match) {
|
||||
p.title = match.title;
|
||||
p.url = match.link;
|
||||
p.date = match.isoDate;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
return p;
|
||||
});
|
||||
|
||||
// sort by hits (descending)
|
||||
stats.pages.sort((a: { hits: number }, b: { hits: number }) => {
|
||||
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;
|
||||
};
|
||||
|
@ -9,8 +9,54 @@ import { gql } from "graphql-tag";
|
||||
|
||||
const username = "jakejarvis";
|
||||
const endpoint = "https://api.github.com/graphql";
|
||||
type Repository = {
|
||||
name: string;
|
||||
url: string;
|
||||
description: string;
|
||||
primaryLanguage?: {
|
||||
color: string;
|
||||
name: string;
|
||||
};
|
||||
stargazerCount: number;
|
||||
stargazerCount_pretty?: string;
|
||||
forkCount: number;
|
||||
forkCount_pretty?: string;
|
||||
pushedAt: string;
|
||||
pushedAt_relative?: string;
|
||||
};
|
||||
|
||||
async function fetchRepos(sort: string, limit: number) {
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!process.env.GH_PUBLIC_TOKEN) {
|
||||
throw new Error("GitHub API credentials aren't set.");
|
||||
}
|
||||
|
||||
// default to latest repos
|
||||
let sortBy = "PUSHED_AT";
|
||||
// get most popular repos (/projects/?top)
|
||||
if (typeof req.query.top !== "undefined") sortBy = "STARGAZERS";
|
||||
|
||||
const repos = await fetchRepos(sortBy, 16);
|
||||
|
||||
// let Vercel edge and browser cache results for 15 mins
|
||||
res.setHeader("Cache-Control", "public, max-age=900, s-maxage=900, stale-while-revalidate");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
res.status(200).json(repos);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
const fetchRepos = async (sort: string, limit: number): Promise<Repository[]> => {
|
||||
// https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
|
||||
const client = new GraphQLClient(endpoint, {
|
||||
headers: {
|
||||
@ -53,22 +99,6 @@ async function fetchRepos(sort: string, limit: number) {
|
||||
}
|
||||
`;
|
||||
|
||||
type Repository = {
|
||||
name: string;
|
||||
url: string;
|
||||
description: string;
|
||||
primaryLanguage?: {
|
||||
color: string;
|
||||
name: string;
|
||||
};
|
||||
stargazerCount: number;
|
||||
stargazerCount_pretty?: string;
|
||||
forkCount: number;
|
||||
forkCount_pretty?: string;
|
||||
pushedAt: string;
|
||||
pushedAt_relative?: string;
|
||||
};
|
||||
|
||||
const response = await client.request(query, { sort, limit });
|
||||
const currentRepos: Repository[] = response.user.repositories.edges.map(
|
||||
({ node: repo }: { [key: string]: Repository }) => ({
|
||||
@ -81,35 +111,4 @@ async function fetchRepos(sort: string, limit: number) {
|
||||
);
|
||||
|
||||
return currentRepos;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!process.env.GH_PUBLIC_TOKEN) {
|
||||
throw new Error("GitHub API credentials aren't set.");
|
||||
}
|
||||
|
||||
// default to latest repos
|
||||
let sortBy = "PUSHED_AT";
|
||||
// get most popular repos (/projects/?top)
|
||||
if (typeof req.query.top !== "undefined") sortBy = "STARGAZERS";
|
||||
|
||||
const repos = await fetchRepos(sortBy, 16);
|
||||
|
||||
// let Vercel edge cache results for 15 mins
|
||||
res.setHeader("Cache-Control", "s-maxage=900, stale-while-revalidate");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
res.status(200).json(repos);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
105
api/stats.ts
105
api/stats.ts
@ -1,105 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
import { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
import { Client, query as q } from "faunadb";
|
||||
import numeral from "numeral";
|
||||
import pluralize from "pluralize";
|
||||
import rssParser from "rss-parser";
|
||||
|
||||
const baseUrl = "https://jarv.is/";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (!process.env.FAUNADB_SERVER_SECRET) {
|
||||
throw new Error("Database credentials aren't set.");
|
||||
}
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
|
||||
const parser = new rssParser({
|
||||
timeout: 3000,
|
||||
});
|
||||
const client = new Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
// 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>([
|
||||
parser.parseURL(baseUrl + "feed.xml"),
|
||||
client.query(
|
||||
q.Map(
|
||||
q.Paginate(q.Documents(q.Collection("hits"))),
|
||||
q.Lambda((x) => q.Select("data", q.Get(x)))
|
||||
)
|
||||
),
|
||||
]);
|
||||
|
||||
type PageStats = {
|
||||
title: string;
|
||||
url: string;
|
||||
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[];
|
||||
};
|
||||
|
||||
const pages: PageStats[] = result.data;
|
||||
const stats: OverallStats = {
|
||||
total: { hits: 0 },
|
||||
pages,
|
||||
};
|
||||
|
||||
pages.map((p: PageStats) => {
|
||||
// match URLs from RSS feed with db to populate some metadata
|
||||
const match = feed.items.find((x: { link: string }) => x.link === baseUrl + p.slug + "/");
|
||||
if (match) {
|
||||
p.title = match.title;
|
||||
p.url = match.link;
|
||||
p.date = match.isoDate;
|
||||
delete p.slug;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
return p;
|
||||
});
|
||||
|
||||
// sort by hits (descending)
|
||||
stats.pages.sort((a: { hits: number }, b: { hits: number }) => {
|
||||
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);
|
||||
|
||||
// let Vercel edge cache results for 15 mins
|
||||
res.setHeader("Cache-Control", "s-maxage=900, stale-while-revalidate");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
res.status(200).json(stats);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
@ -1,5 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
// Fetches my Spotify most-played tracks or currently playing track.
|
||||
// Heavily inspired by @leerob: https://leerob.io/snippets/spotify
|
||||
|
||||
import { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
@ -32,7 +33,6 @@ type TrackSchema = {
|
||||
spotify: string;
|
||||
};
|
||||
};
|
||||
|
||||
type Track = {
|
||||
isPlaying: boolean;
|
||||
artist?: string;
|
||||
@ -42,6 +42,43 @@ type Track = {
|
||||
songUrl?: string;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!process.env.SPOTIFY_CLIENT_ID || !process.env.SPOTIFY_CLIENT_SECRET || !process.env.SPOTIFY_REFRESH_TOKEN) {
|
||||
throw new Error("Spotify API credentials aren't set.");
|
||||
}
|
||||
|
||||
// default to top tracks
|
||||
let response;
|
||||
// get currently playing track (/music/?now), otherwise top 10 tracks
|
||||
if (typeof req.query.now !== "undefined") {
|
||||
response = await getNowPlaying();
|
||||
|
||||
// let Vercel edge and browser cache results for 5 mins
|
||||
res.setHeader("Cache-Control", "public, max-age=300, s-maxage=300, stale-while-revalidate");
|
||||
} else {
|
||||
response = await getTopTracks();
|
||||
|
||||
// let Vercel edge and browser cache results for 3 hours
|
||||
res.setHeader("Cache-Control", "public, max-age=10800, s-maxage=10800, stale-while-revalidate");
|
||||
}
|
||||
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
res.status(200).json(response);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
const getAccessToken = async () => {
|
||||
const response = await fetch(TOKEN_ENDPOINT, {
|
||||
method: "POST",
|
||||
@ -124,40 +161,3 @@ const getTopTracks = async (): Promise<Track[]> => {
|
||||
|
||||
return tracks;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!process.env.SPOTIFY_CLIENT_ID || !process.env.SPOTIFY_CLIENT_SECRET || !process.env.SPOTIFY_REFRESH_TOKEN) {
|
||||
throw new Error("Spotify API credentials aren't set.");
|
||||
}
|
||||
|
||||
// default to top tracks
|
||||
let response;
|
||||
// get currently playing track (/music/?now), otherwise top 10 tracks
|
||||
if (typeof req.query.now !== "undefined") {
|
||||
response = await getNowPlaying();
|
||||
|
||||
// let Vercel edge cache results for 5 mins
|
||||
res.setHeader("Cache-Control", "public, s-maxage=300, stale-while-revalidate");
|
||||
} else {
|
||||
response = await getTopTracks();
|
||||
|
||||
// let Vercel edge cache results for 3 hours
|
||||
res.setHeader("Cache-Control", "public, s-maxage=10800, stale-while-revalidate");
|
||||
}
|
||||
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
res.status(200).json(response);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user