Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New datastructure eventdata #6

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
8 changes: 7 additions & 1 deletion configuration-example/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"editor.autoIndent": "none",
"editor.formatOnSave": false,
"workbench.editor.enablePreview": false,
"telemetry.key": "c0f1b3b0-0b3b-4b3b-8b3b-0b3b0b3b0b3b",
"telemetry.activeEvents": [
{
"name": "DocumentOpenEvent",
Expand Down Expand Up @@ -33,6 +37,7 @@
"args": {
"id": "S3",
"url": "https://telemetry.mentoracademy.org/telemetry-edtech-labs-si-umich-edu/dev/test-telemetry",
"consentlink": "https://www.influxdata.com/privacy-policy/",
"env": {
"WORKSPACE_ID": "dev"
}
Expand All @@ -43,6 +48,7 @@
"args": {
"id": "InfluxDB",
"url": "https://68ltdi5iij.execute-api.us-east-1.amazonaws.com/influx-vscode",
"consentlink": "https://www.influxdata.com/privacy-policy/",
"params": {
"influx_bucket": "telemetry_dev",
"influx_measurement": "vscode"
Expand All @@ -60,4 +66,4 @@
]
}
]
}
}
50 changes: 48 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 9 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "telemetry",
"displayName": "Telemetry",
"description": "",
"version": "0.0.8",
"version": "0.0.13",
"repository": "https://github.com/educational-technology-collective/vscode-telemetry",
"publisher": "educational-technology-collective",
"engines": {
Expand Down Expand Up @@ -43,32 +43,7 @@
},
"telemetry.exporters": {
"type": "array",
"default": [
{
"type": "console_exporter"
},
{
"type": "remote_exporter",
"args": {
"id": "InfluxDB",
"url": "https://68ltdi5iij.execute-api.us-east-1.amazonaws.com/influx-vscode",
"params": {
"influx_bucket": "telemetry_dev",
"influx_measurement": "vscode"
}
},
"activeEvents": [
{
"name": "DocumentOpenEvent",
"logWholeDocument": true
},
{
"name": "DocumentChangeEvent",
"logWholeDocument": false
}
]
}
],
"default": [],
"description": "Exporter settings"
}
}
Expand All @@ -86,9 +61,11 @@
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/crypto-js": "^4.2.2",
"@types/mocha": "^10.0.3",
"@types/node": "18.x",
"@types/vscode": "^1.84.0",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"@vscode/test-electron": "^2.3.6",
Expand All @@ -100,5 +77,9 @@
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"crypto-js": "^4.2.0",
"ws": "^8.16.0"
}
}
}
146 changes: 115 additions & 31 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,138 @@
import * as vscode from "vscode";
import { producerCollection } from "./producer";
import { ActiveEvent, Exporter } from "./types";
import * as vscode from 'vscode'
import { producerCollection } from './producer'
import { ActiveEvent, Exporter, EventData } from './types'
import { WebSocket } from 'ws'
import * as CryptoJS from 'crypto-js'
import { publishEvent } from './exporters'
let nextId = 1
let uri2Id: { [key: string]: number } = {}

