From aeaf443f675e6ef6ffa4d7346bbc20431c46cb1b Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 18 Jun 2009 19:47:57 -0400 Subject: [PATCH] Enhancement - statistics - Added total data file size to statistics report. - Implemented util.cpp/formatBytes. --- src/report.cpp | 20 ++++++++++++++++++++ src/util.cpp | 14 ++++++++++++++ src/util.h | 1 + 3 files changed, 35 insertions(+) diff --git a/src/report.cpp b/src/report.cpp index e5a2fddf6..89c40b938 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -1594,6 +1595,21 @@ std::string handleReportStats () { std::stringstream out; + // Go get the file sizes. + size_t dataSize = 0; + + struct stat s; + std::string location = expandPath (context.config.get ("data.location")); + std::string file = location + "/pending.data"; + if (!stat (file.c_str (), &s)) + dataSize += s.st_size; + + file = location + "/completed.data"; + if (!stat (file.c_str (), &s)) + dataSize += s.st_size; + + // TODO Include transaction log? + // Get all the tasks. std::vector tasks; context.tdb.lock (context.config.get ("locking", true)); @@ -1712,6 +1728,10 @@ std::string handleReportStats () table.addCell (row, 0, "Projects"); table.addCell (row, 1, (int)allProjects.size ()); + row = table.addRow (); + table.addCell (row, 0, "Data size"); + table.addCell (row, 1, formatBytes (dataSize)); + if (totalT) { row = table.addRow (); diff --git a/src/util.cpp b/src/util.cpp index 4afb0f3a8..96ddbc7d9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -147,6 +147,20 @@ std::string formatSecondsCompact (time_t delta) return std::string (formatted); } +//////////////////////////////////////////////////////////////////////////////// +// Convert a quantity in seconds to a more readable format. +std::string formatBytes (size_t bytes) +{ + char formatted[24]; + + if (bytes > 1000000000) sprintf (formatted, "%.1f GiB", (bytes / 1000000000.0)); + else if (bytes > 1000000) sprintf (formatted, "%.1f MiB", (bytes / 1000000.0)); + else if (bytes > 1000) sprintf (formatted, "%.1f KiB", (bytes / 1000.0)); + else sprintf (formatted, "%d B", (int)bytes ); + + return commify (formatted); +} + //////////////////////////////////////////////////////////////////////////////// int autoComplete ( const std::string& partial, diff --git a/src/util.h b/src/util.h index 350ffac30..b72e8303f 100644 --- a/src/util.h +++ b/src/util.h @@ -54,6 +54,7 @@ bool confirm (const std::string&); void delay (float); std::string formatSeconds (time_t); std::string formatSecondsCompact (time_t); +std::string formatBytes (size_t); int autoComplete (const std::string&, const std::vector&, std::vector&); const std::string uuid (); int convertDuration (const std::string&);