From 792b904d4c140ae1fea997bc87eef9fc6f512d46 Mon Sep 17 00:00:00 2001 From: Adam Rensel Date: Mon, 10 May 2021 23:02:17 -0400 Subject: [PATCH] Add airtable caching --- .github/workflows/main.yml | 25 ++++++++- .github/workflows/src/app/airtable.js | 71 ++++++++++++++++++++++++- .github/workflows/src/app/fetch-data.js | 16 ++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/src/app/fetch-data.js diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 86845fd6..f45428ca 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,14 +18,37 @@ jobs: run: shell: bash working-directory: ./.github/workflows/src - name: Initiate robot + name: Getting grades + steps: + - name: Get Minute + id: get-minute + run: | + echo "::set-output name=minute::$(/bin/date -u "+%M")" + shell: bash # For Windows, Linux and macOS default to bash alrea + - name: Cache data + id: cache_data + uses: actions/cache@v2 + with: + path: .github/workflows/src/data + key: ${{ runner.OS }}-${{ steps.get-date.outputs.minute }} # cache for 1 minute + - name: Cache Airtable + if: steps.cache_data.outputs.cache-hit != 'true' + run: node fetch-data.js + name: Checking Qualifications steps: - uses: actions/checkout@v2 - name: Setup node uses: actions/setup-node@v2 with: node-version: 14 + - name: Cache modules + id: cache_modules + uses: actions/cache@v2 + with: + path: node_modules + key: ${{ runner.os }}-${{ hashFiles('package.json') }} - name: Setup npm + if: steps.cache_modules.outputs.cache-hit != 'true' run: npm install -g npm - name: Install dependencies run: npm install diff --git a/.github/workflows/src/app/airtable.js b/.github/workflows/src/app/airtable.js index de058dba..4ea588e8 100644 --- a/.github/workflows/src/app/airtable.js +++ b/.github/workflows/src/app/airtable.js @@ -1,4 +1,5 @@ const Airtable = require('airtable') +const fs = require('fs') Airtable.configure({ endpointUrl: 'https://api.airtable.com', @@ -6,22 +7,90 @@ Airtable.configure({ }); const GITHUB_GRADUATION = "bipux0j8oGEJuReSb" -const GRADUATES_2020 = "Graduates Data" +const GRADUATES_2020 = "Graduation 2020" const GRADUATES_2021 = "Graduation 2021" +let cachedJson +let cachedData = {} + +try { + cachedJson = fs.readFileSync('./app/data/airtable.json"', 'utf8') + cachedData = JSON.parse(json) +} catch(err) { + // noop +} + + class ATable { constructor() { } userParticipated2020(githubLogin) { + const cachedData = fetchFromCache(githubLogin, GRADUATES_2020) + if(cachedData) { + return new Promise(cachedData) + } + return this.fetchGraduate(githubLogin, GRADUATES_2020) } fetch2021Graduate(githubLogin) { + const cachedData = fetchFromCache(githubLogin, GRADUATES_2021) + if(cachedData) { + return new Promise(cachedData) + } + return this.fetchGraduate(githubLogin, GRADUATES_2021) } + async fetchAll(table) { + const airtable = Airtable.base(GITHUB_GRADUATION); + const users = [] + + return airtable(GRADUATES_2020).select().eachPage(function page(records, fetchNextPage) { + // This function (`page`) will get called for each page of records. + + records.forEach(function(record) { + users.push(record.fields) + }); + + // To fetch the next page of records, call `fetchNextPage`. + // If there are more records, `page` will get called again. + // If there are no more records, `done` will get called. + fetchNextPage(); + + }, function done(err) { + if (err) { console.error(err); return; } + }); + } + + fetchAll2021() { + return await this.fetchAll(GRADUATES_2021) + } + + fetchAll2020() { + return await this.fetchAll(GRADUATES_2020) + } + + fetchFromCache(githubLogin, table) { + let data, userData + + if(table === GRADUATES_2020) { + data = cachedData["grad2020"] + } else if(table === GRADUATES_2021) { + data = cachedData["grad2021"] + } + + if(data) { + userData = data.find((user)=>{ + return user["GitHub Username"] === githubLogin + }) + } + + return userData + } + fetchGraduate(githubLogin, table) { const airtable = Airtable.base(GITHUB_GRADUATION); return new Promise((resolve, reject)=>{ diff --git a/.github/workflows/src/app/fetch-data.js b/.github/workflows/src/app/fetch-data.js new file mode 100644 index 00000000..2c053d10 --- /dev/null +++ b/.github/workflows/src/app/fetch-data.js @@ -0,0 +1,16 @@ +const airtable = require('./app/airtable.js'); +const actionEvent = require('./app/action-event.js'); +const fs = require('fs') + +(async ()=>{ + const grad2020 = airtable.fetchAll2020(actionEvent.pullAuthor) + const grad2021 = airtable.fetchAll2021(actionEvent.pullAuthor) + const content = JSON.stringify({grad2020, grad2021}) + + fs.writeFile('app/data/airtable.json', content, err => { + if (err) { + console.error(err) + process.exit(1) + } + }) +})() -- knotmirror 0.0.1