Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

dev time tracker when user is idle for more than 5 mins #3861

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
152 changes: 152 additions & 0 deletions Ginger/Ginger/DevTimeTrackerIdle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using Amdocs.Ginger.Repository;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add license part at top of the class

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Threading;
using amdocs.ginger.GingerCoreNET;
using Amdocs.Ginger.Common;

namespace Ginger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add documentation for this class

{
public class DevTimeTrackerIdle
{
private readonly DispatcherTimer idleTimer;

Check notice on line 13 in Ginger/Ginger/DevTimeTrackerIdle.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Ginger/Ginger/DevTimeTrackerIdle.cs#L13

Remove the field 'idleTimer' and declare it as a local variable in the relevant methods.
private DateTime lastActivity;
private bool isPaused;


public DevTimeTrackerIdle()
{
idleTimer = new DispatcherTimer();
idleTimer.Interval = TimeSpan.FromMinutes(1); // Check every minute
idleTimer.Tick += IdleTimer_Tick;
idleTimer.Start();

lastActivity = DateTime.Now;
isPaused = false;
}

public void AttachActivityHandlers(UIElement element)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a destructor method which will remove the attached events.

element.MouseMove += OnActivity;
element.KeyDown += OnActivity;
element.MouseDown += OnActivity;
}


private void IdleTimer_Tick(object sender, EventArgs e)
{
if (DateTime.Now - lastActivity > TimeSpan.FromMinutes(5) && !isPaused)
{
PauseDevelopmentTimeTracker();
}
}

private void OnActivity(object sender, EventArgs e)
{
if (isPaused)
{
ResumeDevelopmentTimeTracker();
}

lastActivity = DateTime.Now;

}
private List<RepositoryItemBase> _itemsWithPausedDevelopmentTimeTracker = [];
AmanPrasad43 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Pauses the development time tracker for modified files in the solution.
/// </summary>
public void PauseDevelopmentTimeTracker()
{
if (WorkSpace.Instance == null ||
WorkSpace.Instance.SolutionRepository == null ||
WorkSpace.Instance.SolutionRepository.ModifiedFiles == null)
{
return;
}

try
{
_itemsWithPausedDevelopmentTimeTracker.Clear();
List<RepositoryItemBase> modifiedFiles = new(WorkSpace.Instance.SolutionRepository.ModifiedFiles);

foreach (RepositoryItemBase modifiedFile in modifiedFiles)
{
if (modifiedFile is GingerCore.BusinessFlow bf && bf.IsTimerRunning())
{
bf.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(bf);

foreach (GingerCore.Activity bfActivity in bf.Activities)
{
if (bfActivity.IsTimerRunning())
{
bfActivity.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(bfActivity);
}
}
}
else if (modifiedFile is GingerCore.Activity activity && activity.IsTimerRunning())
{
activity.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(activity);
}
else if (modifiedFile is Amdocs.Ginger.Repository.ApplicationPOMModel applicationPOMModel && applicationPOMModel.IsTimerRunning())
{
applicationPOMModel.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(applicationPOMModel);
}
}

//Setting flag to true
if (_itemsWithPausedDevelopmentTimeTracker.Count > 0)
{
isPaused = true;
}
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.DEBUG, "error while pausing development tracker", ex);
}
}

/// <summary>
/// Resumes the development time tracker for paused items.
/// </summary>
public void ResumeDevelopmentTimeTracker()
{
try
{
List<RepositoryItemBase> items = new(_itemsWithPausedDevelopmentTimeTracker);
_itemsWithPausedDevelopmentTimeTracker.Clear();

foreach (RepositoryItemBase item in items)
{
if (item is GingerCore.BusinessFlow bf)
{
bf.StartTimer();
}
else if (item is GingerCore.Activity activity)
{
activity.StartTimer();
}
else if (item is Amdocs.Ginger.Repository.ApplicationPOMModel applicationPOMModel)
{
applicationPOMModel.StartTimer();
}
}

//Setting flag to false
if (_itemsWithPausedDevelopmentTimeTracker.Count == 0)
{
isPaused = false;
}
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.DEBUG, "error while resuming development tracker", ex);
}
}
}
}
98 changes: 10 additions & 88 deletions Ginger/Ginger/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public partial class MainWindow : Window
public enum eSolutionTabType { None, BusinessFlows, Run, Configurations, Resources };
public eSolutionTabType SelectedSolutionTab;

