From bc4c3efd191f3cc5b7b9f046775bff706c491a7d Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 22 Apr 2014 20:51:40 -0400 Subject: [PATCH] Align the mailbox counts in the summary report to the same column. Add mailbox counts to the detail report. Remove the 'both' option. --- doc/man1/mailbox-count.1 | 6 +- src/CommandLine.hs | 7 +-- src/Configuration.hs | 3 - src/Main.hs | 2 +- src/OptionalConfiguration.hs | 5 -- src/Report.hs | 92 ++++++++++++++++++----------- test/fixtures/postfixadmin.sqlite3 | Bin 2048 -> 2048 bytes 7 files changed, 61 insertions(+), 54 deletions(-) diff --git a/doc/man1/mailbox-count.1 b/doc/man1/mailbox-count.1 index c7b6164..e674f55 100644 --- a/doc/man1/mailbox-count.1 +++ b/doc/man1/mailbox-count.1 @@ -14,13 +14,9 @@ Either a summary, or detailed report (with \fI\-\-detail\fR) of the number of mailboxes per-domain contained in the database. .P The summary shows only the per-domain mailbox count, while the -detailed report shows each mailbox. -.P -With \fI\-\-both\fR, both reports are produced at the same time. +detailed report shows both the count and a list of mailboxes. .SH OPTIONS -.IP \fB\-\-both\fR,\ \fB-b\fR -Produce both summary and detailed reports. .IP \fB\-\-detail\fR,\ \fB-d\fR Produce a detailed report listing all mailboxes by domain. diff --git a/src/CommandLine.hs b/src/CommandLine.hs index 8a2a811..6c8833c 100644 --- a/src/CommandLine.hs +++ b/src/CommandLine.hs @@ -30,10 +30,6 @@ program_name = "mailbox-count" my_summary :: String my_summary = program_name ++ "-" ++ (showVersion version) -both_help :: String -both_help = - "Produce both summary and detailed reports" - database_help :: String database_help = "The name of the database to which we should connect" @@ -60,8 +56,7 @@ username_help = arg_spec :: OptionalConfiguration arg_spec = OptionalConfiguration - { both = def &= help both_help, - database = def &= help database_help, + { database = def &= help database_help, detail = def &= help detail_help, host = def &= help host_help, password = def &= help password_help, diff --git a/src/Configuration.hs b/src/Configuration.hs index e383abc..8b6a7ce 100644 --- a/src/Configuration.hs +++ b/src/Configuration.hs @@ -18,7 +18,6 @@ import qualified OptionalConfiguration as OC ( -- data Configuration = Configuration { - both :: Bool, database :: Maybe String, detail :: Bool, host :: Maybe String, @@ -31,7 +30,6 @@ data Configuration = -- values. instance Default Configuration where def = Configuration { - both = def, database = def, detail = def, host = def, @@ -47,7 +45,6 @@ merge_optional :: Configuration -> Configuration merge_optional cfg opt_cfg = Configuration - (merge (both cfg) (OC.both opt_cfg)) (OC.merge_maybes (database cfg) (OC.database opt_cfg)) (merge (detail cfg) (OC.detail opt_cfg)) (OC.merge_maybes (host cfg) (OC.host opt_cfg)) diff --git a/src/Main.hs b/src/Main.hs index 633e969..d153716 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -68,6 +68,6 @@ main = do --exitWith (ExitFailure exit_no_username) conn <- connectPostgreSQL (connection_string cfg) - r <- report conn (both cfg) (detail cfg) + r <- report conn (detail cfg) putStrLn r -- disconnect conn diff --git a/src/OptionalConfiguration.hs b/src/OptionalConfiguration.hs index 6bf4f9b..0bd093b 100644 --- a/src/OptionalConfiguration.hs +++ b/src/OptionalConfiguration.hs @@ -34,7 +34,6 @@ import System.IO.Error ( catchIOError ) -- data OptionalConfiguration = OptionalConfiguration { - both :: Maybe Bool, database :: Maybe String, detail :: Maybe Bool, host :: Maybe String, @@ -71,12 +70,10 @@ instance Monoid OptionalConfiguration where Nothing Nothing Nothing - Nothing -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@. cfg1 `mappend` cfg2 = OptionalConfiguration - (merge_maybes (both cfg1) (both cfg2)) (merge_maybes (database cfg1) (database cfg2)) (merge_maybes (detail cfg1) (detail cfg2)) (merge_maybes (host cfg1) (host cfg2)) @@ -107,7 +104,6 @@ from_rc = do let user_config_path = home ".mailbox-countrc" cfg <- DC.load [ DC.Optional global_config_path, DC.Optional user_config_path ] - cfg_both <- DC.lookup cfg "both" cfg_database <- DC.lookup cfg "database" cfg_detail <- DC.lookup cfg "detail" cfg_host <- DC.lookup cfg "host" @@ -116,7 +112,6 @@ from_rc = do cfg_username <- DC.lookup cfg "username" return $ OptionalConfiguration - cfg_both cfg_database cfg_detail cfg_host diff --git a/src/Report.hs b/src/Report.hs index e248879..db9c4ad 100644 --- a/src/Report.hs +++ b/src/Report.hs @@ -5,7 +5,7 @@ module Report ( report_tests ) where -import Data.List ( foldl' ) +import Data.List ( foldl', maximumBy ) import qualified Data.Map as Map ( Map, alter, empty, foldl, mapWithKey ) import Data.Maybe ( catMaybes ) import Data.String.Utils ( join ) @@ -26,19 +26,9 @@ type Count = Int -- | A wrapper around a (domain, count) pair to keep things type-safe. -data DomainCount = DomainCount Domain Count - -instance Show DomainCount where - -- | Display a DomainCount in the form \"domain: count\". - -- - -- Examples: - -- - -- >>> let dc = DomainCount "example.com" 100 - -- >>> show dc - -- "example.com: 100" - -- - show (DomainCount domain count) = domain ++ ": " ++ (show count) - +data DomainCount = + DomainCount Domain Count + deriving (Show) -- | A wrapper around a (domain, user) pair to keep things type-safe. data DomainUser = @@ -61,7 +51,7 @@ type DomainUserMap = Map.Map Domain [Username] -- >>> import Database.HDBC ( iToSql, toSql ) -- -- >>> list_to_domain_count [toSql "example.com", iToSql 100] --- Just example.com: 100 +-- Just (DomainCount "example.com" 100) -- -- >>> list_to_domain_count [toSql "example.com"] -- Nothing @@ -99,6 +89,24 @@ list_to_domain_user _ = Nothing +-- | Pad each count on the left with spaces so that they start on the +-- same column. The 'Int' argument is the length of the longest +-- domain name with which this one will be aligned, so when you take +-- into consideration the colon and subsequent space, the count will +-- be placed in column @longest_length + 3@. +-- +-- Examples: +-- +-- >>> let dc = DomainCount "example.com" 20 +-- >>> format_domain_count 20 dc +-- "example.com: 20" +-- +format_domain_count :: Int -> DomainCount -> String +format_domain_count longest_length (DomainCount d c) = + d ++ ": " ++ (replicate num_spaces ' ') ++ (show c) + where + num_spaces = longest_length - length d + summary_header :: String summary_header = "Summary (number of mailboxes per domain)\n" ++ "----------------------------------------" @@ -108,9 +116,26 @@ report_summary conn = do list_rows <- quickQuery conn query [] let maybe_domain_counts = map list_to_domain_count list_rows let domain_counts = catMaybes maybe_domain_counts - let report_lines = summary_header : (map show domain_counts) + let n = longest_dc_length domain_counts + let formatted_domain_counts = map (format_domain_count n) domain_counts + let report_lines = summary_header : formatted_domain_counts return $ join "\n" report_lines where + -- | Compare two 'DomainCount's by the length of their domain. The + -- one with the longest domain is \"bigger\". + compare_dcs_by_length :: DomainCount -> DomainCount -> Ordering + compare_dcs_by_length (DomainCount x _) (DomainCount y _) = + compare (length x) (length y) + + -- | Find the length of the 'DomainCount' in the list that has the + -- longest domain. We need to know this in order to pad the + -- counts on the left by the correct number of spaces. + longest_dc_length :: [DomainCount] -> Int + longest_dc_length dcs = + let (DomainCount d _) = longest in length d + where + longest = maximumBy compare_dcs_by_length dcs + query = "SELECT domain,COUNT(username) " ++ "FROM mailbox " ++ "GROUP BY domain "++ @@ -175,21 +200,16 @@ report_detail conn = do format_domain domain users = (join "\n" (domain_header : indented_users)) ++ "\n" where - domain_header = "\n" ++ domain ++ ":" + count = length users + domain_header = "\n" ++ domain ++ " (" ++ (show count) ++ ")" ++ ":" indented_users = map (" " ++) users -report_both :: IConnection a => a -> IO String -report_both conn = do - rs <- report_summary conn - rd <- report_detail conn - return (rs ++ rd) - -report :: IConnection a => a -> Bool -> Bool -> IO String -report conn do_both do_detail = - if do_both - then (report_both conn) - else if do_detail then (report_detail conn) else (report_summary conn) +report :: IConnection a => a -> Bool -> IO String +report conn do_detail = + if do_detail + then report_detail conn + else report_summary conn @@ -210,9 +230,10 @@ test_summary_report = desc = "Summary report looks like it should" expected = summary_header ++ "\n" ++ - "example.com: 3\n" ++ - "example.net: 2\n" ++ - "example.org: 1" + "example.com: 3\n" ++ + "example.invalid: 1\n" ++ + "example.net: 2\n" ++ + "example.org: 1" test_detail_report :: TestTree @@ -225,14 +246,17 @@ test_detail_report = desc = "Detail report looks like it should" expected = detail_header ++ "\n" ++ - "example.com:\n" ++ + "example.com (3):\n" ++ " user1\n" ++ " user3\n" ++ " user5\n" ++ "\n" ++ - "example.net:\n" ++ + "example.invalid (1):\n" ++ + " user7\n" ++ + "\n" ++ + "example.net (2):\n" ++ " user2\n" ++ " user4\n" ++ "\n" ++ - "example.org:\n" ++ + "example.org (1):\n" ++ " user6\n" diff --git a/test/fixtures/postfixadmin.sqlite3 b/test/fixtures/postfixadmin.sqlite3 index 5570886b85bde8f9d23963f89d23960a6c88607f..382dd9c2e5fb2adf5c4b2593232c9b925db734c1 100644 GIT binary patch delta 71 zcmZn=Xb_kn&B!rP#+i|0W5N<