[vcpkg] Enable metrics on linux

This commit is contained in:
Robert Schumacher 2018-02-19 12:20:16 -08:00
parent 222fa36012
commit 0ef0300b8e
10 changed files with 94 additions and 45 deletions

View File

@ -13,15 +13,12 @@ endif()
file(GLOB_RECURSE VCPKGLIB_SOURCES src/vcpkg/*.cpp)
add_library(vcpkglib STATIC ${VCPKGLIB_SOURCES})
target_compile_definitions(vcpkglib PRIVATE -DDISABLE_METRICS=0)
target_include_directories(vcpkglib PUBLIC include)
add_executable(vcpkg src/vcpkg.cpp ${VCPKGLIB_SOURCES})
target_compile_definitions(vcpkg PRIVATE -DDISABLE_METRICS=0)
target_include_directories(vcpkg PUBLIC include)
if(GCC)
target_link_libraries(vcpkglib PUBLIC stdc++fs)
target_link_libraries(vcpkg PUBLIC stdc++fs)
elseif(CLANG)
target_link_libraries(vcpkglib PUBLIC c++experimental)
target_link_libraries(vcpkg PUBLIC c++experimental)
endif()
add_executable(vcpkg src/vcpkg.cpp)
target_link_libraries(vcpkg PRIVATE vcpkglib)

View File

@ -38,6 +38,8 @@ namespace vcpkg::Strings
std::string to_utf8(const CWStringView& w);
std::string escape_string(const CStringView& s, char char_to_escape, char escape_char);
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);
bool case_insensitive_ascii_contains(const std::string& s, const std::string& pattern);

View File

@ -171,7 +171,6 @@ static void inner(const VcpkgCmdArguments& args)
static void load_config()
{
#if defined(_WIN32)
auto& fs = Files::get_real_filesystem();
auto config = UserConfig::try_read_data(fs);
@ -185,16 +184,20 @@ static void load_config()
write_config = true;
}
#if defined(_WIN32)
if (config.user_mac.empty())
{
config.user_mac = Metrics::get_MAC_user();
write_config = true;
}
#endif
{
auto locked_metrics = Metrics::g_metrics.lock();
locked_metrics->set_user_information(config.user_id, config.user_time);
#if defined(_WIN32)
locked_metrics->track_property("user_mac", config.user_mac);
#endif
}
if (config.last_completed_survey.empty())
@ -208,7 +211,6 @@ static void load_config()
{
config.try_write_data(fs);
}
#endif
}
static std::string trim_path_from_command_line(const std::string& full_command_line)

View File

@ -31,7 +31,7 @@ namespace vcpkg::Checks
#if defined(_WIN32)
::TerminateProcess(::GetCurrentProcess(), exit_code);
#else
::exit(exit_code);
std::exit(exit_code);
#endif
}

View File

@ -166,7 +166,7 @@ namespace vcpkg::Files
}
virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) override
{
ec = std::error_code();
ec.clear();
FILE* f = nullptr;
#if defined(_WIN32)

View File

@ -74,6 +74,16 @@ namespace vcpkg::Strings
#endif
}
std::string escape_string(const CStringView& s, char char_to_escape, char escape_char)
{
std::string ret = s.c_str();
// Replace '\' with '\\' or '`' with '``'
ret = Strings::replace_all(std::move(ret), {escape_char}, {escape_char, escape_char});
// Replace '"' with '\"' or '`"'
ret = Strings::replace_all(std::move(ret), {char_to_escape}, {escape_char, char_to_escape});
return ret;
}
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern)
{
const std::string pattern_as_lower_case(ascii_to_lowercase(pattern));

View File

@ -30,7 +30,6 @@ namespace vcpkg::Commands::Contact
if (Util::Sets::contains(parsed_args.switches, switches[0].name))
{
#if defined(_WIN32)
auto maybe_now = Chrono::CTime::get_current_date_time();
if (auto p_now = maybe_now.get())
{
@ -39,10 +38,14 @@ namespace vcpkg::Commands::Contact
config.last_completed_survey = p_now->to_string();
config.try_write_data(fs);
}
#endif
#if defined(_WIN32)
System::cmd_execute("start https://aka.ms/NPS_vcpkg");
System::println("Default browser launched to https://aka.ms/NPS_vcpkg, thank you for your feedback!");
System::println("Default browser launched to https://aka.ms/NPS_vcpkg; thank you for your feedback!");
#else
System::println(
"Please navigate to https://aka.ms/NPS_vcpkg in your preferred browser. Thank you for your feedback!");
#endif
}
else
{

View File

@ -8,7 +8,11 @@
#define STRINGIFY(...) #__VA_ARGS__
#define MACRO_TO_STRING(X) STRINGIFY(X)
#if defined(VCPKG_VERSION)
#define VCPKG_VERSION_AS_STRING MACRO_TO_STRING(VCPKG_VERSION)
#else
#define VCPKG_VERSION_AS_STRING "-unknownhash"
#endif
namespace vcpkg::Commands::Version
{

View File

@ -196,8 +196,8 @@ namespace vcpkg::Metrics
"iKey": "b4e88960-4393-4dd9-ab8e-97e8fe6d7603",
"flags": 0.000000,
"tags": {
"ai.device.os": "Windows",
"ai.device.osVersion": "%s",
"ai.device.os": "Other",
"ai.device.osVersion": "%s-%s",
"ai.session.id": "%s",
"ai.user.id": "%s",
"ai.user.accountAcquisitionDate": "%s"
@ -213,6 +213,17 @@ namespace vcpkg::Metrics
}
}])",
timestamp,
#if defined(_WIN32)
"Windows",
#elif defined(__APPLE__)
"OSX",
#elif defined(__linux__)
"Linux",
#elif defined(__unix__)
"Unix",
#elif defined(__unix__)
"Other",
#endif
get_os_version_string(),
session_id,
user_id,
@ -390,24 +401,23 @@ namespace vcpkg::Metrics
void Metrics::flush()
{
#if defined(_WIN32)
const std::string payload = g_metricmessage.format_event_data_template();
if (g_should_print_metrics) std::cerr << payload << "\n";
if (!g_should_send_metrics) return;
// upload(payload);
#if defined(_WIN32)
wchar_t temp_folder[MAX_PATH];
GetTempPathW(MAX_PATH, temp_folder);
const fs::path temp_folder_path = fs::path(temp_folder) / "vcpkg";
const fs::path temp_folder_path_exe =
temp_folder_path / Strings::format("vcpkgmetricsuploader-%s.exe", Commands::Version::base_version());
#endif
auto& fs = Files::get_real_filesystem();
if (true)
{
#if defined(_WIN32)
const fs::path exe_path = [&fs]() -> fs::path {
auto vcpkgdir = System::get_exe_path_of_current_process().parent_path();
auto path = vcpkgdir / "vcpkgmetricsuploader.exe";
@ -424,16 +434,29 @@ namespace vcpkg::Metrics
if (ec) return;
fs.copy_file(exe_path, temp_folder_path_exe, fs::copy_options::skip_existing, ec);
if (ec) return;
}
const fs::path vcpkg_metrics_txt_path = temp_folder_path / ("vcpkg" + generate_random_UUID() + ".txt");
#else
if (!fs.exists("/tmp")) return;
const fs::path temp_folder_path = "/tmp/vcpkg";
std::error_code ec;
fs.create_directory(temp_folder_path, ec);
// ignore error
ec.clear();
#endif
const fs::path vcpkg_metrics_txt_path = temp_folder_path / ("vcpkg" + generate_random_UUID() + ".txt");
fs.write_contents(vcpkg_metrics_txt_path, payload, ec);
if (ec) return;
#if defined(_WIN32)
const std::string cmd_line = Strings::format("start \"vcpkgmetricsuploader.exe\" \"%s\" \"%s\"",
temp_folder_path_exe.u8string(),
vcpkg_metrics_txt_path.u8string());
System::cmd_execute_clean(cmd_line);
#else
auto escaped_path = Strings::escape_string(vcpkg_metrics_txt_path.u8string(), '\'', '\\');
const std::string cmd_line = Strings::format(
R"((curl "https://dc.services.visualstudio.com/v2/track" -H "Content-Type: application/json" -X POST --data '@%s' >/dev/null 2>&1; rm '%s') &)",
escaped_path,
escaped_path);
#endif
System::cmd_execute_clean(cmd_line);
}
}

View File

@ -29,13 +29,22 @@ namespace
namespace vcpkg
{
static fs::path get_config_path()
{
#if defined(_WIN32)
return get_localappdata() / "vcpkg" / "config";
#else
auto maybe_home = System::get_environment_variable("HOME");
return fs::path(maybe_home.value_or("/var")) / "vcpkg" / "config";
#endif
}
UserConfig UserConfig::try_read_data(const Files::Filesystem& fs)
{
UserConfig ret;
#if defined(_WIN32)
try
{
auto maybe_pghs = Paragraphs::get_paragraphs(fs, get_localappdata() / "vcpkg" / "config");
auto maybe_pghs = Paragraphs::get_paragraphs(fs, get_config_path());
if (const auto p_pghs = maybe_pghs.get())
{
const auto& pghs = *p_pghs;
@ -58,19 +67,19 @@ namespace vcpkg
catch (...)
{
}
#endif
return ret;
}
void UserConfig::try_write_data(Files::Filesystem& fs) const
{
#if defined(_WIN32)
try
{
auto config_path = get_config_path();
auto config_dir = config_path.parent_path();
std::error_code ec;
fs.create_directory(get_localappdata() / "vcpkg", ec);
fs.write_contents(get_localappdata() / "vcpkg" / "config",
fs.create_directory(config_dir, ec);
fs.write_contents(config_path,
Strings::format("User-Id: %s\n"
"User-Since: %s\n"
"Mac-Hash: %s\n"
@ -84,6 +93,5 @@ namespace vcpkg
catch (...)
{
}
#endif
}
}