Skip to content

Commit

Permalink
fix db projects updating due to createdWithinTrendingWindow false != …
Browse files Browse the repository at this point in the history
…undefined

add forceUpdateAll functionality to update endpint
  • Loading branch information
peripheralist committed Dec 8, 2023
1 parent 80a92b4 commit 47d5af2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/lib/api/supabase/projects/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type DBPLogOpts =
notif: keyof typeof DBP_NOTIFS
}

const logger = getLogger('lib/sepana')
const logger = getLogger('lib/dbProjects')

export async function dbpLog(
opts: DBPLogOpts & {
Expand Down
12 changes: 7 additions & 5 deletions src/lib/api/supabase/projects/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { isBigNumberish } from 'utils/bigNumbers'
import { formatError } from 'utils/format/formatError'
import { formatWad } from 'utils/format/formatNumber'
import {
formatSgProjectsForUpdate,
formatWithMetadata,
getChangedSubgraphProjects,
} from 'utils/sgDbProjects'
import { dbpQueryAll, queryAllSGProjectsForServer, writeDBProjects } from '.'
import { dbpLog } from './logger'

export async function updateDBProjects(
res: NextApiResponse,
retryIpfs: boolean,
forceUpdateAll?: boolean,
) {
try {
// Load all database projects
Expand All @@ -35,21 +36,22 @@ export async function updateDBProjects(
// Load all projects from Subgraph
const sgProjects = await queryAllSGProjectsForServer()

// Determine which subgraph projects have changed from what we have stored in the database
// Determine which subgraph projects should be updated
const {
changedSubgraphProjects,
subgraphProjects: sgProjectsToUpdate,
retryMetadataCount,
updatedProperties,
idsOfNewProjects,
} = getChangedSubgraphProjects({
} = formatSgProjectsForUpdate({
sgProjects,
dbProjects,
retryIpfs,
returnAllProjects: forceUpdateAll,
})

// Append metadata props to all changed subgraph projects, and re-resolve metadata where needed
const resolveMetadataResults = await Promise.all(
changedSubgraphProjects.map(sgProject =>
sgProjectsToUpdate.map(sgProject =>
formatWithMetadata({
sgProject,
dbProject: dbProjects[sgProject.id],
Expand Down
6 changes: 4 additions & 2 deletions src/pages/api/projects/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { updateDBProjects } from 'lib/api/supabase/projects'
import { NextApiHandler } from 'next'

// Synchronizes projects in the database with the latest Juicebox Subgraph/IPFS data
const handler: NextApiHandler = async (_, res) => {
await updateDBProjects(res, false)
const handler: NextApiHandler = async (req, res) => {
const { forceUpdateAll } = req.query
const _forceUpdateAll = forceUpdateAll === 'true' ? true : false
await updateDBProjects(res, false, _forceUpdateAll)
}

export default handler
108 changes: 60 additions & 48 deletions src/utils/sgDbProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,18 @@ export function formatDBProjectRow(
* @param sgProjects List of Subgraph projects
* @param dbProjects List of database projects
* @param retryIpfs If true, any project that previously failed to resolve metadata will be marked for change, regardless of if the project has since changed in the Subgraph.
* @param returnAllProjects If true, all projects will be returned.
*/
export function getChangedSubgraphProjects({
export function formatSgProjectsForUpdate({
sgProjects,
dbProjects,
retryIpfs,
returnAllProjects,
}: {
sgProjects: Json<Pick<Project, SGSBCompareKey>>[]
dbProjects: Record<string, Json<DBProject>>
retryIpfs?: boolean
returnAllProjects?: boolean
}) {
const idsOfNewProjects = new Set<string>([])

Expand All @@ -166,60 +169,69 @@ export function getChangedSubgraphProjects({

let retryMetadataCount = 0

const changedSubgraphProjects = sgProjects
.map(formatSGProjectForDB)
.filter(sgProject => {
const id = sgProject.id

const dbProject = dbProjects[id]

if (!dbProject) {
idsOfNewProjects.add(id)
return true
}

const { _hasUnresolvedMetadata, _metadataRetriesLeft } = dbProject

if (
retryIpfs &&
_hasUnresolvedMetadata &&
(_metadataRetriesLeft || _metadataRetriesLeft === undefined)
) {
retryMetadataCount += 1
return true
}

if (dbProject.tags?.some(t => !isValidProjectTag(t))) {
return true
}

// Deep compare Subgraph project vs. database project and find any discrepancies
const propertiesToUpdate = sgDbCompareKeys.filter(k => {
const oldVal = dbProject[k]
const newVal = sgProject[k]

// Store a record of properties that need updating
if (oldVal !== newVal) {
updatedProperties[id] = [
...(updatedProperties[id] ?? []),
{
key: k,
oldVal: oldVal?.toString(),
newVal: newVal?.toString(),
},
]
const formattedSgProjects = sgProjects.map(formatSGProjectForDB)

const subgraphProjects = returnAllProjects
? formattedSgProjects
: formattedSgProjects.filter(sgProject => {
const id = sgProject.id

const dbProject = dbProjects[id]

if (!dbProject) {
idsOfNewProjects.add(id)
return true
}

const { _hasUnresolvedMetadata, _metadataRetriesLeft } = dbProject

if (
retryIpfs &&
_hasUnresolvedMetadata &&
(_metadataRetriesLeft || _metadataRetriesLeft === undefined)
) {
retryMetadataCount += 1
return true
}

return false
if (dbProject.tags?.some(t => !isValidProjectTag(t))) {
return true
}

// Deep compare Subgraph project vs. database project and find any discrepancies
const propertiesToUpdate = sgDbCompareKeys.filter(k => {
const oldVal = dbProject[k]
const newVal = sgProject[k]

// Store a record of properties that need updating
if (oldVal !== newVal) {
if (k === 'createdWithinTrendingWindow') {
// Hack. DB only stores boolean for this property, but subgraph may return undefined/null
if (!oldVal && !newVal) return false
}

updatedProperties[id] = [
...(updatedProperties[id] ?? []),
{
key: k,
oldVal: oldVal?.toString(),
newVal: newVal?.toString(),
},
]
return true
}

return false
})

// Return true if any properties are out of date
return propertiesToUpdate.length
})

// Return true if any properties are out of date
return propertiesToUpdate.length
})
// console.log('asdf', { changedSubgraphProjects })

return {
changedSubgraphProjects,
subgraphProjects,
updatedProperties,
retryMetadataCount,
idsOfNewProjects,
Expand Down

2 comments on commit 47d5af2

@vercel
Copy link

@vercel vercel bot commented on 47d5af2 Dec 8, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 47d5af2 Dec 8, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.