2011-05-17 01:18:34 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Status where
|
|
|
|
|
|
|
|
import Control.Monad.State
|
|
|
|
import qualified Data.Map as M
|
2011-09-20 22:57:05 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
import Data.Set (Set)
|
2011-11-20 18:12:48 +00:00
|
|
|
import Text.JSON
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-06-02 01:56:04 +00:00
|
|
|
import qualified Types.Backend as B
|
|
|
|
import qualified Types.Remote as R
|
2011-05-17 01:18:34 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Command.Unused
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-11-14 23:27:00 +00:00
|
|
|
import qualified Annex
|
2011-05-17 01:18:34 +00:00
|
|
|
import Command
|
2011-07-06 00:36:43 +00:00
|
|
|
import Utility.DataUnits
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Key
|
2011-07-05 22:31:46 +00:00
|
|
|
import Backend
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.UUID
|
2011-11-14 20:14:17 +00:00
|
|
|
import Logs.Trust
|
2011-09-30 07:20:24 +00:00
|
|
|
import Remote
|
2011-05-17 01:18:34 +00:00
|
|
|
|
|
|
|
-- a named computation that produces a statistic
|
2011-09-20 22:57:05 +00:00
|
|
|
type Stat = StatState (Maybe (String, StatState String))
|
2011-05-17 01:18:34 +00:00
|
|
|
|
|
|
|
-- cached info that multiple Stats may need
|
|
|
|
data StatInfo = StatInfo
|
2011-09-20 22:57:05 +00:00
|
|
|
{ keysPresentCache :: Maybe (Set Key)
|
|
|
|
, keysReferencedCache :: Maybe (Set Key)
|
2011-05-17 01:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- a state monad for running Stats in
|
|
|
|
type StatState = StateT StatInfo Annex
|
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
|
|
|
def = [command "status" paramNothing seek
|
2011-05-17 01:18:34 +00:00
|
|
|
"shows status information about the annex"]
|
|
|
|
|
|
|
|
seek :: [CommandSeek]
|
|
|
|
seek = [withNothing start]
|
|
|
|
|
|
|
|
{- Order is significant. Less expensive operations, and operations
|
|
|
|
- that share data go together.
|
|
|
|
-}
|
2011-11-14 23:27:00 +00:00
|
|
|
fast_stats :: [Stat]
|
|
|
|
fast_stats =
|
2011-05-17 01:18:34 +00:00
|
|
|
[ supported_backends
|
|
|
|
, supported_remote_types
|
2011-11-14 20:14:17 +00:00
|
|
|
, remote_list Trusted "trusted"
|
|
|
|
, remote_list SemiTrusted "semitrusted"
|
2011-11-14 20:15:48 +00:00
|
|
|
, remote_list UnTrusted "untrusted"
|
2011-12-02 23:21:56 +00:00
|
|
|
, remote_list DeadTrusted "dead"
|
2011-11-14 23:27:00 +00:00
|
|
|
]
|
|
|
|
slow_stats :: [Stat]
|
|
|
|
slow_stats =
|
|
|
|
[ tmp_size
|
2011-05-17 02:01:50 +00:00
|
|
|
, bad_data_size
|
2011-09-20 22:13:08 +00:00
|
|
|
, local_annex_keys
|
2011-05-17 02:19:15 +00:00
|
|
|
, local_annex_size
|
2011-10-29 23:03:43 +00:00
|
|
|
, visible_annex_keys
|
|
|
|
, visible_annex_size
|
2011-05-17 01:18:34 +00:00
|
|
|
, backend_usage
|
|
|
|
]
|
|
|
|
|
2011-09-15 20:50:49 +00:00
|
|
|
start :: CommandStart
|
2011-05-17 01:18:34 +00:00
|
|
|
start = do
|
2011-11-14 23:27:00 +00:00
|
|
|
fast <- Annex.getState Annex.fast
|
|
|
|
let stats = if fast then fast_stats else fast_stats ++ slow_stats
|
2011-11-15 04:30:00 +00:00
|
|
|
showCustom "status" $ do
|
|
|
|
evalStateT (mapM_ showStat stats) (StatInfo Nothing Nothing)
|
|
|
|
return True
|
|
|
|
stop
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-11-20 18:12:48 +00:00
|
|
|
stat :: String -> (String -> StatState String) -> Stat
|
|
|
|
stat desc a = return $ Just (desc, a desc)
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-05-17 02:01:50 +00:00
|
|
|
nostat :: Stat
|
2011-07-15 16:47:14 +00:00
|
|
|
nostat = return Nothing
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-11-20 18:12:48 +00:00
|
|
|
json :: JSON j => (j -> String) -> StatState j -> String -> StatState String
|
|
|
|
json serialize a desc = do
|
|
|
|
j <- a
|
|
|
|
lift $ maybeShowJSON [(desc, j)]
|
|
|
|
return $ serialize j
|
|
|
|
|
|
|
|
nojson :: StatState String -> String -> StatState String
|
|
|
|
nojson a _ = a
|
|
|
|
|
2011-05-17 02:01:50 +00:00
|
|
|
showStat :: Stat -> StatState ()
|
|
|
|
showStat s = calc =<< s
|
|
|
|
where
|
2011-09-20 22:57:05 +00:00
|
|
|
calc (Just (desc, a)) = do
|
2011-11-15 04:30:00 +00:00
|
|
|
(lift . showHeader) desc
|
|
|
|
lift . showRaw =<< a
|
2011-05-17 02:01:50 +00:00
|
|
|
calc Nothing = return ()
|
2011-05-17 01:18:34 +00:00
|
|
|
|
|
|
|
supported_backends :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
supported_backends = stat "supported backends" $ json unwords $
|
|
|
|
return $ map B.name Backend.list
|
2011-05-17 01:18:34 +00:00
|
|
|
|
|
|
|
supported_remote_types :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
supported_remote_types = stat "supported remote types" $ json unwords $
|
|
|
|
return $ map R.typename Remote.remoteTypes
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-11-14 20:14:17 +00:00
|
|
|
remote_list :: TrustLevel -> String -> Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
remote_list level desc = stat n $ nojson $ lift $ do
|
2011-11-18 17:22:48 +00:00
|
|
|
us <- M.keys <$> (M.union <$> uuidMap <*> remoteMap)
|
2011-11-15 04:33:54 +00:00
|
|
|
rs <- fst <$> trustPartition level us
|
|
|
|
s <- prettyPrintUUIDs n rs
|
2011-12-15 22:11:42 +00:00
|
|
|
return $ if null s then "0" else show (length rs) ++ "\n" ++ beginning s
|
2011-11-14 20:14:17 +00:00
|
|
|
where
|
|
|
|
n = desc ++ " repositories"
|
2011-09-30 07:20:24 +00:00
|
|
|
|
2011-05-17 01:18:34 +00:00
|
|
|
local_annex_size :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
local_annex_size = stat "local annex size" $ json id $
|
2011-09-21 03:24:48 +00:00
|
|
|
keySizeSum <$> cachedKeysPresent
|
2011-05-17 01:18:34 +00:00
|
|
|
|
|
|
|
local_annex_keys :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
local_annex_keys = stat "local annex keys" $ json show $
|
|
|
|
S.size <$> cachedKeysPresent
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-10-29 23:03:43 +00:00
|
|
|
visible_annex_size :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
visible_annex_size = stat "visible annex size" $ json id $
|
2011-10-29 23:03:43 +00:00
|
|
|
keySizeSum <$> cachedKeysReferenced
|
|
|
|
|
|
|
|
visible_annex_keys :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
visible_annex_keys = stat "visible annex keys" $ json show $
|
|
|
|
S.size <$> cachedKeysReferenced
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-05-17 02:01:50 +00:00
|
|
|
tmp_size :: Stat
|
|
|
|
tmp_size = staleSize "temporary directory size" gitAnnexTmpDir
|
|
|
|
|
|
|
|
bad_data_size :: Stat
|
|
|
|
bad_data_size = staleSize "bad keys size" gitAnnexBadDir
|
|
|
|
|
2011-05-17 01:18:34 +00:00
|
|
|
backend_usage :: Stat
|
2011-11-20 18:12:48 +00:00
|
|
|
backend_usage = stat "backend usage" $ nojson $
|
|
|
|
usage <$> cachedKeysReferenced <*> cachedKeysPresent
|
2011-05-17 01:18:34 +00:00
|
|
|
where
|
2011-10-29 23:03:43 +00:00
|
|
|
usage a b = pp "" $ reverse . sort $ map swap $ splits $ S.toList $ S.union a b
|
2011-05-17 01:18:34 +00:00
|
|
|
splits :: [Key] -> [(String, Integer)]
|
|
|
|
splits ks = M.toList $ M.fromListWith (+) $ map tcount ks
|
|
|
|
tcount k = (keyBackendName k, 1)
|
2011-05-28 15:56:48 +00:00
|
|
|
swap (a, b) = (b, a)
|
2011-05-17 01:18:34 +00:00
|
|
|
pp c [] = c
|
|
|
|
pp c ((n, b):xs) = "\n\t" ++ b ++ ": " ++ show n ++ pp c xs
|
|
|
|
|
2011-09-20 22:57:05 +00:00
|
|
|
cachedKeysPresent :: StatState (Set Key)
|
2011-05-17 01:18:34 +00:00
|
|
|
cachedKeysPresent = do
|
|
|
|
s <- get
|
|
|
|
case keysPresentCache s of
|
|
|
|
Just v -> return v
|
|
|
|
Nothing -> do
|
2011-09-20 22:57:05 +00:00
|
|
|
keys <- S.fromList <$> lift getKeysPresent
|
|
|
|
put s { keysPresentCache = Just keys }
|
|
|
|
return keys
|
2011-05-17 01:18:34 +00:00
|
|
|
|
2011-09-20 22:57:05 +00:00
|
|
|
cachedKeysReferenced :: StatState (Set Key)
|
2011-05-17 01:18:34 +00:00
|
|
|
cachedKeysReferenced = do
|
|
|
|
s <- get
|
|
|
|
case keysReferencedCache s of
|
|
|
|
Just v -> return v
|
|
|
|
Nothing -> do
|
2011-09-20 22:57:05 +00:00
|
|
|
keys <- S.fromList <$> lift Command.Unused.getKeysReferenced
|
|
|
|
put s { keysReferencedCache = Just keys }
|
|
|
|
return keys
|
|
|
|
|
2011-09-21 00:18:43 +00:00
|
|
|
keySizeSum :: Set Key -> String
|
|
|
|
keySizeSum s = total ++ missingnote
|
|
|
|
where
|
|
|
|
knownsizes = mapMaybe keySize $ S.toList s
|
|
|
|
total = roughSize storageUnits False $ sum knownsizes
|
|
|
|
missing = S.size s - genericLength knownsizes
|
|
|
|
missingnote
|
|
|
|
| missing == 0 = ""
|
|
|
|
| otherwise = aside $
|
2011-09-30 07:20:24 +00:00
|
|
|
"+ " ++ show missing ++
|
|
|
|
" keys of unknown size"
|
2011-05-17 02:01:50 +00:00
|
|
|
|
|
|
|
staleSize :: String -> (Git.Repo -> FilePath) -> Stat
|
|
|
|
staleSize label dirspec = do
|
|
|
|
keys <- lift (Command.Unused.staleKeys dirspec)
|
|
|
|
if null keys
|
|
|
|
then nostat
|
2011-12-09 05:57:13 +00:00
|
|
|
else stat label $ json (++ aside "clean up with git-annex unused") $
|
|
|
|
return $ keySizeSum $ S.fromList keys
|
2011-05-17 02:49:41 +00:00
|
|
|
|
|
|
|
aside :: String -> String
|
2011-09-30 07:05:10 +00:00
|
|
|
aside s = " (" ++ s ++ ")"
|