mrv777 / ckstats

Stats for ckpool
GNU General Public License v3.0
8 stars 0 forks source link

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data #5

Open kic0 opened 3 days ago

kic0 commented 3 days ago

Hi,

I installed ckpool-solo and then this repo, everything seems fine with instalation of both repos although the file provided under ~/ckpool-solo/src/logs/pool/pool.status seems to be broken for a valid json file and this breaks the instalation of ckstats when running pnpm seed:

$ pnpm seed

> ckstats@0.2.3 seed /home/user/ckstats
> node scripts/seed.js

Fetching pool stats...
Fetching pool stats...
Error seeding database: SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/user/ckstats/scripts/seed.js:11:73
    at Array.reduce (<anonymous>)
    at fetchPoolStats (/home/user/ckstats/scripts/seed.js:11:32)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async seed (/home/user/ckstats/scripts/seed.js:18:19)

The API seems correctly setup from this request:

https://pool.address/api/users/btcaddress

Shows a correctly parsed json:

{
 "hashrate1m": "3.7T",
 "hashrate5m": "3.88T",
 "hashrate1hr": "4.01T",
 "hashrate1d": "3.97T",
 "hashrate7d": "2.67T",
 "lastshare": 1732006226,
 "workers": 1,
 "shares": 632592715,
 "bestshare": 2921653605.24227,
 "bestever": 2921653605,
 "authorised": 1731297741,
 "worker": [
  {
   "workername": "btcaddress.worker1",
   "hashrate1m": "3.7T",
   "hashrate5m": "3.88T",
   "hashrate1hr": "4.01T",
   "hashrate1d": "3.97T",
   "hashrate7d": "2.67T",
   "lastshare": 1732006226,
   "shares": 632592715,
   "bestshare": 2921653605.24227,
   "bestever": 2921653605
  }
 ]
}

While https://pool.address/api/pool/pool.status shows:

{"runtime": 9120, "lastupdate": 1732006226, "Users": 1, "Workers": 1, "Idle": 0, "Disconnected": 0}
{"hashrate1m": "3.49T", "hashrate5m": "3.85T", "hashrate15m": "4.04T", "hashrate1hr": "4.01T", "hashrate6hr": "3.98T", "hashrate1d": "3.96T", "hashrate7d": "2.67T"}
{"diff": 0.0, "accepted": 632592715, "rejected": 545404, "bestshare": 2921653605, "SPS1m": 0.274, "SPS5m": 0.302, "SPS15m": 0.317, "SPS1h": 0.309}

Error:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data

Same reply from an online Json parser:

Error: Parse error on line 1:
... "Disconnected": 0}{"hashrate1m": "3.49
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'

Either the file under ~/ckpool-solo/src/logs/pool/pool.status needs to be generated in another way to make it a valid Json file or it needs to be parsed differently under ~/ckstats/scripts/seed.js in order to be correctly parsed.

After fiddling around with the Json output a bit it seems it should be:

[{"runtime": 15300, "lastupdate": 1732012407, "Users": 1, "Workers": 1, "Idle": 0, "Disconnected": 0},
{"hashrate1m": "5.12T", "hashrate5m": "4.42T", "hashrate15m": "4.07T", "hashrate1hr": "3.91T", "hashrate6hr": "3.96T", "hashrate1d": "3.95T", "hashrate7d": "2.68T"},
{"diff": 0.0, "accepted": 638159590, "rejected": 548373, "bestshare": 2921653605, "SPS1m": 0.402, "SPS5m": 0.347, "SPS15m": 0.319, "SPS1h": 0.306}]

This parses to:

[
  {
    "runtime": 9120,
    "lastupdate": 1732006226,
    "Users": 1,
    "Workers": 1,
    "Idle": 0,
    "Disconnected": 0
  },
  {
    "hashrate1m": "3.49T",
    "hashrate5m": "3.85T",
    "hashrate15m": "4.04T",
    "hashrate1hr": "4.01T",
    "hashrate6hr": "3.98T",
    "hashrate1d": "3.96T",
    "hashrate7d": "2.67T"
  },
  {
    "diff": 0,
    "accepted": 632592715,
    "rejected": 545404,
    "bestshare": 2921653605,
    "SPS1m": 0.274,
    "SPS5m": 0.302,
    "SPS15m": 0.317,
    "SPS1h": 0.309
  }
]

Given this the issue seems to be in the way the log file "json" is being created under ~/ckpool-solo/src/logs/pool/pool.status and maybe this issue belongs on the other repo?

Any help would be great, thanks in advance!

mrv777 commented 2 days ago

Hmm, I'll have to check it again, but this line should convert it to proper json array so I don't think that's the issue:

const jsonLines = data.split('\n').filter(Boolean);

mrv777 commented 2 days ago

Can you double check your .env variable? Does it work if you don't set the variable and it defaults to https://solo.ckpool.org

kic0 commented 2 days ago

Hello @mrv777 thanks for the reply.

I tried setting the .env variable as requested although the issue persists, same error while running pnpm seed:

~/ckstats$ pnpm seed

> ckstats@0.2.3 seed /home/user/ckstats
> node scripts/seed.js

Fetching pool stats...
Fetching pool stats...
Error seeding database: SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/user/ckstats/scripts/seed.js:11:73
    at Array.reduce (<anonymous>)
    at fetchPoolStats (/home/user/ckstats/scripts/seed.js:11:32)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async seed (/home/user/ckstats/scripts/seed.js:18:19)
kic0 commented 2 days ago

I noticed while running "pnpm build" that:

✓ Finalizing page optimization    

Route (app)                               Size     First Load JS
┌ ○ /                                     2.14 kB         197 kB
├ ○ /_not-found                           146 B          87.6 kB
├ ƒ /api/resetUser                        0 B                0 B
├ ƒ /api/users                            0 B                0 B
├ ƒ /api/users/togglePrivacy              0 B                0 B
├ ○ /top-difficulties                     179 B          94.4 kB
├ ○ /top-hashrates                        179 B          94.4 kB
├ ƒ /users/[address]                      2.89 kB         202 kB
└ ƒ /users/[address]/workers/[[...name]]  1.43 kB         196 kB
+ First Load JS shared by all             87.4 kB
  ├ chunks/111-fed466ce3ba24250.js        31.8 kB
  ├ chunks/3067790c-b8794aaead367cb1.js   53.7 kB
  └ other shared chunks (total)           2 kB

/api/* shows 0B which seems strange to me could this be related?

Some info that could help:

$ uname -ar
Linux btc-node 6.8.0-48-generic #48-Ubuntu SMP PREEMPT_DYNAMIC Fri Sep 27 14:04:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
$ pnpm --version
9.13.2
$ pnpm next --version
Next.js v14.2.14

Let me know if you need something else from my side, thanks in advance.