Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,48 @@ export async function fetchMembersForEnrichment(
SELECT 1 FROM "memberEnrichmentCache" mec
WHERE mec."memberId" = members.id
AND mec.source = '${input.source}'
AND EXTRACT(EPOCH FROM (now() - mec."updatedAt")) < ${input.cacheObsoleteAfterSeconds})
AND mec."updatedAt" > now() - make_interval(secs => ${input.cacheObsoleteAfterSeconds}))
)`,
)
}

enrichableBySqlConditions.push(`(${input.enrichableBySql})`)
})

let enrichableBySqlJoined = ''

if (enrichableBySqlConditions.length > 0) {
enrichableBySqlJoined = `(${enrichableBySqlConditions.join(' OR ')}) `
}
const enrichableBySqlJoined =
enrichableBySqlConditions.length > 0 ? `(${enrichableBySqlConditions.join(' OR ')})` : 'TRUE'

// Pick top-N by activity first, then load identities for those rows only.
return db.connection().query(
`
WITH candidates AS (
SELECT
members.id,
members."displayName",
members.attributes->'location'->>'default' AS location,
members.attributes->'websiteUrl'->>'default' AS website,
coalesce("membersGlobalActivityCount".total_count, 0) AS "activityCount"
FROM "membersGlobalActivityCount"
INNER JOIN members ON members.id = "membersGlobalActivityCount"."memberId"
Comment thread
skwowet marked this conversation as resolved.
WHERE members."deletedAt" IS NULL
AND coalesce((members.attributes ->'isBot'->>'default')::boolean, false) = false
AND coalesce((members.attributes ->'isOrganization'->>'default')::boolean, false) = false
AND EXISTS (
SELECT 1
FROM "memberIdentities" mi
WHERE mi."memberId" = members.id
AND mi."deletedAt" IS NULL
AND ${enrichableBySqlJoined}
)
AND (${cacheAgeInnerQueryItems.join(' OR ')})
ORDER BY "membersGlobalActivityCount".total_count DESC
LIMIT $1
)
SELECT
members."id",
members."displayName",
members.attributes->'location'->>'default' AS location,
members.attributes->'websiteUrl'->>'default' AS website,
c.id,
c."displayName",
c.location,
c.website,
JSON_AGG(
JSON_BUILD_OBJECT(
'platform', mi.platform,
Expand All @@ -166,19 +187,18 @@ export async function fetchMembersForEnrichment(
'verified', mi.verified
)
) AS identities,
MAX(coalesce("membersGlobalActivityCount".total_count, 0)) AS "activityCount"
FROM members
INNER JOIN "memberIdentities" mi ON mi."memberId" = members.id and mi."deletedAt" is null
LEFT JOIN "membersGlobalActivityCount" ON "membersGlobalActivityCount"."memberId" = members.id
WHERE
${enrichableBySqlJoined}
AND coalesce((members.attributes ->'isBot'->>'default')::boolean, false) = false
AND coalesce((members.attributes ->'isOrganization'->>'default')::boolean, false) = false
AND members."deletedAt" IS NULL
AND (${cacheAgeInnerQueryItems.join(' OR ')})
GROUP BY members.id
ORDER BY "activityCount" DESC
LIMIT $1;
c."activityCount"
FROM candidates c
INNER JOIN members ON members.id = c.id
INNER JOIN "memberIdentities" mi
ON mi."memberId" = c.id
AND mi."deletedAt" IS NULL
CROSS JOIN LATERAL (
SELECT c."activityCount" AS total_count
) AS "membersGlobalActivityCount"
WHERE ${enrichableBySqlJoined}
Comment thread
skwowet marked this conversation as resolved.
GROUP BY c.id, c."displayName", c.location, c.website, c."activityCount"
ORDER BY c."activityCount" DESC;
`,
[limit],
)
Expand Down
Loading