@@ -43,8 +43,8 @@ import LogAnalysis.KnowledgeCSVParser (parseKnowLedgeBase)
4343import LogAnalysis.Types (ErrorCode (.. ), Knowledge ,
4444 setupAnalysis , toTag , toComment )
4545import Types (Attachment (.. ), Comment (.. ),
46- CommentOuter (.. ), Ticket (.. ),
47- TicketId ( .. ) , TicketList (.. ),
46+ CommentOuter (.. ), Ticket (.. ), TicketInfo ( .. ),
47+ TicketId , TicketList (.. ),
4848 TicketStatus (.. ),
4949 parseAgentId , parseComments ,
5050 parseTickets )
@@ -73,34 +73,46 @@ tokenPath = "token"
7373assignToPath :: FilePath
7474assignToPath = " assign_to"
7575
76+ analyzedIndicatorTag :: Text
77+ analyzedIndicatorTag = tshow AnalyzedByScript
78+
7679main :: IO ()
7780main = do
81+ putStrLn " Welcome to Zendesk classifier!"
82+ putStrLn " Setting config variables"
7883 token <- B8. readFile tokenPath -- Zendesk token
7984 assignto <- B8. readFile assignToPath -- Select assignee
80- putStrLn " Reading knowledge base"
8185 knowledges <- setupKnowledgebaseEnv knowledgebasePath
82- putStrLn " Knowledgebase setup complete"
8386 let cfg = defaultConfig { cfgToken = T. stripEnd $ T. decodeUtf8 token
8487 , cfgAssignTo = read $ T. unpack $ T. decodeUtf8 assignto
8588 , cfgKnowledgebase = knowledges
8689 }
8790 agentId <- getAgentId cfg
8891 args <- getArgs
8992 case args of
93+ -- Process given ticket
9094 [ " processTicket" , idNumber ] -> do
9195 putStrLn " Processing single ticket"
92- processTicketAndId cfg agentId $ TicketId $ read idNumber
93- [ " listAssigned" ] -> do
94- T. putStrLn $ " The script is going to look through tickets assign to: " <> cfgEmail cfg
96+ processTicketAndId cfg agentId $ read idNumber
97+ putStrLn " Process finished, please see the following url"
98+ putStrLn $ " https://iohk.zendesk.com/agent/tickets/" <> idNumber
99+ -- Count assigned tickets
100+ [ " countAssigned" ] -> do
101+ T. putStrLn $ " Classifier is going to count tickets assign to: " <> cfgEmail cfg
102+ printWarning
95103 tickets <- listAssignedTickets cfg agentId
96- T. putStrLn $ " Done: there are currently " <> tshow ( length tickets )
97- <> " tickets in the system assigned to " <> cfgEmail cfg
104+ printTicketCountMessage tickets (cfgEmail cfg )
105+ -- Process all the tickets (WARNING: This is really long process)
98106 [ " processTickets" ] -> do
99- T. putStrLn $ " The script is going to process tickets assign to: " <> cfgEmail cfg
100- ticketIds <- listAssignedTickets cfg agentId
101- print $ " found: " <> show (length ticketIds) <> " tickets"
102- print ticketIds
103- mapM_ (processTicketAndId cfg agentId) ticketIds
107+ T. putStrLn $ " Classifier is going to process tickets assign to: " <> cfgEmail cfg
108+ printWarning
109+ tickets <- listAssignedTickets cfg agentId
110+ printTicketCountMessage tickets (cfgEmail cfg)
111+ let filteredTicketIds = filterAnalyzedTickets tickets
112+ putStrLn " Processing tickets, this may take hours to finish."
113+ mapM_ (processTicketAndId cfg agentId) filteredTicketIds
114+ putStrLn " All the tickets has been processed."
115+ -- Return raw request
104116 [ " raw_request" , url ] -> do
105117 let
106118 req = apiRequest cfg (T. pack url)
@@ -109,11 +121,27 @@ main = do
109121 _ -> do
110122 let cmdItem = [ " processTicket <id> : Process single ticket of id"
111123 , " listAssigned : Print list of ticket Ids that agent has been assigned"
112- , " processTickets : Process all the tickets i.e add comments, tags then assign to someone"
124+ , " processTickets : Process all the tickets i.e add comments, tags. WARNING:" <>
125+ " This is really long process, please use with care"
113126 , " raw_request <url> : Request raw request to the given url" ]
114127 putStrLn " Invalid argument, please add following argument to run the command:"
115128 mapM_ putStrLn cmdItem
116- putStrLn " Process finished!"
129+
130+ -- | Warning
131+ printWarning :: IO ()
132+ printWarning = putStrLn " Note that this process may take a while. Please do not kill the process"
133+
134+ -- | Print how many tickets are assinged, analyzed, and unanalyzed
135+ printTicketCountMessage :: [TicketInfo ] -> Text -> IO ()
136+ printTicketCountMessage tickets email = do
137+ let ticketCount = length tickets
138+ putStrLn " Done!"
139+ T. putStrLn $ " There are currently " <> tshow ticketCount
140+ <> " tickets in the system assigned to " <> email
141+ putStrLn " Filtering analyzed tickets.."
142+ let filteredTicketCount = length $ filterAnalyzedTickets tickets
143+ putStrLn $ show (ticketCount - filteredTicketCount) <> " tickets has been analyzed."
144+ putStrLn $ show filteredTicketCount <> " tickets are not analyzed yet."
117145
118146-- | Read CSV file and setup knowledge base
119147setupKnowledgebaseEnv :: FilePath -> IO [Knowledge ]
@@ -172,19 +200,27 @@ inspectAttachment num ks att = do
172200 putStrLn result
173201 return (LT. toStrict (LT. pack result), [tshow NoKnownIssue ], False )
174202
203+ -- | Filter analyzed tickets
204+ filterAnalyzedTickets :: [TicketInfo ] -> [TicketId ]
205+ filterAnalyzedTickets = foldr (\ TicketInfo {.. } acc ->
206+ if analyzedIndicatorTag `elem` ticketTags
207+ then acc
208+ else ticketId : acc
209+ ) []
210+
175211-- | Return list of ticketIds that has been requested by config user (don't need..?)
176- listRequestedTicketIds :: Config -> Integer -> IO [TicketId ]
212+ listRequestedTicketIds :: Config -> Integer -> IO [TicketInfo ]
177213listRequestedTicketIds cfg agentId = do
178214 let req = apiRequest cfg (" /users/" <> tshow agentId <> " /tickets/requested.json" )
179215 (TicketList page0 nextPage) <- apiCall parseTickets req
180216 pure page0
181217
182218-- | Return list of ticketIds that has been assigned to config user
183- listAssignedTickets :: Config -> Integer -> IO [ TicketId ]
219+ listAssignedTickets :: Config -> Integer -> IO [ TicketInfo ]
184220listAssignedTickets cfg agentId = do
185221 let
186222 req = apiRequest cfg (" /users/" <> tshow agentId <> " /tickets/assigned.json" )
187- go :: [ TicketId ] -> Text -> IO [ TicketId ]
223+ go :: [ TicketInfo ] -> Text -> IO [ TicketInfo ]
188224 go list nextPage' = do
189225 let req' = apiRequestAbsolute cfg nextPage'
190226 (TicketList pagen nextPagen) <- apiCall parseTickets req'
@@ -199,7 +235,7 @@ listAssignedTickets cfg agentId = do
199235
200236-- | Send API request to post comment
201237postTicketComment :: Config -> Integer -> TicketId -> Text -> [Text ] -> Bool -> IO ()
202- postTicketComment cfg agentId ( TicketId tid) body tags public = do
238+ postTicketComment cfg agentId tid body tags public = do
203239 let
204240 req1 = apiRequest cfg (" tickets/" <> tshow tid <> " .json" )
205241 req2 = addJsonBody
@@ -225,7 +261,7 @@ getAttachment Attachment{..} = getResponseBody <$> httpLBS req
225261
226262-- | Get ticket's comments
227263getTicketComments :: Config -> TicketId -> IO [ Comment ]
228- getTicketComments cfg ( TicketId tid) = do
264+ getTicketComments cfg tid = do
229265 let req = apiRequest cfg (" tickets/" <> tshow tid <> " /comments.json" )
230266 apiCall parseComments req
231267
0 commit comments