-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic telemetry to track anonymous usage (#48)
* Add basic telemetry to track anonymous usage * Use generic type Co-authored-by: Alex Khomenko <[email protected]> * Better date handling * Switch to daily post * PR feedback * Fix logic (thanks Dan) * Logging, and add timestamp * Adjust post body to usageStats needs * Adjust post body to usageStats needs * Fix telemetry url and add opt-out * Share number of 'views' * Correct comment --------- Co-authored-by: Alex Khomenko <[email protected]>
- Loading branch information
1 parent
6c9a905
commit 96dc2e7
Showing
6 changed files
with
2,828 additions
and
4,303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as vscode from "vscode"; | ||
import axios from "axios"; | ||
import {v4 as uuidv4} from 'uuid'; | ||
import * as util from "./util"; | ||
|
||
const LAST_UPDATED_DATE = "lastUpdatedDate"; | ||
const INSTALLATION_DATE = "installDate"; | ||
const INSTALLATION_UUID = "installUUID"; | ||
const RECENT_VIEWS = "recentViews"; | ||
|
||
const URL = "https://stats.grafana.org/vscode-usage-report"; | ||
|
||
/* | ||
* Sends a single anonymous telemetry call once per day, allowing tracking of | ||
* usage - reports on first opening of a dashboard each day. | ||
*/ | ||
export async function sendTelemetry(ctx: vscode.ExtensionContext) { | ||
|
||
const settings = vscode.workspace.getConfiguration("grafana-vscode"); | ||
const enableTelemetry = settings.get<boolean>("telemetry"); | ||
if (!enableTelemetry) { | ||
return; | ||
} | ||
const lastUpdatedDate = ctx.globalState.get<string | undefined>(LAST_UPDATED_DATE); | ||
const today = new Date(); | ||
|
||
if (lastUpdatedDate === undefined) { | ||
const uuid = uuidv4(); | ||
await sendEvent("first", uuid, today.toISOString(), 1); | ||
ctx.globalState.update(LAST_UPDATED_DATE, today); | ||
ctx.globalState.update(INSTALLATION_UUID, uuid); | ||
ctx.globalState.update(INSTALLATION_DATE, today); | ||
ctx.globalState.update(RECENT_VIEWS, 0); | ||
} else { | ||
let recentViews = ctx.globalState.get<number | undefined>(RECENT_VIEWS); | ||
recentViews = (recentViews === undefined) ? 1 : recentViews+1; | ||
|
||
if (differentDay(new Date(lastUpdatedDate), today)) { | ||
let uuid = ctx.globalState.get(INSTALLATION_UUID); | ||
let installDate = ctx.globalState.get(INSTALLATION_DATE); | ||
if (uuid === undefined) { | ||
console.log("UUID undefined. Shouldn't happen."); | ||
uuid = uuidv4(); | ||
ctx.globalState.update(INSTALLATION_UUID, uuid); | ||
} | ||
if (installDate === undefined) { | ||
console.log("Install date undefined. Shouldn't happen."); | ||
installDate = (new Date(lastUpdatedDate)).toISOString(); | ||
ctx.globalState.update(INSTALLATION_DATE, installDate); | ||
} | ||
await sendEvent("subsequent", uuid as string, installDate as string, recentViews); | ||
ctx.globalState.update(LAST_UPDATED_DATE, today); | ||
recentViews = 0; | ||
} | ||
ctx.globalState.update(RECENT_VIEWS, recentViews); | ||
} | ||
} | ||
|
||
function differentDay(d1: Date, d2: Date) { | ||
return d1.getDate() !== d2.getDate() || | ||
d1.getMonth() !== d2.getMonth() || | ||
d1.getFullYear() !== d2.getFullYear(); | ||
} | ||
|
||
async function sendEvent(eventType: string, uuid: string, installDate: string, views: number | undefined) { | ||
try { | ||
const data = { | ||
uuid: uuid, | ||
eventType: eventType, | ||
timestamp: Date(), | ||
createdAt: installDate, | ||
os: process.platform, | ||
arch: process.arch, | ||
packaging: "unknown", | ||
views: views, | ||
version: util.getVersion(), | ||
}; | ||
|
||
await axios.post(URL, data, { | ||
headers: { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'User-Agent': util.getUserAgent(), | ||
}, | ||
}); | ||
} catch(e) { | ||
console.log("Telemetry error", e, "for event", eventType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
export function getNonce() { | ||
let text = ""; | ||
const possible = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
for (let i = 0; i < 32; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
let userAgent: string; | ||
let version: string; | ||
|
||
export function setVersion(v: string) { | ||
version = v; | ||
userAgent = `Grafana VSCode Extension/v${version}`; | ||
} | ||
|
||
export function getVersion(): string { | ||
return version; | ||
} | ||
|
||
export function getUserAgent(): string { | ||
return userAgent; | ||
} |
Oops, something went wrong.