Skip to content

Commit

Permalink
Settings telemetry for PT Run (#10328)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykhailopylyp committed Mar 19, 2021
1 parent 057e92a commit 612e9f8
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/common/interop/interop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public
return gcnew String(CommonSharedConstants::POWER_LAUNCHER_SHARED_EVENT);
}

static String ^ RunSendSettingsTelemetryEvent() {
return gcnew String(CommonSharedConstants::RUN_SEND_SETTINGS_TELEMETRY_EVENT);
}

static String ^ ShowColorPickerSharedEvent() {
return gcnew String(CommonSharedConstants::SHOW_COLOR_PICKER_SHARED_EVENT);
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/interop/shared_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace CommonSharedConstants
// Path to the event used by PowerLauncher
const wchar_t POWER_LAUNCHER_SHARED_EVENT[] = L"Local\\PowerToysRunInvokeEvent-30f26ad7-d36d-4c0e-ab02-68bb5ff3c4ab";

const wchar_t RUN_SEND_SETTINGS_TELEMETRY_EVENT[] = L"Local\\PowerToysRunInvokeEvent-638ec522-0018-4b96-837d-6bd88e06f0d6";

// Path to the event used to show Color Picker
const wchar_t SHOW_COLOR_PICKER_SHARED_EVENT[] = L"Local\\ShowColorPickerEvent-8c46be2a-3e05-4186-b56b-4ae986ef2525";

Expand Down
4 changes: 4 additions & 0 deletions src/modules/interface/powertoy_module_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class PowertoyModuleIface
* if the key press is to be swallowed.
*/
virtual bool on_hotkey(size_t hotkeyId) { return false; }

virtual void send_settings_telemetry()
{
}
};

/*
Expand Down
28 changes: 23 additions & 5 deletions src/modules/launcher/Microsoft.Launcher/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class Microsoft_Launcher : public PowertoyModuleIface
// Handle to event used to invoke the Runner
HANDLE m_hEvent;

HANDLE send_telemetry_event;

SECURITY_ATTRIBUTES getDefaultSecurityAttribute()
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.bInheritHandle = false;
sa.lpSecurityDescriptor = NULL;
return sa;
}

public:
// Constructor
Microsoft_Launcher()
Expand All @@ -93,11 +104,10 @@ class Microsoft_Launcher : public PowertoyModuleIface
Logger::info("Launcher object is constructing");
init_settings();

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.bInheritHandle = false;
sa.lpSecurityDescriptor = NULL;
m_hEvent = CreateEventW(&sa, FALSE, FALSE, CommonSharedConstants::POWER_LAUNCHER_SHARED_EVENT);
auto sa1 = getDefaultSecurityAttribute();
m_hEvent = CreateEventW(&sa1, FALSE, FALSE, CommonSharedConstants::POWER_LAUNCHER_SHARED_EVENT);
auto sa2 = getDefaultSecurityAttribute();
send_telemetry_event = CreateEventW(&sa2, FALSE, FALSE, CommonSharedConstants::RUN_SEND_SETTINGS_TELEMETRY_EVENT);
};

~Microsoft_Launcher()
Expand Down Expand Up @@ -185,6 +195,7 @@ class Microsoft_Launcher : public PowertoyModuleIface
{
Logger::info("Launcher is enabling");
ResetEvent(m_hEvent);
ResetEvent(send_telemetry_event);
// Start PowerLauncher.exe only if the OS is 19H1 or higher
if (UseNewSettings())
{
Expand Down Expand Up @@ -265,6 +276,7 @@ class Microsoft_Launcher : public PowertoyModuleIface
if (m_enabled)
{
ResetEvent(m_hEvent);
ResetEvent(send_telemetry_event);
terminateProcess();
}

Expand Down Expand Up @@ -347,6 +359,12 @@ class Microsoft_Launcher : public PowertoyModuleIface
}
*/
}

virtual void send_settings_telemetry() override
{
Logger::info("Send settings telemetry");
SetEvent(send_telemetry_event);
}
};

// Load the settings file.
Expand Down
18 changes: 18 additions & 0 deletions src/modules/launcher/PowerLauncher.Telemetry/Events/PluginModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.Tracing;

