Skip to content

Commit

Permalink
support database stat (#60)
Browse files Browse the repository at this point in the history
Signed-off-by: catcherwong <catcher_hwq@outlook.com>
  • Loading branch information
catcherwong committed Aug 26, 2024
1 parent d05b758 commit 80de6c0
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
4 changes: 2 additions & 2 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>

<RDBParserVersion>0.9.4</RDBParserVersion>
<RDBCliVersion>0.9.4</RDBCliVersion>
<RDBParserVersion>0.9.5</RDBParserVersion>
<RDBCliVersion>0.9.5</RDBCliVersion>

</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions src/RDBCli/Commands/MemoryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -250,6 +252,7 @@ public class MemoryAnslysisResult
public List<IdleOrFreqRecord> idleOrFreqInfo { get; set; }
public List<FunctionsRecord> functions { get; set; }
public List<StreamsRecord> largestStreams { get; set; }
public List<DBRecord> dbInfo { get; set; }

internal static MemoryAnslysisResult BuildBasicFromRdbDataInfo(RdbDataInfo rdbDataInfo)
{
Expand Down
4 changes: 2 additions & 2 deletions src/RDBCli/Helpers/CommonHelper.Const.cs

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/RDBCli/Stats/PrefixRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ public override string ToString()
}
}

public class DBRecord
{
/// <summary>
/// The redis type, such as string hash..
/// </summary>
public string DB { get; set; }

/// <summary>
/// The redis type's total bytes
/// </summary>
public ulong Bytes { get; set; }

/// <summary>
/// The redis type's total count
/// </summary>
public ulong Num { get; set; }

public override string ToString()
{
return $"{DB}-{Bytes}-{Num}";
}
}

public class ExpiryRecord
{
/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions src/RDBCli/Stats/RdbDataCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class RdbDataCounter
private Dictionary<string, CommonStatValue> _typeDict;
private Dictionary<string, CommonStatValue> _expiryDict;
private Dictionary<string, CommonStatValue> _idleOrFreqDict;
private Dictionary<string, CommonStatValue> _dbDict;

private readonly MemoryCallback _cb;
private readonly BlockingCollection<AnalysisRecord> _records;
Expand All @@ -35,6 +36,7 @@ public RdbDataCounter(MemoryCallback cb, string separators = "", int sepCount =
this._typeDict = new Dictionary<string, CommonStatValue>();
this._expiryDict = new Dictionary<string, CommonStatValue>();
this._idleOrFreqDict = new Dictionary<string, CommonStatValue>();
this._dbDict = new Dictionary<string, CommonStatValue>();

if (!string.IsNullOrWhiteSpace(separators))
{
Expand All @@ -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
Expand Down Expand Up @@ -99,6 +102,15 @@ public List<Record> GetLargestRecords(int num = 100)
.ToList();
}

public List<DBRecord> 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<TypeRecord> GetTypeRecords()
{
return _typeDict
Expand Down Expand Up @@ -131,6 +143,12 @@ public List<StreamsRecord> 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);
Expand Down
45 changes: 39 additions & 6 deletions src/RDBCli/Tpl/tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>rdb-cli, offline key analysis</title>
<link rel="stylesheet" href="https://unpkg.com/bootstrap@5.1.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table.min.css">
<link rel="stylesheet" href="https://{{CDNDOMAIN}}/bootstrap@5.1.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://{{CDNDOMAIN}}/bootstrap-table@1.19.1/dist/bootstrap-table.min.css">
<style> .bootstrap-table .fixed-table-pagination > .pagination-detail .page-list { display: none }</style>
<script src="https://unpkg.com/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://unpkg.com/bootstrap@5.1.3/dist/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/chart.js@3.7.1/dist/chart.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table.min.js"></script>
<script src="https://{{CDNDOMAIN}}/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://{{CDNDOMAIN}}/bootstrap@5.1.3/dist/js/bootstrap.min.js"></script>
<script src="https://{{CDNDOMAIN}}/chart.js@3.7.1/dist/chart.min.js"></script>
<script src="https://{{CDNDOMAIN}}/bootstrap-table@1.19.1/dist/bootstrap-table.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -116,6 +116,11 @@ <h3>
<div class="col-md-6"><canvas id="idleOrFreqByte" width="400" height="400"></canvas></div>
</div>
<br /><br />
<div class="row">
<div class="col-md-6"><canvas id="databaseNum" width="400" height="400"></canvas></div>
<div class="col-md-6"><canvas id="databaseByte" width="400" height="400"></canvas></div>
</div>
<br /><br />
<div class="row">
<div class="col-md-12">
<ul class="nav nav-tabs" role="tablist">
Expand Down Expand Up @@ -176,6 +181,8 @@ <h3>
buildExpiryByteChart();
buildIdleOrFreqNumChart();
buildIdleOrFreqByteChart();
buildDatabaseNumChart();
buildDatabaseByteChart();

$('#topPrefixTable').bootstrapTable({
data: cliData.largestKeyPrefix,
Expand Down Expand Up @@ -354,6 +361,32 @@ <h3>
'expiryByte');
};

function buildDatabaseNumChart() {
const dbLabels = cliData.dbInfo.map(item => item.DB);
const dbData = cliData.dbInfo.map(item => item.Num);

buildBarChart(
dbLabels,
dbData,
'Total Keys',
'Distribution of Key Database (Quantity)',
formatNumber,
'databaseNum');
};

function buildDatabaseByteChart() {
const dbLabels = cliData.dbInfo.map(item => item.DB);
const dbData = cliData.dbInfo.map(item => item.Bytes);

buildBarChart(
dbLabels,
dbData,
'Memory Usage of Keys',
'Distribution of Key Database (Memory)',
formatBytes,
'databaseByte');
};

function buildIdleOrFreqNumChart() {
const labels = cliData.idleOrFreqInfo.map(item => item.Category);
const data = cliData.idleOrFreqInfo.map(item => item.Num);
Expand Down

0 comments on commit 80de6c0

Please sign in to comment.