@@ -1,5 +1,12 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE ExtendedDefaultRules #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1−{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE TemplateHaskell #-}
6+{-# LANGUAGE TemplateHaskell #-}
7+{-# LANGUAGE TypeFamilies #-}
9+{-# OPTIONS_GHC -fno-warn-type-defaults #-}
4-- Module : Network.PagerDuty.REST.Incidents
11-- Module : Network.PagerDuty.REST.Incidents
5-- Copyright : (c) 2013-2014 Brendan Hay <brendan.g.hay@gmail.com>
12-- Copyright : (c) 2013-2014 Brendan Hay <brendan.g.hay@gmail.com>
@@ -33,7 +40,7 @@
34-- Incidents and Incident Counts from the last 30 days are returned by
41-- Incidents and Incident Counts from the last 30 days are returned by
35-- default. To change this default date range, see the documentation for the
42-- default. To change this default date range, see the documentation for the
36−-- since, until or date_range parameters.
43+-- since, until or dateRange parameters.
38-- Note: that to create an incident you need to trigger it through the
45-- Note: that to create an incident you need to trigger it through the
39-- "Network.PagerDuty.Integration" API.
46-- "Network.PagerDuty.Integration" API.
@@ -44,17 +51,227 @@ module Network.PagerDuty.REST.Incidents
54+import Control.Applicative hiding (empty)
47import Control.Lens
55import Control.Lens
48−import Data.Monoid
56+import Data.Aeson
57+import Data.ByteString.Conversion (ToByteString(..))
59+import Data.Monoid hiding (All)
49−import Data.Text (Text)
60+import Data.Text (Text)
50import Data.Time
61import Data.Time
51import Network.HTTP.Types
62import Network.HTTP.Types
63+import Network.PagerDuty.Query
52import Network.PagerDuty.TH
64import Network.PagerDuty.TH
53import Network.PagerDuty.Types
65import Network.PagerDuty.Types
55incidents :: Path
69incidents :: Path
56incidents = "incidents"
70incidents = "incidents"
81+ -- ^ The number of your incident.
83+ -- ^ The date/time the incident was triggered.
85+ -- ^ The date/time the incident was resolved.
88+instance QueryValues (Sort Field) where
89+ queryValues s = case s of
90+ Desc f -> ["desc:" <> field f]
91+ Asc f -> ["asc:" <> field f]
93+ field IncidentNumber = "incidentNumber"
94+ field CreatedOn = "createdOn"
95+ field ResolvedOn = "resolvedOn"
100+instance QueryValues Range where
101+ queryValues All = ["all"]
110+deriveNullary ''IncidentStatus
112+instance ToByteString IncidentStatus where
113+ builder Triggered = "triggered"
114+ builder Acknowledged = "acknowledged"
115+ builder Resolved = "resolved"
116+ builder (Other t) = builder t
118+data Assignee = Assignee
120+ , _aObject :: Object
121+ } deriving (Eq, Show)
123+deriveRecord ''Assignee
125+data Incident = Incident
126+ { _iIncidentNumber :: !Int
127+ , _iStatus :: !IncidentStatus
128+ , _iCreatedOn :: Date
130+ , _iIncidentKey :: Maybe IncidentKey
131+ , _iService :: Object
132+ , _iEscalationPolicy :: Maybe Object
133+ , _iAssignedTo :: [Assignee]
134+ , _iAcknowledgers :: [Assignee]
135+ , _iLastStatusChangeBy :: Maybe Assignee
136+ , _iLastStatusChangeOn :: Maybe Date
137+ , _iTriggerSummaryData :: Object
138+ , _iTriggerDetailsHtmlUrl :: Text
139+ } deriving (Eq, Show)
141+deriveJSON ''Incident
143+-- | The number of the incident. This is unique across your account.
144+makeLens "_iIncidentNumber" ''Incident
146+-- | The current status of the incident. Valid statuses are:
147+makeLens "_iStatus" ''Incident
149+-- | The date/time the incident was triggered.
150+iCreatedOn :: Lens' Incident UTCTime
151+iCreatedOn = lens _iCreatedOn (\i x -> i { _iCreatedOn = x }) . _D
153+-- | The PagerDuty website URL where the incident can be viewed and further
154+-- actions taken. This is not the resource URL.
155+makeLens "_iHtmlUrl" ''Incident
157+-- | The incident's de-duplication key. See the PagerDuty Integration API docs
158+-- for further details.
159+makeLens "_iIncidentKey" ''Incident
161+-- | The PagerDuty service that the incident belongs to. The service will
162+-- contain fields of its own.
163+makeLens "_iService" ''Incident
165+-- | The escalation policy that the incident belongs to. The policy will
166+-- contain fields of its own.
167+makeLens "_iEscalationPolicy" ''Incident
169+-- | The list of assignments of the incident. An assignment is an object
170+-- containing the assigned user as well as the date/time the incident was assigned
171+-- to that user. The user will contain fields of its own. This list is empty if
172+-- the status of the incident is resolved.
173+makeLens "_iAssignedTo" ''Incident
175+-- | The list of acknowledgements of the incident. An acknowledgement is an
176+-- object containing the acknowleding object (either a user or the integration
177+-- API) as well as the date/time the incident was acknowledged. This field is only
178+-- present if the status of the incident is acknowledged. This field is sorted in
179+-- ascending order by acknowledgement time.
180+makeLens "_iAcknowledgers" ''Incident
182+-- | The user who is responsible for the incident's last status change. If the incident is in the acknowledged or resolved status, this will be the user that took the first acknowledged or resolved action. If the incident was automatically resolved (say through the Event Integration API), or if the incident is in the triggered state, this will be null. User fields are the same as in the assignedToUser field above.
183+makeLens "_iLastStatusChangeBy" ''Incident
185+-- | The date/time the incident's status last changed.
186+iLastStatusChangeOn :: Lens' Incident (Maybe UTCTime)
187+iLastStatusChangeOn =
188+ lens _iLastStatusChangeOn (\i x -> i { _iLastStatusChangeOn = x })
191+-- | Some condensed information regarding the initial event that triggered this incident. This data will be a set of key/value pairs that vary depending on what sort of event triggered the incident (email, Event API request, etc). For instance, if an email triggered the incident, then the triggerSummaryData will likely contain a subject. There are no guarantees on the full presence of this data for every incident.
192+makeLens "_iTriggerSummaryData" ''Incident
194+-- | The PagerDuty website URL where the full details regarding the initial event that triggered this incident can be found. (This is not the resource URL.)
195+makeLens "_iTriggerDetailsHtmlUrl" ''Incident
197+data ListIncidents = ListIncidents
198+ { _lsSince' :: Maybe Date
199+ , _lsUntil' :: Maybe Date
200+ , _lsDateRange' :: Maybe Range
201+ , _lsStatus' :: Maybe (CSV IncidentStatus)
202+ , _lsIncidentKey' :: Maybe IncidentKey
203+ , _lsService' :: Maybe (CSV ServiceId)
204+ , _lsAssignedToUser' :: Maybe (CSV UserId)
205+ , _lsTimeZone' :: !TZ
206+ , _lsSortBy' :: Maybe (Sort Field)
207+ } deriving (Eq, Show)
209+queryRequest ''ListIncidents
211+instance Paginate ListIncidents
213+-- | The start of the date range over which you want to search.
214+lsSince :: Lens' (Request ListIncidents s b) (Maybe UTCTime)
215+lsSince = upd.lsSince'.mapping _D
217+-- | The end of the date range over which you want to search.
219+-- /Note: If you leave off either since or until, a 30 day default range is
220+-- applied to your open ended range. Not including the since parameter will set
221+-- the date range to until - 30 days. Likewise, if you leave off until,
222+-- it is set to since + 30 days.
224+-- Defaults to the last 30 days if you leave off both. The size of the date
225+-- range must be less than 180 days.
226+lsUntil :: Lens' (Request ListIncidents s b) (Maybe UTCTime)
227+lsUntil = upd.lsUntil'.mapping _D
229+-- | When set, the since and until parameters and defaults are
230+-- ignored. Use this to get all incidents since the account was created.
231+lsDateRange :: Lens' (Request ListIncidents s b) Bool
232+lsDateRange = upd . lens get' set'
235+ | Just All <- _lsDateRange' x = True
238+ set' l x = l { _lsDateRange' = if x then Just All else Nothing }
240+-- | Returns only the incidents currently in the passed status(es).
241+lsStatus :: Lens' (Request ListIncidents s b) (Maybe [IncidentStatus])
242+lsStatus = upd.lsStatus'.mapping _CSV
244+-- | Returns only the incidents with the passed de-duplication key.
246+-- /See:/ "Network.PagerDuty.Integration"
247+lsIncidentKey :: Lens' (Request ListIncidents s b) (Maybe IncidentKey)
248+lsIncidentKey = upd.lsIncidentKey'
250+-- | Returns only the incidents associated with the passed service(s). This
251+-- expects one or more service IDs.
252+lsService :: Lens' (Request ListIncidents s b) (Maybe [ServiceId])
253+lsService = upd.lsService'.mapping _CSV
255+-- | Returns only the incidents currently assigned to the passed user(s). This
256+-- expects one or more user IDs.
258+-- /Note:/ When using the assignedToUser filter, you will only receive
259+-- incidents with statuses of triggered or acknowledged. This is because resolved
260+-- incidents are not assigned to any user.
261+lsAssignedToUser :: Lens' (Request ListIncidents s b) (Maybe [UserId])
262+lsAssignedToUser = upd.lsAssignedToUser'.mapping _CSV
264+-- | Time zone in which dates in the result will be rendered.
267+lsTimeZone :: Lens' (Request ListIncidents s b) TimeZone
268+lsTimeZone = upd.lsTimeZone'._TZ
270+-- | Used to specify both the field you wish to sort the results on, as well as
271+-- the direction (ascending/descending) of the results.
272+lsSortBy :: Lens' (Request ListIncidents s b) (Maybe (Sort Field))
273+lsSortBy = upd.lsSortBy'
58-- | The PagerDuty incidents query API can be used to query current and
275-- | The PagerDuty incidents query API can be used to query current and
59-- historical PagerDuty incidents over a date range, letting you build custom
276-- historical PagerDuty incidents over a date range, letting you build custom
60-- dashboards or incident reports. The API allows for searching for incidents
277-- dashboards or incident reports. The API allows for searching for incidents
@@ -64,7 +281,19 @@ incidents = "incidents"
64-- @GET \/incidents@
281-- @GET \/incidents@
66-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/list>
283-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/list>
67−listIncidents = undefined
284+listIncidents :: Request ListIncidents s [Incident]
287+ { _lsSince' = Nothing
288+ , _lsUntil' = Nothing
289+ , _lsDateRange' = Nothing
290+ , _lsStatus' = Nothing
291+ , _lsIncidentKey' = Nothing
292+ , _lsService' = Nothing
293+ , _lsAssignedToUser' = Nothing
294+ , _lsTimeZone' = def
295+ , _lsSortBy' = Nothing
296+ } & path .~ incidents
69-- | Get detailed information about an incident. Accepts either an incident id,
298-- | Get detailed information about an incident. Accepts either an incident id,
70-- or an incident number.
299-- or an incident number.
@@ -72,7 +301,77 @@ listIncidents = undefined
72-- @GET \/incidents\/\:id@
301-- @GET \/incidents\/\:id@
74-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/show>
303-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/show>
75−getIncident = undefined
304+getIncident :: IncidentId -> Request Empty s Incident
305+getIncident i = empty & path .~ incidents % i
307+data CountIncidents = CountIncidents
308+ { _cSince' :: Maybe Date
309+ , _cUntil' :: Maybe Date
310+ , _cDateRange' :: Maybe Range
311+ , _cStatus' :: Maybe (CSV IncidentStatus)
312+ , _cIncidentKey' :: Maybe IncidentKey
313+ , _cService' :: Maybe (CSV ServiceId)
314+ , _cAssignedToUser' :: Maybe (CSV UserId)
315+ } deriving (Eq, Show)
317+queryRequest ''CountIncidents
319+-- | The start of the date range over which you want to search.
320+cSince :: Lens' (Request CountIncidents s b) (Maybe UTCTime)
321+cSince = upd.cSince'.mapping _D
323+-- | The end of the date range over which you want to search.
325+-- /Note: If you leave off either since or until, a 30 day default range is
326+-- applied to your open ended range. Not including the since parameter will set
327+-- the date range to until - 30 days. Likewise, if you leave off until,
328+-- it is set to since + 30 days.
330+-- Defaults to the last 30 days if you leave off both. The size of the date
331+-- range must be less than 180 days.
332+cUntil :: Lens' (Request CountIncidents s b) (Maybe UTCTime)
333+cUntil = upd.cUntil'.mapping _D
335+-- | When set, the since and until parameters and defaults are
336+-- ignored. Use this to get all counts since the account was created.
337+cDateRange :: Lens' (Request CountIncidents s b) Bool
338+cDateRange = upd . lens get' set'
341+ | Just All <- _cDateRange' x = True
344+ set' l x = l { _cDateRange' = if x then Just All else Nothing }
346+-- | Only counts the incidents currently in the passed status(es).
347+cStatus :: Lens' (Request CountIncidents s b) (Maybe [IncidentStatus])
348+cStatus = upd.cStatus'.mapping _CSV
350+-- | Returns only the incidents with the passed de-duplication key.
352+-- /See:/ "Network.PagerDuty.Integration"
353+cIncidentKey :: Lens' (Request CountIncidents s b) (Maybe IncidentKey)
354+cIncidentKey = upd.cIncidentKey'
356+-- | Only counts the incidents associated with the passed service(s).
357+-- This is expecting one or more service IDs.
358+cService :: Lens' (Request CountIncidents s b) (Maybe [ServiceId]) -- list1
359+cService = upd.cService'.mapping _CSV
361+-- | Only counts the incidents currently assigned to the passed user(s).
362+-- This is expecting one or more user IDs.
364+-- /Note:/ When using the assigned_to_user filter, you will only count
365+-- incidents with statuses of triggered or acknowledged. This is because resolved
366+-- incidents are not assigned to any user.
367+cAssignedToUser :: Lens' (Request CountIncidents s b) (Maybe [UserId]) -- list1
368+cAssignedToUser = upd.cAssignedToUser'.mapping _CSV
370+newtype Count = Count Int
371+ deriving (Eq, Ord, Show, Bounded, Enum, Num, Integral, Real)
373+instance FromJSON Count where
374+ parseJSON = withObject "count" $ \o -> Count <$> o .: "total"
77-- | Use this query if you are simply looking for the count of incidents that
376-- | Use this query if you are simply looking for the count of incidents that
78-- match a given query. This should be used if you don't need access to the
377-- match a given query. This should be used if you don't need access to the
@@ -81,7 +380,17 @@ getIncident = undefined
81-- @GET \/incidents\/count@
380-- @GET \/incidents\/count@
83-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/count>
382-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/count>
84−countIncidents = undefined
383+countIncidents :: Request CountIncidents s Count
386+ { _cSince' = Nothing
387+ , _cUntil' = Nothing
388+ , _cDateRange' = Nothing
389+ , _cStatus' = Nothing
390+ , _cIncidentKey' = Nothing
391+ , _cService' = Nothing
392+ , _cAssignedToUser' = Nothing
393+ } & path .~ incidents % "count"
86-- | Acknowledge, resolve, escalate or reassign one or more incidents.
395-- | Acknowledge, resolve, escalate or reassign one or more incidents.