mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
Settings telemetry for PT Run (#10328)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -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">
|
||||
|
||||
93
src/runner/settings_telemetry.cpp
Normal file
93
src/runner/settings_telemetry.cpp
Normal 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();
|
||||
}
|
||||
7
src/runner/settings_telemetry.h
Normal file
7
src/runner/settings_telemetry.h
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user