packages feed

silvi (empty) → 0.0.2

raw patch · 8 files changed

+416/−0 lines, 8 filesdep +basedep +bytestringdep +chronossetup-changed

Dependencies added: base, bytestring, chronos, http-types, ip, quantification, savage, text

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright 2017 Daniel Cartwright++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,15 @@+# `silvi`++[![Hackage][hackage-badge]][hackage-link]+[![License][license-badge]][license-link]++`silvi` is a Haskell library for generating fake logs from user specifications.++[hackage-badge]:+    https://img.shields.io/hackage/v/silvi.svg?label=Hackage+[hackage-link]:+    https://hackage.haskell.org/package/silvi+[license-badge]:+    https://img.shields.io/badge/License-BSD3-blue.svg+[license-link]:+    https://spdx.org/licenses/BSD3.html
+ Setup.hs view
@@ -0,0 +1,3 @@+import           Distribution.Simple+main = defaultMain+
+ silvi.cabal view
@@ -0,0 +1,48 @@+---------------------------------------------------------------------++name:                      silvi+version:                   0.0.2+build-type:                Simple+cabal-version:             >= 1.10+category:                  Testing+author:                    Daniel Cartwright+maintainer:                dcartwright@layer3com.com+license:                   BSD3+license-file:              LICENSE+homepage:                  https://github.com/chessai/silvi#readme+bug-reports:               https://github.com/chessai/silvi/issues+synopsis:                  A generator for different kinds of logs.+description:               A Haskell library for generating logs+                           from user specifications.+extra-source-files:        README.md+tested-with:               GHC == 8.0.2+                         , GHC == 8.2.1++---------------------------------------------------------------------++source-repository head+    type:                git+    branch:              master+    location:            https://github.com/chessai/silvi.git++---------------------------------------------------------------------++library+    hs-source-dirs:        src+    build-depends:         base       >= 4.9 && < 5.0+                         , bytestring+                         , chronos+                         , http-types+                         , ip+                         , quantification+                         , savage+                         , text+    exposed-modules:       Silvi+                         , Silvi.Random+                         , Silvi.Record+                         , Silvi.Types+    default-language:      Haskell2010+    other-extensions:     ++---------------------------------------------------------------------+
+ src/Silvi.hs view
@@ -0,0 +1,23 @@+-- -*- coding: utf-8; mode: haskell; -*-++-- File: src/Silvi.hs++{-# OPTIONS_GHC -fno-warn-missing-import-lists #-}++-- |+--   Module     : Silvi+--   Copyright  : Copyright 2017 Daniel Cartwright+--   License    : BSD3+--   Maintainer : dcartwright@layer3com.com+--   Stabiility : Stable+++module Silvi+ ( module Silvi.Random+ , module Silvi.Record+ , module Silvi.Types+ ) where++import           Silvi.Random+import           Silvi.Record+import           Silvi.Types
+ src/Silvi/Random.hs view
@@ -0,0 +1,139 @@+{-# LANGUAGE DataKinds           #-}+{-# LANGUAGE GADTs               #-}+{-# LANGUAGE LambdaCase          #-}+{-# LANGUAGE OverloadedStrings   #-}+{-# LANGUAGE PolyKinds           #-}+{-# LANGUAGE RankNTypes          #-}+{-# LANGUAGE ScopedTypeVariables #-}++module Silvi.Random+  ( rand+  , randLogExplicit+  , randLog+  ) where++import           Chronos.Types+import           Data.Exists                (Exists (..), Reify (..),+                                             SingList (..))+import           Data.Text                  (Text)+import           Data.Word                  (Word8)+import           Net.IPv4                   (ipv4)+import           Network.HTTP.Types.Method+import           Network.HTTP.Types.Status+import           Network.HTTP.Types.Version+import           Savage+import           Savage.Randy               (element, enum, int, print, word8)+import           Savage.Range               (constantBounded)+import           Silvi.Record               (Field (..), NcsaLog,+                                             SingField (..), TestLog,+                                             Value (..), rmap, rtraverse)+import           Silvi.Types+import           Topaz.Rec                  (Rec (..), fromSingList)++rand :: SingField a -> Gen (Value a)+rand = \case+  SingHttpMethod -> fmap ValueHttpMethod randomHttpMethod+  SingHttpStatus -> fmap ValueHttpStatus randomHttpStatus+  SingHttpProtocol -> fmap ValueHttpProtocol randomHttpProtocol+  SingHttpProtocolVersion -> fmap ValueHttpProtocolVersion randomHttpProtocolVersion+  SingUrl -> fmap ValueUrl randomUrl+  SingUserId -> fmap ValueUserId randomUserident+  SingObjSize -> fmap ValueObjSize randomObjSize+  SingIp -> fmap ValueIp randomIPv4+  SingTimestamp -> fmap ValueTimestamp randomOffsetDatetime++randLog :: forall as. (Reify as) => Gen (Rec Value as)+randLog = randLogExplicit (fromSingList (reify :: SingList as))++randLogExplicit :: Rec SingField rs -> Gen (Rec Value rs)+randLogExplicit = rtraverse rand++randomIPv4 :: Gen IPv4+randomIPv4 = ipv4+  <$> word8 constantBounded+  <*> word8 constantBounded+  <*> word8 constantBounded+  <*> word8 constantBounded++randomHttpMethod :: Gen HttpMethod+randomHttpMethod = element httpMethods++randomHttpStatus :: Gen HttpStatus+randomHttpStatus = element httpStatuses++randomHttpProtocol :: Gen HttpProtocol+randomHttpProtocol = element httpProtocols++randomHttpProtocolVersion :: Gen HttpProtocolVersion+randomHttpProtocolVersion = element httpProtocolVersions++randomUserident :: Gen Text+randomUserident = element userIdents++randomObjSize :: Gen Int+randomObjSize = int constantBounded++randomQuote :: Gen Text+randomQuote = element quotes++randomUrl :: Gen Text+randomUrl = element urls++randomDatetime :: Gen Datetime+randomDatetime = Datetime+  <$> randomDate+  <*> randomTimeOfDay++randomTimeOfDay :: Gen TimeOfDay+randomTimeOfDay = TimeOfDay+  <$> enum 0    24+  <*> enum 0    59+  <*> enum 0 59999++randomDate :: Gen Date+randomDate = Date+  <$> (Year       <$> enum 1995 2100)+  <*> (Month      <$> enum    0   11)+  <*> (DayOfMonth <$> enum    1   31)+++randomOffsetDatetime :: Gen OffsetDatetime+randomOffsetDatetime = OffsetDatetime+  <$> randomDatetime+  <*> randomOffset++randomOffset :: Gen Offset+randomOffset = element offsets++-- | List of HTTP Methods.+httpMethods :: [HttpMethod]+httpMethods = [methodGet,methodPost,methodHead,methodPut,methodDelete,methodTrace,methodConnect,methodOptions,methodPatch]++-- | List of HTTP Protocols.+httpProtocols :: [HttpProtocol]+httpProtocols = [HTTP,HTTPS,FTP]++-- | List of HTTP Protocol Versions.+httpProtocolVersions :: [HttpProtocolVersion]+httpProtocolVersions = [http09,http10,http11,http20]++-- | List of HTTP Status Codes.+httpStatuses :: [HttpStatus]+httpStatuses = [status200,status204,status301,status400,status401,status403,status404,status405,status500,status503,status504]++-- | List of sample Useridents.+userIdents :: [Text]+userIdents = ["-","userFoo","userBar","userBaz"]++-- | List of sample URLs.+urls :: [Text]+urls = ["https://github.com","http://bar.com","ftp://baz.com"]++quotes :: [Text]+quotes = ["customerA got caught putting their hand in the cookie jar","customerB uses firefox instead of chrome"]++-- | List of sample+-- | List of Time Zone Offsets. See:+--   https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations+offsets :: [Offset]+offsets = map Offset [100,200,300,330,400,430,500,530,545,600,630,700,800,845,900,930,1000,1030,1100,1200,1245,1300,1345,1400,0,(-100),(-200),(-230),(-300),(-330),(-400),(-500),(-600),(-700),(-800),(-900),(-930),(-1000),(-1100),(-1200)]
+ src/Silvi/Record.hs view
@@ -0,0 +1,124 @@+{-# LANGUAGE DataKinds         #-}+{-# LANGUAGE DeriveGeneric     #-}+{-# LANGUAGE GADTs             #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE PolyKinds         #-}+{-# LANGUAGE RankNTypes        #-}+{-# LANGUAGE TypeFamilies      #-}+{-# LANGUAGE TypeInType        #-}+{-# LANGUAGE TypeOperators     #-}++module Silvi.Record+  ( rmap+  , rtraverse+  , NcsaLog+  , TestLog+  , Field(..)+  , Value(..)+  , SingField(..)+  ) where++import           Chronos.Types (OffsetDatetime (..))+import           Data.Exists   (Exists (..), Reify (..), ShowForall (..), Sing)+import           Data.Kind     (Type)+import           Data.Text     (Text)+import           GHC.Generics  (Generic)+import           Net.Types     (IPv4)+import           Silvi.Types+import           Topaz.Rec     (Rec (..), traverse)+import qualified Topaz.Rec     as Topaz++-- | Different types present in logs.+data Field+  = FieldHttpMethod          -- ^ More explicit name for Network.HTTP.Types.Method+  | FieldHttpStatus          -- ^ More explicit name for Network.HTTP.Types.Status+  | FieldHttpProtocol        -- ^ The HTTP Protocol used+  | FieldHttpProtocolVersion -- ^ More explicit name for Network.HTTP.Types.Version+  | FieldUrl                 -- ^ a url, e.g. "https://hackage.haskell.org"+  | FieldUserId              -- ^ userId as Text+  | FieldObjSize             -- ^ usually requested resource size+  | FieldIp                  -- ^ FieldIp present in log+  | FieldTimestamp           -- ^ Timestamp+  deriving (Bounded,Enum,Eq,Generic,Ord,Read,Show)++data Value :: Field -> Type where+  ValueHttpMethod :: HttpMethod -> Value 'FieldHttpMethod+  ValueHttpStatus :: HttpStatus -> Value 'FieldHttpStatus+  ValueHttpProtocol :: HttpProtocol -> Value 'FieldHttpProtocol+  ValueHttpProtocolVersion :: HttpProtocolVersion -> Value 'FieldHttpProtocolVersion+  ValueUrl :: Text -> Value 'FieldUrl+  ValueUserId :: Text -> Value 'FieldUserId+  ValueObjSize :: Int -> Value 'FieldObjSize+  ValueIp :: IPv4 -> Value 'FieldIp+  ValueTimestamp :: OffsetDatetime -> Value 'FieldTimestamp++instance ShowForall Value where+  showsPrecForall p (ValueHttpMethod x) = showParen (p > 10) $ showString "ValueHttpMethod" . showsPrec 11 x+  showsPrecForall p (ValueHttpStatus x) = showParen (p > 10) $ showString "ValueHttpStatus" . showsPrec 11 x+  showsPrecForall p (ValueHttpProtocol x) = showParen (p > 10) $ showString "ValueHttpProtocol" . showsPrec 11 x+  showsPrecForall p (ValueHttpProtocolVersion x) = showParen (p > 10) $ showString "ValueHttpProtocolVersion" . showsPrec 11 x+  showsPrecForall p (ValueUrl x) = showParen (p > 10) $ showString "ValueUrl" . showsPrec 11 x+  showsPrecForall p (ValueUserId x) = showParen (p > 10) $ showString "ValueUserId" . showsPrec 11 x+  showsPrecForall p (ValueObjSize x) = showParen (p > 10) $ showString "ValueObjSize" . showsPrec 11 x+  showsPrecForall p (ValueIp x) = showParen (p > 10) $ showString "ValueIp" . showsPrec 11 x+  showsPrecForall p (ValueTimestamp x) = showParen (p > 10) $ showString "ValueTimestamp" . showsPrec 11 x++data SingField :: Field -> Type where+  SingHttpMethod          :: SingField 'FieldHttpMethod+  SingHttpStatus          :: SingField 'FieldHttpStatus+  SingHttpProtocol        :: SingField 'FieldHttpProtocol+  SingHttpProtocolVersion :: SingField 'FieldHttpProtocolVersion+  SingUrl                 :: SingField 'FieldUrl+  SingUserId              :: SingField 'FieldUserId+  SingObjSize             :: SingField 'FieldObjSize+  SingIp                  :: SingField 'FieldIp+  SingTimestamp           :: SingField 'FieldTimestamp+++type TestLog = '[ FieldIp+                , FieldTimestamp+                ]+-- | A single log entry from NCSA Common or Extended-formatted log. See:+--   http://publib.boulder.ibm.com/tividd/td/ITWSA/ITWSA_info45/en_US/HTML/guide/c-logs.html#combined+type NcsaLog = '[ FieldIp+                , FieldUserId+                , FieldTimestamp+                , FieldHttpMethod+                , FieldUrl+                , FieldHttpProtocol+                , FieldHttpProtocolVersion+                , FieldHttpStatus+                , FieldObjSize+                ]++type instance Sing = SingField+instance Reify 'FieldHttpMethod where+  reify = SingHttpMethod+instance Reify 'FieldHttpStatus where+  reify = SingHttpStatus+instance Reify 'FieldHttpProtocol where+  reify = SingHttpProtocol+instance Reify 'FieldHttpProtocolVersion where+  reify = SingHttpProtocolVersion+instance Reify 'FieldUrl where+  reify = SingUrl+instance Reify 'FieldUserId where+  reify = SingUserId+instance Reify 'FieldObjSize where+  reify = SingObjSize+instance Reify 'FieldIp where+  reify = SingIp+instance Reify 'FieldTimestamp where+  reify = SingTimestamp++-- | Alias for `Topaz.map`.+rmap :: (forall x. f x -> g x) -> Rec f as -> Rec g as+rmap = Topaz.map++-- | Alias for `Topaz.traverse`+rtraverse+  :: Applicative h+  => (forall x. f x -> h (g x))+  -> Rec f rs+  -> h (Rec g rs)+rtraverse = Topaz.traverse
+ src/Silvi/Types.hs view
@@ -0,0 +1,53 @@+module Silvi.Types+  ( HttpMethod(..)+  , HttpStatus(..)+  , HttpProtocol(..)+  , HttpProtocolVersion(..)+  , Url(..)+  , UserId(..)+  , ObjSize(..)+  , IPv4(..)+  , OffsetDatetime(..)+  ) where++import           Chronos.Types              (Offset (..), OffsetDatetime (..))+import           Data.Text                  (Text)+import           Net.Types                  (IPv4 (..))+import           Network.HTTP.Types.Method+import           Network.HTTP.Types.Status+import           Network.HTTP.Types.Version++-- | Type alias for Netowrk.HTTP.Types.Method, renamed+--   to be more explicit.+--+type HttpMethod = Method++-- | Type alias for Network.HTTP.Types.Status, renamed+--   to be more explicit.+--+type HttpStatus = Status++-- | The HTTP Protocol used.+--+data HttpProtocol = HTTP | HTTPS | FTP deriving (Show, Eq)++-- | Type alias for Network.HTTP.Types.Version, renamed+--   to be more explicit.+--+type HttpProtocolVersion = HttpVersion++-- | Url type.+--   TODO: Expand on this for better randomisation.+--+newtype Url = Url { getUrl :: Text }+  deriving (Show,Eq)++-- | UserId type.+--+newtype UserId = UserId { getUserId :: Text }+  deriving (Show,Eq)++-- | Requested resource size.+--+newtype ObjSize = ObjSize { getObjSize :: Text }+  deriving (Show,Eq)