aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/metrics.cpp
blob: ada065fd6b90ff85be781d57e1f2194ba58af752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "metrics.h"
#include <utility>
#include <array>
#include <string>
#include <iostream>
#include <vector>
#include <sys/timeb.h>
#include <time.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <winhttp.h>
#include <fstream>
#include <filesystem>
#include "vcpkg_Strings.h"
#include "vcpkg_System.h"

namespace fs = std::tr2::sys;

namespace vcpkg
{
    static std::string GetCurrentDateTime()
    {
        struct tm newtime;
        time_t now;
        int milli;
        std::array<char, 80> date;
        date.fill(0);

        struct _timeb timebuffer;

        _ftime_s(&timebuffer);
        now = timebuffer.time;
        milli = timebuffer.millitm;

        errno_t err = gmtime_s(&newtime, &now);
        if (err)
        {
            return "";
        }

        strftime(&date[0], date.size(), "%Y-%m-%dT%H:%M:%S", &newtime);
        return std::string(&date[0]) + "." + std::to_string(milli) + "Z";
    }

    static std::string GenerateRandomUUID()
    {
        int partSizes[] = {8, 4, 4, 4, 12};
        char uuid[37];
        memset(uuid, 0, sizeof(uuid));
        int num;
        srand(static_cast<int>(time(nullptr)));
        int index = 0;
        for (int part = 0; part < 5; part++)
        {
            if (part > 0)
            {
                uuid[index] = '-';
                index++;
            }

            // Generating UUID format version 4
            // http://en.wikipedia.org/wiki/Universally_unique_identifier
            for (int i = 0; i < partSizes[part]; i++ , index++)
            {
                if (part == 2 && i == 0)
                {
                    num = 4;
                }
                else if (part == 4 && i == 0)
                {
                    num = (rand() % 4) + 8;
                }
                else
                {
                    num = rand() % 16;
                }

                if (num < 10)
                {
                    uuid[index] = static_cast<char>('0' + num);
                }
                else
                {
                    uuid[index] = static_cast<char>('a' + (num - 10));
                }
            }
        }

        return uuid;
    }

    static const std::string& get_session_id()
    {
        static const std::string id = GenerateRandomUUID();
        return id;
    }

    static std::string to_json_string(const std::string& str)
    {
        std::string encoded = "\"";
        for (auto&& ch : str)
        {
            if (ch == '\\')
            {
                encoded.append("\\\\");
            }
            else if (ch == '"')
            {
                encoded.append("\\\"");
            }
            else if (ch < 0x20 || ch >= 0x80)
            {
                // Note: this treats incoming Strings as Latin-1
                static constexpr const char hex[16] = {
                    '0', '1', '2', '3', '4', '5', '6', '7',
                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
                encoded.append("\\u00");
                encoded.push_back(hex[ch / 16]);
                encoded.push_back(hex[ch % 16]);
            }
            else
            {
                encoded.push_back(ch);
            }
        }
        encoded.push_back('"');
        return encoded;
    }

    static std::string get_os_version_string()
    {
        std::wstring path;
        path.resize(MAX_PATH);
        auto n = GetSystemDirectoryW(&path[0], static_cast<UINT>(path.size()));
        path.resize(n);
        path += L"\\kernel32.dll";

        auto versz = GetFileVersionInfoSizeW(path.c_str(), nullptr);
        if (versz == 0)
            return "";

        std::vector<char> verbuf;
        verbuf.resize(versz);

        if (!GetFileVersionInfoW(path.c_str(), 0, static_cast<DWORD>(verbuf.size()), &verbuf[0]))
            return "";

        void* rootblock;
        UINT rootblocksize;
        if (!VerQueryValueW(&verbuf[0], L"\\", &rootblock, &rootblocksize))
            return "";

        auto rootblock_ffi = static_cast<VS_FIXEDFILEINFO *>(rootblock);

        return Strings::format("%d.%d.%d",
                               static_cast<int>(HIWORD(rootblock_ffi->dwProductVersionMS)),
                               static_cast<int>(LOWORD(rootblock_ffi->dwProductVersionMS)),
                               static_cast<int>(HIWORD(rootblock_ffi->dwProductVersionLS)));
    }

    struct MetricMessage
    {
        std::string user_id = GenerateRandomUUID();
        std::string user_timestamp;
        std::string timestamp = GetCurrentDateTime();
        std::string properties;
        std::string measurements;

        void TrackProperty(const std::string& name, const std::string& value)
        {
            if (properties.size() != 0)
                properties.push_back(',');
            properties.append(to_json_string(name));
            properties.push_back(':');
            properties.append(to_json_string(value));
        }

        void TrackMetric(const std::string& name, double value)
        {
            if (measurements.size() != 0)
                measurements.push_back(',');
            measurements.append(to_json_string(name));
            measurements.push_back(':');
            measurements.append(std::to_string(value));
        }

        std::string format_event_data_template() const
        {
            const std::string& session_id = get_session_id();
            return Strings::format(R"([{
    "ver": 1,
    "name": "Microsoft.ApplicationInsights.Event",
    "time": "%s",
    "sampleRate": 100.000000,
    "seq": "0:0",
    "iKey": "b4e88960-4393-4dd9-ab8e-97e8fe6d7603",
    "flags": 0.000000,
    "tags": {
        "ai.device.os": "Windows",
        "ai.device.osVersion": "%s",
        "ai.session.id": "%s",
        "ai.user.id": "%s",
        "ai.user.accountAcquisitionDate": "%s"
    },
    "data": {
        "baseType": "EventData",
        "baseData": {
            "ver": 2,
            "name": "commandline_test7",
            "properties": { %s },
            "measurements": { %s }
        }
    }
}])",
                                   timestamp,
                                   get_os_version_string(),
                                   session_id,
                                   user_id,
                                   user_timestamp,
                                   properties,
                                   measurements);
        }
    };

    static MetricMessage g_metricmessage;
    static bool g_should_send_metrics =
