-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ⚗ [RUMF-971] experiment to detect when the computer goes to sleep * [RUMF-971] add sleep duration on resources `_dd.sleep_duration` * 🔊 [RUMF-971] add sleep duration to resource > 1 day IML context * 🔊 [RUMF-971] add sleep duration to view timings > 1 day IML context * 🔊 display "context" when printing an internal monitoring log * 👌 rename variable
- Loading branch information
1 parent
d9fa056
commit 2916261
Showing
9 changed files
with
144 additions
and
7 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
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,70 @@ | ||
import { TimeStamp } from '@datadog/browser-core' | ||
import { SLEEP_THRESHOLD, trackSleep, getSleepDuration, SLEEP_CHECK_DELAY } from './trackSleep' | ||
|
||
describe('trackSleep', () => { | ||
let stopSleepTracking: () => void | ||
let setIntervalCallback: () => void | ||
let dateNowSpy: jasmine.Spy<typeof Date.now> | ||
|
||
beforeEach(() => { | ||
// Jasmine date mock doesn't work here because it keeps the time and timeouts exactly | ||
// synchronized, and we specifically want to detect when they drift away. | ||
dateNowSpy = spyOn(Date, 'now').and.returnValue(0) | ||
spyOn(window, 'setInterval').and.callFake((callback) => { | ||
setIntervalCallback = callback as () => void | ||
return 1 | ||
}) | ||
;({ stop: stopSleepTracking } = trackSleep()) | ||
}) | ||
|
||
afterEach(() => { | ||
stopSleepTracking() | ||
}) | ||
|
||
describe('getSleepDuration', () => { | ||
it('returns 0 if it was not previously sleeping', () => { | ||
tick(SLEEP_THRESHOLD - 1) | ||
expect(getSleepDuration()).toBe(0) | ||
}) | ||
|
||
it('returns the sleep duration if it was previously sleeping', () => { | ||
tick(SLEEP_THRESHOLD) | ||
expect(getSleepDuration()).toBe(SLEEP_THRESHOLD) | ||
}) | ||
|
||
it('returns 0 if it was not sleeping since a given timestamp', () => { | ||
tick(SLEEP_THRESHOLD) | ||
expect(getSleepDuration((SLEEP_THRESHOLD + 1) as TimeStamp)).toBe(0) | ||
}) | ||
|
||
it('returns the sleep duration if it was sleeping since a given timestamp', () => { | ||
tick(SLEEP_THRESHOLD) | ||
expect(getSleepDuration(0 as TimeStamp)).toBe(SLEEP_THRESHOLD) | ||
}) | ||
|
||
it('collects the sleep periods across time', () => { | ||
tick(SLEEP_CHECK_DELAY) | ||
setIntervalCallback() | ||
|
||
// Sleep now | ||
tick(SLEEP_THRESHOLD) | ||
setIntervalCallback() | ||
|
||
tick(SLEEP_CHECK_DELAY) | ||
setIntervalCallback() | ||
|
||
// Sleep now | ||
tick(SLEEP_THRESHOLD) | ||
setIntervalCallback() | ||
|
||
setIntervalCallback() | ||
tick(SLEEP_CHECK_DELAY) | ||
|
||
expect(getSleepDuration(SLEEP_CHECK_DELAY as TimeStamp)).toBe(SLEEP_THRESHOLD * 2) | ||
}) | ||
}) | ||
|
||
function tick(delay: number) { | ||
dateNowSpy.and.returnValue(dateNowSpy() + delay) | ||
} | ||
}) |
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,39 @@ | ||
import { monitor, ONE_MINUTE, ONE_SECOND, TimeStamp, timeStampNow } from '@datadog/browser-core' | ||
|
||
export const SLEEP_CHECK_DELAY = ONE_SECOND | ||
export const SLEEP_THRESHOLD = ONE_MINUTE | ||
|
||
let sleepPeriods: Array<{ start: TimeStamp; end: TimeStamp }> | undefined | ||
let lastWoke: TimeStamp | undefined | ||
|
||
export function trackSleep() { | ||
lastWoke = timeStampNow() | ||
sleepPeriods = [] | ||
const intervalId = setInterval(monitor(checkSleep), SLEEP_CHECK_DELAY) | ||
return { stop: () => clearInterval(intervalId) } | ||
} | ||
|
||
export function getSleepDuration(since?: TimeStamp) { | ||
if (!sleepPeriods) { | ||
return 0 | ||
} | ||
checkSleep() | ||
let filteredPeriods | ||
if (since === undefined) { | ||
filteredPeriods = sleepPeriods | ||
} else { | ||
filteredPeriods = sleepPeriods.filter((period) => period.end >= since) | ||
} | ||
return filteredPeriods.reduce((total, period) => total + (period.end - period.start), 0) | ||
} | ||
|
||
function checkSleep() { | ||
if (lastWoke === undefined || !sleepPeriods) { | ||
return | ||
} | ||
const now = timeStampNow() | ||
if (now - lastWoke >= SLEEP_THRESHOLD) { | ||
sleepPeriods.push({ start: lastWoke, end: now }) | ||
} | ||
lastWoke = now | ||
} |
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