Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dogfuntom/686e4ecf84f5b72eb4d700e98ce02227 to your computer and use it in GitHub Desktop.
Save dogfuntom/686e4ecf84f5b72eb4d700e98ce02227 to your computer and use it in GitHub Desktop.
On Windows, noise input from a bad/old gamepad causes ActivityWatch to think that the user is not AFK. But if this happens in all-windows-minimized/lockscreen/screensaving states, the window title will be empty. So, we can just delete such events, but for this to work better, develop a habit of minimizing everything and/or reduce time before loc…
$bucket = 'aw-watcher-window_MainPC-W10P'
'Getting events…'
# Limiting to about 512 during experimenting is quicker (and safer) to work this.
# Once you're sure that everything works as needed, you can just comment it out
# (note that initialization will take a few minutes in that case).
$events = Invoke-RestMethod -Uri ("http://localhost:5600/api/0/buckets/$bucket/events"<# + '?limit=512' #>)
"Number of events received: $($events.Length)…"
$hashTable = @{}
foreach ($e in $events) {
if ($e.data.title -match '^$') {
$hashTable[$e.data.app] ??= @()
$hashTable[$e.data.app] += $e
}
}
if ($hashTable.Count -eq 0) {
'Nothing found.'
} else {
'Found this many events with empty titles:'
$hashTable | Format-Table @{Label='App'; Expression={$_.Key}}, @{Label='Count'; Expression={$_.Value.Length}}
Pause
}
$invokeDeleteMethod = {
param (
# A single event, one of those gotten using REST Method.
[Parameter(Mandatory)]
$e,
# The name/ID of the bucket.
[Parameter(Mandatory)]
[string]
$bucket
)
# ("http://localhost:5600/api/0/buckets/$bucket/events/$($e.id)")
$response = Invoke-RestMethod -Uri "http://localhost:5600/api/0/buckets/$bucket/events/$($e.id)" -Method Delete
if ($response.success -ne 1) {
"Failed to delete event $($e.data) because '$response'."
}
else {
#"Deleted event: $($e.data)."
Write-Host -NoNewline "."
}
}
'Creating delete jobs…'
$jobs = foreach ($array in $hashTable.Values) {
foreach ($e in $array) {
Start-ThreadJob -ScriptBlock $invokeDeleteMethod -ArgumentList $e, $bucket
}
}
"Jobs created: $($jobs.Count)…"
if ($jobs) {
Receive-Job -Job $jobs -AutoRemoveJob -Wait
''
}
else {
'Nothing done.'
}
# Note about aw-server-rust:
# It doesn’t seem to support deleting events yet, so aw-server must be used instead.
# (Namely, the `Invoke-RestMethod ... Delete` results in an empty string.)
# Note that PS6 or newer are assumed.
# If you don’t have PS6+:
# 1. replace `??=` operator with comparing with $null in an `if`
# 2. simply replace `Start-ThreadJob` with `Start-Job` or rewrite to completely get rid of parallelism
# (however, both seem to be very much slower)
# Maintainability:
# The jobs are a bit of error-prone mine-field, be careful with editing code involving them: https://stackoverflow.com/a/76847044/776442.
# Implementation note:
# The `foreach` statement is better than ForEach-Object command or ForEach() method
# because it gives full freedom, including producing smaller array or nothing at all,
# while the alternatives do strict mapping, i.e. produce array of the same size
# (i.e. skipped elements are $null instead of not existing at all).
# Links:
# - inspired by: https://forum.activitywatch.net/t/add-an-exclude-list/345/17
# - possible optimizations: https://docs.activitywatch.net/en/latest/api/python.html#module-aw_transform
# (the link is about Python but this API is mostly mirrored to REST, AFAIK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment