Skip to content

Commit

Permalink
Check DB before processing repo
Browse files Browse the repository at this point in the history
If we find a repo entry in the DB, we can just use that data
  • Loading branch information
domoscargin committed Jan 8, 2025
1 parent 2c5b10f commit 885bb10
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build-filtered-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export async function analyseRepo (repo) {
await repoData.fetchAndValidateRepoInfo()
repoData.log('repo metadata and latest commit details fetched and validated.')
repoData.log(`GraphQL rate limit remaining: ${repoData.graphQLRateLimit.remaining}`)
if (db.isRepoUpToDate(repoOwner, repoName, repoData.lastUpdated)) {
repoData.log('repo has had no updates since we last checked. Skipping.')
return db.getRepoData(repoOwner, repoName)
}
await repoData.fetchAndValidateRepoTree()
repoData.log('tree fetched and validated.')

Expand Down
46 changes: 46 additions & 0 deletions helpers/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ export class RepoDB {
)
`)
setup.run()

// Create an index on repoOwner and repoName for faster lookups
const indexSetup = this.db.prepare(`
CREATE INDEX IF NOT EXISTS idx_repo_owner_name ON repos (repoOwner, repoName)
`)
indexSetup.run()
}

getRepoData (repoOwner, repoName) {
const query = this.db.prepare(`
SELECT * FROM repos
WHERE repoOwner = ? AND repoName = ?
`)

const result = query.get(repoOwner, repoName)
if (!result) {
return null
}
return this.denormaliseData(result)
}

isRepoUpToDate (repoOwner, repoName, lastUpdated) {
const query = this.db.prepare(`
SELECT lastUpdated FROM repos
WHERE repoOwner = ? AND repoName = ?
`)
const result = query.get(repoOwner, repoName)
return result && result.lastUpdated === lastUpdated
}

insertRepoData (repo) {
Expand Down Expand Up @@ -112,6 +140,24 @@ export class RepoDB {
return normalisedData
}

denormaliseData (dataset) {
const denormalisedData = {}
for (const [key, value] of Object.entries(dataset)) {
if (value === 0 || value === 1) {
denormalisedData[key] = value === 1
} else if (value === '') {
denormalisedData[key] = null
} else {
try {
denormalisedData[key] = JSON.parse(value)
} catch (error) {
denormalisedData[key] = value
}
}
}
return denormalisedData
}

close () {
this.db.close()
}
Expand Down

0 comments on commit 885bb10

Please sign in to comment.