Skip to content

Commit 77856e6

Browse files
author
HirotoShioi
committed
Add agentId to Config
1 parent 3e5075a commit 77856e6

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

Zendesk.hs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ import Types (Attachment (..), Comment (..),
5757
import Util (tshow)
5858

5959
data Config = Config
60-
{ cfgZendesk :: Text
60+
{ cfgAgentId :: Integer
61+
, cfgZendesk :: Text
6162
, cfgToken :: Text
6263
, cfgEmail :: Text
6364
, cfgAssignTo :: Integer
@@ -81,7 +82,7 @@ runApp (App a) = runReaderT a
8182

8283
-- | This scirpt will look through tickets that are assigned by cfgEmail
8384
defaultConfig :: Config
84-
defaultConfig = Config "https://iohk.zendesk.com" "" "daedalus-bug-reports@iohk.io" 0 [] 5
85+
defaultConfig = Config 0 "https://iohk.zendesk.com" "" "daedalus-bug-reports@iohk.io" 0 [] 5
8586

8687
-- | Path to knowledgebase
8788
knowledgebasePath :: FilePath
@@ -93,44 +94,42 @@ tokenPath = "token"
9394
assignToPath :: FilePath
9495
assignToPath = "assign_to"
9596

96-
analyzedIndicatorTag :: Text
97-
analyzedIndicatorTag = tshow AnalyzedByScript
98-
9997
main :: IO ()
10098
main = do
10199
putStrLn "Welcome to Zendesk classifier!"
102100
token <- B8.readFile tokenPath -- Zendesk token
103101
assignto <- B8.readFile assignToPath -- Select assignee
104102
knowledges <- setupKnowledgebaseEnv knowledgebasePath
105-
let cfg = defaultConfig { cfgToken = T.stripEnd $ T.decodeUtf8 token
103+
let cfg' = defaultConfig { cfgToken = T.stripEnd $ T.decodeUtf8 token
106104
, cfgAssignTo = read $ T.unpack $ T.decodeUtf8 assignto
107105
, cfgKnowledgebase = knowledges
108106
}
109-
agentId <- getAgentId cfg
107+
agentId <- runApp getAgentId cfg'
108+
let cfg = cfg' { cfgAgentId = agentId }
110109
args <- getArgs
111110
case args of
112111
-- Process all the tikects that are requested by agent
113112
[ "extractEmailAddress" ] -> do
114113
T.putStrLn $ "Classifier is going to extract emails requested by: " <> cfgEmail cfg
115-
tickets <- runApp (listTickets agentId Requested) cfg
114+
tickets <- runApp (listTickets Requested) cfg
116115
putStrLn $ "There are " <> show (length tickets) <> " tickets requested by this user."
117116
let ticketIds = foldr (\TicketInfo{..} acc -> ticketId : acc) [] tickets
118-
mapM_ (\tid -> runApp (extractEmailAddress agentId tid) cfg) ticketIds
117+
mapM_ (\tid -> runApp (extractEmailAddress tid) cfg) ticketIds
119118
-- Process given ticket
120-
[ "processTicket", idNumber ] -> do
119+
[ "processTicket", ticketId ] -> do
121120
putStrLn "Processing single ticket"
122-
runApp (processTicketAndId agentId $ read idNumber) cfg
121+
runApp (processTicketAndId $ read ticketId) cfg
123122
putStrLn "Process finished, please see the following url"
124-
putStrLn $ "https://iohk.zendesk.com/agent/tickets/" <> idNumber
123+
putStrLn $ "https://iohk.zendesk.com/agent/tickets/" <> ticketId
125124
-- Process all the tickets (WARNING: This is really long process)
126125
[ "processTickets" ] -> do
127126
T.putStrLn $ "Classifier is going to process tickets assign to: " <> cfgEmail cfg
128127
printWarning
129-
tickets <- runApp (listTickets agentId Assigned) cfg
128+
tickets <- runApp (listTickets Assigned) cfg
130129
let filteredTicketIds = filterAnalyzedTickets tickets
131130
putStrLn $ "There are " <> show (length filteredTicketIds) <> " unanalyzed tickets."
132131
putStrLn "Processing tickets, this may take hours to finish."
133-
mapM_ (\tid -> runApp (processTicketAndId agentId tid) cfg) filteredTicketIds
132+
mapM_ (\tid -> runApp (processTicketAndId tid) cfg) filteredTicketIds
134133
putStrLn "All the tickets has been processed."
135134
-- Return raw request
136135
[ "raw_request", url ] -> do
@@ -142,7 +141,7 @@ main = do
142141
[ "showStats" ] -> do
143142
T.putStrLn $ "Classifier is going to gather ticket information assigned to: " <> cfgEmail cfg
144143
printWarning
145-
tickets <- runApp (listTickets agentId Assigned) cfg
144+
tickets <- runApp (listTickets Assigned) cfg
146145
printTicketCountMessage tickets (cfgEmail cfg)
147146
_ -> do
148147
let cmdItem = [ "extractEmailAddress : Collect emails requested by single user"
@@ -192,8 +191,8 @@ setupKnowledgebaseEnv path = do
192191
Right ks -> return ks
193192

194193
-- | Collect email
195-
extractEmailAddress :: Integer -> TicketId -> App ()
196-
extractEmailAddress agentId ticketId = do
194+
extractEmailAddress :: TicketId -> App ()
195+
extractEmailAddress ticketId = do
197196
comments <- getTicketComments ticketId
198197
let commentWithEmail = commentBody $ head comments
199198
emailAddress = head $ T.lines commentWithEmail
@@ -202,8 +201,8 @@ extractEmailAddress agentId ticketId = do
202201
liftIO $ T.putStrLn emailAddress
203202

204203
-- | Process specifig ticket id (can be used for testing) only inspects the one's with logs
205-
processTicketAndId :: Integer -> TicketId -> App ()
206-
processTicketAndId agentId ticketId = do
204+
processTicketAndId :: TicketId -> App ()
205+
processTicketAndId ticketId = do
207206
comments <- getTicketComments ticketId
208207
let
209208
-- Filter tickets without logs
@@ -214,16 +213,19 @@ processTicketAndId agentId ticketId = do
214213
attachments :: [ Attachment ]
215214
attachments = concatMap commentAttachments commentsWithAttachments
216215
justLogs = filter (\x -> "application/zip" == attachmentContentType x) attachments
217-
mapM_ (inspectAttachmentAndPostComment agentId ticketId) justLogs
216+
mapM_ (inspectAttachmentAndPostComment ticketId) justLogs
218217
pure ()
219218

220219
-- | Inspect attachment then post comment to the ticket
221-
inspectAttachmentAndPostComment :: Integer -> TicketId -> Attachment -> App ()
222-
inspectAttachmentAndPostComment agentId ticketId att = do
220+
inspectAttachmentAndPostComment :: TicketId -> Attachment -> App ()
221+
inspectAttachmentAndPostComment ticketId att = do
223222
Config{..} <- ask
224223
liftIO $ putStrLn $ "Analyzing ticket id: " <> show ticketId
225-
(comment, tags, isPublicComment) <- liftIO $ inspectAttachment cfgNumOfLogsToAnalyze cfgKnowledgebase att
226-
postTicketComment agentId ticketId comment tags isPublicComment
224+
(comment, tags, isPublicComment) <- liftIO $ inspectAttachment
225+
cfgNumOfLogsToAnalyze
226+
cfgKnowledgebase
227+
att
228+
postTicketComment ticketId comment tags isPublicComment
227229

228230
-- | Given number of file of inspect, knowledgebase and attachment,
229231
-- analyze the logs and return the results.
@@ -259,12 +261,15 @@ filterAnalyzedTickets = foldr (\TicketInfo{..} acc ->
259261
then acc
260262
else ticketId : acc
261263
) []
264+
where analyzedIndicatorTag :: Text
265+
analyzedIndicatorTag = tshow AnalyzedByScript
262266

263267
-- | Return list of ticketIds that has been requested by config user (not used)
264-
listTickets :: Integer -> RequestType -> App [ TicketInfo ]
265-
listTickets agentId request = do
268+
listTickets :: RequestType -> App [ TicketInfo ]
269+
listTickets request = do
266270
cfg <- ask
267-
let url = case request of
271+
let agentId = cfgAgentId cfg
272+
url = case request of
268273
Requested -> "/users/" <> tshow agentId <> "/tickets/requested.json"
269274
Assigned -> "/users/" <> tshow agentId <> "/tickets/assigned.json"
270275
req = apiRequest cfg url
@@ -282,14 +287,14 @@ listTickets agentId request = do
282287
Nothing -> pure page0
283288

284289
-- | Send API request to post comment
285-
postTicketComment :: Integer -> TicketId -> Text -> [ Text ] -> Bool -> App ()
286-
postTicketComment agentId tid body tags public = do
290+
postTicketComment :: TicketId -> Text -> [ Text ] -> Bool -> App ()
291+
postTicketComment tid body tags public = do
287292
cfg <- ask
288293
let
289294
req1 = apiRequest cfg ("tickets/" <> tshow tid <> ".json")
290295
req2 = addJsonBody
291296
(Ticket
292-
(Comment ("**Log classifier**\n\n" <> body) [] False agentId)
297+
(Comment ("**Log classifier**\n\n" <> body) [] False (cfgAgentId cfg))
293298
(cfgAssignTo cfg)
294299
(tshow AnalyzedByScript:tags)
295300
)
@@ -298,10 +303,11 @@ postTicketComment agentId tid body tags public = do
298303
pure ()
299304

300305
-- | Get agent id that has been set on Config
301-
getAgentId :: Config -> IO Integer
302-
getAgentId cfg = do
306+
getAgentId :: App Integer
307+
getAgentId = do
308+
cfg <- ask
303309
let req = apiRequest cfg "users/me.json"
304-
apiCall parseAgentId req
310+
liftIO $ apiCall parseAgentId req
305311

306312
-- | Given attachmentUrl, return attachment in bytestring
307313
getAttachment :: Attachment -> IO BL.ByteString

0 commit comments

Comments
 (0)