/**
 * Copyright 2018 Twitter, Inc.
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 */ import { DatabaseSet , SqrlKey } from "sqrl" ; import { createRedisKey } from "./RedisInterface" ; export function getBucketKey ( databaseSet , prefix , key , windowMs , bucketTime ) { return createRedisKey ( databaseSet , prefix , key , windowMs , bucketTime ) ; } export function getAllBucketKeys ( databaseSet , prefix , key , timeMs , windowMs , numBuckets ) { const bucketSize = getBucketSize ( windowMs , numBuckets ) ; const startTime = getWindowStart ( timeMs , windowMs ) ; const startBucket = getBucketTimeForTimeMs ( startTime , bucketSize ) ; const keyHex = key . getHex ( ) ; return Array . from ( { length : numBuckets + 1 } , ( _ , currentBucket ) => getBucketKey ( databaseSet , prefix , keyHex , windowMs , startBucket + currentBucket * bucketSize ) ) ; } export function getBucketSize ( windowMs , numBuckets ) { return Math . floor ( windowMs / numBuckets ) ; } export function getBucketTimeForTimeMs ( timeMs , bucketSize ) { return timeMs - ( timeMs % bucketSize ) ; } export function getCurrentBucketExpirySeconds ( windowMs , bucketSize ) { return Math . ceil ( ( windowMs + bucketSize ) / 1000 ) ; } export function getWindowStart ( timeMs , windowMs ) { return timeMs - windowMs ; }