Skip to content

Commit bd6c413

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

31 files changed

+581
-212
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: 83 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,30 @@ private static class PendingRequestEventInfo {
147150
}
148151

149152
public TypeScriptServiceClient(final File projectDir, File tsserverFile, File nodeFile) throws TypeScriptException {
153+
this(projectDir, tsserverFile, nodeFile, false);
154+
}
155+
156+
public TypeScriptServiceClient(final File projectDir, File tsserverFile, File nodeFile, boolean enableTelemetry)
157+
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+
}
158169
return args;
159170
}
160171
}, TSSERVER_FILE_TYPE));
161172
}
162173

163174
public TypeScriptServiceClient(INodejsProcess process) {
164175
this.listeners = new ArrayList<>();
176+
this.installTypesListener = new ArrayList<>();
165177
this.stateLock = new ReentrantReadWriteLock();
166178
this.dispose = false;
167179
this.sentRequestMap = new LinkedHashMap<>();
@@ -212,6 +224,27 @@ private void dispatchMessage(String message) {
212224
if (pendingRequestEventInfo != null) {
213225
pendingRequestEventInfo.eventHandler.accept(response);
214226
}
227+
} else if ("telemetry".equals(event)) {
228+
// TelemetryEventBody telemetryData =
229+
// GsonHelper.DEFAULT_GSON.fromJson(json,
230+
// TelemetryEvent.class)
231+
// .getBody();
232+
//
233+
JsonObject telemetryData = json.get("body").getAsJsonObject();
234+
JsonObject payload = telemetryData.has("payload") ? telemetryData.get("payload").getAsJsonObject()
235+
: null;
236+
if (payload != null) {
237+
String telemetryEventName = telemetryData.get("telemetryEventName").getAsString();
238+
fireLogTelemetry(telemetryEventName, payload);
239+
}
240+
} else if ("beginInstallTypes".equals(event)) {
241+
BeginInstallTypesEventBody data = GsonHelper.DEFAULT_GSON.fromJson(json,
242+
BeginInstallTypesEventBody.class);
243+
fireBeginInstallTypes(data);
244+
} else if ("endInstallTypes".equals(event)) {
245+
EndInstallTypesEventBody data = GsonHelper.DEFAULT_GSON.fromJson(json,
246+
EndInstallTypesEventBody.class);
247+
fireEndInstallTypes(data);
215248
}
216249
break;
217250
default:
@@ -320,8 +353,8 @@ public CompletableFuture<List<DiagnosticEvent>> geterr(String[] files, int delay
320353
}
321354

322355
@Override
323-
public CompletableFuture<List<DiagnosticEvent>> geterrForProject(String file, int delay,
324-
ProjectInfo projectInfo) throws TypeScriptException {
356+
public CompletableFuture<List<DiagnosticEvent>> geterrForProject(String file, int delay, ProjectInfo projectInfo)
357+
throws TypeScriptException {
325358
return execute(new GeterrForProjectRequest(file, delay, projectInfo), true);
326359
}
327360

@@ -381,13 +414,14 @@ public CompletableFuture<DiagnosticEventBody> syntacticDiagnosticsSync(String fi
381414
// Since 2.0.5
382415

383416
@Override
384-
public CompletableFuture<Boolean> compileOnSaveEmitFile(String fileName, Boolean forced) throws TypeScriptException {
417+
public CompletableFuture<Boolean> compileOnSaveEmitFile(String fileName, Boolean forced)
418+
throws TypeScriptException {
385419
return execute(new CompileOnSaveEmitFileRequest(fileName, forced), true);
386420
}
387-
421+
388422
@Override
389-
public CompletableFuture<List<CompileOnSaveAffectedFileListSingleProject>> compileOnSaveAffectedFileList(String fileName)
390-
throws TypeScriptException {
423+
public CompletableFuture<List<CompileOnSaveAffectedFileListSingleProject>> compileOnSaveAffectedFileList(
424+
String fileName) throws TypeScriptException {
391425
return execute(new CompileOnSaveAffectedFileListRequest(fileName), true);
392426
}
393427

@@ -497,22 +531,60 @@ public void removeClientListener(ITypeScriptClientListener listener) {
497531
}
498532
}
499533

500-
protected void fireStartServer() {
534+
private void fireStartServer() {
501535
synchronized (listeners) {
502536
for (ITypeScriptClientListener listener : listeners) {
503537
listener.onStart(this);
504538
}
505539
}
506540
}
507541

508-
protected void fireEndServer() {
542+
private void fireEndServer() {
509543
synchronized (listeners) {
510544
for (ITypeScriptClientListener listener : listeners) {
511545
listener.onStop(this);
512546
}
513547
}
514548
}
515549

550+
@Override
551+
public void addInstallTypesListener(IInstallTypesListener listener) {
552+
synchronized (installTypesListener) {
553+
installTypesListener.add(listener);
554+
}
555+
}
556+
557+
@Override
558+
public void removeInstallTypesListener(IInstallTypesListener listener) {
559+
synchronized (installTypesListener) {
560+
installTypesListener.remove(listener);
561+
}
562+
}
563+
564+
private void fireBeginInstallTypes(BeginInstallTypesEventBody body) {
565+
synchronized (installTypesListener) {
566+
for (IInstallTypesListener listener : installTypesListener) {
567+
listener.onBegin(body);
568+
}
569+
}
570+
}
571+
572+
private void fireEndInstallTypes(EndInstallTypesEventBody body) {
573+
synchronized (installTypesListener) {
574+
for (IInstallTypesListener listener : installTypesListener) {
575+
listener.onEnd(body);
576+
}
577+
}
578+
}
579+
580+
private void fireLogTelemetry(String telemetryEventName, JsonObject payload) {
581+
synchronized (installTypesListener) {
582+
for (IInstallTypesListener listener : installTypesListener) {
583+
listener.logTelemetry(telemetryEventName, payload);
584+
}
585+
}
586+
}
587+
516588
@Override
517589
public void addInterceptor(IInterceptor interceptor) {
518590
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class BasicTypeScriptProjectSettings implements ITypeScriptProjectSetting
3232
private final ITypeScriptRepository repository;
3333
private ICompletionEntryMatcher completionEntryMatcher;
3434
private TslintSettingsStrategy tslintStrategy;
35+
private boolean enableTelemetry;
3536

3637
public BasicTypeScriptProjectSettings(File nodejsInstallPath, File typeScriptDir)
3738
throws TypeScriptRepositoryException {
@@ -119,4 +120,13 @@ public FormatCodeSettings getFormatOptions() {
119120
public String getTypeScriptVersion() {
120121
return repository.getTypesScriptVersion();
121122
}
123+
124+
@Override
125+
public boolean isEnableTelemetry() {
126+
return enableTelemetry;
127+
}
128+
129+
public void setEnableTelemetry(boolean enableTelemetry) {
130+
this.enableTelemetry = enableTelemetry;
131+
}
122132
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@ 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();
97103
}

0 commit comments

Comments
 (0)