#if __GLASGOW_HASKELL__ >= 800
#else
#endif
module Database.InfluxDB.Manage
(
Query
, manage
, QueryParams
, queryParams
, server
, database
, precision
, manager
, ShowQuery
, qid
, queryText
, duration
, ShowSeries
, key
) where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Lens
import Data.Aeson
import Data.Scientific (toBoundedInteger)
import Data.Text (Text)
import Data.Time.Clock
import Data.Void
import qualified Data.Aeson.Types as A
import qualified Data.Attoparsec.Combinator as AC
import qualified Data.Attoparsec.Text as AT
import qualified Data.Text.Encoding as TE
import qualified Data.Vector as V
import qualified Network.HTTP.Client as HC
import qualified Network.HTTP.Types as HT
import Database.InfluxDB.JSON (getField, parseQueryField)
import Database.InfluxDB.Types as Types
import Database.InfluxDB.Query hiding (query)
import qualified Database.InfluxDB.Format as F
manage :: QueryParams -> Query -> IO ()
manage params q = do
manager' <- either HC.newManager return $ params^.manager
response <- HC.httpLbs request manager' `catch` (throwIO . HTTPException)
let body = HC.responseBody response
case eitherDecode' body of
Left message ->
throwIO $ UnexpectedResponse message body
Right val -> case A.parse (parseResults (params^.precision)) val of
A.Success (_ :: V.Vector Void) -> return ()
A.Error message -> do
let status = HC.responseStatus response
when (HT.statusIsServerError status) $
throwIO $ ServerError message
when (HT.statusIsClientError status) $
throwIO $ ClientError message request
fail $ "BUG: " ++ message ++ " in Database.InfluxDB.Manage.manage"
where
request = HC.setQueryString qs $ manageRequest params
qs =
[ ("q", Just $ F.fromQuery q)
]
manageRequest :: QueryParams -> HC.Request
manageRequest params = HC.defaultRequest
{ HC.host = TE.encodeUtf8 $ params^.server.host
, HC.port = fromIntegral $ params^.server.port
, HC.secure = params^.server.ssl
, HC.method = "POST"
, HC.path = "/query"
}
where
Server {..} = params^.server
data ShowQuery = ShowQuery
{ showQueryQid :: !Int
, showQueryText :: !Query
, showQueryDatabase :: !Database
, showQueryDuration :: !NominalDiffTime
}
instance QueryResults ShowQuery where
parseResults _ = parseResultsWith $ \_ _ columns fields ->
maybe (fail "parseResults: parse error") return $ do
Number (toBoundedInteger -> Just showQueryQid) <-
V.elemIndex "qid" columns >>= V.indexM fields
String (F.formatQuery F.text -> showQueryText) <-
V.elemIndex "query" columns >>= V.indexM fields
String (F.formatDatabase F.text -> showQueryDatabase) <-
V.elemIndex "database" columns >>= V.indexM fields
String (parseDuration -> Right showQueryDuration) <-
V.elemIndex "duration" columns >>= V.indexM fields
return ShowQuery {..}
parseDuration :: Text -> Either String NominalDiffTime
parseDuration = AT.parseOnly $ sum <$!> durations
where
durations = some $ (*)
<$> fmap fromIntegral int
<*> unit
where
int :: AT.Parser Int
int = AT.decimal
unit = AC.choice
[ 10^^(6 :: Int) <$ AT.char 'u'
, 1 <$ AT.char 's'
, 60 <$ AT.char 'm'
, 3600 <$ AT.char 'h'
]
newtype ShowSeries = ShowSeries
{ _key :: Key
}
instance QueryResults ShowSeries where
parseResults _ = parseResultsWith $ \_ _ columns fields -> do
FieldString name <- getField "key" columns fields >>= parseQueryField
return $ ShowSeries $ F.formatKey F.text name
makeLensesWith
( lensRules
& generateSignatures .~ False
& lensField .~ lookingupNamer
[ ("showQueryQid", "qid")
, ("showQueryText", "queryText")
, ("showQueryDatabase", "_database")
, ("showQueryDuration", "duration")
]
) ''ShowQuery
qid :: Lens' ShowQuery Int
queryText :: Lens' ShowQuery Query
instance HasDatabase ShowQuery where
database = _database
duration :: Lens' ShowQuery NominalDiffTime
makeLensesWith (lensRules & generateSignatures .~ False) ''ShowSeries
key :: Lens' ShowSeries Key