private DevTimeTrackerIdle dttIdle; //for Pause/resume the development time

private bool mAskUserIfToClose = true;

ObservableList<HelpLayoutArgs> mHelpLayoutList = new ObservableList<HelpLayoutArgs>();
Expand All @@ -84,8 +86,13 @@ public enum eSolutionTabType { None, BusinessFlows, Run, Configurations, Resourc
public MainWindow()
{
InitializeComponent();

dttIdle = new();
dttIdle.AttachActivityHandlers(this);

StateChanged -= MainWindow_StateChanged;
StateChanged += MainWindow_StateChanged;

mRestartApplication = false;
lblAppVersion.Content = "Version " + Amdocs.Ginger.Common.GeneralLib.ApplicationInfo.ApplicationUIversion;
xVersionAndNewsIcon.Visibility = Visibility.Collapsed;
Expand All @@ -101,96 +108,11 @@ private void MainWindow_StateChanged(object? sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
PauseDevelopmentTimeTracker();
dttIdle.PauseDevelopmentTimeTracker();
}
else
{
ResumeDevelopmentTimeTracker();
}
}

private List<RepositoryItemBase> _itemsWithPausedDevelopmentTimeTracker = [];

/// <summary>
/// Pauses the development time tracker for modified files in the solution.
/// </summary>
private void PauseDevelopmentTimeTracker()
{
if (WorkSpace.Instance == null ||
WorkSpace.Instance.SolutionRepository == null ||
WorkSpace.Instance.SolutionRepository.ModifiedFiles == null)
{
return;
}

try
{
_itemsWithPausedDevelopmentTimeTracker.Clear();
List<RepositoryItemBase> modifiedFiles = new(WorkSpace.Instance.SolutionRepository.ModifiedFiles);

foreach (RepositoryItemBase modifiedFile in modifiedFiles)
{
if (modifiedFile is BusinessFlow bf && bf.IsTimerRunning())
{
bf.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(bf);

foreach (GingerCore.Activity bfActivity in bf.Activities)
{
if (bfActivity.IsTimerRunning())
{
bfActivity.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(bfActivity);
}
}
}
else if (modifiedFile is GingerCore.Activity activity && activity.IsTimerRunning())
{
activity.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(activity);
}
else if (modifiedFile is ApplicationPOMModel applicationPOMModel && applicationPOMModel.IsTimerRunning())
{
applicationPOMModel.StopTimer();
_itemsWithPausedDevelopmentTimeTracker.Add(applicationPOMModel);
}
}
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.DEBUG, "error while pausing development tracker", ex);
}
}

/// <summary>
/// Resumes the development time tracker for paused items.
/// </summary>
private void ResumeDevelopmentTimeTracker()
{
try
{
List<RepositoryItemBase> items = new(_itemsWithPausedDevelopmentTimeTracker);
_itemsWithPausedDevelopmentTimeTracker.Clear();

foreach (RepositoryItemBase item in items)
{
if (item is BusinessFlow bf)
{
bf.StartTimer();
}
else if (item is GingerCore.Activity activity)
{
activity.StartTimer();
}
else if (item is ApplicationPOMModel applicationPOMModel)
{
applicationPOMModel.StartTimer();
}
}
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.DEBUG, "error while resuming development tracker", ex);
dttIdle.ResumeDevelopmentTimeTracker();
}
}

Expand All @@ -204,7 +126,7 @@ private void TelemetryEventHandler(object sender, Telemetry.TelemetryEventArgs e


}

private void XVersionAndNewsIcon_MouseDown(object sender, MouseButtonEventArgs e)
{
xVersionAndNewsIcon.Visibility = Visibility.Collapsed;
Expand Down
Loading