Skip to content

Commit

Permalink
feat: update algolia indexes with a local script (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito authored Jan 11, 2024
1 parent 124352d commit c59e0bc
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 16 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/algolia.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Update Algolia Index

on: [workflow_dispatch]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v3

- name: Enable PNPM
uses: pnpm/action-setup@v2

- name: Set node version to 20
uses: actions/setup-node@v3
with:
node-version: "20"

- name: Install
run: pnpm install --frozen-lockfile

- name: Update Algolia Index
run: pnpm run algolia
env:
ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }}
12 changes: 4 additions & 8 deletions .github/workflows/cron-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Trigger DB updates
uses: fjogeleit/http-request-action@v1
with:
url: "https://fontsource.org/actions/update"
method: "POST"
bearerToken: ${{ secrets.WEBSITE_UPDATE_TOKEN }}
data: '{"fonts": true, "algolia": true, "download": true, "axisRegistry": true, "docs": true}'
preventFailureOnNoResponse: true
- name: Update Algolia Index
run: pnpm run algolia
env:
ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }}
12 changes: 4 additions & 8 deletions .github/workflows/manual-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Trigger DB updates
uses: fjogeleit/http-request-action@v1
with:
url: "https://fontsource.org/actions/update"
method: "POST"
bearerToken: ${{ secrets.WEBSITE_UPDATE_TOKEN }}
data: '{"fonts": true, "algolia": true, "download": true, "axisRegistry": true, "docs": true}'
preventFailureOnNoResponse: true
- name: Update Algolia Index
run: pnpm run algolia
env:
ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"packageManager": "[email protected]",
"private": true,
"scripts": {
"algolia": "tsx scripts/algolia.ts",
"check-duplicates": "tsx scripts/check-duplicates.ts",
"fontlist": "tsx scripts/fontlist.ts",
"gfm-metadata": "tsx scripts/gfm-metadata.ts",
Expand All @@ -18,6 +19,7 @@
"@fontsource-utils/cli": "0.4.1",
"@fontsource-utils/publish": "^0.2.8",
"@types/node": "^20.5.7",
"algoliasearch": "^4.22.1",
"consola": "^3.2.3",
"google-font-metadata": "^5.2.1",
"json-stringify-pretty-compact": "^4.0.0",
Expand Down
112 changes: 112 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions scripts/algolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import algoliasearch from 'algoliasearch';
import metadataImport from '../metadata/fontsource.json';

interface AlgoliaMetadata {
objectID: string;
family: string;
subsets: string[];
weights: number[];
styles: string[];
defSubset: string;
category: string;
variable: boolean;
lastModified: number;
downloadMonth: number;
randomIndex: number;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const client = algoliasearch('WNATE69PVR', process.env.ALGOLIA_ADMIN_KEY!);

const shuffleArray = (size: number) => {
// Generate array of numbers from 0 to size
const arr: number[] = [...Array.from({ length: size }).keys()];

// Durstenfeld shuffle to randomly sort array
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
};

const updateAlgoliaIndex = async (force?: boolean) => {
try {
// Get font list
const list = Object.keys(metadataImport);
const indexArray: AlgoliaMetadata[] = [];

// For the random shuffle, we need a presorted index
// as Algolia does not support random sorting natively
const randomIndexArr = shuffleArray(list.length);

let index = 0;
for (const id of list) {
const metadata = metadataImport[id];
if (!metadata)
console.warn(`No metadata found for ${id} when updating Algolia index`);

const stats = (
await fetch(`https://api.fontsource.org/v1/stats/${id}`)
).json() as any;
const downloadCountMonthly = stats?.total?.npmDownloadMonthly;

const obj = {
objectID: id,
family: metadata.family,
subsets: metadata.subsets,
weights: metadata.weights,
styles: metadata.styles,
category: metadata.category,
defSubset: metadata.defSubset,
variable: metadata.variable,
// Algolia sorts date using a unix timestamp instead
lastModified: Math.floor(
new Date(metadata.lastModified).getTime() / 1000
),
downloadMonth: downloadCountMonthly ?? 0,
randomIndex: randomIndexArr[index],
};

indexArray.push(obj);
index++;
}

const searchIndex = client.initIndex('prod_NAME');
if (force) {
await searchIndex.replaceAllObjects(indexArray);
console.log('Replaced Algolia index');
} else {
await searchIndex.saveObjects(indexArray);
console.log('Updated Algolia index');
}
} catch (error) {
console.error(error);
}
};

updateAlgoliaIndex();

0 comments on commit c59e0bc

Please sign in to comment.