@@ -28,13 +28,17 @@ export class OverviewService {
28
28
client : IORedis . Redis | IORedis . Cluster ,
29
29
) : Promise < DatabaseOverview > {
30
30
let nodesInfo = [ ] ;
31
+ let currentDbIndex = 0 ;
32
+
31
33
if ( client instanceof IORedis . Cluster ) {
34
+ currentDbIndex = get ( client , [ 'options' , 'db' ] , 0 ) ;
32
35
nodesInfo = await this . getNodesInfo ( client ) ;
33
36
} else {
37
+ currentDbIndex = get ( client , [ 'options' , 'db' ] , 0 ) ;
34
38
nodesInfo = [ await this . getNodeInfo ( client ) ] ;
35
39
}
36
40
37
- const [ totalKeys , totalKeysPerDb ] = this . calculateTotalKeys ( nodesInfo ) ;
41
+ const [ totalKeys , totalKeysPerDb ] = this . calculateTotalKeys ( nodesInfo , currentDbIndex ) ;
38
42
return {
39
43
version : this . getVersion ( nodesInfo ) ,
40
44
totalKeys,
@@ -192,9 +196,10 @@ export class OverviewService {
192
196
* Sum of keys for primary shards
193
197
* In case when shard has multiple logical databases shard total keys = sum of all dbs keys
194
198
* @param nodes
199
+ * @param index
195
200
* @private
196
201
*/
197
- private calculateTotalKeys ( nodes = [ ] ) : [ number , Record < string , number > ] {
202
+ private calculateTotalKeys ( nodes = [ ] , index : number ) : [ number , Record < string , number > ] {
198
203
if ( ! this . isMetricsAvailable ( nodes , 'keyspace' , [ undefined ] ) ) {
199
204
return [ undefined , undefined ] ;
200
205
}
@@ -221,15 +226,9 @@ export class OverviewService {
221
226
) ;
222
227
} ) ;
223
228
224
- // filter 0 values
225
- Object . keys ( totalKeysPerDb ) . forEach ( ( key ) => {
226
- if ( ! totalKeysPerDb [ key ] ) {
227
- delete totalKeysPerDb [ key ] ;
228
- }
229
- } ) ;
230
-
231
229
const totalKeys = totalKeysPerDb ? sum ( Object . values ( totalKeysPerDb ) ) : undefined ;
232
- return [ totalKeys , Object . keys ( totalKeysPerDb ) . length > 1 ? totalKeysPerDb : undefined ] ;
230
+ const dbIndexKeys = totalKeysPerDb [ `db${ index } ` ] || 0 ;
231
+ return [ totalKeys , dbIndexKeys === totalKeys ? undefined : { [ `db${ index } ` ] : dbIndexKeys } ] ;
233
232
} catch ( e ) {
234
233
return [ null , null ] ;
235
234
}
0 commit comments