Skip to content

Commit

Permalink
Optimize logging and add mouse and key press events log
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusongit committed Dec 6, 2023
1 parent 5135512 commit 3cfa16c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
38 changes: 35 additions & 3 deletions src/DynamoCore/Logging/DynamoAnalyticsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Autodesk.ADPDesktopSDK;
using Autodesk.Analytics.ADP;
using Autodesk.Analytics.Core;
using Autodesk.Analytics.Events;
Expand Down Expand Up @@ -97,6 +98,10 @@ public void Dispose() { }

public virtual IAnalyticsSession Session { get; private set; }

private DateTime LastMachineHBLogTime;
private DateTime LastUserHBLogTime;
private readonly int HeartBeatInterval = 4;

/// <summary>
/// Return if Analytics Client is allowed to send any analytics information
/// </summary>
Expand Down Expand Up @@ -261,8 +266,13 @@ public void TrackScreenView(string viewName)
}
/// <summary>
/// This API is used to track user/machine's activity status.
/// Note: This will not trigger the API at each call, instead it will
/// send out one call for every 4(HeartBeatInterval) minutes for each user and machine type activity.
/// For example, if the method gets called 100 times in 8 minutes, then it will only
/// trigger the HeartBeat API twice. This is to avoid sending out too many calls
/// as the API expects a call every 5 minutes, to mark the user/machine active.
/// </summary>
/// <param name="activityType">Value must be either machine or user. If no value is provided the API will default to user activity type.</param>
/// <param name="activityType">Value must be either Machine or User. If no value is provided the API will default to user activity type.</param>
public void TrackActivityStatus(string activityType)
{
if (Analytics.DisableAnalytics) return;
Expand All @@ -275,11 +285,33 @@ public void TrackActivityStatus(string activityType)
{
if (!ReportingAnalytics) return;

var hbType = (new[] { "machine", "user" }).Contains(activityType?.ToLower()) ? activityType : "user";
var hbType = (new[] { HeartBeatType.Machine.ToString(), HeartBeatType.User.ToString() }).Contains(activityType) ? activityType : HeartBeatType.User.ToString();
LogHeartBeat(hbType);
}
});
}

private void LogHeartBeat(string activityType)
{
if (activityType == HeartBeatType.Machine.ToString())
{
//Only send log if atleast 4 minutes have been passed since the last log.
if (LastMachineHBLogTime != null && DateTime.UtcNow > LastMachineHBLogTime.AddMinutes(HeartBeatInterval))
{
LastMachineHBLogTime = DateTime.UtcNow;
var e = new HeartBeatEvent(activityType);
e.Track();
}
});
}
else
{
if (LastUserHBLogTime != null && DateTime.UtcNow > LastUserHBLogTime.AddMinutes(HeartBeatInterval))
{
LastUserHBLogTime = DateTime.UtcNow;
var e = new HeartBeatEvent(activityType);
e.Track();
}
}
}

public void TrackException(Exception ex, bool isFatal)
Expand Down
1 change: 0 additions & 1 deletion src/DynamoCoreWpf/ViewModels/Core/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ private void SetCurrentState(State newState)

internal bool HandleLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Analytics.TrackActivityStatus(HeartBeatType.User.ToString());
if (ignoreMouseClick)
{
ignoreMouseClick = false;
Expand Down
2 changes: 2 additions & 0 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,7 @@ private void WindowClosed(object sender, EventArgs e)
// passes it to thecurrent workspace
private void DynamoView_KeyDown(object sender, KeyEventArgs e)
{
Analytics.TrackActivityStatus(HeartBeatType.User.ToString());
if (e.Key != Key.Escape || !IsMouseOver) return;

var vm = dynamoViewModel.BackgroundPreviewViewModel;
Expand Down Expand Up @@ -2784,6 +2785,7 @@ private void _resizeTimer_Tick(object sender, EventArgs e)

private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Analytics.TrackActivityStatus(HeartBeatType.User.ToString());
dynamoViewModel.IsMouseDown = true;
}

Expand Down

0 comments on commit 3cfa16c

Please sign in to comment.