Commit f0213b38 authored by Yuanle Song's avatar Yuanle Song
Browse files

humanReadableSize: show MiB as float with 1 digit

after decimal point
parent 037833f5
Loading
Loading
Loading
Loading
+3 −7
Original line number Diff line number Diff line
@@ -25,11 +25,11 @@ import qualified Data.ByteString.Char8 as Char8
import Network.HTTP.Types (statusCode)
import Network.HTTP.Simple
import Network.HTTP.Client (path, responseStatus)
import Formatting hiding (bytes)
import Formatting
import Control.Retry (retrying, constantDelay, limitRetries, rsIterNumber)
import qualified System.Logger as L

import Lib (sha1sumOnBytes, guessFilename)
import Lib (sha1sumOnBytes, guessFilename, humanReadableSize)
import CliVersion (cliVersion)
import Utils
import Type
@@ -59,10 +59,6 @@ warnl = clientLogl L.Warn
errorl :: RDClientRuntimeConfig -> T.Text -> IO ()
errorl = clientLogl L.Error

-- | convert byte number to MiB. small number will become 0.
humanReadableSize :: Integer -> String
humanReadableSize bytes = show (bytes `div` 1048576) <> " MiB"

-- | best padding for this many blocks
bestPadding :: Integer -> Int
bestPadding = length . show
@@ -236,7 +232,7 @@ downloadFile rc url = do
    mzero
  liftIO $ do
    infol rc $ "Downloading file: " <> respPath rdResp <> ", "
             <> T.pack (humanReadableSize (respFileSize rdResp))
             <> humanReadableSize (respFileSize rdResp)
             <> ", " <> showt (respBlockCount rdResp) <> " blocks"
    rdResp2 <- loopUntilAllBlocksReady rc url rdResp [] downloadTask
    results <- getTaskResults downloadTask
+4 −4
Original line number Diff line number Diff line
* COMMENT -*- mode: org -*-
#+Date: 2018-05-04
Time-stamp: <2018-05-09>
Time-stamp: <2018-05-10>
#+STARTUP: content
* notes                                                               :entry:
** 2018-05-09 how to release latest code on PyPI?
@@ -312,9 +312,6 @@ this does not require redis at all.
if not set, require redis connection at start up time. show error explicitly
if can't connect to redis.

** 2018-05-08 humanReadableSize: round up when convert byte to MiB. rd shows 377 MiB
or just show as floating point with 1 digit after point.

** 2018-05-07 loopUntilAllBlocksReady, how to track progress?
use a thread pool to download blocks, print overall progress when some parts
done or some time elapsed.
@@ -419,6 +416,9 @@ I remember there are tools that can simulate packet loss.
policy in ovs can do it.

* done                                                                :entry:
** 2018-05-08 humanReadableSize: round up when convert byte to MiB. rd shows 377 MiB
or just show as floating point with 1 digit after point.

** 2018-05-09 distribute on pypi.
- problems
  - project description is not parsed as rst.
+1 −1
Original line number Diff line number Diff line
name:                reliable-download
version:             1.0.0.3
version:             1.0.1.0
synopsis:            provide reliable download service via HTTP
description:         reliable-download web application and cli tool
homepage:            "https://github.com/sylecn/reliable-download#readme"
+8 −1
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@ module Lib
    ( sha1sum
    , sha1sumOnBytes
    , guessFilename
    , genBlocks )
    , genBlocks
    , humanReadableSize )
where

import qualified Data.Text as T
@@ -10,9 +11,15 @@ import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LTE
import qualified Data.ByteString.Lazy as LB
import Crypto.Hash (digestToHexByteString, hashlazy, Digest, SHA1)
import Formatting hiding (bytes)

import Type

-- | convert byte number to MiB. small number will become 0.
humanReadableSize :: Integer -> T.Text
humanReadableSize bytes = sformat (fixed 1 % " MiB")
                          (fromInteger bytes / (1048576 :: Double))

-- | get sha1sum hex string for given bytes
sha1sumOnBytes :: LB.ByteString -> LB.ByteString
sha1sumOnBytes bytes = LB.fromStrict $ digestToHexByteString (hashlazy bytes :: Digest SHA1)
+10 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ import qualified Data.Text as T
import qualified Data.HashMap.Strict as H

import Config
import Lib (sha1sumOnBytes, guessFilename, genBlocks)
import Lib (sha1sumOnBytes, guessFilename, genBlocks, humanReadableSize)
import Worker (fileRange, sha1sumFileRange)
import App (mkWaiApp)

@@ -72,6 +72,15 @@ spec = do
         return result
      contentLB `shouldBe` "abcdef"

  describe "humanReadableSize" $ do
    it "should work" $ do
      humanReadableSize 123 `shouldBe` "0.0 MiB"
      humanReadableSize 1048576 `shouldBe` "1.0 MiB"
      humanReadableSize 1048579 `shouldBe` "1.0 MiB"
      humanReadableSize (1048576 * 2) `shouldBe` "2.0 MiB"
      humanReadableSize 1572864 `shouldBe` "1.5 MiB"
      humanReadableSize 1572865 `shouldBe` "1.5 MiB"

  describe "sha1sumOnBytes" $ do
    it "should work" $ do
      sha1sumOnBytes "\n" `shouldBe` "adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"