From 2c496b1725eadb4c1811dbca3fce9527354f590a Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 5 Sep 2015 22:41:47 +0530
Subject: [PATCH] Peer: function to calculate the total bytes downloaded so far

---
 src/FuncTorrent/Peer.hs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 0b9ece7..9ca40f4 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -36,7 +36,7 @@ data PieceDlState = Pending
                   | Have
                   deriving (Show, Eq)
 
--- todo - map with index to a new data structure (peers who have that piece amd state)
+-- todo - map with index to a new data structure (peers who have that piece and state)
 data PieceData = PieceData { peers :: [Peer]        -- ^ list of peers who have this piece
                            , dlstate :: PieceDlState  -- ^ state of the piece from download perspective.
                            , hash  :: ByteString    -- ^ piece hash
@@ -106,6 +106,10 @@ pickPiece :: PieceMap -> Maybe Integer
 pickPiece =
   (fst `liftM`) . headMay . toList . filter (\v -> dlstate v == Pending)
 
+bytesDownloaded :: PieceMap -> Integer
+bytesDownloaded =
+  sum . (map (len . snd)) . toList . filter (\v -> dlstate v == Have)
+
 updatePieceAvailability :: PieceMap -> Peer -> [Integer] -> PieceMap
 updatePieceAvailability pieceStatus p pieceList =
   mapWithKey (\k pd -> if k `elem` pieceList
-- 
2.45.2