This commit is contained in:
2020-06-02 15:59:29 +00:00
parent 843599e452
commit 06147627ac

View File

@@ -206,11 +206,11 @@ namespace AyaNova.Api.Controllers
dsDBTotalSize = Util.DataUtil.LargestTriangleThreeBuckets(dsDBTotalSize, (int)maxRecords) as List<Tuple<double, double>>;
//table distribution, top 10
List<MetricNameSize> TopTen = new List<MetricNameSize>();
List<MetricNameLongValue> TopTables = new List<MetricNameLongValue>();
using (var command = ct.Database.GetDbConnection().CreateCommand())
{
//top 10 tables by size
command.CommandText = @"SELECT relname AS ""table_name"", pg_table_size(C.oid) AS ""table_size"" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND nspname !~ '^pg_toast' AND relkind IN ('r') ORDER BY pg_table_size(C.oid) DESC LIMIT 10;";
command.CommandText = @"SELECT relname AS ""table_name"", pg_table_size(C.oid) AS ""table_size"" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND nspname !~ '^pg_toast' AND relkind IN ('r') ORDER BY pg_table_size(C.oid) DESC;";
ct.Database.OpenConnection();
using (var dr = command.ExecuteReader())
{
@@ -218,13 +218,21 @@ namespace AyaNova.Api.Controllers
{
while (dr.Read())
{
TopTen.Add(new MetricNameSize() { name = dr.GetString(0), size = dr.GetInt64(1) });
long tableSize = dr.GetInt64(1);
if (tableSize > 0)
{
tableSize = tableSize / MB;
}
TopTables.Add(new MetricNameLongValue() { name = dr.GetString(0), value = tableSize });
}
}
ct.Database.CloseConnection();
}
}
//trim out tables we don't want here
TopTables = TopTables.Where(z => z.value > 0).OrderByDescending(z => z.value).ToList();
long DBTotalSize = 0;
using (var command = ct.Database.GetDbConnection().CreateCommand())
{
@@ -234,25 +242,23 @@ namespace AyaNova.Api.Controllers
{
if (dr.HasRows)
{
DBTotalSize = dr.Read() ? dr.GetInt64(0) : 0;
DBTotalSize = dr.Read() ? (dr.GetInt64(0) / MB) : 0;
}
ct.Database.CloseConnection();
}
}
long ttSize = 0;
foreach (MetricNameSize tt in TopTen)
foreach (MetricNameLongValue tt in TopTables)
{
ttSize += tt.size;
ttSize += tt.value;
}
TopTen.Add(new MetricNameSize() { name = "other", size = DBTotalSize - ttSize });
TopTables.Add(new MetricNameLongValue() { name = "other", value = DBTotalSize - ttSize });
var ret = new
{
totalSize = dsDBTotalSize.Select(z => new MetricLong(DateTime.FromOADate(z.Item1), z.Item2 / MB)).ToArray(),
TopTen = TopTen
TopTables = TopTables,
totalSize = dsDBTotalSize.Select(z => new MetricLong(DateTime.FromOADate(z.Item1), z.Item2 / MB)).ToArray()
};
@@ -300,10 +306,10 @@ namespace AyaNova.Api.Controllers
}
}
public class MetricNameSize
public class MetricNameLongValue
{
public string name { get; set; }
public long size { get; set; }
public long value { get; set; }
}
//----------