forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
85 lines (76 loc) · 3.77 KB
/
index.ts
File metadata and controls
85 lines (76 loc) · 3.77 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { commands } from 'vscode';
import { StopWatch } from './stopWatch';
import { getTelemetryReporter } from './telemetry';
import { TelemetryProperties } from './types';
export function sendTelemetryEvent(eventName: string, durationMs?: number, properties?: TelemetryProperties) {
const reporter = getTelemetryReporter();
const measures = typeof durationMs === 'number' ? { duration: durationMs } : undefined;
// tslint:disable-next-line:no-any
const customProperties: { [key: string]: string } = {};
if (properties) {
// tslint:disable-next-line:prefer-type-cast no-any
const data = properties as any;
Object.getOwnPropertyNames(data).forEach(prop => {
// tslint:disable-next-line:prefer-type-cast no-any no-unsafe-any
(customProperties as any)[prop] = typeof data[prop] === 'string' ? data[prop] : data[prop].toString();
});
}
commands.executeCommand('python.updateFeedbackCounter', eventName);
reporter.sendTelemetryEvent(eventName, properties ? customProperties : undefined, measures);
}
// tslint:disable-next-line:no-any function-name
export function captureTelemetry(eventName: string, properties?: TelemetryProperties) {
// tslint:disable-next-line:no-function-expression no-any
return function (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
const originalMethod = descriptor.value;
// tslint:disable-next-line:no-function-expression no-any
descriptor.value = function (...args: any[]) {
const stopWatch = new StopWatch();
// tslint:disable-next-line:no-invalid-this no-use-before-declare no-unsafe-any
const result = originalMethod.apply(this, args);
// If method being wrapped returns a promise then wait for it.
// tslint:disable-next-line:no-unsafe-any
if (result && typeof result.then === 'function' && typeof result.catch === 'function') {
// tslint:disable-next-line:prefer-type-cast
(result as Promise<void>)
.then(data => {
sendTelemetryEvent(eventName, stopWatch.elapsedTime, properties);
return data;
})
// tslint:disable-next-line:promise-function-async
.catch(ex => {
sendTelemetryEvent(eventName, stopWatch.elapsedTime, properties);
return Promise.reject(ex);
});
} else {
sendTelemetryEvent(eventName, stopWatch.elapsedTime, properties);
}
return result;
};
return descriptor;
};
}
// tslint:disable-next-line:no-any function-name
export function sendTelemetryWhenDone(eventName: string, promise: Promise<any> | Thenable<any>,
stopWatch?: StopWatch, properties?: TelemetryProperties) {
stopWatch = stopWatch ? stopWatch : new StopWatch();
if (typeof promise.then === 'function') {
// tslint:disable-next-line:prefer-type-cast no-any
(promise as Promise<any>)
.then(data => {
// tslint:disable-next-line:no-non-null-assertion
sendTelemetryEvent(eventName, stopWatch!.elapsedTime, properties);
return data;
// tslint:disable-next-line:promise-function-async
}, ex => {
// tslint:disable-next-line:no-non-null-assertion
sendTelemetryEvent(eventName, stopWatch!.elapsedTime, properties);
return Promise.reject(ex);
});
} else {
throw new Error('Method is neither a Promise nor a Theneable');
}
}