Skip to content

Commit a2c1e22

Browse files
author
HirotoShioi
committed
Refactor code style
1 parent 77856e6 commit a2c1e22

File tree

5 files changed

+207
-208
lines changed

5 files changed

+207
-208
lines changed

LogAnalysis/Classifier.hs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
module LogAnalysis.Classifier
66
( extractIssuesFromLogs
7-
, extractLogsFromZip
87
, extractErrorCodes
98
, prettyFormatAnalysis
109
) where
1110

12-
import qualified Codec.Archive.Zip as Zip
13-
import qualified Data.ByteString.Lazy as LBS
11+
import qualified Data.ByteString.Lazy as LBS
1412
import Data.List (foldl')
1513
import Data.Map.Strict (Map)
1614
import qualified Data.Map.Strict as Map
@@ -58,23 +56,6 @@ filterAnalysis as = do
5856
then Left "Cannot find any known issues"
5957
else return $ Map.map (take numberOfErrorText) filteredAnalysis
6058

61-
readZip :: LBS.ByteString -> Either String (Map FilePath LBS.ByteString)
62-
readZip rawzip = case Zip.toArchiveOrFail rawzip of
63-
Left err -> Left err
64-
Right archive -> Right $ finishProcessing archive
65-
where
66-
finishProcessing :: Zip.Archive -> Map FilePath LBS.ByteString
67-
finishProcessing = Map.fromList . map handleEntry . Zip.zEntries
68-
handleEntry :: Zip.Entry -> (FilePath, LBS.ByteString)
69-
handleEntry entry = (Zip.eRelativePath entry, Zip.fromEntry entry)
70-
71-
-- | Extract log file from given zip file
72-
extractLogsFromZip :: Int -> LBS.ByteString -> Either String [LBS.ByteString]
73-
extractLogsFromZip numberOfFiles file = do
74-
zipMap <- readZip file -- Read File
75-
let extractedLogs = Map.elems $ Map.take numberOfFiles zipMap -- Extract selected logs
76-
return extractedLogs
77-
7859
extractErrorCodes :: Analysis -> [ Text ]
7960
extractErrorCodes as = map (\(Knowledge{..}, _) -> toTag kErrorCode) $ Map.toList as
8061

LogAnalysis/Types.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ toComment SentLogCorrupted = "Log file is corrupted"
6464
toComment _ = "Error"
6565

6666
-- | Map used to collect error lines
67-
type Analysis = Map Knowledge [ LT.Text ]
67+
type Analysis = Map Knowledge [LT.Text]
6868

6969
-- | Create initial analysis environment
70-
setupAnalysis :: [ Knowledge ] -> Analysis
70+
setupAnalysis :: [Knowledge] -> Analysis
7171
setupAnalysis kbase = Map.fromList $ map (\kn -> (kn, [])) kbase
7272

7373
instance Eq Knowledge where

Types.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import Data.Text (Text)
2020

2121
-- | Comments
2222
data Comment = Comment
23-
{ commentBody :: Text -- ^ Body of comment
24-
, commentAttachments :: [Attachment] -- ^ Attachment
25-
, commentPublic :: Bool -- ^ Flag of whether comment should be public
26-
, commentAuthor :: Integer -- ^ Auther of comment
23+
{ commentBody :: !Text -- ^ Body of comment
24+
, commentAttachments :: ![Attachment] -- ^ Attachment
25+
, commentPublic :: !Bool -- ^ Flag of whether comment should be public
26+
, commentAuthor :: !Integer -- ^ Auther of comment
2727
} deriving (Show, Eq)
2828

2929
-- | Outer comment ??
@@ -33,29 +33,29 @@ newtype CommentOuter = CommentOuter {
3333

3434
-- | Attachment of the ticket
3535
data Attachment = Attachment
36-
{ attachmentURL :: Text -- ^ URL of the attachment
37-
, attachmentContentType :: Text -- ^ ContentType of the attachment
38-
, attachmentSize :: Int -- ^ Attachment size
36+
{ attachmentURL :: !Text -- ^ URL of the attachment
37+
, attachmentContentType :: !Text -- ^ ContentType of the attachment
38+
, attachmentSize :: !Int -- ^ Attachment size
3939
} deriving (Show, Eq)
4040

4141
-- | Zendexk ticket
4242
data Ticket = Ticket
43-
{ ticketComment :: Comment -- ^ Ticket comment
44-
, ticketAssignee :: Integer -- ^ Assignee of the ticket
45-
, ticketTag :: [ Text ] -- ^ Tags attached to ticket
43+
{ ticketComment :: !Comment -- ^ Ticket comment
44+
, ticketAssignee :: !Integer -- ^ Assignee of the ticket
45+
, ticketTag :: ![Text] -- ^ Tags attached to ticket
4646
} deriving (Show, Eq)
4747

4848
-- | List of zendesk ticket
4949
data TicketList = TicketList
50-
{ ticketListTickets :: [ TicketInfo ] -- ^ Information of tickets
50+
{ ticketListTickets :: ![TicketInfo] -- ^ Information of tickets
5151
, nextPage :: Maybe Text -- ^ Next page
5252
} deriving (Show, Eq)
5353

5454
type TicketId = Int
5555

5656
data TicketInfo = TicketInfo
57-
{ ticketId :: Int -- ^ Id of an ticket
58-
, ticketTags :: [Text] -- ^ Tags associated with ticket
57+
{ ticketId :: !Int -- ^ Id of an ticket
58+
, ticketTags :: ![Text] -- ^ Tags associated with ticket
5959
} deriving (Eq)
6060

6161
instance Show TicketInfo where

Util.hs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
module Util
2-
( tshow
2+
( extractLogsFromZip
3+
, tshow
4+
, readZip
35
) where
46

5-
import Data.Text (Text)
6-
import qualified Data.Text as T
7+
import qualified Codec.Archive.Zip as Zip
8+
import qualified Data.ByteString.Lazy as LBS
9+
import Data.Map.Strict (Map)
10+
import qualified Data.Map.Strict as Map
11+
import Data.Text (Text)
12+
import qualified Data.Text as T
713

814
tshow :: Show a => a -> Text
9-
tshow = T.pack . show
15+
tshow = T.pack . show
16+
17+
-- | Extract log file from given zip file
18+
extractLogsFromZip :: Int -> LBS.ByteString -> Either String [LBS.ByteString]
19+
extractLogsFromZip numberOfFiles file = do
20+
zipMap <- readZip file -- Read File
21+
let extractedLogs = Map.elems $ Map.take numberOfFiles zipMap -- Extract selected logs
22+
return extractedLogs
23+
24+
readZip :: LBS.ByteString -> Either String (Map FilePath LBS.ByteString)
25+
readZip rawzip = case Zip.toArchiveOrFail rawzip of
26+
Left err -> Left err
27+
Right archive -> Right $ finishProcessing archive
28+
where
29+
finishProcessing :: Zip.Archive -> Map FilePath LBS.ByteString
30+
finishProcessing = Map.fromList . map handleEntry . Zip.zEntries
31+
handleEntry :: Zip.Entry -> (FilePath, LBS.ByteString)
32+
handleEntry entry = (Zip.eRelativePath entry, Zip.fromEntry entry)

0 commit comments

Comments
 (0)