namespace PowerLauncher.Telemetry.Events
{
[EventData]
public class PluginModel
{
public bool Disabled { get; set; }

public bool IsGlobal { get; set; }

public string ActionKeyword { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;

namespace PowerLauncher.Telemetry.Events
{
[EventData]
public class RunPluginsSettingsEvent : EventBase, IEvent
{
public RunPluginsSettingsEvent(IDictionary<string, PluginModel> pluginManager)
{
PluginManager = pluginManager;
}

public IDictionary<string, PluginModel> PluginManager { get; private set; }

public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void WaitForEventLoop(string eventName, Action callback)
{
if (eventHandle.WaitOne())
{
Log.Info("Successfully waited for POWER_LAUNCHER_SHARED_EVENT", MethodBase.GetCurrentMethod().DeclaringType);
Log.Info($"Successfully waited for {eventName}", MethodBase.GetCurrentMethod().DeclaringType);
Application.Current.Dispatcher.Invoke(callback);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/modules/launcher/PowerLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

using System;
using System.ComponentModel;
using System.Linq;
using System.Timers;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using interop;
using Microsoft.PowerLauncher.Telemetry;
using Microsoft.PowerToys.Telemetry;
using PowerLauncher.Helper;
using PowerLauncher.Plugin;
using PowerLauncher.Telemetry.Events;
using PowerLauncher.ViewModel;
using Wox.Infrastructure.UserSettings;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
Expand Down Expand Up @@ -40,6 +44,21 @@ public MainWindow(PowerToysRunSettings settings, MainViewModel mainVM)

_firstDeleteTimer.Elapsed += CheckForFirstDelete;
_firstDeleteTimer.Interval = 1000;
NativeEventWaiter.WaitForEventLoop(Constants.RunSendSettingsTelemetryEvent(), SendSettingsTelemetry);
}

private void SendSettingsTelemetry()
{
Log.Info("Send Run settings telemetry", this.GetType());
var plugins = PluginManager.AllPlugins.ToDictionary(x => x.Metadata.Name, x => new PluginModel()
{
Disabled = x.Metadata.Disabled,
ActionKeyword = x.Metadata.ActionKeyword,
IsGlobal = x.Metadata.IsGlobal,
});

var telemetryEvent = new RunPluginsSettingsEvent(plugins);
PowerToysTelemetry.Log.WriteEvent(telemetryEvent);
}

private void CheckForFirstDelete(object sender, ElapsedEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "restart_elevated.h"
#include "RestartManagement.h"
#include "Generated files/resource.h"
#include "settings_telemetry.h"

#include <common/comUtils/comUtils.h>
#include <common/display/dpi_aware.h>
Expand Down Expand Up @@ -163,6 +164,7 @@ int runner(bool isProcessElevated, bool openSettings, bool openOobe)
open_oobe_window();
}

settings_telemetry::init();
result = run_message_loop();
}
catch (std::runtime_error& err)
Expand Down
2 changes: 2 additions & 0 deletions src/runner/runner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="restart_elevated.cpp" />
<ClCompile Include="centralized_kb_hook.cpp" />
<ClCompile Include="settings_telemetry.cpp" />
<ClCompile Include="settings_window.cpp" />
<ClCompile Include="trace.cpp" />
<ClCompile Include="tray_icon.cpp" />
Expand All @@ -69,6 +70,7 @@
<ClInclude Include="general_settings.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="centralized_kb_hook.h" />
<ClInclude Include="settings_telemetry.h" />
<ClInclude Include="update_utils.h" />
<ClInclude Include="update_state.h" />
<ClInclude Include="powertoy_module.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/runner/runner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<ClCompile Include="..\common\interop\two_way_pipe_message_ipc.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="settings_telemetry.cpp">
<Filter>Utils</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -84,6 +87,9 @@
<ClInclude Include="centralized_kb_hook.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="settings_telemetry.h">
<Filter>Utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Utils">
Expand Down
93 changes: 93 additions & 0 deletions src/runner/settings_telemetry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma once
#include "pch.h"
#include "settings_telemetry.h"
#include <Windows.h>
#include <thread>
#include <common/logger/logger.h>
#include <common/utils/timeutil.h>
#include <common/SettingsAPI/settings_helpers.h>
#include <filesystem>
#include "powertoy_module.h"

using JsonObject = winrt::Windows::Data::Json::JsonObject;
using JsonValue = winrt::Windows::Data::Json::JsonValue;

std::wstring get_info_file_path()
{
std::filesystem::path settingsFilePath(PTSettingsHelper::get_root_save_folder_location());
settingsFilePath = settingsFilePath.append(settings_telemetry::send_info_file);
return settingsFilePath.wstring();
}

std::optional<time_t> get_last_send_time()
{
auto settings = json::from_file(get_info_file_path());
if (!settings.has_value() || !settings.value().HasKey(settings_telemetry::last_send_option))
{
return std::nullopt;
}

auto stringTime = (std::wstring)settings.value().GetNamedString(settings_telemetry::last_send_option);
return timeutil::from_string(stringTime);
}

void update_last_send_time(time_t time)
{
auto settings = JsonObject();
settings.SetNamedValue(settings_telemetry::last_send_option, JsonValue::CreateStringValue(timeutil::to_string(time)));
json::to_file(get_info_file_path(), settings);
}

void send()
{
for (auto& [name, powertoy] : modules())
{
if (powertoy->is_enabled())
{
try
{
powertoy->send_settings_telemetry();
}
catch(...)
{
Logger::error(L"Failed to send telemetry for {} module", name);
}
}
}
}

void run_interval()
{
auto time = get_last_send_time();
long long wait_time = 24*3600;
long long left_to_wait = 0;
if (time.has_value())
{
left_to_wait = max(0, wait_time - timeutil::diff::in_seconds(timeutil::now(), time.value()));
}

Sleep((DWORD)left_to_wait * 1000);
send();
update_last_send_time(timeutil::now());

while (true)
{
Sleep((DWORD)wait_time * 1000);
send();
update_last_send_time(timeutil::now());
}
}

void settings_telemetry::init()
{
std::thread([]() {
try
{
run_interval();
}
catch (...)
{
Logger::error("Failed to send settings telemetry");
}
}).detach();
}
7 changes: 7 additions & 0 deletions src/runner/settings_telemetry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
namespace settings_telemetry
{
static std::wstring send_info_file = L"settings-telemetry.json";
static std::wstring last_send_option = L"last_send_time";
void init();
}

0 comments on commit 612e9f8

Please sign in to comment.