diff --git a/functions/src/api.ts b/functions/src/api.ts index 7134c8d8..24677567 100644 --- a/functions/src/api.ts +++ b/functions/src/api.ts @@ -146,3 +146,24 @@ export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => { }, } as EndpointDefinition } + +export const newEndpointNoAuth = ( + endpointOpts: EndpointOptions, + fn: (req: Request) => Promise +) => { + const opts = Object.assign({}, DEFAULT_OPTS, endpointOpts) + return { + opts, + handler: async (req: Request, res: Response) => { + log(`${req.method} ${req.url} ${JSON.stringify(req.body)}`) + try { + if (opts.method !== req.method) { + throw new APIError(405, `This endpoint supports only ${opts.method}.`) + } + res.status(200).json(await fn(req)) + } catch (e) { + writeResponseError(e, res) + } + }, + } as EndpointDefinition +} diff --git a/functions/src/index.ts b/functions/src/index.ts index f5c45004..763fd8bb 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -9,7 +9,7 @@ export * from './on-create-user' export * from './on-create-bet' export * from './on-create-comment-on-contract' export * from './on-view' -export * from './update-metrics' +export { updateMetrics } from './update-metrics' export * from './update-stats' export * from './update-loans' export * from './backup-db' @@ -77,6 +77,7 @@ import { getcurrentuser } from './get-current-user' import { acceptchallenge } from './accept-challenge' import { createpost } from './create-post' import { savetwitchcredentials } from './save-twitch-credentials' +import { updatemetrics } from './update-metrics' const toCloudFunction = ({ opts, handler }: EndpointDefinition) => { return onRequest(opts, handler as any) @@ -106,6 +107,7 @@ const getCurrentUserFunction = toCloudFunction(getcurrentuser) const acceptChallenge = toCloudFunction(acceptchallenge) const createPostFunction = toCloudFunction(createpost) const saveTwitchCredentials = toCloudFunction(savetwitchcredentials) +const updateMetricsFunction = toCloudFunction(updatemetrics) export { healthFunction as health, @@ -133,4 +135,5 @@ export { saveTwitchCredentials as savetwitchcredentials, addCommentBounty as addcommentbounty, awardCommentBounty as awardcommentbounty, + updateMetricsFunction as updatemetrics, } diff --git a/functions/src/update-metrics.ts b/functions/src/update-metrics.ts index 24dc07e7..887dbbdc 100644 --- a/functions/src/update-metrics.ts +++ b/functions/src/update-metrics.ts @@ -1,10 +1,11 @@ import * as functions from 'firebase-functions' import * as admin from 'firebase-admin' import { groupBy, isEmpty, keyBy, last, sortBy } from 'lodash' +import fetch from 'node-fetch' + import { getValues, log, logMemory, writeAsync } from './utils' import { Bet } from '../../common/bet' import { Contract, CPMM } from '../../common/contract' - import { PortfolioMetrics, User } from '../../common/user' import { DAY_MS } from '../../common/util/time' import { getLoanUpdates } from '../../common/loans' @@ -20,13 +21,35 @@ import { import { getProbability } from '../../common/calculate' import { Group } from '../../common/group' import { batchedWaitAll } from '../../common/util/promise' +import { newEndpointNoAuth } from './api' +import { getFunctionUrl } from '../../common/api' const firestore = admin.firestore() -export const updateMetrics = functions - .runWith({ memory: '8GB', timeoutSeconds: 540 }) - .pubsub.schedule('every 15 minutes') - .onRun(updateMetricsCore) +export const updateMetrics = functions.pubsub + .schedule('every 15 minutes') + .onRun(async () => { + const response = await fetch(getFunctionUrl('updatemetrics'), { + headers: { + 'Content-Type': 'application/json', + }, + method: 'POST', + body: JSON.stringify({}), + }) + + const json = await response.json() + + if (response.ok) console.log(json) + else console.error(json) + }) + +export const updatemetrics = newEndpointNoAuth( + { timeoutSeconds: 2000, memory: '8GiB', minInstances: 0 }, + async (_req) => { + await updateMetricsCore() + return { success: true } + } +) export async function updateMetricsCore() { console.log('Loading users')