dcrdata icon indicating copy to clipboard operation
dcrdata copied to clipboard

Feature Request: Dynamic Scaling of Displayed Values on Home Page

Open davecgh opened this issue 2 years ago • 9 comments

Notice in the screenshot how the Mining difficulty says "0 Mil". I suggest this should be a dynamically scaled humanized value instead. Similarly, the hashrate is being reproted in Ph/s. It should scale dynamically too.

image

Example code:

func humanizeHPS(b uint64) string {
	const unit = 1000
	if b < unit {
		return fmt.Sprintf("%d h/s", b)
	}
	div, exp := uint64(unit), 0
	for n := b / unit; n >= unit; n /= unit {
		div *= unit
		exp++
	}
	return fmt.Sprintf("%.2f %ch/s", float64(b)/float64(div), "kMGTPE"[exp])
}
func main() {
	const maxUint64 = 1<<64 - 1
	testVals := []uint64{0}
	for hps := uint64(1); hps < 10e18; hps *= 10 {
		testVals = append(testVals, hps)
	}
	testVals = append(testVals, maxUint64)

	for _, hps := range testVals {
		fmt.Printf("%v (input: %d)\n", humanizeHPS(max(hps, 1)), hps)
	}
}

Output:

1 h/s (input: 0)
1 h/s (input: 1)
10 h/s (input: 10)
100 h/s (input: 100)
1.00 kh/s (input: 1000)
10.00 kh/s (input: 10000)
100.00 kh/s (input: 100000)
1.00 Mh/s (input: 1000000)
10.00 Mh/s (input: 10000000)
100.00 Mh/s (input: 100000000)
1.00 Gh/s (input: 1000000000)
10.00 Gh/s (input: 10000000000)
100.00 Gh/s (input: 100000000000)
1.00 Th/s (input: 1000000000000)
10.00 Th/s (input: 10000000000000)
100.00 Th/s (input: 100000000000000)
1.00 Ph/s (input: 1000000000000000)
10.00 Ph/s (input: 10000000000000000)
100.00 Ph/s (input: 100000000000000000)
1.00 Eh/s (input: 1000000000000000000)
18.45 Eh/s (input: 18446744073709551615)

davecgh avatar Sep 04 '23 07:09 davecgh

Yes please! Big zero in Difficulty and Hashrate look like something is broken.

While at it we may also change h/s to H/s per this discussion but it is a minor thing and can be skipped.

xaur avatar Sep 05 '23 18:09 xaur

I spent a long time trying to get familiar with the codebase so I could make this change. I'm nearly there, but it's more challenging than just plug-n-play Dave's code.

I'm going to write some notes down because I'm about to go on vacation, and I'll need a reminder when I get back.

  • The hashrate value itself is calculated in db/dbtypes/conversion.go as CalculateHashRate. However, it's calculated in PH/s for some reason. It's simple to just remove the final / 1000000 so that it's now calculated H/s, but I'm not sure what else is using this code and if changing it will break things. In a pinch, I could also just multiply the value by 1e6, but that feels bad.
  • Once you have the true H/s value, you have to Humanize it. That part is pretty easy. There's basically two options:
    • use the already-included dependency of "github.com/dustin/go-humanize", and call `humanize.SIWithDigits(hashrate, 8, "H/s")
    • Use Dave's code with some minor modifications
  • Unfortunately, both of the two options above return Strings with the unit attached. And the Home page itself has a lot of formatting applied. The first few digits are a different weight/size, and the PH/s unit itself is another style. So I got stuck trying to figure out different ways to solve the formatting issues.
  • In addition to home.tmpl, homepage_controller.js also needs to be updated so that anyone viewing with JS active doesn't get a different output
  • All this stuff needs to be double-checked that it doesn't break the hash rate values and scaling (especially on the Y axis) of the charts.

If anyone wants to pick this up in the next two weeks, feel free, or I'll just continue when I get back.

matthawkins90 avatar Sep 25 '23 06:09 matthawkins90

If anyone wants to pick this up in the next two weeks, feel free,

@matthawkins90 are you back from vacation? I could look into this issue if you’re still away.

ukane-philemon avatar Jan 08 '24 02:01 ukane-philemon

I'm back, haha. Just haven't prioritized this yet. I was actually going to take a stab at it again this week based on Xaur's feedback about how the difficulty could be formatted.

The difficulty on the front page is one thing, but the difficulty in the charts / graphs seems like it's a whole lot harder and I don't even know where to begin.

matthawkins90 avatar Jan 08 '24 03:01 matthawkins90

Oh, okay. Sounds good.

ukane-philemon avatar Jan 08 '24 10:01 ukane-philemon

@ukane-philemon I just finished the difficulty, where I ended up just using threeSigFigs, which is possible because it doesn't need any units.

But since the hash rate requires units, I don't think you can use threeSigFigs for it, and might need to use Dave's original code at the top. Can you take care of that?

matthawkins90 avatar Jan 16 '24 03:01 matthawkins90

matthawkins90 commented 14 hours ago @ukane-philemon I just finished the difficulty, where I ended up just using threeSigFigs, which is possible because it doesn't need any units.

Do you have a PR for this yet?

ukane-philemon avatar Jan 16 '24 18:01 ukane-philemon

https://github.com/decred/dcrdata/pull/1975

I'll be honest, I'm not sure why the build is failing.

matthawkins90 avatar Jan 16 '24 18:01 matthawkins90

#1975

I'll be honest, I'm not sure why the build is failing.

I think the Go test failure is not from you, but the JS, you can fix it by running npm run build and committing the changes. You edited a js file and now the hash does not match what we have in the script tag.

ukane-philemon avatar Jan 16 '24 21:01 ukane-philemon