-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Settings telemetry for PT Run (#10328)
- Loading branch information
1 parent
057e92a
commit 612e9f8
Showing
13 changed files
with
205 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/modules/launcher/PowerLauncher.Telemetry/Events/PluginModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/modules/launcher/PowerLauncher.Telemetry/Events/RunPluginsSettingsEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |