Skip to content
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

fix: create tpw table if doesn't exist #229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Use Node.js 16.x
- name: Use Node.js 22.x
uses: actions/setup-node@v4
with:
node-version: '16'
node-version: '22'
- name: apt-get install
run: sudo apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
- run: corepack enable
- run: yarn
- run: yarn test
- name: yarn semantic-release
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ gcp-credentials.json
*-subset.js
*-subset.d.ts
*.generated.sql


.yarn
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty ❤️

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
49 changes: 33 additions & 16 deletions bin/automated-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {BigQuery} = require('@google-cloud/bigquery')

const {getEntity} = require('../lib/')

const bigQuery = new BigQuery()

const HA_REQUESTS_TABLE_REGEX = /`httparchive\.requests\.\w+`/g
const HA_LH_TABLE_REGEX = /`httparchive\.lighthouse\.\w+`/g
const LH_3P_TABLE_REGEX = /`lighthouse-infrastructure\.third_party_web\.\w+`/g
Expand Down Expand Up @@ -81,7 +83,7 @@ async function getTargetDatasetDate() {
}

const getQueryResultStream = async query => {
const [job] = await new BigQuery().createQueryJob({
const [job] = await bigQuery.createQueryJob({
query,
location: 'US',
useQueryCache: false,
Expand All @@ -96,8 +98,10 @@ const getJSONStringTransformer = rowCounter => {
return new Transform({
objectMode: true,
transform(row, _, callback) {
const prefix = rowCounter === undefined ? '' : !rowCounter++ ? '[\n' : ',\n'
callback(null, prefix + JSON.stringify(row))
const toJSONArrayString = rowCounter !== undefined
const prefix = toJSONArrayString ? (!rowCounter++ ? '[\n' : ',\n') : ''
const suffix = toJSONArrayString ? '' : '\n'
callback(null, prefix + JSON.stringify(row) + suffix)
},
})
}
Expand All @@ -114,6 +118,23 @@ const EntityCanonicalDomainTransformer = new Transform({
},
})

const getThirdPartyWebTable = async tableName => {
const thirdPartyWebDataset = bigQuery.dataset('third_party_web', {
projectId: process.env.OVERRIDE_LH_PROJECT,
})
const thirdPartyWebTable = thirdPartyWebDataset.table(tableName)
const thirdPartyWebTableExits = await thirdPartyWebTable.exists()
if (thirdPartyWebTableExits) return thirdPartyWebTable
const [table] = await thirdPartyWebDataset.createTable(tableName, {
schema: [
{name: 'domain', type: 'STRING'},
{name: 'canonicalDomain', type: 'STRING'},
{name: 'category', type: 'STRING'},
],
})
return table
}

async function main() {
const {dateStringUnderscore, dateStringHypens} = await getTargetDatasetDate()

Expand Down Expand Up @@ -153,20 +174,17 @@ async function main() {
.pipe(observedDomainsFileWriterStream)

// Observed domain entity mapping table pipe
const thirdPartyWebTableWriterStream = new BigQuery()
.dataset('third_party_web')
.table(dateStringUnderscore)
.createWriteStream({
schema: [
{name: 'domain', type: 'STRING'},
{name: 'canonicalDomain', type: 'STRING'},
{name: 'category', type: 'STRING'},
],
})
const thirdPartyWebTableWriterStream = await getThirdPartyWebTable(dateStringUnderscore).then(
table =>
table.createWriteStream({
sourceFormat: 'NEWLINE_DELIMITED_JSON',
})
)

resultsStream
// map observed domain to entity
.pipe(EntityCanonicalDomainTransformer)
// stringify json
// stringify json with new line delimiter
.pipe(getJSONStringTransformer())
// write to thrid_party_web table
.pipe(thirdPartyWebTableWriterStream)
Expand All @@ -182,8 +200,7 @@ async function main() {
)
},
deleteFn: async () => {
const bqClient = new BigQuery()
await bqClient
await bigQuery
.dataset('third_party_web')
.table(dateStringUnderscore)
.delete()
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"license": "MIT",
"devDependencies": {
"@google-cloud/bigquery": "^5.10.0",
"@google-cloud/bigquery": "^7.9.0",
"chart.js": "^2.9.4",
"chartjs-node-canvas": "^3.2.0",
"colors": "^1.4.0",
Expand All @@ -41,5 +41,6 @@
"httparchive-nostats-subset": "./lib/subsets/httparchive-nostats.js",
"httparchive-subset": "./lib/subsets/httparchive.js"
}
}
},
"packageManager": "[email protected]"
}
Loading