utils.mjs 885 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { ObjectId } from 'mongodb'
  2. import config from 'config'
  3. export const RPO = parseInt(config.get('backupRPOInMS'), 10)
  4. /**
  5. * @param {Date} time
  6. * @return {ObjectId}
  7. */
  8. export function objectIdFromDate(time) {
  9. return ObjectId.createFromTime(time.getTime() / 1000)
  10. }
  11. /**
  12. * @param {number} [factor] - Multiply RPO by this factor, default is 1
  13. * @return {Date}
  14. */
  15. export function getEndDateForRPO(factor = 1) {
  16. return new Date(Date.now() - RPO * factor)
  17. }
  18. /**
  19. * Creates a startDate, endDate pair that checks a period of time before the RPO horizon
  20. *
  21. * @param {number} offset - How many seconds we should check
  22. * @return {{endDate: Date, startDate: Date}}
  23. */
  24. export function getDatesBeforeRPO(offset) {
  25. const now = new Date()
  26. const endDate = new Date(now.getTime() - RPO)
  27. return {
  28. endDate,
  29. startDate: new Date(endDate.getTime() - offset * 1000),
  30. }
  31. }