1
1
import { AkashBlock as Block } from "@akashnetwork/database/dbSchemas/akash" ;
2
2
import { Day } from "@akashnetwork/database/dbSchemas/base" ;
3
- import { subHours } from "date-fns" ;
3
+ import { format , subHours } from "date-fns" ;
4
4
import { Op , QueryTypes } from "sequelize" ;
5
5
6
6
import { cacheKeys , cacheResponse } from "@src/caching/helpers" ;
@@ -202,40 +202,42 @@ export const getProviderGraphData = async (dataName: ProviderStatsKey) => {
202
202
203
203
const getter = ( block : ProviderStats ) => ( typeof block [ dataName ] === "number" ? ( block [ dataName ] as number ) : parseInt ( block [ dataName ] as string ) || 0 ) ;
204
204
205
- const result = await cacheResponse (
206
- 60 * 5 , // 5 minutes
207
- cacheKeys . getProviderGraphData ,
208
- async ( ) => {
209
- return await chainDb . query < ProviderStats > (
210
- `SELECT d."date", (SUM("activeCPU") + SUM("pendingCPU") + SUM("availableCPU")) AS "cpu", (SUM("activeGPU") + SUM("pendingGPU") + SUM("availableGPU")) AS "gpu", (SUM("activeMemory") + SUM("pendingMemory") + SUM("availableMemory")) AS memory, (SUM("activeStorage") + SUM("pendingStorage") + SUM("availableStorage")) as storage, COUNT(*) as count
211
- FROM "day" d
212
- INNER JOIN (
213
- SELECT DISTINCT ON("hostUri",DATE("checkDate"))
214
- DATE("checkDate") AS date,
215
- ps."activeCPU", ps."pendingCPU", ps."availableCPU",
216
- ps."activeGPU", ps."pendingGPU", ps."availableGPU",
217
- ps."activeMemory", ps."pendingMemory", ps."availableMemory",
218
- ps."activeEphemeralStorage" + COALESCE(ps."activePersistentStorage", 0) AS "activeStorage", ps."pendingEphemeralStorage" + COALESCE(ps."pendingPersistentStorage", 0) AS "pendingStorage", ps."availableEphemeralStorage" + COALESCE(ps."availablePersistentStorage", 0) AS "availableStorage",
205
+ const result = removeLastAroundMidnight (
206
+ await cacheResponse (
207
+ 60 * 5 , // 5 minutes
208
+ cacheKeys . getProviderGraphData ,
209
+ async ( ) => {
210
+ return await chainDb . query < ProviderStats > (
211
+ `SELECT d."date", (SUM("activeCPU") + SUM("pendingCPU") + SUM("availableCPU")) AS "cpu", (SUM("activeGPU") + SUM("pendingGPU") + SUM("availableGPU")) AS "gpu", (SUM("activeMemory") + SUM("pendingMemory") + SUM("availableMemory")) AS memory, (SUM("activeStorage") + SUM("pendingStorage") + SUM("availableStorage")) as storage, COUNT(*) as count
212
+ FROM "day" d
213
+ INNER JOIN (
214
+ SELECT DISTINCT ON("hostUri",DATE("checkDate"))
215
+ DATE("checkDate") AS date,
216
+ ps."activeCPU", ps."pendingCPU", ps."availableCPU",
217
+ ps."activeGPU", ps."pendingGPU", ps."availableGPU",
218
+ ps."activeMemory", ps."pendingMemory", ps."availableMemory",
219
+ ps."activeEphemeralStorage" + COALESCE(ps."activePersistentStorage", 0) AS "activeStorage", ps."pendingEphemeralStorage" + COALESCE(ps."pendingPersistentStorage", 0) AS "pendingStorage", ps."availableEphemeralStorage" + COALESCE(ps."availablePersistentStorage", 0) AS "availableStorage",
219
220
ps."isOnline"
220
221
FROM "providerSnapshot" ps
221
222
INNER JOIN "day" d ON d."date" = DATE(ps."checkDate")
222
223
INNER JOIN "block" b ON b.height=d."lastBlockHeightYet"
223
224
INNER JOIN "provider" ON "provider"."owner"=ps."owner"
224
225
WHERE ps."isLastSuccessOfDay" = TRUE AND ps."checkDate" >= b."datetime" - (:grace_duration * INTERVAL '1 minutes')
225
226
ORDER BY "hostUri",DATE("checkDate"),"checkDate" DESC
226
- ) "dailyProviderStats"
227
- ON DATE(d."date")="dailyProviderStats"."date"
228
- GROUP BY d."date"
229
- ORDER BY d."date" ASC` ,
230
- {
231
- type : QueryTypes . SELECT ,
232
- replacements : {
233
- grace_duration : env . PROVIDER_UPTIME_GRACE_PERIOD_MINUTES
227
+ ) "dailyProviderStats"
228
+ ON DATE(d."date")="dailyProviderStats"."date"
229
+ GROUP BY d."date"
230
+ ORDER BY d."date" ASC` ,
231
+ {
232
+ type : QueryTypes . SELECT ,
233
+ replacements : {
234
+ grace_duration : env . PROVIDER_UPTIME_GRACE_PERIOD_MINUTES
235
+ }
234
236
}
235
- }
236
- ) ;
237
- } ,
238
- true
237
+ ) ;
238
+ } ,
239
+ true
240
+ )
239
241
) ;
240
242
241
243
if ( result . length < 2 ) {
@@ -280,13 +282,25 @@ export const getProviderGraphData = async (dataName: ProviderStatsKey) => {
280
282
} ;
281
283
} ;
282
284
285
+ const removeLastAroundMidnight = ( stats : ProviderStats [ ] ) => {
286
+ const now = new Date ( ) ;
287
+ const isFirstFifteenMinuesOfDay = now . getHours ( ) === 0 && now . getMinutes ( ) < 15 ;
288
+ const dateToday = format ( now , "yyyy-MM-dd" ) ;
289
+ const lastItemIsForToday = stats . length > 0 && stats [ stats . length - 1 ] . date . startsWith ( dateToday ) ;
290
+ if ( isFirstFifteenMinuesOfDay && lastItemIsForToday ) {
291
+ return stats . slice ( 0 , stats . length - 1 ) ;
292
+ }
293
+
294
+ return stats ;
295
+ } ;
296
+
283
297
export const getProviderActiveLeasesGraphData = async ( providerAddress : string ) => {
284
298
console . log ( "getProviderActiveLeasesGraphData" ) ;
285
299
286
300
const result = await chainDb . query < ProviderActiveLeasesStats > (
287
301
`SELECT "date" AS date, COUNT(l."id") AS count
288
302
FROM "day" d
289
- LEFT JOIN "lease" l
303
+ LEFT JOIN "lease" l
290
304
ON l."providerAddress" = :providerAddress
291
305
AND l."createdHeight" <= d."lastBlockHeightYet"
292
306
AND COALESCE(l."closedHeight", l."predictedClosedHeight") > d."lastBlockHeightYet"
@@ -370,7 +384,7 @@ export async function getProviderActiveResourcesAtHeight(provider: string, heigh
370
384
SUM("persistentStorageQuantity") AS "persistentStorage",
371
385
SUM("gpuUnits") AS "gpu"
372
386
FROM lease
373
- WHERE
387
+ WHERE
374
388
"providerAddress"=:provider
375
389
AND "createdHeight" <= :height
376
390
AND COALESCE("closedHeight", "predictedClosedHeight") > :height` ,
@@ -394,18 +408,18 @@ export async function getProviderEarningsAtHeight(provider: string, providerCrea
394
408
FROM lease
395
409
WHERE "providerAddress"=:provider
396
410
)
397
- SELECT
411
+ SELECT
398
412
d.date, d."aktPrice",s."totalUAkt",s."totalUUsdc"
399
413
FROM day d
400
414
LEFT JOIN LATERAL (
401
415
WITH active_leases AS (
402
- SELECT
416
+ SELECT
403
417
l.dseq,
404
418
l.price,
405
419
l.denom,
406
420
(LEAST(d."lastBlockHeightYet", COALESCE(l."closedHeight", l."predictedClosedHeight"), :height) - GREATEST(d."firstBlockHeight", l."createdHeight", :providerCreatedHeight)) AS duration
407
421
FROM provider_leases l
408
- WHERE
422
+ WHERE
409
423
l."createdHeight" <= LEAST(d."lastBlockHeightYet", :height)
410
424
AND COALESCE(l."closedHeight", l."predictedClosedHeight") >= GREATEST(d."firstBlockHeight", :providerCreatedHeight)
411
425
),
@@ -415,7 +429,7 @@ export async function getProviderEarningsAtHeight(provider: string, providerCrea
415
429
(CASE WHEN l.denom='uusdc' THEN l.price*l.duration ELSE 0 END) AS "uusdc_earned"
416
430
FROM active_leases l
417
431
)
418
- SELECT
432
+ SELECT
419
433
SUM(l.uakt_earned) AS "totalUAkt",
420
434
SUM(l.uusdc_earned) AS "totalUUsdc"
421
435
FROM billed_leases l
0 commit comments