-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add daily limits support for events (1 startup/day)
Signed-off-by: Fred Bricon <[email protected]>
- Loading branch information
Showing
10 changed files
with
137 additions
and
17 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
import { Memento } from "vscode"; | ||
import { AnalyticsEvent } from '../api/analyticsEvent'; | ||
|
||
|
||
interface EventTracking { | ||
lastUpdated: number; | ||
count: number; | ||
} | ||
|
||
export class EventTracker { | ||
|
||
constructor(private globalState: Memento) { } | ||
|
||
async storeEventCount(payload: AnalyticsEvent, newCount: number): Promise<void> { | ||
const newTracking = { | ||
count: newCount, | ||
lastUpdated: this.getTodaysTimestamp() | ||
} as EventTracking; | ||
return this.globalState.update(this.getEventTrackingKey(payload.event), newTracking); | ||
} | ||
|
||
async getEventCount(payload: AnalyticsEvent): Promise<number> { | ||
const eventTracking = this.globalState.get<EventTracking>(this.getEventTrackingKey(payload.event)); | ||
if (eventTracking) { | ||
//Check if eventTracking timestamp is older than today | ||
let today = this.getTodaysTimestamp(); | ||
let lastEventDay = eventTracking.lastUpdated; | ||
//check if now and lastEventTime are in the same day | ||
if (lastEventDay === today) { | ||
return eventTracking.count; | ||
} | ||
// new day, reset count | ||
} | ||
return 0; | ||
} | ||
|
||
private getTodaysTimestamp(): number { | ||
const now = new Date(); | ||
now.setHours(0, 0, 0, 0); | ||
return now.getTime(); | ||
} | ||
|
||
private getEventTrackingKey(eventName: string): string { | ||
//replace all non alphanumeric characters with a _ | ||
const key = eventName.replace(/[^a-zA-Z0-9]/g, '_'); | ||
return `telemetry.events.tracking.${key}`; | ||
} | ||
} | ||
|
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
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