function updateAndGetId(uri: string) {
if (!uri2Id.hasOwnProperty(uri)) uri2Id[uri] = nextId++
return uri2Id[uri]
}
export function activate(context: vscode.ExtensionContext) {
const activeEvents: ActiveEvent[] | undefined = vscode.workspace
.getConfiguration("telemetry")
.get("activeEvents");
.getConfiguration('telemetry')
.get('activeEvents')
const exporters: Exporter[] | undefined = vscode.workspace
.getConfiguration("telemetry")
.get("exporters");

.getConfiguration('telemetry')
.get('exporters')
const key: string | undefined = vscode.workspace
.getConfiguration('telemetry')
.get('key')
const processedExporters =
activeEvents && activeEvents.length
? exporters?.map((e) => {
if (!e.activeEvents) {
e.activeEvents = activeEvents;
return e;
e.activeEvents = activeEvents
return e
} else {
return e;
return e
}
})
: exporters?.filter((e) => e.activeEvents && e.activeEvents.length);
: exporters?.filter((e) => e.activeEvents && e.activeEvents.length)
console.log(
'vscode.workspace',
vscode.workspace.getConfiguration('telemetry')
)
console.log('processedExporters', processedExporters)
console.log('exporters', exporters)
console.log('key', key)
// Exporters without specifying the corresponding activeEvents will use the global activeEvents configuration.
// When the global activeEvents configuration is null, exporters that do not have corresponding activeEvents will be ignored.

const exporterIds = processedExporters
?.map((each) => each.args?.id)
.filter((e) => e !== undefined);
vscode.env.isTelemetryEnabled
? vscode.window.showInformationMessage(
`Telemetry data is being logged ${exporterIds?.join(" & ")}.`,
)
: console.log("Telemetry extension is disabled");
vscode.env.onDidChangeTelemetryEnabled((e: boolean) =>
e
const exporterIds: string[] = []
const exporterconsentlinks: string[] = []
processedExporters?.forEach((each) => {
const id = each.args?.id
const consentlink = each.args?.consentlink
if (id !== undefined) {
exporterIds.push(id)
exporterconsentlinks.push(consentlink || 'No consent link provided')
}
})
console.log(
'exporterIds:',
exporterIds,
'exporterconsentlinks:',
exporterconsentlinks
)
if (exporterIds.length > 0) {
vscode.env.isTelemetryEnabled
? vscode.window.showInformationMessage(
`Telemetry data is being logged to ${exporterIds?.join(" & ")}.`,
`Telemetry data is being logged to the following exporters,
here are their privacy policy links:
${exporterIds
.map((id, index) => `${id}:${exporterconsentlinks[index]}`)
.join('\n')}`
)
: vscode.window.showInformationMessage(
"Telemetry extension is disabled.",
),
);
: console.log('Telemetry extension is disabled')

// vscode.env.onDidChangeTelemetryEnabled((e: boolean) =>
// e
// ? vscode.window.showInformationMessage(
// `Telemetry extension is installed.`
// )
// : vscode.window.showInformationMessage(
// 'Telemetry extension is disabled.'
// )
// )
}
processedExporters?.forEach((exporter) => {
producerCollection.forEach((producer) => {
if (exporter.activeEvents?.map((o) => o.name).includes(producer.id)) {
new producer().listen(context, exporter);
new producer(key).listen(context, exporter)
}
});
});
})
})
const clientId = vscode.env.machineId
const clientIdBuffer = Buffer.from(clientId.toString())
const socket = new WebSocket('ws://localhost:8080?clientId=' + clientId)
console.log('Connecting to server', socket)
socket.on('open', () => {
console.log('Connected to server now!')
})

socket.addEventListener('message', (event) => {
const data_string =
typeof event.data === 'string'
? JSON.parse(event.data)
: JSON.parse(event.data.toString())
console.log('Message from server ', data_string)
const filePath = data_string
try {
const uri = vscode.Uri.parse(data_string)
const filePath = uri.fsPath
vscode.workspace.openTextDocument(filePath).then((document) => {
const id = updateAndGetId(document.uri.toString())
const rangestart = new vscode.Position(0, 0)
const rangeend = new vscode.Position(0, 0)
const hash = CryptoJS.SHA256(document.getText()).toString()
const eventdata: EventData = {
key: key,
eventName: 'reopen',
eventTime: Date.now(),
sessionId: vscode.env.sessionId,
machineId: vscode.env.machineId,
documentUri: document.uri.toString(),
documentLanguageId: document.languageId,
documentId: id,
operation: 'open',
value: document.getText(),
rangeOffset: '0',
rangeLength: '0',
rangestart_line: rangestart.line.toString(),
rangestart_character: rangestart.character.toString(),
rangeend_line: rangeend.line.toString(),
rangeend_character: rangeend.character.toString(),
hash: hash,
}
exporters?.forEach((exporter) => publishEvent(eventdata, exporter))
})
} catch (error) {
console.error('Error opening document:', error)
}
})
}

export function deactivate() {}
Loading