|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 7 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | + }); |
| 9 | +}; |
| 10 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 11 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 12 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 13 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 14 | + function step(op) { |
| 15 | + if (f) throw new TypeError("Generator is already executing."); |
| 16 | + while (_) try { |
| 17 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 18 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 19 | + switch (op[0]) { |
| 20 | + case 0: case 1: t = op; break; |
| 21 | + case 4: _.label++; return { value: op[1], done: false }; |
| 22 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 23 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 24 | + default: |
| 25 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 26 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 27 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 28 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 29 | + if (t[2]) _.ops.pop(); |
| 30 | + _.trys.pop(); continue; |
| 31 | + } |
| 32 | + op = body.call(thisArg, _); |
| 33 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 34 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 35 | + } |
| 36 | +}; |
| 37 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 38 | +var fs = require("fs"); |
| 39 | +var os = require("os"); |
| 40 | +var path = require("path"); |
| 41 | +var vscode = require("vscode"); |
| 42 | +var AnalyticsReporter = /** @class */ (function () { |
| 43 | + function AnalyticsReporter(extensionId, extensionVersion, client) { |
| 44 | + var _this = this; |
| 45 | + this.userOptIn = false; |
| 46 | + this.commonAttributes = {}; |
| 47 | + var logFilePath = process.env["VSCODE_LOGS"] || ""; |
| 48 | + if (logFilePath && |
| 49 | + extensionId && |
| 50 | + process.env["VSCODE_LOG_LEVEL"] === "trace") { |
| 51 | + logFilePath = path.join(logFilePath, extensionId + ".txt"); |
| 52 | + this.logStream = fs.createWriteStream(logFilePath, { |
| 53 | + flags: "a", |
| 54 | + encoding: "utf8", |
| 55 | + autoClose: true |
| 56 | + }); |
| 57 | + } |
| 58 | + this.extensionId = extensionId; |
| 59 | + this.extensionVersion = extensionVersion; |
| 60 | + this.analyticsClient = client; |
| 61 | + this.updateUserOptIn(); |
| 62 | + this.configListener = vscode.workspace.onDidChangeConfiguration(function () { |
| 63 | + return _this.updateUserOptIn(); |
| 64 | + }); |
| 65 | + } |
| 66 | + AnalyticsReporter.prototype.updateUserOptIn = function () { |
| 67 | + var config = vscode.workspace.getConfiguration(AnalyticsReporter.TELEMETRY_CONFIG_ID); |
| 68 | + this.userOptIn = config.get(AnalyticsReporter.TELEMETRY_CONFIG_ENABLED_ID, true); |
| 69 | + if (this.userOptIn) { |
| 70 | + this.initialiseAnalyticsClient(); |
| 71 | + } |
| 72 | + else { |
| 73 | + this.dispose(); |
| 74 | + } |
| 75 | + }; |
| 76 | + AnalyticsReporter.prototype.initialiseAnalyticsClient = function () { |
| 77 | + this.commonAttributes = this.getCommonAttributes(); |
| 78 | + this.analyticsClient.initialise(); |
| 79 | + }; |
| 80 | + // __GDPR__COMMON__ "common.os" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" } |
| 81 | + // __GDPR__COMMON__ "common.platformversion" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" } |
| 82 | + // __GDPR__COMMON__ "common.extname" : { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" } |
| 83 | + // __GDPR__COMMON__ "common.extversion" : { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" } |
| 84 | + // __GDPR__COMMON__ "common.vscodemachineid" : { "endPoint": "MacAddressHash", "classification": "EndUserPseudonymizedInformation", "purpose": "FeatureInsight" } |
| 85 | + // __GDPR__COMMON__ "common.vscodesessionid" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" } |
| 86 | + // __GDPR__COMMON__ "common.vscodeversion" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" } |
| 87 | + AnalyticsReporter.prototype.getCommonAttributes = function () { |
| 88 | + var commonAttributes = {}; |
| 89 | + commonAttributes["common.os"] = os.platform(); |
| 90 | + commonAttributes["common.platformversion"] = (os.release() || "").replace(/^(\d+)(\.\d+)?(\.\d+)?(.*)/, "$1$2$3"); |
| 91 | + commonAttributes["common.extname"] = this.extensionId; |
| 92 | + commonAttributes["common.extversion"] = this.extensionVersion; |
| 93 | + if (vscode && vscode.env) { |
| 94 | + commonAttributes["common.vscodemachineid"] = vscode.env.machineId; |
| 95 | + commonAttributes["common.vscodesessionid"] = vscode.env.sessionId; |
| 96 | + commonAttributes["common.vscodeversion"] = vscode.version; |
| 97 | + } |
| 98 | + return commonAttributes; |
| 99 | + }; |
| 100 | + AnalyticsReporter.prototype.sendEvent = function (event) { |
| 101 | + if (this.userOptIn && this.analyticsClient) { |
| 102 | + event.update(this.commonAttributes); |
| 103 | + this.analyticsClient.sendEvent(event); |
| 104 | + if (this.logStream) { |
| 105 | + this.logStream.write("telemetry/" + event.getName() + " " + JSON.stringify(event.toJSON()) + "\n"); |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | + AnalyticsReporter.prototype.sendException = function (exception) { |
| 110 | + if (this.userOptIn) { |
| 111 | + exception.update(this.commonAttributes); |
| 112 | + this.analyticsClient.sendException(exception); |
| 113 | + if (this.logStream) { |
| 114 | + this.logStream.write("telemetry/" + exception.getName() + " " + exception.getMessage() + " " + JSON.stringify(exception.toJSON()) + "\n"); |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + AnalyticsReporter.prototype.dispose = function () { |
| 119 | + return __awaiter(this, void 0, void 0, function () { |
| 120 | + var flushEventsToLogger, flushEventsToAI; |
| 121 | + var _this = this; |
| 122 | + return __generator(this, function (_a) { |
| 123 | + switch (_a.label) { |
| 124 | + case 0: |
| 125 | + this.configListener.dispose(); |
| 126 | + flushEventsToLogger = new Promise(function (resolve) { |
| 127 | + if (_this.logStream) { |
| 128 | + _this.logStream.on("finish", resolve); |
| 129 | + _this.logStream.end(); |
| 130 | + } |
| 131 | + else { |
| 132 | + return resolve(void 0); |
| 133 | + } |
| 134 | + }); |
| 135 | + flushEventsToAI = new Promise(function (resolve) { |
| 136 | + if (_this.analyticsClient) { |
| 137 | + return _this.analyticsClient.flush(); |
| 138 | + } |
| 139 | + else { |
| 140 | + resolve(void 0); |
| 141 | + } |
| 142 | + }); |
| 143 | + return [4 /*yield*/, Promise.all([flushEventsToAI, flushEventsToLogger])]; |
| 144 | + case 1: |
| 145 | + _a.sent(); |
| 146 | + return [2 /*return*/]; |
| 147 | + } |
| 148 | + }); |
| 149 | + }); |
| 150 | + }; |
| 151 | + AnalyticsReporter.TELEMETRY_CONFIG_ID = "telemetry"; |
| 152 | + AnalyticsReporter.TELEMETRY_CONFIG_ENABLED_ID = "enableTelemetry"; |
| 153 | + return AnalyticsReporter; |
| 154 | +}()); |
| 155 | +exports.AnalyticsReporter = AnalyticsReporter; |
0 commit comments