Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add "total time today" properties and updated events #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/WakaTime.Shared.ExtensionUtils/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private async Task CheckAndInstallCli()
catch (Exception ex)
{
if (!installed) throw;
Logger.Error($"Error updating", ex);
_logger.Error($"Error updating", ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace WakaTime.Shared.ExtensionUtils
{
public class TotalTimeTodayUpdatedEventArgs : EventArgs
{
public string TotalTimeToday { get; }
public string TotalTimeTodayDetailed { get; }

public TotalTimeTodayUpdatedEventArgs(string totalTimeToday, string totalTimeTodayDetailed)
{
TotalTimeToday = totalTimeToday;
TotalTimeTodayDetailed = totalTimeTodayDetailed;
}
}
}
97 changes: 89 additions & 8 deletions src/WakaTime.Shared.ExtensionUtils/WakaTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class WakaTime : IDisposable
private readonly Metadata _metadata;
private readonly CliParameters _cliParameters;
private readonly Dependencies _dependencies;
private readonly Timer _timer;
private readonly Timer _heartbeatsProcessTimer;
private readonly Timer _totalTimeTodayUpdateTimer;

private string _lastFile;
private DateTime _lastHeartbeat;
Expand All @@ -23,6 +24,21 @@ public class WakaTime : IDisposable

public ILogger Logger { get; }

/// <summary>
/// A string like "3 hrs 42 mins".
/// </summary>
public string TotalTimeToday { get; private set; }

/// <summary>
/// A string like "3 hrs 4 mins Coding, 20 mins Building, 18 mins Debugging".
/// </summary>
public string TotalTimeTodayDetailed { get; private set; }

/// <summary>
/// Fired when <see cref="TotalTimeToday"/> and <see cref="TotalTimeTodayDetailed"/> are updated.
/// </summary>
public event EventHandler<TotalTimeTodayUpdatedEventArgs> TotalTimeTodayUpdated;

public WakaTime(Metadata metadata, ILogger logger)
{
if (metadata is null)
Expand All @@ -43,7 +59,8 @@ public WakaTime(Metadata metadata, ILogger logger)
$"{_metadata.EditorName}/{_metadata.EditorVersion} {_metadata.PluginName}/{_metadata.PluginVersion}"
};
_dependencies = new Dependencies(logger, Config);
_timer = new Timer(10000);
_heartbeatsProcessTimer = new Timer(10000);
_totalTimeTodayUpdateTimer = new Timer(60000);
_lastHeartbeat = DateTime.UtcNow.AddMinutes(-3);
}

Expand All @@ -55,8 +72,12 @@ public async Task InitializeAsync()
{
await _dependencies.CheckAndInstall();

_timer.Elapsed += ProcessHeartbeats;
_timer.Start();
_heartbeatsProcessTimer.Elapsed += ProcessHeartbeats;
_heartbeatsProcessTimer.Start();

UpdateTotalTimeToday(null, null); // Invoke the event handler immediately
_totalTimeTodayUpdateTimer.Elapsed += UpdateTotalTimeToday;
_totalTimeTodayUpdateTimer.Start();

Logger.Info($"Finished initializing WakaTime v{_metadata.PluginVersion}");
}
Expand Down Expand Up @@ -190,13 +211,73 @@ private void ProcessHeartbeats()
}
}

private void UpdateTotalTimeToday(object sender, ElapsedEventArgs e)
{
_ = Task.Run(() =>
{
// ReSharper disable once ConvertClosureToMethodGroup
UpdateTotalTimeToday();
});
}

private void UpdateTotalTimeToday()
{
var binary = _dependencies.GetCliLocation();
var apiKey = Config.GetSetting("api_key");

Logger.Debug("Fetching TotalTimeToday...");
var totalTimeTodayProcess = new RunProcess(
binary,
"--key", apiKey,
"--today",
"--today-hide-categories", "true"
);
totalTimeTodayProcess.Run();
string totalTimeToday = totalTimeTodayProcess.Output.Trim();
Logger.Debug($"Fetched TotalTimeToday: {totalTimeToday}");

if (!string.IsNullOrEmpty(totalTimeToday))
{
TotalTimeToday = totalTimeToday;
}

Logger.Debug("Fetching TotalTimeTodayDetailed...");
var totalTimeTodayDetailedProcess = new RunProcess(
binary,
"--key", apiKey,
"--today",
"--today-hide-categories", "false"
);
totalTimeTodayDetailedProcess.Run();
string totalTimeTodayDetailed = totalTimeTodayDetailedProcess.Output.Trim();
Logger.Debug($"Fetched TotalTimeTodayDetailed: {totalTimeTodayDetailed}");

if (!string.IsNullOrEmpty(totalTimeTodayDetailed))
{
TotalTimeTodayDetailed = totalTimeTodayDetailed;
}

// If fetch was successful, fire "TotalTimeTodayUpdated" event
if (!(string.IsNullOrEmpty(totalTimeToday) && string.IsNullOrEmpty(totalTimeTodayDetailed)))
{
TotalTimeTodayUpdated?.Invoke(this, new TotalTimeTodayUpdatedEventArgs(TotalTimeToday, TotalTimeTodayDetailed));
}
}

public void Dispose()
{
if (_timer != null)
if (_heartbeatsProcessTimer != null)
{
_heartbeatsProcessTimer.Stop();
_heartbeatsProcessTimer.Elapsed -= ProcessHeartbeats;
_heartbeatsProcessTimer.Dispose();
}

if (_totalTimeTodayUpdateTimer != null)
{
_timer.Stop();
_timer.Elapsed -= ProcessHeartbeats;
_timer.Dispose();
_totalTimeTodayUpdateTimer.Stop();
_totalTimeTodayUpdateTimer.Elapsed -= UpdateTotalTimeToday;
_totalTimeTodayUpdateTimer.Dispose();
}

// make sure the queue is empty
Expand Down