Skip to content

Commit 3d4b318

Browse files
committed
Added telemetric collection with Application Insights
1 parent dde9e6f commit 3d4b318

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"skipFiles": [
1212
"<node_internals>/**"
1313
],
14-
"program": "${workspaceFolder}\\index.js",
14+
"program": "${workspaceFolder}\\ai.js",
1515
"console": "externalTerminal"
1616
},
1717
{

GameController/position.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ class Position {
33
this.column = column;
44
this.row = row;
55
}
6+
7+
toString() {
8+
return this.column.toString() + this.row.toString()
9+
}
10+
611
}
712

813
module.exports = Position;

TelemetryClient/telemetryClient.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
const { Worker, isMainThread, parentPort } = require('worker_threads');
2+
13
const appInsights = require('applicationinsights');
24

3-
class TelemetryClient {
4-
constructor() {
5-
let connectionString = 'InstrumentationKey=c764f176-19a5-4949-825d-9f30db2f14e8;IngestionEndpoint=https://germanywestcentral-1.in.applicationinsights.azure.com/';
6-
appInsights.setup(connectionString).start();
7-
}
5+
let connectionString = 'InstrumentationKey=c764f176-19a5-4949-825d-9f30db2f14e8;IngestionEndpoint=https://germanywestcentral-1.in.applicationinsights.azure.com/';
6+
appInsights.setup(connectionString)
7+
.setAutoCollectConsole(true)
8+
.setAutoCollectExceptions(true)
9+
.start();
10+
11+
parentPort.on('message', (message) => {
12+
appInsights.defaultClient.trackEvent({name: message.eventName, properties: message.properties});
13+
appInsights.defaultClient.flush();
14+
});
815

9-
trackevent(eventName, properties, callback) {
10-
appInsights.defaultClient.trackEvent({name: eventName, properties: properties});
11-
appInsights.defaultClient.flush({callback: callback});
12-
}
13-
}
1416

15-
module.exports = TelemetryClient;
17+

battleship.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1+
const { Worker, isMainThread } = require('worker_threads');
12
const readline = require('readline-sync');
23
const gameController = require("./GameController/gameController.js");
34
const cliColor = require('cli-color');
45
const beep = require('beepbeep');
56
const position = require("./GameController/position.js");
67
const letters = require("./GameController/letters.js");
7-
const telemetryClient = require("./TelemetryClient/telemetryClient.js");
8-
const appInsights = require('applicationinsights');
98

10-
let telemetry = new telemetryClient();
9+
let telemetryWorker = new Worker("./TelemetryClient/telemetryClient.js");
1110

1211
class Battleship {
13-
constructor () {
14-
this.start = this.start.bind(this);
15-
this.printWelcomeScreen = this.printWelcomeScreen.bind(this);
16-
}
17-
1812
start() {
1913
console.log("Starting...");
20-
telemetry.trackevent("ApplicationStarted", {Technolog: "Node.js"}, this.printWelcomeScreen);
21-
};
14+
telemetryWorker.postMessage({eventName: 'ApplicationStarted', properties: {Technology: 'Node.js'}});
2215

23-
printWelcomeScreen() {
2416
console.log(cliColor.magenta(" |__"));
2517
console.log(cliColor.magenta(" |\\/"));
2618
console.log(cliColor.magenta(" ---"));
@@ -60,6 +52,8 @@ class Battleship {
6052
var position = Battleship.ParsePosition(readline.question());
6153
var isHit = gameController.CheckIsHit(this.enemyFleet, position);
6254

55+
telemetryWorker.postMessage({eventName: 'Player_ShootPosition', properties: {Position: position.toString(), IsHit: isHit}});
56+
6357
if (isHit) {
6458
beep();
6559

@@ -77,6 +71,9 @@ class Battleship {
7771

7872
var computerPos = this.GetRandomPosition();
7973
var isHit = gameController.CheckIsHit(this.myFleet, computerPos);
74+
75+
telemetryWorker.postMessage({eventName: 'Computer_ShootPosition', properties: {Position: computerPos.toString(), IsHit: isHit}});
76+
8077
console.log();
8178
console.log(`Computer shot in ${computerPos.column}${computerPos.row} and ` + (isHit ? `has hit your ship !` : `miss`));
8279
if (isHit) {
@@ -127,6 +124,7 @@ class Battleship {
127124
for (var i = 1; i < ship.size + 1; i++) {
128125
console.log(`Enter position ${i} of ${ship.size} (i.e A3):`);
129126
const position = readline.question();
127+
telemetryWorker.postMessage({eventName: 'Player_PlaceShipPosition', properties: {Position: position, Ship: ship.name, PositionInShip: i}});
130128
ship.addPosition(Battleship.ParsePosition(position));
131129
}
132130
})

0 commit comments

Comments
 (0)