-
Notifications
You must be signed in to change notification settings - Fork 27
feat: Auto update algolia index #488
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
48f7f9a
af08200
14cd92e
c92ab90
49ccbe1
5e355fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| name: Script - upserts course information to Algolia | ||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| environment: | ||
| description: Application environment to execute in. | ||
| required: true | ||
| term: | ||
| description: Academic term to execute the script with. ie 202009, 202001 | ||
| required: true | ||
| jobs: | ||
| lint: | ||
| name: Lint | ||
| runs-on: ubuntu-latest | ||
| defaults: | ||
| run: | ||
| working-directory: ./functions | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: 14 | ||
| - name: Cache npm dependencies | ||
| uses: actions/cache@v2 | ||
| with: | ||
| key: npm-${{ hashFiles('package-lock.json') }} | ||
| path: ~/.npm | ||
| restore-keys: | | ||
| npm- | ||
| - name: Install dependencies | ||
| run: npm ci --ignore-scripts --no-audit --no-progress | ||
| - name: Lint | ||
| run: npm run lint | ||
| update-algolia: | ||
| name: Update Algolia index | ||
| needs: [lint] | ||
| runs-on: ubuntu-latest | ||
| environment: ${{ github.event.inputs.environment }} | ||
| defaults: | ||
| run: | ||
| working-directory: ./functions | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: 14 | ||
| - name: Cache npm dependencies | ||
| uses: actions/cache@v2 | ||
| with: | ||
| key: npm-${{ hashFiles('package-lock.json') }} | ||
| path: ~/.npm | ||
| restore-keys: | | ||
| npm- | ||
| - name: Install dependencies | ||
| run: npm ci --ignore-scripts --no-audit --no-progress | ||
| - name: Set up Cloud SDK | ||
| uses: google-github-actions/setup-gcloud@v0 | ||
| with: | ||
| service_account_key: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} | ||
| export_default_credentials: true | ||
|
szeckirjr marked this conversation as resolved.
Outdated
|
||
| - name: Run Update Algolia Script | ||
| run: npx ts-node ./scripts/update-algolia-index.ts ${{ github.event.inputs.term }} | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import fetch from 'node-fetch'; | ||
| import algoliasearch from 'algoliasearch'; | ||
|
|
||
| if (process.argv.length != 3) throw Error('Term argument not found.'); | ||
|
|
||
| const term: string = process.argv[2]; | ||
|
|
||
| if (!/20\d{2}0[1,5,9]/.test(term.trim())) | ||
| throw Error('Invalid term argument format'); | ||
|
|
||
| const BASE = 'https://courseup.vikelabs.ca/api'; | ||
|
|
||
| const client = algoliasearch('ALGOLIA_APP_ID', 'ALGOLIA_ADMIN_KEY'); | ||
| const index = client.initIndex('ALGOLIA_INDEX_NAME'); | ||
|
szeckirjr marked this conversation as resolved.
Outdated
|
||
|
|
||
| interface Course { | ||
| subject: string; | ||
| code: string; | ||
| pid: string; | ||
| description: string; | ||
| title: string; | ||
| } | ||
|
|
||
| const getCourses = async (term: string): Promise<Course[]> => { | ||
| const res = await fetch(`${BASE}/courses/${term}`); | ||
| const data: Course[] = await res.json(); | ||
| return data; | ||
| }; | ||
|
|
||
| const getCourse = async (term: string, course: Course): Promise<Course> => { | ||
| const res = await fetch( | ||
| `${BASE}/courses/${term}/${course.subject}/${course.code}` | ||
| ); | ||
| const data: Course = await res.json(); | ||
| return data; | ||
| }; | ||
|
|
||
| const main = async () => { | ||
| const courses: Course[] = await getCourses(term); | ||
|
|
||
| console.log(`Found ${courses.length} courses for ${term}`); | ||
|
|
||
| const values = courses.values(); | ||
|
|
||
| const vals: Course[] = []; | ||
|
|
||
| const workers = Array(50) | ||
| .fill(values) | ||
| .map(async (iterator) => { | ||
| for (const course of iterator) { | ||
| const courseDetails = await getCourse(term, course); | ||
| console.log(course.subject, course.code); | ||
| vals.push({ ...courseDetails }); | ||
| } | ||
| }); | ||
|
|
||
| await Promise.allSettled(workers); | ||
|
|
||
| const coursesForIndex = vals.map((course) => ({ | ||
| objectID: `${course.subject}${course.code}`, | ||
| subject: course.subject, | ||
| code: course.code, | ||
|
Comment on lines
+69
to
+70
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a field that's just the concatenation like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and re: synonyms for courses, are you thinking a sort of look up table with pre defined synonyms for the most common stuff? |
||
| pid: course.pid, | ||
| description: course.description, | ||
| title: course.title, | ||
| })); | ||
|
|
||
| index | ||
| .saveObjects(coursesForIndex) | ||
| .then(({ objectIDs }) => { | ||
| console.log(objectIDs); | ||
| }) | ||
| .catch((err) => { | ||
| console.log(err); | ||
| }); | ||
|
Comment on lines
+76
to
+83
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you going to update a single index by clearing it and then adding new data or doing this? iirc if you say run this twice in succession you'll have duplicate records in the index. I think the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the plan for stale courses? ie. courses that don't exist anymore cause this will make it so some will linger.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep you're right! I ran this twice and I don't get duplicate records because of the but hmmm that's a good point 🤔 wiping it clear and then populating might be good, and switching to |
||
| }; | ||
|
|
||
| main(); | ||
Uh oh!
There was an error while loading. Please reload this page.