Skip to content

Commit 2c19ee9

Browse files
author
HirotoShioi
committed
Filter analyzed tickets, show nice messages
1 parent e9f9c93 commit 2c19ee9

File tree

2 files changed

+68
-27
lines changed

2 files changed

+68
-27
lines changed

Types.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ data Ticket = Ticket {
3535

3636
-- | List of zendesk ticket
3737
data TicketList = TicketList {
38-
ticketListTickets :: [ TicketId ]
38+
ticketListTickets :: [ TicketInfo ]
3939
, nextPage :: Maybe Text
4040
} deriving (Show, Eq)
4141

42-
newtype TicketId = TicketId Int deriving (Eq)
42+
type TicketId = Int
4343

44-
instance Show TicketId where
45-
show (TicketId tid) = show tid
44+
data TicketInfo = TicketInfo
45+
{ ticketId :: Int
46+
, ticketTags :: [Text]
47+
} deriving (Eq)
48+
49+
instance Show TicketInfo where
50+
show (TicketInfo tid _) = show tid
4651

4752
-- | Ticket status
4853
data TicketStatus =
@@ -80,8 +85,8 @@ instance ToJSON Attachment where
8085
toJSON (Attachment url contenttype size) =
8186
object [ "content_url" .= url, "content_type" .= contenttype, "size" .= size]
8287

83-
instance FromJSON TicketId where
84-
parseJSON = withObject "ticket" $ \o -> TicketId <$> (o .: "id")
88+
instance FromJSON TicketInfo where
89+
parseJSON = withObject "ticket" $ \o -> TicketInfo <$> (o .: "id") <*> (o .: "tags")
8590

8691
instance FromJSON TicketList where
8792
parseJSON = withObject "ticketList" $ \o -> TicketList <$> o .: "tickets" <*> o .: "next_page"

Zendesk.hs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ import LogAnalysis.KnowledgeCSVParser (parseKnowLedgeBase)
4343
import LogAnalysis.Types (ErrorCode (..), Knowledge,
4444
setupAnalysis, toTag, toComment)
4545
import 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"
7373
assignToPath :: FilePath
7474
assignToPath = "assign_to"
7575

76+
analyzedIndicatorTag :: Text
77+
analyzedIndicatorTag = tshow AnalyzedByScript
78+
7679
main :: IO ()
7780
main = 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
119147
setupKnowledgebaseEnv :: 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]
177213
listRequestedTicketIds 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 ]
184220
listAssignedTickets 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
201237
postTicketComment :: 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
227263
getTicketComments :: 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

Comments
 (0)