implement dataUnits finally
authorJoey Hess <joeyh@joeyh.name>
Thu, 5 May 2022 19:22:11 +0000 (15:22 -0400)
committerJoey Hess <joeyh@joeyh.name>
Thu, 5 May 2022 19:25:11 +0000 (15:25 -0400)
Added support for "megabit" and related bandwidth units in
annex.stalldetection and everywhere else that git-annex parses data units.

Note that the short form is "Mbit" not "Mb" because that differs from "MB"
only in case, and git-annex parses units case-insensitively. It would be
horrible if two different versions of git-annex parsed the same value
differently, so I don't think "Mb" can be supported.

See comment for bonus sad story from my childhood.

Sponsored-by: Nicholas Golder-Manning
CHANGELOG
Utility/DataUnits.hs
doc/todo/Mbps.mdwn

index b391c69860b28d08be657595615c6b9c72828d4a..506f3cd0e724e37d263f598bc7a04a05338d160f 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,13 @@
+git-annex (10.20220505) UNRELEASED; urgency=medium
+
+  * Added support for "megabit" and related bandwidth units
+    in annex.stalldetection and everywhere else that git-annex parses
+    data units. Note that the short form is "Mbit" not "Mb" because
+    that differs from "MB" only in case, and git-annex parses units
+    case-insensitively.
+
+ -- Joey Hess <id@joeyh.name>  Thu, 05 May 2022 15:08:07 -0400
+
 git-annex (10.20220504) upstream; urgency=medium
 
   * Ignore annex.numcopies set to 0 in gitattributes or git config,
index a6c9ffcf19fb53ba862fac845bb999582cfad58a..7fe4a0a4ae99f2014f0871fec9cfe421cf3b5c79 100644 (file)
@@ -1,6 +1,6 @@
 {- data size display and parsing
  -
- - Copyright 2011 Joey Hess <id@joeyh.name>
+ - Copyright 2011-2022 Joey Hess <id@joeyh.name>
  -
  - License: BSD-2-clause
  -
  -
  - So, a committee was formed. And it arrived at a committee-like decision,
  - which satisfied noone, confused everyone, and made the world an uglier
- - place. As with all committees, this was meh.
+ - place. As with all committees, this was meh. Or in this case, "mib".
  -
  - And the drive manufacturers happily continued selling drives that are
  - increasingly smaller than you'd expect, if you don't count on your
  - fingers. But that are increasingly too big for anyone to much notice.
  - This caused me to need git-annex.
  -
+ - Meanwhile, over in telecommunications land, they were using entirely
+ - different units that differ only in capitalization sometimes.
+ - (At one point this convinced me that it was a good idea to buy an ISDN
+ - line because 128 kb/s sounded really fast! But it was really only 128
+ - kbit/s...)
+ -
  - Thus, I use units here that I loathe. Because if I didn't, people would
  - be confused that their drives seem the wrong size, and other people would
  - complain at me for not being standards compliant. And we call this
@@ -62,7 +68,7 @@ data Unit = Unit ByteSize Abbrev Name
        deriving (Ord, Show, Eq)
 
 dataUnits :: [Unit]
-dataUnits = storageUnits ++ memoryUnits
+dataUnits = storageUnits ++ memoryUnits ++ bandwidthUnits
 
 {- Storage units are (stupidly) powers of ten. -}
 storageUnits :: [Unit]
@@ -75,7 +81,7 @@ storageUnits =
        , Unit (p 3) "GB" "gigabyte"
        , Unit (p 2) "MB" "megabyte"
        , Unit (p 1) "kB" "kilobyte" -- weird capitalization thanks to committe
-       , Unit (p 0) "B" "byte"
+       , Unit 1 "B" "byte"
        ]
   where
        p :: Integer -> Integer
@@ -92,15 +98,33 @@ memoryUnits =
        , Unit (p 3) "GiB" "gibibyte"
        , Unit (p 2) "MiB" "mebibyte"
        , Unit (p 1) "KiB" "kibibyte"
-       , Unit (p 0) "B" "byte"
+       , Unit 1 "B" "byte"
        ]
   where
        p :: Integer -> Integer
        p n = 2^(n*10)
 
-{- Bandwidth units are only measured in bits if you're some crazy telco. -}
+{- Bandwidth units are (stupidly) measured in bits, not bytes, and are
+ - (also stupidly) powers of ten. 
+ -
+ - While it's fairly common for "Mb", "Gb" etc to be used, that differs
+ - from "MB", "GB", etc only in case, and readSize is case-insensitive.
+ - So "Mbit", "Gbit" etc are used instead to avoid parsing ambiguity.
+ -}
 bandwidthUnits :: [Unit]
-bandwidthUnits = error "stop trying to rip people off"
+bandwidthUnits =
+       [ Unit (p 8) "Ybit" "yottabit"
+       , Unit (p 7) "Zbit" "zettabit"
+       , Unit (p 6) "Ebit" "exabit"
+       , Unit (p 5) "Pbit" "petabit"
+       , Unit (p 4) "Tbit" "terabit"
+       , Unit (p 3) "Gbit" "gigabit"
+       , Unit (p 2) "Mbit" "megabit"
+       , Unit (p 1) "kbit" "kilobit" -- weird capitalization thanks to committe
+       ]
+  where
+       p :: Integer -> Integer
+       p n = (1000^n) `div` 8
 
 {- Do you yearn for the days when men were men and megabytes were megabytes? -}
 oldSchoolUnits :: [Unit]
index 8710bdd7c2663a2a5f108c5d2e1c24f157c04188..a411df7310690a172069d66f6c6de635838a29d9 100644 (file)
@@ -9,3 +9,9 @@ and match up with the other bandwidth displays.
 
 This might make sense as a per-remote configurable value. Allowing
 using MiB/s for a hard drive and Mbps for a network remote. --[[Joey]]
+
+> I've implemented bandwidthUnits now, but it's not used for display yet.
+> It is possible to specify such units in eg annex.stalldetection now.
+> Note that it uses Mbit, not Mb because that is just confusingly close to
+> "MB" and git-annex parses data units case insensitively. So the actual
+> display will end up being "Mbit/s" rather than Mbps probably. --[[Joey]]