Commits

Konstantine Rybnikov  committed e52e403

Add displaying of size info.

  • Participants
  • Parent commits 94e7d18

Comments (0)

Files changed (3)

File Duplicates/Duplicates.hs

 
 module Duplicates.Duplicates (
   indexPath,
-  getDuplicatesCount,
+  getDuplicatesSummary,
   printIndex,
+  showSummary,
 
   getDuplicatesCountByInfo,
   NodeInfo(..),
             , ixFun getNodeFolders
             ]
 
+newtype DuplicatesCount = DuplicatesCount { unDuplicatesCount :: Integer }
+                        deriving ( Show )
+newtype DuplicatesSize = DuplicatesSize { unDuplicatesSize :: Integer }
+                       deriving ( Show )
+data DuplicateSummary = DuplicateSummary { getDuplicatesCount :: DuplicatesCount
+                                         , getDuplicatesSize :: DuplicatesSize }
+                      deriving ( Show )
+
+showSummary :: DuplicateSummary -> String
+showSummary summary =
+  "count: " ++ (show $ unDuplicatesCount $ getDuplicatesCount summary)
+  ++ ", size: " ++ (show $ unDuplicatesSize $ getDuplicatesSize summary)
+
 getInfos :: [FilePath] -> IO [NodeInfo]
 getInfos = mapM getInfo
 
   let entries = fromList nodes
   return entries
 
-getDuplicatesCount :: NodesIndex -> FilePath -> Integer
-getDuplicatesCount index folder = do
+getDuplicatesSummary :: NodesIndex -> FilePath -> DuplicateSummary
+getDuplicatesSummary index folder = 
   let infos = getInfosInsideFolder index folder
-      indexFiltered = index @= (NodeFolder $ folder)
-      counts = filter (>0) $ map (getDuplicatesCountByInfo indexFiltered) infos
-    in toInteger $ length counts
+      withDuplicates = filter (infoHasDuplicate index) infos
+      count = toInteger $ length withDuplicates
+      size' = sum $ map (unNodeSize . getSize) withDuplicates
+  in DuplicateSummary { getDuplicatesCount = DuplicatesCount count
+                      , getDuplicatesSize = DuplicatesSize size' }
 
-getInfosInsideFolder :: NodesIndex -> FilePath -> [NodeInfo]
-getInfosInsideFolder index path =
-  toList elems
-  where elems = index @= (NodeFolder path)
+infoHasDuplicate :: NodesIndex -> NodeInfo -> Bool
+infoHasDuplicate index info =
+  size (getSimilarItems index info) > 0
+
+getSimilarItems :: NodesIndex -> NodeInfo -> NodesIndex
+getSimilarItems index info = ((index @= (getChecksum info)) &&& (index @= (getSize info)))
+
+
+-- getDuplicatesCount :: NodesIndex -> FilePath -> Integer
+-- getDuplicatesCount index folder = do
+--   let infos = getInfosInsideFolder index folder
+-- --       indexFiltered = index @= (NodeFolder $ folder)
+--       counts = filter (>0) $ map (getDuplicatesCountByInfo index) infos
+--     in toInteger $ length counts
 
 getDuplicatesCountByInfo :: NodesIndex -> NodeInfo -> Integer
 getDuplicatesCountByInfo index info =
           (index @= (getChecksum info))
           &&& (index @= (getSize info))
 
+getInfosInsideFolder :: NodesIndex -> FilePath -> [NodeInfo]
+getInfosInsideFolder index path =
+  toList elems
+  where elems = index @= (NodeFolder path)
+
 printIndex :: NodesIndex -> IO ()
 printIndex index = do
   let elems = toList index

File duplicates.hs

 import System.Environment    ( getArgs )
 import System.Directory      ( doesDirectoryExist, canonicalizePath )
 import Duplicates.Utils      ( safeHead, allPossibleDirs )
-import Duplicates.Duplicates ( indexPath, getDuplicatesCount, printIndex )
+import Duplicates.Duplicates ( showSummary, indexPath, getDuplicatesSummary, printIndex )
 
 getPathFromArgs :: [String] -> Maybe String
 getPathFromArgs args = safeHead args
           let dirs = canonicalized : dirs'
           putStrLn "Dirs:"
           putStrLn $ intercalate "\n" dirs
-          let dirCounts = map (getDuplicatesCount index) dirs
-          let dirsWithCounts = zip dirs dirCounts
+          let dirSummaries = map (getDuplicatesSummary index) dirs
+          let dirsWithSummary = zip dirs dirSummaries
           putStrLn "Number of duplicate files:"
-          let showDirWithCount (dir, count) = dir ++ ": " ++ (show count)
-          let stats = map showDirWithCount dirsWithCounts
+          let showDirWithSummary (dir, summary) = dir ++ ": " ++ (showSummary summary)
+          let stats = map showDirWithSummary dirsWithSummary
           putStrLn $ intercalate "\n" $ map ("> " ++) $ stats
           return ()

File tests/Test.hs

         index = fromList items
         dir = "/testdata/2"
 
+test8 =
+  TestCase (
+    assertEqual "root folder with subfolders"
+    5
+    (getDuplicatesCount index dir))
+  where items = [
+          NodeInfo {getPath = NodePath {unNodePath = "/testdata/1/john.txt"},
+                    getSize = NodeSize {unNodeSize = 5},
+                    getChecksum = NodeChecksum {unNodeChecksum = 730483211}},
+          NodeInfo {getPath = NodePath {unNodePath = "/testdata/1/john_1.txt"},
+                    getSize = NodeSize {unNodeSize = 5},
+                    getChecksum = NodeChecksum {unNodeChecksum = 730483211}},
+          NodeInfo {getPath = NodePath {unNodePath = "/testdata/1/paul.txt"},
+                    getSize = NodeSize {unNodeSize = 5},
+                    getChecksum = NodeChecksum {unNodeChecksum = 3352158394}},
+          NodeInfo {getPath = NodePath {unNodePath = "/testdata/2/john.txt"},
+                    getSize = NodeSize {unNodeSize = 5},
+                    getChecksum = NodeChecksum {unNodeChecksum = 730483211}},
+          NodeInfo {getPath = NodePath {unNodePath = "/testdata/2/paul.txt"},
+                    getSize = NodeSize {unNodeSize = 5},
+                    getChecksum = NodeChecksum {unNodeChecksum = 3352158394}}
+          ]
+        index = fromList items
+        dir = "/testdata"
+
 tests = TestList [
   TestLabel "test1" test1,
   TestLabel "test2" test2,
   TestLabel "test4" test4,
   TestLabel "test5" test5,
   TestLabel "test6" test6,
-  TestLabel "test7" test7
+  TestLabel "test7" test7,
+  TestLabel "test8" test8
   ]
 
 main = do