<commit_before>{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}

-- Module      : Khan.Model.ELB.HealthCheck
-- Copyright   : (c) 2013 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Khan.Model.ELB.Types
    ( -- * Frontend to Backend Mapping
      Mapping           (..)
    , Frontend
    , Backend
    , HealthCheckTarget (healthCheckText)
    , Protocol          (..)
    , PortNumber
    , backendProtocol
    , backendPort
    , backendHealthCheck
    , frontendProtocol
    , frontendPort
    , protocolText

      -- * Naming
    , Name (nameText)
    , mkName
    , namesFromASG
    , nameFromDescription
    ) where

import Data.Attoparsec.Text
import Data.String                  (fromString)
import Data.Text                    (Text, toLower, pack, unpack)
import Data.Text.Buildable          (Buildable (..))
import Data.Text.Lazy               (toStrict)
import Data.Text.Lazy.Builder       (toLazyText)
import Khan.Internal
import Network.AWS.AutoScaling      (AutoScalingGroup (..))
import Network.AWS.ELB              hiding (Listener)
import Network.Socket               (PortNumber (..))
import Prelude                      hiding (takeWhile)
import Text.PrettyPrint.ANSI.Leijen (Pretty (..), text)

import qualified Data.Text.Lazy.Builder.Int as Build

-------------------------------------------------------------------------------
-- Frontend to Backend Mapping

data Protocol
    = HTTP
    | HTTPS
    | TCP
    | SSL
    deriving (Show)

instance Buildable Protocol where
    build = fromString . show

data Mapping = Mapping
    { frontend :: !Frontend
    , backend  :: !Backend
    } deriving (Show)

newtype Frontend = FE Endpoint deriving (Show)

newtype HealthCheckTarget = HCT
    { healthCheckText :: Text
    } deriving (Show, Pretty)

data Backend = BE !Endpoint !HealthCheckTarget deriving (Show)

data Endpoint = EP
    { endpointProtocol :: !Protocol
    , endpointPort     :: !PortNumber
    } deriving (Show)

backendProtocol :: Backend -> Protocol
backendProtocol (BE ep _) = endpointProtocol ep

backendPort :: Backend -> PortNumber
backendPort (BE ep _) = endpointPort ep

backendHealthCheck :: Backend -> HealthCheckTarget
backendHealthCheck (BE _ hc) = hc

frontendProtocol :: Frontend -> Protocol
frontendProtocol (FE ep) = endpointProtocol ep

frontendPort :: Frontend -> PortNumber
frontendPort (FE ep) = endpointPort ep

mkHealthCheck :: Endpoint -> Text -> HealthCheckTarget
mkHealthCheck (EP pr po) path =
    HCT . toStrict . toLazyText $ build (proto pr)
        <> ":"
        <> Build.decimal po
        <> build path
  where
    proto HTTP  = HTTP
    proto HTTPS = HTTPS
    proto TCP   = HTTP
    proto SSL   = HTTPS

protocolText :: Protocol -> Text
protocolText = toLower . pack . show

instance TextParser Mapping where
    parser = do
        fe <- parser
        skipSpace
        _  <- asciiCI "TO"
        skipSpace
        be <- parser
        return $ Mapping fe be

instance TextParser Frontend where
    parser = FE <$> parser<commit_msg>Use '-' as separator between frontend and backend in the balancer shizzle.<commit_after>{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}

-- Module      : Khan.Model.ELB.HealthCheck
-- Copyright   : (c) 2013 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Khan.Model.ELB.Types
    ( -- * Frontend to Backend Mapping
      Mapping           (..)
    , Frontend
    , Backend
    , HealthCheckTarget (healthCheckText)
    , Protocol          (..)
    , PortNumber
    , backendProtocol
    , backendPort
    , backendHealthCheck
    , frontendProtocol
    , frontendPort
    , protocolText

      -- * Naming
    , Name (nameText)
    , mkName
    , namesFromASG
    , nameFromDescription
    ) where

import Data.Attoparsec.Text
import Data.String                  (fromString)
import Data.Text                    (Text, toLower, pack, unpack)
import Data.Text.Buildable          (Buildable (..))
import Data.Text.Lazy               (toStrict)
import Data.Text.Lazy.Builder       (toLazyText)
import Khan.Internal
import Network.AWS.AutoScaling      (AutoScalingGroup (..))
import Network.AWS.ELB              hiding (Listener)
import Network.Socket               (PortNumber (..))
import Prelude                      hiding (takeWhile)
import Text.PrettyPrint.ANSI.Leijen (Pretty (..), text)

import qualified Data.Text.Lazy.Builder.Int as Build

-------------------------------------------------------------------------------
-- Frontend to Backend Mapping

data Protocol
    = HTTP
    | HTTPS
    | TCP
    | SSL
    deriving (Show)

instance Buildable Protocol where
    build = fromString . show

data Mapping = Mapping
    { frontend :: !Frontend
    , backend  :: !Backend
    } deriving (Show)

newtype Frontend = FE Endpoint deriving (Show)

newtype HealthCheckTarget = HCT
    { healthCheckText :: Text
    } deriving (Show, Pretty)

data Backend = BE !Endpoint !HealthCheckTarget deriving (Show)

data Endpoint = EP
    { endpointProtocol :: !Protocol
    , endpointPort     :: !PortNumber
    } deriving (Show)

backendProtocol :: Backend -> Protocol
backendProtocol (BE ep _) = endpointProtocol ep

backendPort :: Backend -> PortNumber
backendPort (BE ep _) = endpointPort ep

backendHealthCheck :: Backend -> HealthCheckTarget
backendHealthCheck (BE _ hc) = hc

frontendProtocol :: Frontend -> Protocol
frontendProtocol (FE ep) = endpointProtocol ep

frontendPort :: Frontend -> PortNumber
frontendPort (FE ep) = endpointPort ep

mkHealthCheck :: Endpoint -> Text -> HealthCheckTarget
mkHealthCheck (EP pr po) path =
    HCT . toStrict . toLazyText $ build (proto pr)
        <> ":"
        <> Build.decimal po
        <> build path
  where
    proto HTTP  = HTTP
    proto HTTPS = HTTPS
    proto TCP   = HTTP
    proto SSL   = HTTPS

protocolText :: Protocol -> Text
protocolText = toLower . pack . show

instance TextParser Mapping where
    parser = do
        fe <- parser
        _  <- char '-'
        be <- parser
        return $ Mapping fe be

instance TextParser Frontend where
    parser = FE <$> parser
{'fraction_non_alphanumeric': 0.11447465099191771, 'fraction_numerical': 0.002351212343864805, 'mean_word_length': 2.227121858700806, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2200080', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1913, 'n_words': 584}