module Network.PagerDuty.REST.LogEntries
(
ListLogs
, listLogs
, listUserLogs
, listIncidentLogs
, llTimeZone
, llSince
, llUntil
, llIsOverview
, GetLog
, getLog
, glTimeZone
, Channel (..)
, _ChanNagios
, _ChanAPI
, _ChanEmail
, _ChanUserEmail
, _ChanWebTrigger
, _ChanSMS
, _ChanWeb
, _ChanNote
, _ChanAuto
, _ChanTimeout
, NagiosChannel
, ncSummary
, ncHost
, ncService
, ncState
, ncDetails
, APIChannel
, acSummary
, acServiceKey
, acDescription
, acIncidentKey
, acDetails
, EmailType (..)
, EmailChannel
, ecSummary
, ecTo
, ecFrom
, ecSubject
, ecBody
, ecBodyContentType
, ecRawUrl
, ecHtmlUrl
, WebTriggerChannel
, wcSummary
, wcSubject
, wcDetails
, Entry (..)
, _LogEntry
, _NotifyEntry
, LogEntryType (..)
, LogEntry
, leId
, leType
, leCreatedAt
, leNote
, leAssignedUser
, leChannel
, NotifyEntry
, neCreatedAt
, neUser
, neNotification
, NotificationType (..)
, NotificationStatus (..)
, Notification
, nType
, nStatus
, nAddress
) where
import Control.Applicative
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens
import Data.Default.Class
import qualified Data.HashMap.Strict as Map
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Time
import Network.HTTP.Types
import Network.PagerDuty.REST.Users (User)
import Network.PagerDuty.Internal.TH
import Network.PagerDuty.Internal.Types
logs :: Path
logs = "log_entries"
includes :: Query
includes =
[ ("include[]", Just "channel")
, ("include[]", Just "incident")
, ("include[]", Just "service")
]
data NagiosChannel = NagiosChannel
{ _ncSummary :: Text
, _ncHost :: Text
, _ncService :: Maybe Text
, _ncState :: Text
, _ncDetails :: Object
} deriving (Eq, Show)
deriveJSON ''NagiosChannel
makeLens "_ncSummary" ''NagiosChannel
makeLens "_ncHost" ''NagiosChannel
makeLens "_ncService" ''NagiosChannel
makeLens "_ncState" ''NagiosChannel
makeLens "_ncDetails" ''NagiosChannel
data APIChannel = APIChannel
{ _acSummary :: Text
, _acServiceKey :: ServiceKey
, _acDescription :: Text
, _acIncidentKey :: IncidentKey
, _acDetails :: Object
} deriving (Eq, Show)
deriveJSON ''APIChannel
makeLens "_acSummary" ''APIChannel
makeLens "_acServiceKey" ''APIChannel
makeLens "_acDescription" ''APIChannel
makeLens "_acIncidentKey" ''APIChannel
makeLens "_acDetails" ''APIChannel
data EmailType
= Plain
| HTML
deriving (Eq, Show)
deriveNullary ''EmailType
data EmailChannel = EmailChannel
{ _ecSummary :: Text
, _ecTo :: Text
, _ecFrom :: Text
, _ecSubject :: Text
, _ecBody :: Text
, _ecBodyContentType :: EmailType
, _ecRawUrl :: Text
, _ecHtmlUrl :: Maybe Text
} deriving (Eq, Show)
deriveJSON ''EmailChannel
makeLens "_ecSummary" ''EmailChannel
makeLens "_ecTo" ''EmailChannel
makeLens "_ecFrom" ''EmailChannel
makeLens "_ecSubject" ''EmailChannel
makeLens "_ecBody" ''EmailChannel
makeLens "_ecBodyContentType" ''EmailChannel
makeLens "_ecRawUrl" ''EmailChannel
makeLens "_ecHtmlUrl" ''EmailChannel
data WebTriggerChannel = WebTriggerChannel
{ _wcSummary :: Text
, _wcSubject :: Text
, _wcDetails :: Maybe Text
} deriving (Eq, Show)
deriveJSON ''WebTriggerChannel
makeLens "_wcSummary" ''WebTriggerChannel
makeLens "_wcSubject" ''WebTriggerChannel
makeLens "_wcDetails" ''WebTriggerChannel
data Channel
= ChanNagios NagiosChannel
| ChanAPI APIChannel
| ChanEmail EmailChannel
| ChanUserEmail User EmailChannel
| ChanWebTrigger User WebTriggerChannel
| ChanSMS User
| ChanWeb User
| ChanNote User
| ChanAuto
| ChanTimeout
deriving (Eq, Show)
makePrisms ''Channel
instance FromJSON Channel where
parseJSON = withObject "agent+channel" $ \o -> do
t <- o .: "channel" >>= (.: "type")
let u = Map.member "user" o
case t of
"email" | not u -> ChanEmail <$> o .: "channel"
"nagios" -> ChanNagios <$> o .: "channel"
"api" -> ChanAPI <$> o .: "channel"
"email" | u -> ChanUserEmail <$> o .: "user" <*> o .: "channel"
"web_trigger" -> ChanWebTrigger <$> o .: "user" <*> o .: "channel"
"sms" -> ChanSMS <$> o .: "user"
"web" -> ChanWeb <$> o .: "user"
"note" -> ChanNote <$> o .: "user"
"auto" -> pure ChanAuto
"timeout" -> pure ChanTimeout
_ -> fail $
"unrecognised service channel type: " ++ Text.unpack t
data LogEntryType
= Trigger
| Acknowledge
| Unacknowledge
| Resolve
| Escalate
| Assign
| Annotate
| ReachTriggerLimit
| RepeatEscalationPath
| ExhaustEscalationPath
| Notify
deriving (Eq, Show)
deriveNullary ''LogEntryType
data LogEntry = LogEntry'
{ _leId :: LogEntryId
, _leType :: LogEntryType
, _leCreatedAt :: Date
, _leNote :: Maybe Text
, _leAssignedUser :: Maybe User
, _leChannel :: Channel
} deriving (Eq, Show)
instance FromJSON LogEntry where
parseJSON = withObject "log_entry" $ \o ->
LogEntry' <$> o .: "id"
<*> o .: "type"
<*> o .: "created_at"
<*> o .:? "note"
<*> o .:? "assigned_user"
<*> parseJSON (Object o)
makeLens "_leId" ''LogEntry
makeLens "_leCreatedAt" ''LogEntry
makeLens "_leType" ''LogEntry
makeLens "_leNote" ''LogEntry
makeLens "_leAssignedUser" ''LogEntry
makeLens "_leChannel" ''LogEntry
data NotificationType
= SMS
| Email
| Phone
| IosPushNotification
deriving (Eq, Show)
deriveNullary ''NotificationType
data NotificationStatus
= Success
| Blocked
| Busy
| Failed
| NoAnswer
| BadAddress
deriving (Eq, Show)
deriveNullary ''NotificationStatus
data Notification = Notification
{ _nType :: !NotificationType
, _nStatus :: !NotificationStatus
, _nAddress :: Address
} deriving (Eq, Show)
deriveJSON ''Notification
makeLens "_nType" ''Notification
makeLens "_nStatus" ''Notification
makeLens "_nAddress" ''Notification
data NotifyEntry = NotifyEntry'
{ _neCreatedAt' :: Date
, _neUser' :: User
, _neNotification' :: Notification
} deriving (Eq, Show)
deriveRecord ''NotifyEntry
neCreatedAt :: Lens' NotifyEntry UTCTime
neCreatedAt = neCreatedAt'._D
neUser :: Lens' NotifyEntry User
neUser = neUser'
neNotification :: Lens' NotifyEntry Notification
neNotification = neNotification'
data Entry
= LogEntry LogEntry
| NotifyEntry NotifyEntry
deriving (Eq, Show)
makePrisms ''Entry
data ListLogs = ListLogs
{ _llTimeZone' :: TZ
, _llSince' :: Maybe Date
, _llUntil' :: Maybe Date
, _llIsOverview' :: Bool'
} deriving (Eq, Show)
instance Paginate ListLogs
queryRequest ''ListLogs
llTimeZone :: Lens' (Request ListLogs s b) TimeZone
llTimeZone = upd.llTimeZone'._TZ
llSince :: Lens' (Request ListLogs s b) (Maybe UTCTime)
llSince = upd.llSince'.mapping _D
llUntil :: Lens' (Request ListLogs s b) (Maybe UTCTime)
llUntil = upd.llUntil'.mapping _D
llIsOverview :: Lens' (Request ListLogs s b) Bool
llIsOverview = upd.llIsOverview'._B
listLogs :: Request ListLogs s [LogEntry]
listLogs =
mk ListLogs
{ _llTimeZone' = def
, _llSince' = Nothing
, _llUntil' = Nothing
, _llIsOverview' = F
} & path .~ logs
& query .~ includes
& unwrap .~ key "log_entries"
listUserLogs :: UserId -> Request ListLogs s [LogEntry]
listUserLogs u = listLogs & path .~ "users" % u % logs
listIncidentLogs :: IncidentKey -> Request ListLogs s [LogEntry]
listIncidentLogs i = listLogs & path .~ "incidents" % i % logs
newtype GetLog = GetLog
{ _glTimeZone' :: TZ
} deriving (Eq, Show)
queryRequest ''GetLog
glTimeZone :: Lens' (Request GetLog s b) TimeZone
glTimeZone = upd.glTimeZone'._TZ
getLog :: LogEntryId -> Request GetLog s LogEntry
getLog l =
mk GetLog
{ _glTimeZone' = def
} & path .~ logs % l
& query .~ includes