Skip to content

Commit 2d6527d

Browse files
authored
[TSD-41] Run the debugger on everything pending, open, new, on-hold and unassigned for all agents. (#19)
* Add test cases * Process hold, new, open, pending tickets * Use Zendesk search API to fetch ticket information * Organize type file * Fetch Zendesk agents * Apply fixes to pass the test * Process all the tickets assgined to the agents * Fix test cases * Fix group api url * Fix ZendeskLayer * Generalize iteratePages function * Add comments on postTicketCommnet * [TSD-41] Fix type generalization. * Apply fixes
1 parent 278df29 commit 2d6527d

File tree

6 files changed

+346
-212
lines changed

6 files changed

+346
-212
lines changed

src/CLI.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import Paths_log_classifier (version)
1414
-- use direct function calls.
1515
data CLI
1616
= CollectEmails -- ^ Collect email addresses
17+
| FetchAgents -- ^ List agents
18+
| FetchTickets -- ^ Fetch all the tickets in Zendesk
1719
| ProcessTicket Int -- ^ Process ticket of an given ticket id
1820
| ProcessTickets -- ^ Process all the tickets in Zendesk
19-
| FetchTickets -- ^ Fetch all the tickets in Zendesk
2021
| ShowStatistics -- ^ Show statistics
2122
deriving (Show)
2223

@@ -31,10 +32,12 @@ cli :: Parser CLI
3132
cli = hsubparser $ mconcat
3233
[ command "collect-emails" (info (pure CollectEmails)
3334
(progDesc "Collect emails requested by single user"))
34-
, command "process-tickets" (info (pure ProcessTickets)
35-
(progDesc "Process all the tickets i.e add comments, tags."))
35+
, command "fetch-agents" (info (pure FetchAgents)
36+
(progDesc "Fetch Zendesk agents"))
3637
, command "fetch-tickets" (info (pure FetchTickets)
3738
(progDesc "Fetch all the tickets that need to be analyzes."))
39+
, command "process-tickets" (info (pure ProcessTickets)
40+
(progDesc "Process all the tickets i.e add comments, tags."))
3841
, command "process-ticket" (info cmdProcessTicket
3942
(progDesc "Process Zendesk ticket of an given ticket id"))
4043
, command "show-stats" (info (pure ShowStatistics)

src/DataSource/DB.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ cachedZendeskLayer = ZendeskLayer
3636
{ zlGetTicketInfo = liftIO . getTicketInfoByTicketId
3737
, zlListAssignedTickets = liftIO . getAllAssignedTicketsByUser
3838
, zlListRequestedTickets = liftIO . getAllRequestedTicketsByUser
39+
, zlListAdminAgents = error "We won't use this for now!"
3940
, zlGetAttachment = liftIO . DataSource.DB.getAttachmentContent
4041
, zlGetTicketComments = liftIO . getTicketComments
4142
, zlPostTicketComment = error "We won't use this for now!"

src/DataSource/Http.hs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
{-# LANGUAGE OverloadedStrings #-}
2-
{-# LANGUAGE RecordWildCards #-}
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
34

45
module DataSource.Http
56
( basicZendeskLayer
@@ -19,11 +20,12 @@ import Network.HTTP.Simple (Request, addRequestHeader, getResponseBody
1920
setRequestMethod, setRequestPath)
2021

2122
import DataSource.Types (Attachment (..), AttachmentContent (..), Comment (..),
22-
CommentBody (..), CommentId (..), Config (..), IOLayer (..),
23-
Ticket (..), TicketId (..), TicketInfo (..), TicketList (..),
24-
TicketTag (..), User, UserId (..), ZendeskAPIUrl (..),
25-
ZendeskLayer (..), ZendeskResponse (..), parseComments,
26-
parseTicket, parseTickets, renderTicketStatus, showURL)
23+
CommentBody (..), CommentId (..), Config (..),
24+
FromPageResultList (..), IOLayer (..), PageResultList (..),
25+
Ticket (..), TicketId (..), TicketInfo (..), TicketTag (..),
26+
User, UserId (..), ZendeskAPIUrl (..), ZendeskLayer (..),
27+
ZendeskResponse (..), parseComments, parseTicket,
28+
renderTicketStatus, showURL)
2729

2830

2931
-- | The default configuration.
@@ -48,6 +50,7 @@ basicZendeskLayer = ZendeskLayer
4850
{ zlGetTicketInfo = getTicketInfo
4951
, zlListRequestedTickets = listRequestedTickets
5052
, zlListAssignedTickets = listAssignedTickets
53+
, zlListAdminAgents = listAdminAgents
5154
, zlPostTicketComment = postTicketComment
5255
, zlGetAttachment = getAttachment
5356
, zlGetTicketComments = getTicketComments
@@ -61,17 +64,17 @@ basicIOLayer = IOLayer
6164
}
6265

6366
-- | The non-implemented Zendesk layer.
64-
emptyZendeskLayer :: forall m. ZendeskLayer m
67+
emptyZendeskLayer :: forall m. (Monad m) => ZendeskLayer m
6568
emptyZendeskLayer = ZendeskLayer
6669
{ zlGetTicketInfo = \_ -> error "Not implemented zlGetTicketInfo!"
6770
, zlListRequestedTickets = \_ -> error "Not implemented zlListRequestedTickets!"
6871
, zlListAssignedTickets = \_ -> error "Not implemented zlListAssignedTickets!"
72+
, zlListAdminAgents = pure []
6973
, zlPostTicketComment = \_ -> error "Not implemented zlPostTicketComment!"
7074
, zlGetAttachment = \_ -> error "Not implemented zlGetAttachment!"
7175
, zlGetTicketComments = \_ -> error "Not implemented zlGetTicketComments!"
7276
}
7377

74-
7578
-- | Get single ticket info.
7679
getTicketInfo
7780
:: (MonadIO m, MonadReader Config m)
@@ -84,7 +87,6 @@ getTicketInfo ticketId = do
8487
let req = apiRequest cfg url
8588
liftIO $ Just <$> apiCall parseTicket req
8689

87-
8890
-- | Return list of ticketIds that has been requested by config user.
8991
listRequestedTickets
9092
:: forall m. (MonadIO m, MonadReader Config m)
@@ -96,7 +98,7 @@ listRequestedTickets userId = do
9698
let url = showURL $ UserRequestedTicketsURL userId
9799
let req = apiRequest cfg url
98100

99-
iterateTicketPages req
101+
iteratePages req
100102

101103
-- | Return list of ticketIds that has been assigned by config user.
102104
listAssignedTickets
@@ -109,25 +111,33 @@ listAssignedTickets userId = do
109111
let url = showURL $ UserAssignedTicketsURL userId
110112
let req = apiRequest cfg url
111113

112-
iterateTicketPages req
114+
iteratePages req
113115

114-
-- | Iterate all the ticket pages and combine into a result.
115-
iterateTicketPages
116-
:: forall m. (MonadIO m, MonadReader Config m)
117-
=> Request -> m [TicketInfo]
118-
iterateTicketPages req = do
116+
listAdminAgents :: forall m. (MonadIO m, MonadReader Config m) => m [User]
117+
listAdminAgents = do
118+
cfg <- ask
119+
let url = showURL AgentGroupURL
120+
let req = apiRequest cfg url
119121

122+
iteratePages req
123+
124+
-- | Iterate all the ticket pages and combine into a result.
125+
iteratePages
126+
:: forall m a. (MonadIO m, MonadReader Config m, FromPageResultList a)
127+
=> Request
128+
-> m [a]
129+
iteratePages req = do
120130
cfg <- ask
121131

122-
let go :: [TicketInfo] -> Text -> IO [TicketInfo]
132+
let go :: [a] -> Text -> IO [a]
123133
go list' nextPage' = do
124-
let req' = apiRequestAbsolute cfg nextPage'
125-
(TicketList pagen nextPagen) <- apiCall parseTickets req'
134+
let req' = apiRequestAbsolute cfg nextPage'
135+
(PageResultList pagen nextPagen) <- apiCall parseJSON req'
126136
case nextPagen of
127137
Just nextUrl -> go (list' <> pagen) nextUrl
128138
Nothing -> pure (list' <> pagen)
129139

130-
(TicketList page0 nextPage) <- liftIO $ apiCall parseTickets req
140+
(PageResultList page0 nextPage) <- liftIO $ apiCall parseJSON req
131141
case nextPage of
132142
Just nextUrl -> liftIO $ go page0 nextUrl
133143
Nothing -> pure page0
@@ -144,8 +154,12 @@ postTicketComment ZendeskResponse{..} = do
144154
let req1 = apiRequest cfg url
145155
let req2 = addJsonBody
146156
(Ticket
147-
(Comment (CommentId 0) (CommentBody $ "**Log classifier**\n\n" <> zrComment) [] zrIsPublic (cfgAgentId cfg))
148-
(cfgAssignTo cfg)
157+
(Comment (CommentId 0)
158+
(CommentBody $ "**Log classifier**\n\n" <> zrComment)
159+
[]
160+
zrIsPublic
161+
(cfgAgentId cfg))
162+
Nothing -- If Nothing, assigned_id field will be left untouched
149163
(renderTicketStatus AnalyzedByScriptV1_0:zrTags)
150164
)
151165
req1

0 commit comments

Comments
 (0)