Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How difficult would it be to have this support fast-cli instead of speedtest? #154

Open
jaxjexjox opened this issue Oct 30, 2023 · 1 comment

Comments

@jaxjexjox
Copy link

Thanks for your time, I'm curious how difficult it would be to adapt this to fast-cli?

I'm looking inside the code but I doubt I come anywhere near close to the skills to convert it.

@ansemjo
Copy link
Owner

ansemjo commented Oct 30, 2023

Leaving aside how to get fast-cli installed in the container for a moment, you would need to replace the command in take_measurement and the JSON parser in parse_measurement. The latter should output a dict with the fieldnames from speedtest-cli's CSV output, so the rest of the application can remain unchanged.

This is untested and just a quick draft, but:

# take a new measurement with fast-cli
def take_measurement_fastcli(attempt=0):
  cmd = ["fast", "--upload", "--json"]
  now = utcnow().isoformat()
  res = run(cmd)
  if res.returncode != 0:
    stderr = res.stderr.decode()
    if attempt < 3:
      # silently retry after a moment
      time.sleep(15)
      return take_measurement_fastcli(attempt+1)
    else:
      raise SpeedTestError(stderr)
  r = parse_measurement_fastcli(json.loads(res.stdout), now)
  table.insert(r)
  print(r)

# column names in speedtest-cli csv output
FIELDNAMES = ("Server ID", "Sponsor", "Server Name", "Timestamp", "Distance",
  "Ping", "Download", "Upload", "Share", "IP Address")

# parse the json output of sindresorhus/fast-cli
def parse_measurement_fastcli(js, now=utcnow().isoformat()):
  # TODO: does fast have an error prop?
  if (err := js.get("error")) is not None:
    raise SpeedTestError(err)
  return dict(zip(FIELDNAMES, (
    "fast.com",
    "fast.com",
    "fast.com",
    now,
    "",
    js["latency"],
    js["downloadSpeed"],
    js["uploadSpeed"],
    "",
    js["userIp"],
  )))

There's a few more places that reference the speedtest.net network of course but this might get you started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants