Make feed updating do much less work (#455)
This commit is contained in:
parent
30eac1bd96
commit
6fac56d2c9
|
@ -42,8 +42,8 @@ export async function getTaggedContracts(tag: string) {
|
||||||
return taggedContracts.filter((c) => (c.closeTime ?? Infinity) > Date.now())
|
return taggedContracts.filter((c) => (c.closeTime ?? Infinity) > Date.now())
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRecentBetsAndComments(contract: Contract) {
|
export async function getRecentBetsAndComments(contractId: string) {
|
||||||
const contractDoc = firestore.collection('contracts').doc(contract.id)
|
const contractDoc = firestore.collection('contracts').doc(contractId)
|
||||||
|
|
||||||
const [recentBets, recentComments] = await Promise.all([
|
const [recentBets, recentComments] = await Promise.all([
|
||||||
getValues<Bet>(
|
getValues<Bet>(
|
||||||
|
@ -64,7 +64,6 @@ export async function getRecentBetsAndComments(contract: Contract) {
|
||||||
])
|
])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
contract,
|
|
||||||
recentBets,
|
recentBets,
|
||||||
recentComments,
|
recentComments,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { shuffle, sortBy } from 'lodash'
|
import { flatten, shuffle, sortBy, uniq, zip, zipObject } from 'lodash'
|
||||||
|
|
||||||
import { getValue, getValues } from './utils'
|
import { getValue, getValues } from './utils'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
|
@ -67,15 +67,16 @@ export const updateFeedBatch = functions.https.onCall(
|
||||||
async (data: { users: User[] }) => {
|
async (data: { users: User[] }) => {
|
||||||
const { users } = data
|
const { users } = data
|
||||||
const contracts = await getFeedContracts()
|
const contracts = await getFeedContracts()
|
||||||
|
const feeds = await getNewFeeds(users, contracts)
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
users.map(async (user) => {
|
zip(users, feeds).map(([user, feed]) =>
|
||||||
const feed = await computeFeed(user, contracts)
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
||||||
await getUserCacheCollection(user).doc('feed').set({ feed })
|
getUserCacheCollection(user!).doc('feed').set({ feed })
|
||||||
})
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
export const updateCategoryFeed = functions.https.onCall(
|
export const updateCategoryFeed = functions.https.onCall(
|
||||||
async (data: { category: string }) => {
|
async (data: { category: string }) => {
|
||||||
const { category } = data
|
const { category } = data
|
||||||
|
@ -96,16 +97,28 @@ export const updateCategoryFeedBatch = functions.https.onCall(
|
||||||
async (data: { users: User[]; category: string }) => {
|
async (data: { users: User[]; category: string }) => {
|
||||||
const { users, category } = data
|
const { users, category } = data
|
||||||
const contracts = await getTaggedContracts(category)
|
const contracts = await getTaggedContracts(category)
|
||||||
|
const feeds = await getNewFeeds(users, contracts)
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
users.map(async (user) => {
|
zip(users, feeds).map(([user, feed]) =>
|
||||||
const feed = await computeFeed(user, contracts)
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
||||||
await getUserCacheCollection(user).doc(`feed-${category}`).set({ feed })
|
getUserCacheCollection(user!).doc(`feed-${category}`).set({ feed })
|
||||||
})
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const getNewFeeds = async (users: User[], contracts: Contract[]) => {
|
||||||
|
const feeds = await Promise.all(users.map((u) => computeFeed(u, contracts)))
|
||||||
|
const contractIds = uniq(flatten(feeds).map((c) => c.id))
|
||||||
|
const data = await Promise.all(contractIds.map(getRecentBetsAndComments))
|
||||||
|
const dataByContractId = zipObject(contractIds, data)
|
||||||
|
return feeds.map((feed) =>
|
||||||
|
feed.map((contract) => {
|
||||||
|
return { contract, ...dataByContractId[contract.id] }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const getUserCacheCollection = (user: User) =>
|
const getUserCacheCollection = (user: User) =>
|
||||||
firestore.collection(`private-users/${user.id}/cache`)
|
firestore.collection(`private-users/${user.id}/cache`)
|
||||||
|
|
||||||
|
@ -135,14 +148,7 @@ export const computeFeed = async (user: User, contracts: Contract[]) => {
|
||||||
|
|
||||||
// console.log(sortedContracts.map(([c, score]) => c.question + ': ' + score))
|
// console.log(sortedContracts.map(([c, score]) => c.question + ': ' + score))
|
||||||
|
|
||||||
const feedContracts = sortedContracts
|
return sortedContracts.slice(0, MAX_FEED_CONTRACTS).map(([c]) => c)
|
||||||
.slice(0, MAX_FEED_CONTRACTS)
|
|
||||||
.map(([c]) => c)
|
|
||||||
|
|
||||||
const feed = await Promise.all(
|
|
||||||
feedContracts.map((contract) => getRecentBetsAndComments(contract))
|
|
||||||
)
|
|
||||||
return feed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function scoreContract(
|
function scoreContract(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user