Skip to content

Commit 7dff073

Browse files
committed
Display install @types telemetry in an Eclipse console. See
#116
1 parent e189041 commit 7dff073

37 files changed

+936
-222
lines changed

core/ts.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Export-Package: ts,
1818
ts.client.configure,
1919
ts.client.diagnostics,
2020
ts.client.format,
21+
ts.client.installtypes,
2122
ts.client.navbar,
2223
ts.client.occurrences,
2324
ts.client.projectinfo,

core/ts.core/src/ts/client/ITypeScriptServiceClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import ts.client.configure.ConfigureRequestArguments;
2323
import ts.client.diagnostics.DiagnosticEvent;
2424
import ts.client.diagnostics.DiagnosticEventBody;
25+
import ts.client.installtypes.IInstallTypesListener;
2526
import ts.client.navbar.NavigationBarItem;
2627
import ts.client.occurrences.OccurrencesResponseItem;
2728
import ts.client.projectinfo.ProjectInfo;
@@ -264,6 +265,10 @@ CompletableFuture<List<CodeAction>> getCodeFixes(String fileName, IPositionProvi
264265

265266
void removeClientListener(ITypeScriptClientListener listener);
266267

268+
void addInstallTypesListener(IInstallTypesListener listener);
269+
270+
void removeInstallTypesListener(IInstallTypesListener listener);
271+
267272
void addInterceptor(IInterceptor interceptor);
268273

269274
void removeInterceptor(IInterceptor interceptor);

core/ts.core/src/ts/client/TypeScriptServiceClient.java

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import ts.client.diagnostics.Diagnostic;
3636
import ts.client.diagnostics.DiagnosticEvent;
3737
import ts.client.diagnostics.DiagnosticEventBody;
38+
import ts.client.installtypes.BeginInstallTypesEventBody;
39+
import ts.client.installtypes.EndInstallTypesEventBody;
40+
import ts.client.installtypes.IInstallTypesListener;
3841
import ts.client.navbar.NavigationBarItem;
3942
import ts.client.occurrences.OccurrencesResponseItem;
4043
import ts.client.projectinfo.ProjectInfo;
@@ -78,7 +81,6 @@
7881
import ts.nodejs.INodejsProcessListener;
7982
import ts.nodejs.NodejsProcessAdapter;
8083
import ts.nodejs.NodejsProcessManager;
81-
import ts.utils.FileUtils;
8284

8385
/**
8486
* TypeScript service client implementation.
@@ -92,6 +94,7 @@ public class TypeScriptServiceClient implements ITypeScriptServiceClient {
9294
private INodejsProcess process;
9395
private List<INodejsProcessListener> nodeListeners;
9496
private final List<ITypeScriptClientListener> listeners;
97+
private final List<IInstallTypesListener> installTypesListener;
9598
private final ReentrantReadWriteLock stateLock;
9699
private boolean dispose;
97100

@@ -147,21 +150,34 @@ private static class PendingRequestEventInfo {
147150
}
148151

149152
public TypeScriptServiceClient(final File projectDir, File tsserverFile, File nodeFile) throws TypeScriptException {
153+
this(projectDir, tsserverFile, nodeFile, false, false);
154+
}
155+
156+
public TypeScriptServiceClient(final File projectDir, File tsserverFile, File nodeFile, boolean enableTelemetry,
157+
boolean disableAutomaticTypingAcquisition) throws TypeScriptException {
150158
this(NodejsProcessManager.getInstance().create(projectDir, tsserverFile, nodeFile,
151159
new INodejsLaunchConfiguration() {
152160

153161
@Override
154162
public List<String> createNodeArgs() {
155163
List<String> args = new ArrayList<String>();
156-
args.add("-p");
157-
args.add(FileUtils.getPath(projectDir));
164+
// args.add("-p");
165+
// args.add(FileUtils.getPath(projectDir));
166+
if (enableTelemetry) {
167+
args.add("--enableTelemetry");
168+
}
169+
if (disableAutomaticTypingAcquisition) {
170+
args.add("--disableAutomaticTypingAcquisition");
171+
}
172+
//args.add("--useSingleInferredProject");
158173
return args;
159174
}
160175
}, TSSERVER_FILE_TYPE));
161176
}
162177

163178
public TypeScriptServiceClient(INodejsProcess process) {
164179
this.listeners = new ArrayList<>();
180+
this.installTypesListener = new ArrayList<>();
165181
this.stateLock = new ReentrantReadWriteLock();
166182
this.dispose = false;
167183
this.sentRequestMap = new LinkedHashMap<>();
@@ -212,6 +228,27 @@ private void dispatchMessage(String message) {
212228
if (pendingRequestEventInfo != null) {
213229
pendingRequestEventInfo.eventHandler.accept(response);
214230
}
231+
} else if ("telemetry".equals(event)) {
232+
// TelemetryEventBody telemetryData =
233+
// GsonHelper.DEFAULT_GSON.fromJson(json,
234+
// TelemetryEvent.class)
235+
// .getBody();
236+
//
237+
JsonObject telemetryData = json.get("body").getAsJsonObject();
238+
JsonObject payload = telemetryData.has("payload") ? telemetryData.get("payload").getAsJsonObject()
239+
: null;
240+
if (payload != null) {
241+
String telemetryEventName = telemetryData.get("telemetryEventName").getAsString();
242+
fireLogTelemetry(telemetryEventName, payload);
243+
}
244+
} else if ("beginInstallTypes".equals(event)) {
245+
BeginInstallTypesEventBody data = GsonHelper.DEFAULT_GSON.fromJson(json,
246+
BeginInstallTypesEventBody.class);
247+
fireBeginInstallTypes(data);
248+
} else if ("endInstallTypes".equals(event)) {
249+
EndInstallTypesEventBody data = GsonHelper.DEFAULT_GSON.fromJson(json,
250+
EndInstallTypesEventBody.class);
251+
fireEndInstallTypes(data);
215252
}
216253
break;
217254
default:
@@ -320,8 +357,8 @@ public CompletableFuture<List<DiagnosticEvent>> geterr(String[] files, int delay
320357
}
321358

322359
@Override
323-
public CompletableFuture<List<DiagnosticEvent>> geterrForProject(String file, int delay,
324-
ProjectInfo projectInfo) throws TypeScriptException {
360+
public CompletableFuture<List<DiagnosticEvent>> geterrForProject(String file, int delay, ProjectInfo projectInfo)
361+
throws TypeScriptException {
325362
return execute(new GeterrForProjectRequest(file, delay, projectInfo), true);
326363
}
327364

@@ -381,13 +418,14 @@ public CompletableFuture<DiagnosticEventBody> syntacticDiagnosticsSync(String fi
381418
// Since 2.0.5
382419

383420
@Override
384-
public CompletableFuture<Boolean> compileOnSaveEmitFile(String fileName, Boolean forced) throws TypeScriptException {
421+
public CompletableFuture<Boolean> compileOnSaveEmitFile(String fileName, Boolean forced)
422+
throws TypeScriptException {
385423
return execute(new CompileOnSaveEmitFileRequest(fileName, forced), true);
386424
}
387-
425+
388426
@Override
389-
public CompletableFuture<List<CompileOnSaveAffectedFileListSingleProject>> compileOnSaveAffectedFileList(String fileName)
390-
throws TypeScriptException {
427+
public CompletableFuture<List<CompileOnSaveAffectedFileListSingleProject>> compileOnSaveAffectedFileList(
428+
String fileName) throws TypeScriptException {
391429
return execute(new CompileOnSaveAffectedFileListRequest(fileName), true);
392430
}
393431

@@ -497,22 +535,60 @@ public void removeClientListener(ITypeScriptClientListener listener) {
497535
}
498536
}
499537

500-
protected void fireStartServer() {
538+
private void fireStartServer() {
501539
synchronized (listeners) {
502540
for (ITypeScriptClientListener listener : listeners) {
503541
listener.onStart(this);
504542
}
505543
}
506544
}
507545

508-
protected void fireEndServer() {
546+
private void fireEndServer() {
509547
synchronized (listeners) {
510548
for (ITypeScriptClientListener listener : listeners) {
511549
listener.onStop(this);
512550
}
513551
}
514552
}
515553

554+
@Override
555+
public void addInstallTypesListener(IInstallTypesListener listener) {
556+
synchronized (installTypesListener) {
557+
installTypesListener.add(listener);
558+
}
559+
}
560+
561+
@Override
562+
public void removeInstallTypesListener(IInstallTypesListener listener) {
563+
synchronized (installTypesListener) {
564+
installTypesListener.remove(listener);
565+
}
566+
}
567+
568+
private void fireBeginInstallTypes(BeginInstallTypesEventBody body) {
569+
synchronized (installTypesListener) {
570+
for (IInstallTypesListener listener : installTypesListener) {
571+
listener.onBegin(body);
572+
}
573+
}
574+
}
575+
576+
private void fireEndInstallTypes(EndInstallTypesEventBody body) {
577+
synchronized (installTypesListener) {
578+
for (IInstallTypesListener listener : installTypesListener) {
579+
listener.onEnd(body);
580+
}
581+
}
582+
}
583+
584+
private void fireLogTelemetry(String telemetryEventName, JsonObject payload) {
585+
synchronized (installTypesListener) {
586+
for (IInstallTypesListener listener : installTypesListener) {
587+
listener.logTelemetry(telemetryEventName, payload);
588+
}
589+
}
590+
}
591+
516592
@Override
517593
public void addInterceptor(IInterceptor interceptor) {
518594
beginWriteState();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ts.client.installtypes;
2+
3+
public class BeginInstallTypesEventBody extends InstallTypesEventBody {
4+
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ts.client.installtypes;
2+
3+
public class EndInstallTypesEventBody extends InstallTypesEventBody {
4+
5+
/**
6+
* true if installation succeeded, otherwise false
7+
*/
8+
private boolean success;
9+
10+
public boolean isSuccess() {
11+
return success;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ts.client.installtypes;
2+
3+
import com.google.gson.JsonObject;
4+
5+
public interface IInstallTypesListener {
6+
7+
void onBegin(BeginInstallTypesEventBody body);
8+
9+
void logTelemetry(String telemetryEventName, JsonObject payload);
10+
11+
void onEnd(EndInstallTypesEventBody body);
12+
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ts.client.installtypes;
2+
3+
import java.util.List;
4+
5+
public class InstallTypesEventBody {
6+
7+
/**
8+
* correlation id to match begin and end events
9+
*/
10+
int eventId;
11+
12+
/**
13+
* list of packages to install
14+
*/
15+
List<String> packages;
16+
17+
public int getEventId() {
18+
return eventId;
19+
}
20+
21+
public List<String> getPackages() {
22+
return packages;
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ts.internal.client.protocol.installtypes;
2+
3+
import ts.client.Event;
4+
import ts.client.installtypes.BeginInstallTypesEventBody;
5+
6+
public class BeginInstallTypesEvent extends Event<BeginInstallTypesEventBody>{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ts.internal.client.protocol.installtypes;
2+
3+
import ts.client.Event;
4+
import ts.client.installtypes.EndInstallTypesEventBody;
5+
6+
public class EndInstallTypesEvent extends Event<EndInstallTypesEventBody>{
7+
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ts.internal.client.protocol.installtypes;
2+
3+
import ts.client.Event;
4+
5+
public class TelemetryEvent extends Event<TelemetryEventBody>{
6+
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ts.internal.client.protocol.installtypes;
2+
3+
public class TelemetryEventBody {
4+
5+
private String telemetryEventName;
6+
7+
private Object paylod;
8+
9+
public String getTelemetryEventName() {
10+
return telemetryEventName;
11+
}
12+
13+
public Object getPaylod() {
14+
return paylod;
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ts.internal.client.protocol.installtypes;
2+
3+
public class TypingsInstalledTelemetryEventBody extends TelemetryEventBody{
4+
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ts.internal.client.protocol.installtypes;
2+
3+
public class TypingsInstalledTelemetryEventPayload {
4+
5+
/**
6+
* Comma separated list of installed typing packages
7+
*/
8+
private String installedPackages;
9+
/**
10+
* true if install request succeeded, otherwise - false
11+
*/
12+
private boolean installSuccess;
13+
14+
/**
15+
* version of typings installer
16+
*/
17+
private String typingsInstallerVersion;
18+
19+
public String getInstalledPackages() {
20+
return installedPackages;
21+
}
22+
23+
public boolean isInstallSuccess() {
24+
return installSuccess;
25+
}
26+
27+
public String getTypingsInstallerVersion() {
28+
return typingsInstallerVersion;
29+
}
30+
}

core/ts.core/src/ts/resources/BasicTypeScriptProjectSettings.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class BasicTypeScriptProjectSettings implements ITypeScriptProjectSetting
3232
private final ITypeScriptRepository repository;
3333
private ICompletionEntryMatcher completionEntryMatcher;
3434
private TslintSettingsStrategy tslintStrategy;
35+
private boolean enableTelemetry;
36+
37+
private boolean disableAutomaticTypingAcquisition;
3538

3639
public BasicTypeScriptProjectSettings(File nodejsInstallPath, File typeScriptDir)
3740
throws TypeScriptRepositoryException {
@@ -119,4 +122,22 @@ public FormatCodeSettings getFormatOptions() {
119122
public String getTypeScriptVersion() {
120123
return repository.getTypesScriptVersion();
121124
}
125+
126+
@Override
127+
public boolean isEnableTelemetry() {
128+
return enableTelemetry;
129+
}
130+
131+
public void setEnableTelemetry(boolean enableTelemetry) {
132+
this.enableTelemetry = enableTelemetry;
133+
}
134+
135+
@Override
136+
public boolean isDisableAutomaticTypingAcquisition() {
137+
return disableAutomaticTypingAcquisition;
138+
}
139+
140+
public void setDisableAutomaticTypingAcquisition(boolean disableAutomaticTypingAcquisition) {
141+
this.disableAutomaticTypingAcquisition = disableAutomaticTypingAcquisition;
142+
}
122143
}

core/ts.core/src/ts/resources/ITypeScriptProjectSettings.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,12 @@ public interface ITypeScriptProjectSettings {
9494

9595
FormatCodeSettings getFormatOptions();
9696

97+
/**
98+
* Returns true if telemetry must be enable and false otherwise.
99+
*
100+
* @return true if telemetry must be enable and false otherwise.
101+
*/
102+
boolean isEnableTelemetry();
103+
104+
boolean isDisableAutomaticTypingAcquisition();
97105
}

0 commit comments

Comments
 (0)