MCPcopy
hub / github.com/FlowiseAI/Flowise / updatePredictionsUsage

Function updatePredictionsUsage

packages/server/src/utils/quotaUsage.ts:78–126  ·  view source on GitHub ↗
(
    orgId: string,
    subscriptionId: string,
    _: string = '',
    usageCacheManager?: UsageCacheManager
)

Source from the content-addressed store, hash-verified

76
77// As predictions limit renew per month, we set to cache with 1 month TTL
78export const updatePredictionsUsage = async (
79 orgId: string,
80 subscriptionId: string,
81 _: string = '',
82 usageCacheManager?: UsageCacheManager
83) => {
84 if (!usageCacheManager) return
85
86 const quotas = await usageCacheManager.getQuotas(subscriptionId)
87 const predictionsLimit = quotas[LICENSE_QUOTAS.PREDICTIONS_LIMIT]
88
89 let currentPredictions = 0
90 const existingPredictions = await usageCacheManager.get(`predictions:${orgId}`)
91 if (existingPredictions) {
92 currentPredictions = 1 + (existingPredictions as number) > predictionsLimit ? predictionsLimit : 1 + (existingPredictions as number)
93 } else {
94 currentPredictions = 1
95 }
96
97 const currentTTL = await usageCacheManager.getTTL(`predictions:${orgId}`)
98 if (currentTTL) {
99 const currentTimestamp = Date.now()
100 const timeLeft = currentTTL - currentTimestamp
101 usageCacheManager.set(`predictions:${orgId}`, currentPredictions, timeLeft)
102 } else {
103 const subscriptionDetails = await usageCacheManager.getSubscriptionDetails(subscriptionId)
104 if (subscriptionDetails && subscriptionDetails.created) {
105 const MS_PER_DAY = 24 * 60 * 60 * 1000
106 const DAYS = 30
107 const approximateMonthMs = DAYS * MS_PER_DAY
108
109 // Calculate time elapsed since subscription creation
110 const createdTimestamp = subscriptionDetails.created * 1000 // Convert to milliseconds if timestamp is in seconds
111 const currentTimestamp = Date.now()
112 const timeElapsed = currentTimestamp - createdTimestamp
113
114 // Calculate remaining time in the current month period
115 const timeLeft = approximateMonthMs - (timeElapsed % approximateMonthMs)
116
117 usageCacheManager.set(`predictions:${orgId}`, currentPredictions, timeLeft)
118 } else {
119 // Fallback to default 30 days if no creation date
120 const MS_PER_DAY = 24 * 60 * 60 * 1000
121 const DAYS = 30
122 const approximateMonthMs = DAYS * MS_PER_DAY
123 usageCacheManager.set(`predictions:${orgId}`, currentPredictions, approximateMonthMs)
124 }
125 }
126}
127
128export const checkPredictions = async (orgId: string, subscriptionId: string, usageCacheManager: UsageCacheManager) => {
129 if (!usageCacheManager || !subscriptionId) return

Callers 2

utilBuildChatflowFunction · 0.90
_executeAgentflowFunction · 0.90

Calls 5

getQuotasMethod · 0.80
getTTLMethod · 0.80
setMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected