]> gitweb @ CieloNegro.org - hs-rrdtool.git/blobdiff - Database/RRDtool/Create.hs
major rename
[hs-rrdtool.git] / Database / RRDtool / Create.hs
index e0146c0170035054ba447e57799dac9e47e09867..f07797acf5f2dadc9c976cc611f457e4065d0aa4 100644 (file)
@@ -1,14 +1,27 @@
+{-# LANGUAGE
+  ExistentialQuantification,
+  FlexibleContexts,
+  FlexibleInstances,
+  MultiParamTypeClasses,
+  OverlappingInstances,
+  TypeFamilies,
+  UndecidableInstances
+  #-}
+{-# LANGUAGE QuasiQuotes #-} -- DELETE THIS
 module Database.RRDtool.Create
-    ( DataSource(..)
+    ( DataSource
+    , ExternalDSType(..)
+    , ExternalDataSource(..)
+    , ComputedDataSource(..)
     , createRRD
 
     -- Data.HList
-    , (.*.)    
-    , HNil(..)
+    , (.&.)
+    , Nil(..)
 
     -- Database.RRDtool.Expression
     , Constant(..)
-    , IsVarName(..)
+    , IsVarName
     , Variable(..)
     , CommonUnaryOp(..)
     , CommonBinaryOp(..)
@@ -22,9 +35,11 @@ module Database.RRDtool.Create
     where
 
 import Data.HList
+import Data.HList.Graph
 import Data.Time.Clock
 import Data.Time.Clock.POSIX
 import Database.RRDtool.Expression
+import Types.Data.Bool
 
 
 -- |A single RRD can accept input from several data sources (DS), for
@@ -51,24 +66,57 @@ import Database.RRDtool.Expression
 -- 32bit counter, DERIVE with @'dsMin' = 0@ is probably preferable. If
 -- you are using a 64bit counter, just about any max setting will
 -- eliminate the possibility of mistaking a reset for a counter wrap.
-data DataSource
-    = -- |GAUGE is for things like temperatures or number of people in
-      -- a room or the value of a RedHat share.
-    GAUGE {
+class DataSource ds where
+    type DSName ds
+
+data ExternalDSType
+    = GAUGE    -- ^GAUGE is for things like temperatures or number of
+               -- people in a room or the value of a RedHat share.
+    | COUNTER  -- ^COUNTER is for continuous incrementing counters
+               -- like the ifInOctets counter in a router. The COUNTER
+               -- data source assumes that the counter never
+               -- decreases, except when a counter overflows. The
+               -- update function takes the overflow into account. The
+               -- counter is stored as a per-second rate. When the
+               -- counter overflows, RRDtool checks if the overflow
+               -- happened at the 32bit or 64bit border and acts
+               -- accordingly by adding an appropriate value to the
+               -- result.
+    | DERIVE   -- ^DERIVE will store the derivative of the line going
+               -- from the last to the current value of the data
+               -- source. This can be useful for gauges, for example,
+               -- to measure the rate of people entering or leaving a
+               -- room. Internally, derive works exactly like COUNTER
+               -- but without overflow checks. So if your counter does
+               -- not reset at 32 or 64 bit you might want to use
+               -- DERIVE and combine it with a 'dsMin' value of 0.
+    | ABSOLUTE -- ^ABSOLUTE is for counters which get reset upon
+               -- reading. This is used for fast counters which tend
+               -- to overflow. So instead of reading them normally you
+               -- reset them after every read to make sure you have a
+               -- maximum time available before the next
+               -- overflow. Another usage is for things you count like
+               -- number of messages since the last update.
+    deriving (Show, Eq, Ord)
+
+data ExternalDataSource vn
+    = ExternalDataSource {
         -- |The name you will use to reference this particular data
-        -- source from an RRD. A ds-name must be 1 to 19 characters
+        -- source from an RRD. A ds name must be 1 to 19 characters
         -- long in the characters @[a-zA-Z0-9_]@.
-        dsName :: !String
-        -- |Defines the maximum number of seconds that may
-        -- pass between two updates of this data source before the
-        -- value of the data source is assumed to be @*UNKNOWN*@.
-      , dsHeartbeat :: !NominalDiffTime
-        -- |'dsMin' and 'dsMax' Define the expected range values for
-        -- data supplied by a data source. If 'dsMin' and\/or 'dsMax'
+        edsName :: !vn
+        -- |The type of this data source.
+      , edsType :: !ExternalDSType
+        -- |Defines the maximum number of seconds that may pass
+        -- between two updates of this data source before the value of
+        -- the data source is assumed to be @*UNKNOWN*@.
+      , edsHeartbeat :: !NominalDiffTime
+        -- |'edsMin' and 'edsMax' Define the expected range values for
+        -- data supplied by a data source. If 'edsMin' and\/or 'edsMax'
         -- any value outside the defined range will be regarded as
-        -- @*UNKNOWN*@. If you do not know or care about 'dsMin' and
-        -- 'dsMax', set them to 'Nothing' for unknown. Note that
-        -- 'dsMin' and 'dsMax' always refer to the processed values of
+        -- @*UNKNOWN*@. If you do not know or care about 'edsMin' and
+        -- 'edsMax', set them to 'Nothing' for unknown. Note that
+        -- 'edsMin' and 'edsMax' always refer to the processed values of
         -- the DS. For a traffic-'COUNTER' type DS this would be the
         -- maximum and minimum data-rate expected from the device.
         --
@@ -76,62 +124,35 @@ data DataSource
         -- available, always set the min and\/or max properties. This
         -- will help RRDtool in doing a simple sanity check on the
         -- data supplied when running update.
-      , dsMin :: !(Maybe Double)
-        -- |See 'dsMin'.
-      , dsMax :: !(Maybe Double)
-    }
-    -- |COUNTER is for continuous incrementing counters like the
-    -- ifInOctets counter in a router. The COUNTER data source assumes
-    -- that the counter never decreases, except when a counter
-    -- overflows. The update function takes the overflow into
-    -- account. The counter is stored as a per-second rate. When the
-    -- counter overflows, RRDtool checks if the overflow happened at
-    -- the 32bit or 64bit border and acts accordingly by adding an
-    -- appropriate value to the result.
-    | COUNTER {
-        dsName      :: !String
-      , dsHeartbeat :: !NominalDiffTime
-      , dsMin       :: !(Maybe Double)
-      , dsMax       :: !(Maybe Double)
-    }
-    -- |DERIVE will store the derivative of the line going from the
-    -- last to the current value of the data source. This can be
-    -- useful for gauges, for example, to measure the rate of people
-    -- entering or leaving a room. Internally, derive works exactly
-    -- like COUNTER but without overflow checks. So if your counter
-    -- does not reset at 32 or 64 bit you might want to use DERIVE and
-    -- combine it with a 'dsMin' value of 0.
-    | DERIVE {
-        dsName      :: !String
-      , dsHeartbeat :: !NominalDiffTime
-      , dsMin       :: !(Maybe Double)
-      , dsMax       :: !(Maybe Double)
-    }
-    -- |ABSOLUTE is for counters which get reset upon reading. This is
-    -- used for fast counters which tend to overflow. So instead of
-    -- reading them normally you reset them after every read to make
-    -- sure you have a maximum time available before the next
-    -- overflow. Another usage is for things you count like number of
-    -- messages since the last update.
-    | ABSOLUTE {
-        dsName      :: !String
-      , dsHeartbeat :: !NominalDiffTime
-      , dsMin       :: !(Maybe Double)
-      , dsMax       :: !(Maybe Double)
-    }
-    -- |COMPUTE is for storing the result of a formula applied to
-    -- other data sources in the RRD. This data source is not supplied
-    -- a value on update, but rather its Primary Data Points (PDPs)
-    -- are computed from the PDPs of the data sources according to the
-    -- rpn-expression that defines the formula. Consolidation
-    -- functions are then applied normally to the PDPs of the COMPUTE
-    -- data source (that is the rpn-expression is only applied to
-    -- generate PDPs). In database software, such data sets are
-    -- referred to as \"virtual\" or \"computed\" columns.
-    --
-    -- FIXME: doc links
-    | forall a. IsCommonExpr a => COMPUTE {
-        dsName :: !String
+      , edsMin :: !(Maybe Double)
+        -- |See 'edsMin'.
+      , edsMax :: !(Maybe Double)
+      }
+    deriving (Show, Eq, Ord)
+
+instance ( IsVarName vn ~ True
+         )
+    => DataSource (ExternalDataSource vn)
+    where
+      type DSName (ExternalDataSource vn) = vn
+
+type instance MentionedVars (ExternalDataSource vn) = Nil
+
+-- |ComputedDataSource is for storing the result of a formula applied
+-- to other data sources in the RRD. This data source is not supplied
+-- a value on update, but rather its Primary Data Points (PDPs) are
+-- computed from the PDPs of the data sources according to the
+-- rpn-expression that defines the formula. Consolidation functions
+-- are then applied normally to the PDPs of the COMPUTE data source
+-- (that is the rpn-expression is only applied to generate PDPs). In
+-- database software, such data sets are referred to as \"virtual\" or
+-- \"computed\" columns.
+--
+-- FIXME: doc links
+data ComputedDataSource vn e
+    = ComputedDataSource {
+        -- |See 'edsName'
+        cdsName :: !vn
         -- |rpn-expression defines the formula used to compute the
         -- PDPs of a COMPUTE data source from other data sources in
         -- the same \<RRD\>. It is similar to defining a CDEF argument
@@ -145,33 +166,120 @@ data DataSource
         -- graph command.
         -- 
         -- FIXME: doc links
-      , dsExpr :: !a
+      , cdsExpr :: !e
     }
+    deriving (Show, Eq, Ord)
 
-dsTest :: DataSource
-dsTest = COMPUTE {
-           dsName = "foo"
---         , dsExpr = Previous :<: Const 100
---         , dsExpr = Var "foo" :<: Const 100
-           , dsExpr = AverageOf (Const 100 .*. Const 200 .*. HNil)
+instance ( IsVarName vn ~ True
+         , IsCommonExpr e ~ True
+         )
+    => DataSource (ComputedDataSource vn e)
+    where
+      type DSName (ComputedDataSource vn e) = vn
+
+type instance MentionedVars (ComputedDataSource vn e) = MentionedVars e
+
+{-
+dsTest = ComputedDataSource {
+           cdsName = "foo"
+--         , cdsExpr = Previous :<: Const 100
+--         , cdsExpr = Var "foo" :<: Const 100
+         , cdsExpr = AverageOf (Const 100 .*. Const 200 .*. Nil)
          }
+-}
+
+-- |The name of the RRD you want to create. RRD files should end with
+-- the extension @.rrd@. However, RRDtool will accept any filename.
+newtype RRDPath = RRDPath FilePath
+
+-- |Do not clobber an existing file of the same name.
+data KeepOldRRD = KeepOldRRD
+
+-- |Specifies the time in seconds since @1970-01-01 UTC@ when the
+-- first value should be added to the RRD. RRDtool will not accept any
+-- data timed before or at the time specified. (default: @now - 10s@)
+newtype RRDStartTime = RRDStartTime POSIXTime
+
+-- |Specifies the base interval in seconds with which data will be fed
+-- into the RRD. (default: 300 sec)
+newtype RRDInterval = RRDInterval NominalDiffTime
+
+class RRDSpec s
+instance ( Occurs    RRDPath        s
+         , OccursOpt KeepOldRRD     s
+         , OccursOpt RRDStartTime   s
+         , OccursOpt RRDInterval    s
+         , Occurs    RRDDataSources s
+         )
+    => RRDSpec s
+
+class NonEmptyDSList l
+instance ( DSList l
+         , DataSource d
+         )
+    => NonEmptyDSList (Cons d l)
+
+class DSList l
+instance DSList Nil
+instance ( DSList l
+         , DataSource d
+         )
+    => DSList (Cons d l)
+
+data RRDDataSources
+    = forall l.
+      ( NonEmptyDSList l
+      , Graph l -- FIXME: this constraint is too weak
+      )
+    => RRDDataSources l
+
+-- RRDDataSources is a graph.
+instance ( DSList g
+         , NodeSet g
+         , NoDuplicates (Map NodeIDA g)
+         )
+    => Graph g
+    where
+      type Empty g = Nil
+      type Nodes g = g
+
+type instance IsEmpty Nil        = True
+type instance IsEmpty (Cons e l) = False
+
+instance ( NodeIDSet (MentionedVars d)
+         , DataSource d
+         )
+    => Node d
+    where
+      type NodeID d    = DSName d
+      type LinksFrom d = MentionedVars d
 
 -- |The 'createRRD' function lets you set up new Round Robin Database
 -- (RRD) files. The file is created at its final, full size and filled
 -- with @*UNKNOWN*@ data.
-createRRD
-    :: FilePath -- ^The name of the RRD you want to create. RRD files
-                -- should end with the extension @.rrd@. However,
-                -- RRDtool will accept any filename.
-    -> Bool -- ^Do not clobber an existing file of the same name.
-    -> Maybe POSIXTime -- ^Specifies the time in seconds since
-                       -- @1970-01-01 UTC@ when the first value should
-                       -- be added to the RRD. RRDtool will not accept
-                       -- any data timed before or at the time
-                       -- specified. (default: @now - 10s@)
-    -> Maybe NominalDiffTime -- ^Specifies the base interval in
-                             -- seconds with which data will be fed
-                             -- into the RRD. (default: 300 sec)
-    -> [DataSource] -- ^Data sources to accept input from.
-    -> IO ()
+createRRD :: ( RRDSpec s
+             )
+            => s -> IO ()
 createRRD = error "FIXME"
+
+testMain :: IO ()
+testMain = let s = RRDPath "test.rrd" .&.
+                   KeepOldRRD         .&.
+                   RRDDataSources testDSList .&.
+                   Nil
+           in
+             createRRD s
+
+testDSList = let a = ComputedDataSource {
+                       cdsName = [$hString|foo|]
+                     , cdsExpr = Var [$hString|bar|]
+                     }
+                 b = ComputedDataSource {
+                       cdsName = [$hString|bar|]
+                     , cdsExpr = Var [$hString|foo|] -- shouldn't typecheck!
+                     }
+                 c = ComputedDataSource {
+                       cdsName = [$hString|baz|]
+                     , cdsExpr = Var [$hString|foo|] -- should typecheck!
+                     }
+             in a .&. b .&. Nil
\ No newline at end of file