1
1
import { Provider , ProviderAttribute , ProviderAttributeSignature , ProviderSnapshotNode , ProviderSnapshotNodeGPU } from "@akashnetwork/database/dbSchemas/akash" ;
2
2
import { ProviderSnapshot } from "@akashnetwork/database/dbSchemas/akash/providerSnapshot" ;
3
3
import { add , sub } from "date-fns" ;
4
+ import uniqBy from "lodash/uniqBy" ;
4
5
import { Op } from "sequelize" ;
5
6
6
7
import { ProviderDetail } from "@src/types/provider" ;
@@ -24,31 +25,58 @@ export async function getNetworkCapacity() {
24
25
]
25
26
} ) ;
26
27
27
- const filteredProviders = providers . filter ( ( value , index , self ) => self . map ( x => x . hostUri ) . indexOf ( value . hostUri ) === index ) ;
28
+ const filteredProviders = uniqBy ( providers , provider => provider . hostUri ) ;
29
+ const stats = filteredProviders . reduce (
30
+ ( all , provider ) => {
31
+ stats . activeCPU += provider . lastSuccessfulSnapshot . activeCPU ;
32
+ stats . pendingCPU += provider . lastSuccessfulSnapshot . pendingCPU ;
33
+ stats . availableCPU += provider . lastSuccessfulSnapshot . availableCPU ;
28
34
29
- const stats = {
30
- activeProviderCount : filteredProviders . length ,
31
- activeCPU : filteredProviders . map ( x => x . lastSuccessfulSnapshot . activeCPU ) . reduce ( ( a , b ) => a + b , 0 ) ,
32
- activeGPU : filteredProviders . map ( x => x . lastSuccessfulSnapshot . activeGPU ) . reduce ( ( a , b ) => a + b , 0 ) ,
33
- activeMemory : filteredProviders . map ( x => x . lastSuccessfulSnapshot . activeMemory ) . reduce ( ( a , b ) => a + b , 0 ) ,
34
- activeStorage : filteredProviders
35
- . map ( x => x . lastSuccessfulSnapshot . activeEphemeralStorage + x . lastSuccessfulSnapshot . activePersistentStorage )
36
- . reduce ( ( a , b ) => a + b , 0 ) ,
37
- pendingCPU : filteredProviders . map ( x => x . lastSuccessfulSnapshot . pendingCPU ) . reduce ( ( a , b ) => a + b , 0 ) ,
38
- pendingGPU : filteredProviders . map ( x => x . lastSuccessfulSnapshot . pendingGPU ) . reduce ( ( a , b ) => a + b , 0 ) ,
39
- pendingMemory : filteredProviders . map ( x => x . lastSuccessfulSnapshot . pendingMemory ) . reduce ( ( a , b ) => a + b , 0 ) ,
40
- pendingStorage : filteredProviders
41
- . map ( x => x . lastSuccessfulSnapshot . pendingEphemeralStorage + x . lastSuccessfulSnapshot . pendingPersistentStorage )
42
- . reduce ( ( a , b ) => a + b , 0 ) ,
43
- availableCPU : filteredProviders . map ( x => x . lastSuccessfulSnapshot . availableCPU ) . reduce ( ( a , b ) => a + b , 0 ) ,
44
- availableGPU : filteredProviders . map ( x => x . lastSuccessfulSnapshot . availableGPU ) . reduce ( ( a , b ) => a + b , 0 ) ,
45
- availableMemory : filteredProviders . map ( x => x . lastSuccessfulSnapshot . availableMemory ) . reduce ( ( a , b ) => a + b , 0 ) ,
46
- availableStorage : filteredProviders
47
- . map ( x => x . lastSuccessfulSnapshot . availableEphemeralStorage + x . lastSuccessfulSnapshot . availablePersistentStorage )
48
- . reduce ( ( a , b ) => a + b , 0 )
49
- } ;
35
+ stats . activeGPU += provider . lastSuccessfulSnapshot . activeGPU ;
36
+ stats . pendingGPU += provider . lastSuccessfulSnapshot . pendingGPU ;
37
+ stats . availableGPU += provider . lastSuccessfulSnapshot . availableGPU ;
38
+
39
+ stats . activeMemory += provider . lastSuccessfulSnapshot . activeMemory ;
40
+ stats . pendingMemory += provider . lastSuccessfulSnapshot . pendingMemory ;
41
+ stats . availableMemory += provider . lastSuccessfulSnapshot . availableMemory ;
42
+
43
+ stats . activeEphemeralStorage += provider . lastSuccessfulSnapshot . activeEphemeralStorage ;
44
+ stats . pendingEphemeralStorage += provider . lastSuccessfulSnapshot . pendingEphemeralStorage ;
45
+ stats . availableEphemeralStorage += provider . lastSuccessfulSnapshot . availableEphemeralStorage ;
46
+
47
+ stats . activePersistentStorage += provider . lastSuccessfulSnapshot . activePersistentStorage ;
48
+ stats . pendingPersistentStorage += provider . lastSuccessfulSnapshot . pendingPersistentStorage ;
49
+ stats . availablePersistentStorage += provider . lastSuccessfulSnapshot . availablePersistentStorage ;
50
+
51
+ return all ;
52
+ } ,
53
+ {
54
+ activeCPU : 0 ,
55
+ pendingCPU : 0 ,
56
+ availableCPU : 0 ,
57
+ activeGPU : 0 ,
58
+ pendingGPU : 0 ,
59
+ availableGPU : 0 ,
60
+ activeMemory : 0 ,
61
+ pendingMemory : 0 ,
62
+ availableMemory : 0 ,
63
+ activeStorage : 0 ,
64
+ pendingStorage : 0 ,
65
+ availableStorage : 0 ,
66
+ activeEphemeralStorage : 0 ,
67
+ pendingEphemeralStorage : 0 ,
68
+ availableEphemeralStorage : 0 ,
69
+ activePersistentStorage : 0 ,
70
+ pendingPersistentStorage : 0 ,
71
+ availablePersistentStorage : 0
72
+ }
73
+ ) ;
50
74
51
75
return {
76
+ activeProviderCount : filteredProviders . length ,
77
+ activeStorage : stats . activeEphemeralStorage + stats . activePersistentStorage ,
78
+ pendingStorage : stats . pendingEphemeralStorage + stats . pendingPersistentStorage ,
79
+ availableStorage : stats . availableEphemeralStorage + stats . availablePersistentStorage ,
52
80
...stats ,
53
81
totalCPU : stats . activeCPU + stats . pendingCPU + stats . availableCPU ,
54
82
totalGPU : stats . activeGPU + stats . pendingGPU + stats . availableGPU ,
@@ -96,10 +124,8 @@ export const getProviderList = async () => {
96
124
} ) ;
97
125
98
126
const distinctProviders = providersWithAttributesAndAuditors . filter ( ( value , index , self ) => self . map ( x => x . hostUri ) . lastIndexOf ( value . hostUri ) === index ) ;
99
- const providerAttributeSchemaQuery = getProviderAttributesSchema ( ) ;
100
- const auditorsQuery = getAuditors ( ) ;
101
127
102
- const [ auditors , providerAttributeSchema ] = await Promise . all ( [ auditorsQuery , providerAttributeSchemaQuery ] ) ;
128
+ const [ auditors , providerAttributeSchema ] = await Promise . all ( [ getAuditors ( ) , getProviderAttributesSchema ( ) ] ) ;
103
129
104
130
return distinctProviders . map ( x => {
105
131
const lastSuccessfulSnapshot = providerWithNodes . find ( p => p . owner === x . owner ) ?. lastSuccessfulSnapshot ;
@@ -151,10 +177,7 @@ export const getProviderDetail = async (address: string): Promise<ProviderDetail
151
177
} )
152
178
: null ;
153
179
154
- const providerAttributeSchemaQuery = getProviderAttributesSchema ( ) ;
155
- const auditorsQuery = getAuditors ( ) ;
156
-
157
- const [ auditors , providerAttributeSchema ] = await Promise . all ( [ auditorsQuery , providerAttributeSchemaQuery ] ) ;
180
+ const [ auditors , providerAttributeSchema ] = await Promise . all ( [ getAuditors ( ) , getProviderAttributesSchema ( ) ] ) ;
158
181
159
182
return {
160
183
...mapProviderToList ( provider , providerAttributeSchema , auditors , lastSuccessfulSnapshot ) ,
0 commit comments