X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FReport.hs;h=f1ec9ddd3ae6426121ee685957ad7b5ca899b8e0;hb=39e047ccd8422207e01247c63f514c40e7eac31e;hp=76373a7e49313bacdb0fc4ac36c25a1464f8980b;hpb=7dccbdf398b85df91737ab0faf4509315ee4e64d;p=mailbox-count.git diff --git a/src/Report.hs b/src/Report.hs index 76373a7..f1ec9dd 100644 --- a/src/Report.hs +++ b/src/Report.hs @@ -15,9 +15,11 @@ import Database.HDBC ( safeFromSql, quickQuery ) import Database.HDBC.Sqlite3 ( connectSqlite3 ) +import System.Console.CmdArgs.Default ( Default(..) ) import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) +import Configuration ( Configuration(..) ) -- Type synonyms to make the signatures below a little more clear. type Domain = String @@ -107,12 +109,18 @@ format_domain_count longest_length (DomainCount d c) = where num_spaces = longest_length - length d + +-- | The header that gets output before the summary report. +-- summary_header :: String summary_header = "Summary (number of mailboxes per domain)\n" ++ "----------------------------------------" -report_summary :: IConnection a => a -> IO String -report_summary conn = do + +-- | Given a connection, produces the summary report as a 'String'. +-- +report_summary :: IConnection a => a -> String -> IO String +report_summary conn query = do list_rows <- quickQuery conn query [] let maybe_domain_counts = map list_to_domain_count list_rows let domain_counts = catMaybes maybe_domain_counts @@ -136,10 +144,6 @@ report_summary conn = do where longest = maximumBy compare_dcs_by_length dcs - query = "SELECT domain,COUNT(username) " ++ - "FROM mailbox " ++ - "GROUP BY domain "++ - "ORDER BY domain;" -- | Construct a Domain -> [Username] (a DomainUserMap) map from a @@ -176,12 +180,17 @@ construct_domain_user_map = Map.alter (append_func user) domain du_map +-- | The header that gets output before the detail report. +-- detail_header :: String detail_header = "Detail (list of all mailboxes by domain)\n" ++ "----------------------------------------" -report_detail :: IConnection a => a -> IO String -report_detail conn = do + +-- | Given a connection, produces the detail report as a 'String'. +-- +report_detail :: IConnection a => a -> String -> IO String +report_detail conn query = do list_rows <- quickQuery conn query [] let maybe_domain_users = map list_to_domain_user list_rows let domain_users = catMaybes maybe_domain_users @@ -192,10 +201,6 @@ report_detail conn = do let report_body = Map.foldl (++) "" domain_report_map return $ detail_header ++ report_body where - query = "SELECT domain,username " ++ - "FROM mailbox " ++ - "ORDER BY domain;" - format_domain :: Domain -> [Username] -> String format_domain domain users = (join "\n" (domain_header : indented_users)) ++ "\n" @@ -205,11 +210,14 @@ report_detail conn = do indented_users = map (" " ++) users -report :: IConnection a => a -> Bool -> IO String -report conn do_detail = - if do_detail - then report_detail conn - else report_summary conn +-- | Given a connection and a 'Configuration', produces the report as +-- a 'String'. +-- +report :: IConnection a => Configuration -> a -> IO String +report cfg conn = + if (detail cfg) + then report_detail conn (detail_query cfg) + else report_summary conn (summary_query cfg) @@ -224,7 +232,8 @@ test_summary_report :: TestTree test_summary_report = testCase desc $ do conn <- connectSqlite3 "test/fixtures/postfixadmin.sqlite3" - actual <- report_summary conn + let cfg = def :: Configuration + actual <- report_summary conn (summary_query cfg) actual @?= expected where desc = "Summary report looks like it should" @@ -240,7 +249,8 @@ test_detail_report :: TestTree test_detail_report = testCase desc $ do conn <- connectSqlite3 "test/fixtures/postfixadmin.sqlite3" - actual <- report_detail conn + let cfg = def :: Configuration + actual <- report_detail conn (detail_query cfg) actual @?= expected where desc = "Detail report looks like it should"