Skip to content
Snippets Groups Projects
Commit cce8b3f6 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Convert time spans to human readable variants.

All outputs of any kind of duration should be using a human readable
variant of the time span.

Fixes #25
parent 4ea2d8bd
No related branches found
No related tags found
No related merge requests found
......@@ -137,8 +137,9 @@ namespace ATXCommon
foreach (var userdir in expired.Keys) {
report += "\n - user '" + userdir + "'\n";
foreach (var subdir in expired[userdir]) {
report += string.Format(" - {0} [age: {2} days, size: {1}]\n",
subdir.Item1, Conv.BytesToString(subdir.Item2), subdir.Item3);
report += string.Format(" - {0} [age: {2}, size: {1}]\n",
subdir.Item1, Conv.BytesToString(subdir.Item2),
TimeUtils.DaysToHuman(subdir.Item3));
}
}
if (string.IsNullOrEmpty(report))
......
......@@ -322,13 +322,14 @@ namespace ATXCommon.Serializables
"IncomingDirectory: " + IncomingDirectory + "\n" +
"MarkerFile: " + MarkerFile + "\n" +
"ManagedDirectory: " + ManagedDirectory + "\n" +
"GracePeriod: " + GracePeriod + "\n" +
"GracePeriod: " + GracePeriod + " (" +
TimeUtils.DaysToHuman(GracePeriod) + ")\n" +
"DestinationDirectory: " + DestinationDirectory + "\n" +
"TmpTransferDir: " + TmpTransferDir + "\n" +
"EnforceInheritedACLs: " + EnforceInheritedACLs + "\n" +
"ServiceTimer: " + ServiceTimer + "\n" +
"ServiceTimer: " + ServiceTimer + " ms\n" +
"InterPacketGap: " + InterPacketGap + "\n" +
"MaxCpuUsage: " + MaxCpuUsage + "\n" +
"MaxCpuUsage: " + MaxCpuUsage + "%\n" +
"MinAvailableMemory: " + MinAvailableMemory + "\n";
foreach (var processName in BlacklistedProcesses) {
msg += "BlacklistedProcess: " + processName + "\n";
......@@ -347,9 +348,12 @@ namespace ATXCommon.Serializables
"EmailFrom: " + EmailFrom + "\n" +
"AdminEmailAdress: " + AdminEmailAdress + "\n" +
"AdminDebugEmailAdress: " + AdminDebugEmailAdress + "\n" +
"StorageNotificationDelta: " + StorageNotificationDelta + "\n" +
"AdminNotificationDelta: " + AdminNotificationDelta + "\n" +
"GraceNotificationDelta: " + GraceNotificationDelta + "\n";
"StorageNotificationDelta: " + StorageNotificationDelta + " (" +
TimeUtils.MinutesToHuman(StorageNotificationDelta) + ")\n" +
"AdminNotificationDelta: " + AdminNotificationDelta + " (" +
TimeUtils.MinutesToHuman(AdminNotificationDelta) + ")\n" +
"GraceNotificationDelta: " + GraceNotificationDelta + " (" +
TimeUtils.MinutesToHuman(GraceNotificationDelta) + ")\n";
}
return msg;
}
......
......@@ -283,13 +283,21 @@ namespace ATXCommon.Serializables
"TransferInProgress: " + TransferInProgress + "\n" +
"CurrentTransferSize: " + CurrentTransferSize + "\n" +
"LastStatusUpdate: " +
LastStatusUpdate.ToString("yyyy-MM-dd HH:mm:ss") + "\n" +
LastStatusUpdate.ToString("yyyy-MM-dd HH:mm:ss") + " (" +
TimeUtils.SecondsToHuman(TimeUtils.SecondsSince(LastStatusUpdate)) +
" ago)\n" +
"LastStorageNotification: " +
LastStorageNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n" +
LastStorageNotification.ToString("yyyy-MM-dd HH:mm:ss") + " (" +
TimeUtils.SecondsToHuman(TimeUtils.SecondsSince(LastStorageNotification)) +
" ago)\n" +
"LastAdminNotification: " +
LastAdminNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n" +
LastAdminNotification.ToString("yyyy-MM-dd HH:mm:ss") + " (" +
TimeUtils.SecondsToHuman(TimeUtils.SecondsSince(LastAdminNotification)) +
" ago)\n" +
"LastGraceNotification: " +
LastGraceNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
LastGraceNotification.ToString("yyyy-MM-dd HH:mm:ss") + " (" +
TimeUtils.SecondsToHuman(TimeUtils.SecondsSince(LastGraceNotification)) +
" ago)\n";
}
#endregion validate and report
......
......@@ -29,5 +29,58 @@ namespace ATXCommon
public static int SecondsSince(DateTime refDate) {
return (int)(DateTime.Now - refDate).TotalSeconds;
}
/// <summary>
/// Convert a number of seconds to a human readable string.
/// </summary>
/// <param name="delta">The time span in seconds.</param>
/// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns>
public static string SecondsToHuman(int delta) {
const int second = 1;
const int minute = second * 60;
const int hour = minute * 60;
const int day = hour * 24;
const int week = day * 7;
if (delta < minute)
return delta + " seconds";
if (delta < 2 * minute)
return "a minute";
if (delta < hour)
return delta / minute + " minutes";
if (delta < day) {
var hours = delta / hour;
var mins = (delta - hours * hour) / minute;
if (mins > 0)
return hours + " hours " + mins + " minutes";
return hours + " hours";
}
if (delta < 2 * week)
return delta / day + " days";
return delta / week + " weeks";
}
/// <summary>
/// Wrapper to use <see cref="SecondsToHuman"/> with minutes as input.
/// </summary>
/// <param name="delta">The time span in minutes.</param>
/// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns>
public static string MinutesToHuman(int delta) {
return SecondsToHuman(delta * 60);
}
/// <summary>
/// Wrapper to use <see cref="SecondsToHuman"/> with days as input.
/// </summary>
/// <param name="delta">The time span in days.</param>
/// <returns>A string describing the duration, e.g. "12 days" or "3 weeks".</returns>
public static string DaysToHuman(int delta) {
return MinutesToHuman(delta * 60 * 24);
}
}
}
......@@ -417,8 +417,9 @@ namespace AutoTx
if (_mainTimer.Interval > maxInterval)
_mainTimer.Interval = maxInterval;
Log.Error("Unhandled exception in OnTimedEvent(): {0}\n\n" +
"Trying exponential backoff, setting timer interval to {1} ms.\n\n" +
"StackTrace: {2}", ex.Message, _mainTimer.Interval, ex.StackTrace);
"Trying exponential backoff, setting timer interval to {1} ms ({3}).\n\n" +
"StackTrace: {2}", ex.Message, _mainTimer.Interval, ex.StackTrace,
TimeUtils.SecondsToHuman((int)_mainTimer.Interval / 1000));
}
finally {
// make sure to enable the timer again:
......
......@@ -95,7 +95,8 @@ namespace AutoTx
var delta = TimeUtils.MinutesSince(_status.LastAdminNotification);
if (delta < _config.AdminNotificationDelta) {
Log.Warn("Suppressed admin email, interval too short ({0} vs. {1}):\n\n{2}\n{3}",
delta, _config.AdminNotificationDelta, subject, body);
TimeUtils.MinutesToHuman(delta),
TimeUtils.MinutesToHuman(_config.AdminNotificationDelta), subject, body);
return;
}
......@@ -123,7 +124,8 @@ namespace AutoTx
var delta = TimeUtils.MinutesSince(_status.LastStorageNotification);
if (delta < _config.StorageNotificationDelta) {
Log.Trace("Only {0} minutes since last low-space-notification, skipping.", delta);
Log.Trace("Only {0} since last low-space-notification, skipping.",
TimeUtils.MinutesToHuman(delta));
return;
}
......@@ -234,7 +236,8 @@ namespace AutoTx
return "";
var delta = TimeUtils.MinutesSince(_status.LastGraceNotification);
report += "\nTime since last grace notification: " + delta + "\n";
report += "\nTime since last grace notification: " +
TimeUtils.MinutesToHuman(delta) + "\n";
if (delta < _config.GraceNotificationDelta)
return report;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment