Settings telemetry for PT Run (#10328)

This commit is contained in:
Mykhailo Pylyp 2021-03-19 19:03:12 +02:00 committed by GitHub
parent 057e92afb8
commit 612e9f8b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 205 additions and 6 deletions

View File

@ -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);
}

View File

@ -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";

View File

@ -82,6 +82,10 @@ public:
* if the key press is to be swallowed.
*/
virtual bool on_hotkey(size_t hotkeyId) { return false; }
virtual void send_settings_telemetry()
{
}
};
/*

View File

@ -81,6 +81,17 @@ private:
// 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()
@ -93,11 +104,10 @@ public:
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()
@ -185,6 +195,7 @@ public:
{
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())
{
@ -265,6 +276,7 @@ public:
if (m_enabled)
{
ResetEvent(m_hEvent);
ResetEvent(send_telemetry_event);
terminateProcess();
}
@ -347,6 +359,12 @@ public:
}
*/
}
virtual void send_settings_telemetry() override
{
Logger::info("Send settings telemetry");
SetEvent(send_telemetry_event);
}
};
// Load the settings file.

View File

@ -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; }
}
}

View File

@ -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;
}
}

View File

@ -21,7 +21,7 @@ namespace PowerLauncher.Helper
{
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);
}
}

View File

@ -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;
@ -40,6 +44,21 @@ namespace PowerLauncher
_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)

View File

@ -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>
@ -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)

View File

@ -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" />
@ -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" />

View File

@ -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" />
@ -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">

View File

@ -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();
}

View File

@ -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();
}