import { firestore } from 'firebase-admin' ; import * as functions from 'firebase-functions' ; import { FirebaseAdmin } from '../client/firebase-admin' ; import { Logger } from '../client/logger' ; import { MonitorModel , ProjectModel } from '../models/index.model' ; import { ping } from '../monitor/monitor' ; import { deleteProjectPings } from '../project/delete-project' ; type QuerySnapshot = firestore . QuerySnapshot ; type WriteResult = firestore . WriteResult ; export const deletePingsAfter30days = functions . pubsub . schedule ( '00 00 * * *' ) . timeZone ( 'UTC' ) . onRun ( async ( ) => { const snapshots = await FirebaseAdmin . firestore ( ) . collection ( 'projects' ) . get ( ) ; const promises = [ ] ; snapshots . docs . forEach ( ( doc ) => { promises . push ( deleteProjectPings ( < ProjectModel > doc . data ( ) , true ) ) ; } ) ; return Promise . all ( promises ) ; } ) ; export const runAllMonitors60Mins = functions . pubsub . schedule ( 'every 60 mins' ) . onRun ( async ( ) => { const snapshots = await FirebaseAdmin . firestore ( ) . collection ( 'projects' ) . get ( ) ; const promises = [ ] ; let project ; snapshots . docs . forEach ( async ( doc ) => { project = < ProjectModel > doc . data ( ) ; try { if ( project . url && Array . isArray ( project . monitors ) && project . monitors . length > 0 ) { project . monitors . forEach ( ( monitor ) => promises . push ( ping ( project . uid , monitor . uid ) ) ) ; } } catch ( err ) { Logger . error ( err ) ; throw new Error ( err ) ; } } ) ; return Promise . all ( promises ) ; } ) ;