#if defined(NDEBUG) && (DISABLE_METRICS == 0)
true
#else
    false
#endif
    ;
    static bool g_should_print_metrics = false;

    bool GetCompiledMetricsEnabled()
    {
        return DISABLE_METRICS == 0;
    }

    void SetUserInformation(const std::string& user_id, const std::string& first_use_time)
    {
        g_metricmessage.user_id = user_id;
        g_metricmessage.user_timestamp = first_use_time;
    }

    void InitUserInformation(std::string& user_id, std::string& first_use_time)
    {
        user_id = GenerateRandomUUID();
        first_use_time = GetCurrentDateTime();
    }

    void SetSendMetrics(bool should_send_metrics)
    {
        g_should_send_metrics = should_send_metrics;
    }

    void SetPrintMetrics(bool should_print_metrics)
    {
        g_should_print_metrics = should_print_metrics;
    }

    void TrackMetric(const std::string& name, double value)
    {
        g_metricmessage.TrackMetric(name, value);
    }

    void TrackProperty(const std::string& name, const std::wstring& value)
    {
        // Note: this is not valid UTF-16 -> UTF-8, it just yields a close enough approximation for our purposes.
        std::string converted_value;
        converted_value.resize(value.size());
        std::transform(
            value.begin(), value.end(),
            converted_value.begin(),
            [](wchar_t ch)
            {
                return static_cast<char>(ch);
            });

        g_metricmessage.TrackProperty(name, converted_value);
    }

    void TrackProperty(const std::string& name, const std::string& value)
    {
        g_metricmessage.TrackProperty(name, value);
    }

    void Upload(const std::string& payload)
    {
        HINTERNET hSession = nullptr, hConnect = nullptr, hRequest = nullptr;
        BOOL bResults = FALSE;

        hSession = WinHttpOpen(L"vcpkg/1.0",
                               WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                               WINHTTP_NO_PROXY_NAME,
                               WINHTTP_NO_PROXY_BYPASS,
                               0);
        if (hSession)
            hConnect = WinHttpConnect(hSession, L"dc.services.visualstudio.com", INTERNET_DEFAULT_HTTPS_PORT, 0);

        if (hConnect)
            hRequest = WinHttpOpenRequest(hConnect,
                                          L"POST",
                                          L"/v2/track",
                                          nullptr,
                                          WINHTTP_NO_REFERER,
                                          WINHTTP_DEFAULT_ACCEPT_TYPES,
                                          WINHTTP_FLAG_SECURE);

        if (hRequest)
        {
            if (MAXDWORD <= payload.size())
                abort();
            std::wstring hdrs = L"Content-Type: application/json\r\n";
            bResults = WinHttpSendRequest(hRequest,
                                          hdrs.c_str(), static_cast<DWORD>(hdrs.size()),
                                          (void*)&payload[0], static_cast<DWORD>(payload.size()), static_cast<DWORD>(payload.size()),
                                          0);
        }

        if (bResults)
        {
            bResults = WinHttpReceiveResponse(hRequest, nullptr);
        }

        DWORD http_code = 0, junk = sizeof(DWORD);

        if (bResults)
        {
            bResults = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, nullptr, &http_code, &junk, WINHTTP_NO_HEADER_INDEX);
        }

        std::vector<char> responseBuffer;
        if (bResults)
        {
            DWORD availableData = 0, readData = 0, totalData = 0;

            while ((bResults = WinHttpQueryDataAvailable(hRequest, &availableData)) && availableData > 0)
            {
                responseBuffer.resize(responseBuffer.size() + availableData);

                bResults = WinHttpReadData(hRequest, &responseBuffer.data()[totalData], availableData, &readData);

                if (!bResults)
                {
                    break;
                }

                totalData += readData;

                responseBuffer.resize(totalData);
            }
        }

        if (!bResults)
        {
#ifndef NDEBUG
            __debugbreak();
            auto err = GetLastError();
            std::cerr << "[DEBUG] failed to connect to server: " << err << "\n";
#endif
        }

        if (hRequest)
            WinHttpCloseHandle(hRequest);
        if (hConnect)
            WinHttpCloseHandle(hConnect);
        if (hSession)
            WinHttpCloseHandle(hSession);
    }

    static fs::path get_bindir()
    {
        wchar_t buf[_MAX_PATH ];
        int bytes = GetModuleFileNameW(nullptr, buf, _MAX_PATH);
        if (bytes == 0)
            std::abort();
        return fs::path(buf, buf + bytes);
    }

    void Flush()
    {
        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);

        wchar_t temp_folder[MAX_PATH];
        GetTempPathW(MAX_PATH, temp_folder);

        const fs::path temp_folder_path = temp_folder;
        const fs::path temp_folder_path_exe = temp_folder_path / "vcpkgmetricsuploader.exe";

        if (true)
        {
            const fs::path exe_path = []() -> fs::path
                {
                    auto vcpkgdir = get_bindir().parent_path();
                    auto path = vcpkgdir / "vcpkgmetricsuploader.exe";
                    if (fs::exists(path))
                        return path;

                    path = vcpkgdir / "scripts" / "vcpkgmetricsuploader.exe";
                    if (fs::exists(path))
                        return path;

                    return L"";
                }();

            std::error_code ec;
            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" + GenerateRandomUUID() + ".txt");
        std::ofstream(vcpkg_metrics_txt_path) << payload;

        const std::wstring cmdLine = Strings::wformat(L"start %s %s", temp_folder_path_exe.native(), vcpkg_metrics_txt_path.native());
        System::cmd_execute(cmdLine);
    }
}