muety / wakapi

📊 A minimalist, self-hosted WakaTime-compatible backend for coding statistics

Home Page:https://wakapi.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error Submitting Heartbeat Data: 400 Bad Request

wolveix opened this issue · comments

Describe the bug
I've been using Wakapi for a while now, and love all of the metrics it provides :) Thanks for all of your hard work! I try to avoid making bug reports, but I've reached a dead end :(

Unfortunately, as of around a week ago it stopped accepting heartbeats, resulting in no metrics. The container logs show the following (first request to demonstrate that auth and URL are correct, second request to represent the issue):

2024-03-07T10:59:21.422553409Z [INFO ] [request] status=200, method=GET, uri=/api/users/current/statusbar/today, duration=11.600352ms, bytes=431, addr=IP_HERE, user=wolveix
2024-03-07T10:59:23.641782719Z [INFO ] [request] status=400, method=POST, uri=/api/users/current/heartbeats.bulk, duration=1.986689ms, bytes=24, addr=IP_HERE, user=wolveix

I tried adding debug = true to my ~/.wakatime.cfg file to no avail. I'm not too sure how to go about debugging this further, as the local logs in ~/.wakatime/wakatime.log only contain the following:

{"caller":"cmd/run.go:341","file":"/Users/PATH_REDACTED/main.go","func":"cmd.runCmd","level":"error","message":"failed to run command: sending heartbeat(s) failed: bad request at \"https://DOMAIN_REDACTED/api/users/current/heartbeats.bulk\"","now":"2024-03-07T12:32:55Z","os/arch":"darwin/arm64","plugin":"intellijidea/2023.3.4 intellijidea-wakatime/14.3.11","time":1709814755.05,"version":"v1.90.0"}

Please let me know if I can provide any further useful information! Hopefully it's a simple fix, though I'm curious as to what may have caused this. I may have updated the Docker container around the time that the error started occurring, though I can't be sure.

System information

  • Wakapi version: v2.10.5
  • Operating system
    • Docker: ghcr.io/muety/wakapi:latest
  • Database: Postgres
  • IDE: JetBrains IDEA Ultimate
  • IDE Plugin: WakaTime v14.3.11

Thanks a lot for reporting this! I'm traveling at the moment, but will look into it as soon as I get back in a few days.

Looks like I need your help to further debug this. Can you please do me a favor an try the following?

1. Send a crafted request and check the response

Use curl / Postman / an HTTP client of your liking to send a request with the following JSON body to https://YOUR_SERVER/api/users/current/heartbeats.bulk.

[{
    "entity": "/tmp/foo/bar/baz.go",
    "project": "Wakapi Debugging",
    "language": "Go",
    "is_write": true,
    "type": "file",
    "category": null,
    "branch": null,
    "time": 1616680499.113417
}]

Please replace the time field with the output of:

python -c "import time; print(time.time())"

See if that works and, if not, what the response text is.

2. Debug wakatime-cli request

Create a request basket at https://rbaskets.in/web, copy its URL, temporarily set it as the api_url inside ~/.wakatime.cfg, do some coding and then investigate the requests. Possibly send me a link to the basket or, if you want to redact some parts, send the received requests bodies.

3. Provide additional info

  • Is your Wakapi server running in (much) different time zone than your PC? Please post the output of running date in- and outside the container.
  • Can you imagine anything special about the heartbeats (such as weird Unicode characters in file paths / branch names or something)?

Hah, I started filling out a full response to you (with the cURL request showing invalid heartbeat object and such). It was entirely down to the timezone of the server! It seems that when my provider recently migrated the server to new infra, the system time was wrong. While it's 13:52 where I am (in the UK), the server (also in the UK) was reporting 12:01. I've configured NTP, re-deployed the Docker container and everything is working! I'm terribly sorry for the hassle, but I really appreciate your quick and thorough support.

Is there any way to retroactively obtain the lost heartbeats from the past few weeks?

Glad you got it sorted!

The WakaTime CLI keeps heartbeats stored locally as long as there was an error sending them to the server, so there might be a chance they are still not synced. You can check with

~/.wakatime/wakatime-cli-linux-amd64 --offline-count

Otherwise, there's no way to get them back, unfortunately.