From 7d41abd7b83b73540e65326c54ea5a85da5fcec6 Mon Sep 17 00:00:00 2001 From: Catcher Wong Date: Tue, 27 Aug 2024 08:54:42 +0800 Subject: [PATCH] support database stat (#60) (#67) Signed-off-by: catcherwong --- build/version.props | 4 +-- src/RDBCli/Commands/MemoryCommand.cs | 3 ++ src/RDBCli/Helpers/CommonHelper.Const.cs | 4 +-- src/RDBCli/Stats/PrefixRecord.cs | 23 ++++++++++++ src/RDBCli/Stats/RdbDataCounter.cs | 18 ++++++++++ src/RDBCli/Tpl/tpl.html | 45 ++++++++++++++++++++---- 6 files changed, 87 insertions(+), 10 deletions(-) diff --git a/build/version.props b/build/version.props index ebc429e..d544c77 100644 --- a/build/version.props +++ b/build/version.props @@ -1,8 +1,8 @@ - 0.9.4 - 0.9.4 + 0.9.5 + 0.9.5 \ No newline at end of file diff --git a/src/RDBCli/Commands/MemoryCommand.cs b/src/RDBCli/Commands/MemoryCommand.cs index 734432f..d4d4655 100644 --- a/src/RDBCli/Commands/MemoryCommand.cs +++ b/src/RDBCli/Commands/MemoryCommand.cs @@ -84,6 +84,7 @@ private void Do(InvocationContext context, CommandOptions options) var expiryInfo = counter.GetExpiryInfo(); var streamRecords = counter.GetStreamRecords(); var idleOrFreqInfo = counter.GetIdleOrFreqInfo(); + var dbInfo = counter.GetDatabaseInfo(); var dict = MemoryAnslysisResult.BuildBasicFromRdbDataInfo(cb.GetRdbDataInfo()); dict.typeRecords = typeRecords; @@ -92,6 +93,7 @@ private void Do(InvocationContext context, CommandOptions options) dict.expiryInfo = expiryInfo; dict.largestStreams = streamRecords; dict.idleOrFreqInfo = idleOrFreqInfo; + dict.dbInfo = dbInfo; var exp = expiryInfo.FirstOrDefault(x => x.Expiry.Equals(CommonHelper.AlreadyExpired)); var perm = expiryInfo.FirstOrDefault(x => x.Expiry.Equals(CommonHelper.Permanent)); @@ -250,6 +252,7 @@ public class MemoryAnslysisResult public List idleOrFreqInfo { get; set; } public List functions { get; set; } public List largestStreams { get; set; } + public List dbInfo { get; set; } internal static MemoryAnslysisResult BuildBasicFromRdbDataInfo(RdbDataInfo rdbDataInfo) { diff --git a/src/RDBCli/Helpers/CommonHelper.Const.cs b/src/RDBCli/Helpers/CommonHelper.Const.cs index 00fd515..13b5e61 100644 --- a/src/RDBCli/Helpers/CommonHelper.Const.cs +++ b/src/RDBCli/Helpers/CommonHelper.Const.cs @@ -5,8 +5,8 @@ namespace RDBCli internal static partial class CommonHelper { // compress Tpl/tpl.html to get this value. - internal const string TplHtmlString = "rdb-cli,offline key analysis

Basic Information


RDB Version:
Redis Version:

Total Memory Usage of Keys

Total Number of Keys


Expired Keys

Memory Usage by Expired Keys

Permanent Keys

Memory Usage by Permanent Keys


Details Information








" + - "" + + internal const string TplHtmlString = "rdb-cli, offline key analysis

Basic Information


RDB Version:
Redis Version:

Total Memory Usage of Keys

Total Number of Keys


Expired Keys

Memory Usage by Expired Keys

Permanent Keys

Memory Usage by Permanent Keys


Details Information










" + + "" + ""; internal const string AlreadyExpired = "Already Expired"; diff --git a/src/RDBCli/Stats/PrefixRecord.cs b/src/RDBCli/Stats/PrefixRecord.cs index 00a1758..3519997 100644 --- a/src/RDBCli/Stats/PrefixRecord.cs +++ b/src/RDBCli/Stats/PrefixRecord.cs @@ -85,6 +85,29 @@ public override string ToString() } } + public class DBRecord + { + /// + /// The redis type, such as string hash.. + /// + public string DB { get; set; } + + /// + /// The redis type's total bytes + /// + public ulong Bytes { get; set; } + + /// + /// The redis type's total count + /// + public ulong Num { get; set; } + + public override string ToString() + { + return $"{DB}-{Bytes}-{Num}"; + } + } + public class ExpiryRecord { /// diff --git a/src/RDBCli/Stats/RdbDataCounter.cs b/src/RDBCli/Stats/RdbDataCounter.cs index 3047a31..8d02d80 100644 --- a/src/RDBCli/Stats/RdbDataCounter.cs +++ b/src/RDBCli/Stats/RdbDataCounter.cs @@ -18,6 +18,7 @@ internal class RdbDataCounter private Dictionary _typeDict; private Dictionary _expiryDict; private Dictionary _idleOrFreqDict; + private Dictionary _dbDict; private readonly MemoryCallback _cb; private readonly BlockingCollection _records; @@ -35,6 +36,7 @@ public RdbDataCounter(MemoryCallback cb, string separators = "", int sepCount = this._typeDict = new Dictionary(); this._expiryDict = new Dictionary(); this._idleOrFreqDict = new Dictionary(); + this._dbDict = new Dictionary(); if (!string.IsNullOrWhiteSpace(separators)) { @@ -61,6 +63,7 @@ public Task Count() this.CounteByIdleOrFreq(item.Record); this.CountByKeyPrefix(item.Record); this.CountExpiry(item.Record); + this.CountDb(item.Record); this.CountStreams(item.StreamsRecord, 500); } else @@ -99,6 +102,15 @@ public List GetLargestRecords(int num = 100) .ToList(); } + public List GetDatabaseInfo(int num = 10) + { + return _dbDict + .Select(x => new DBRecord { DB = x.Key, Num = x.Value.Num, Bytes = x.Value.Bytes }) + .OrderByDescending(x => x.Bytes) + .Take(num) + .ToList(); + } + public List GetTypeRecords() { return _typeDict @@ -131,6 +143,12 @@ public List GetStreamRecords(int num = 100) .ToList(); } + private void CountDb(Record record) + { + var key = $"db{record.Database}"; + InitOrAddStat(this._dbDict, key, record.Bytes); + } + private void CountExpiry(Record item) { var key = CommonHelper.GetExpireString(item.Expiry); diff --git a/src/RDBCli/Tpl/tpl.html b/src/RDBCli/Tpl/tpl.html index 3094802..dc8d2af 100644 --- a/src/RDBCli/Tpl/tpl.html +++ b/src/RDBCli/Tpl/tpl.html @@ -6,13 +6,13 @@ rdb-cli, offline key analysis - - + + - - - - + + + + @@ -116,6 +116,11 @@



+
+
+
+
+