12 Commits
14 changed files with 557 additions and 35 deletions

No files matched your search

+9 -9
View File
@@ -1,8 +1,8 @@
{ mkDerivation, aeson, annotated-exception, async, base, bytestring { mkDerivation, aeson, annotated-exception, async, base, bytestring
, cereal, cereal-conduit, conduit, containers, directory, ekg-core , cereal, cereal-conduit, conduit, containers, directory, ekg-core
, filepath, hedgehog, hspec, hspec-hedgehog, katip, lens , filepath, hedgehog, hspec, hspec-hedgehog, http-types, katip
, lens-aeson, lib, network, process, stm, text, time , lens, lens-aeson, lib, network, process, scotty, stm, text, time
, unordered-containers, uuid, websockets , unordered-containers, uuid, wai, warp, websockets
}: }:
mkDerivation { mkDerivation {
pname = "home-assistant-controller"; pname = "home-assistant-controller";
@@ -12,15 +12,15 @@ mkDerivation {
isExecutable = true; isExecutable = true;
libraryHaskellDepends = [ libraryHaskellDepends = [
aeson annotated-exception async base bytestring cereal aeson annotated-exception async base bytestring cereal
cereal-conduit conduit containers directory ekg-core filepath katip cereal-conduit conduit containers directory ekg-core filepath
lens lens-aeson network process stm text time unordered-containers http-types katip lens lens-aeson network process scotty stm text
uuid websockets time unordered-containers uuid wai warp websockets
]; ];
executableHaskellDepends = [ base ]; executableHaskellDepends = [ base ];
testHaskellDepends = [ testHaskellDepends = [
aeson annotated-exception async base cereal containers directory aeson annotated-exception async base bytestring cereal containers
ekg-core hedgehog hspec hspec-hedgehog katip process stm text time directory ekg-core hedgehog hspec hspec-hedgehog katip process stm
unordered-containers uuid text time unordered-containers uuid
]; ];
license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause"; license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause";
mainProgram = "home-assistant-controller"; mainProgram = "home-assistant-controller";
+7
View File
@@ -68,6 +68,7 @@ library
, HomeAssistant.Runtime , HomeAssistant.Runtime
, HomeAssistant.Runtime.Bus , HomeAssistant.Runtime.Bus
, HomeAssistant.Runtime.Connection , HomeAssistant.Runtime.Connection
, HomeAssistant.Runtime.Graphing
, HomeAssistant.Runtime.Metrics , HomeAssistant.Runtime.Metrics
, HomeAssistant.Runtime.Supervisor , HomeAssistant.Runtime.Supervisor
@@ -102,6 +103,10 @@ library
, filepath , filepath
, conduit , conduit
, cereal-conduit , cereal-conduit
, scotty
, http-types
, warp
, wai
-- Directories containing source files. -- Directories containing source files.
hs-source-dirs: src hs-source-dirs: src
@@ -148,6 +153,7 @@ test-suite home-assistant-controller-test
, BedroomSpec , BedroomSpec
, BusSpec , BusSpec
, ConnectionSpec , ConnectionSpec
, GraphingSpec
, MetricsSpec , MetricsSpec
, RuntimeSpec , RuntimeSpec
, SupervisorSpec , SupervisorSpec
@@ -169,6 +175,7 @@ test-suite home-assistant-controller-test
build-depends: build-depends:
base ^>=4.20.2.0, base ^>=4.20.2.0,
home-assistant-controller, home-assistant-controller,
bytestring,
hspec, hspec,
stm, stm,
aeson, aeson,
+29 -2
View File
@@ -45,8 +45,8 @@ eventLights = AFRP.changes >>> arr (fmap presenceLights)
presenceLights Occupied = LightsOn presenceLights Occupied = LightsOn
presenceLights Unoccupied = LightsOff presenceLights Unoccupied = LightsOff
kitchenMotionController :: HASS (Event Value) () kitchenCeilingController :: HASS (Event Value) ()
kitchenMotionController = proc x -> do kitchenCeilingController = proc x -> do
now <- AFRP.currentTime -< () now <- AFRP.currentTime -< ()
p <- kitchenMotion >>> kitchenPresence -< x p <- kitchenMotion >>> kitchenPresence -< x
ev <- eventLights -< p ev <- eventLights -< p
@@ -57,3 +57,30 @@ kitchenMotionController = proc x -> do
_ -> returnA -< () _ -> returnA -< ()
where where
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0) lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
dinnerLightLevel :: HASS (Event Value) Int
dinnerLightLevel = entityRead "sensor.sijainti_keittio_light_level" >>> AFRP.hold 0
dinnerPresence :: HASS (Event Value) Presence
dinnerPresence = presence "binary_sensor.sijainti_keittio_occupancy" >>> AFRP.hold Unoccupied
dinnerLightEvents :: HASS (Event Value) (Event Lights)
dinnerLightEvents = ((,) <$> dinnerLightLevel <*> dinnerPresence)
>>> arr lightWanted
>>> AFRP.changes
>>> arr (fmap (bool LightsOff LightsOn))
where
-- This means that both light level and occupancy can change the light state
lightWanted (lightLevel, occ) = occ == Occupied && lightLevel < 13
dinnerTableController :: HASS (Event Value) ()
dinnerTableController = proc x -> do
ev <- dinnerLightEvents -< x
case ev of
Event LightsOn -> callServiceDyn (light [EntityId "light.kitchen_dining_large"]) -< On Nothing
Event LightsOff -> callServiceDyn (light [EntityId "light.kitchen_dining_large"]) -< Off
_ -> returnA -< ()
kitchenMotionController :: HASS (Event Value) ()
kitchenMotionController = kitchenCeilingController <> dinnerTableController
+20 -5
View File
@@ -36,7 +36,9 @@ import HomeAssistant.Controller.Children (schoolLightController)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import qualified System.Metrics import qualified System.Metrics
import qualified HomeAssistant.Runtime.Metrics import qualified HomeAssistant.Runtime.Metrics
import qualified HomeAssistant.Runtime.Graphing
import System.FilePath ((</>)) import System.FilePath ((</>))
import Text.Read (readMaybe)
import HomeAssistant.Controller.Kitchen (kitchenMotionController) import HomeAssistant.Controller.Kitchen (kitchenMotionController)
step :: (MonadIO m) => FilePath -> UUID -> Auto m a b -> a -> m (b, Auto m a b) step :: (MonadIO m) => FilePath -> UUID -> Auto m a b -> a -> m (b, Auto m a b)
@@ -51,7 +53,7 @@ data Controller = forall b. Controller T.Text (HASS (Event Value) b) Bool
controllers :: [Controller] controllers :: [Controller]
controllers = controllers =
[ Controller "bedroom-presence" bedroomPresenceController False [ Controller "bedroom-presence" bedroomPresenceController False
, Controller "bedroom-button" bedroomButtonController False -- This works but leaving for vacation , Controller "bedroom-button" bedroomButtonController True
, Controller "bedroom-drawer" bedroomDrawerController True , Controller "bedroom-drawer" bedroomDrawerController True
, Controller "bedroom-humidifier" humidifierController True , Controller "bedroom-humidifier" humidifierController True
, Controller "ruuvi-controller" ruuviController False , Controller "ruuvi-controller" ruuviController False
@@ -79,24 +81,37 @@ runController rootDir bus (Controller name machine _enabled) = do
(_, next) <- step path uuid f msg (_, next) <- step path uuid f msg
go path inbound next go path inbound next
lookupPort :: IO Int
lookupPort = do
m <- lookupEnv "HA_METRICS_PORT"
case m of
Nothing -> pure 8124
Just s -> case readMaybe s of
Just p | p >= 1 && p <= 65535 -> pure p
_ -> fail ("HA_METRICS_PORT must be a port in 1..65535: " <> s)
defaultMain :: IO () defaultMain :: IO ()
defaultMain = withSocketsDo $ do defaultMain = withSocketsDo $ do
severity <- maybe InfoS (const DebugS) <$> lookupEnv "HA_DEBUG" severity <- maybe InfoS (const DebugS) <$> lookupEnv "HA_DEBUG"
withBus severity $ \bus -> do store <- System.Metrics.newStore
System.Metrics.registerGcMetrics store
appMetrics <- HomeAssistant.Runtime.Metrics.registerAppMetrics store
withBus severity appMetrics $ \bus -> do
token <- getEnv "HA_TOKEN" token <- getEnv "HA_TOKEN"
host <- getEnv "HA_HOST" host <- getEnv "HA_HOST"
rootPath <- fromMaybe "/tmp/" <$> lookupEnv "HA_LIB_DIR" rootPath <- fromMaybe "/tmp/" <$> lookupEnv "HA_LIB_DIR"
store <- System.Metrics.newStore
System.Metrics.registerGcMetrics store
rrdPath <- fromMaybe "hass-controller.rrd" <$> lookupEnv "HA_RRD_PATH" rrdPath <- fromMaybe "hass-controller.rrd" <$> lookupEnv "HA_RRD_PATH"
rrdtool <- fromMaybe "rrdtool" <$> lookupEnv "HA_RRDTOOL" rrdtool <- fromMaybe "rrdtool" <$> lookupEnv "HA_RRDTOOL"
metricsPort <- lookupPort
let active = [c | c@(Controller _ _ True) <- controllers] let active = [c | c@(Controller _ _ True) <- controllers]
ents = foldMap (\(Controller _ m _) -> entities m) active ents = foldMap (\(Controller _ m _) -> entities m) active
workers = workers =
[ ("reader", readerAction host 8123 token ents bus) [ ("reader", readerAction host 8123 token ents bus)
, ("writer", writerAction bus) , ("writer", writerAction bus)
] ++ [ (name, runController rootPath bus c) | c@(Controller name _ True) <- controllers ] ] ++ [ (name, runController rootPath bus c) | c@(Controller name _ True) <- controllers ]
++ [("metrics", HomeAssistant.Runtime.Metrics.metricsAction store rrdPath rrdtool)] ++ [ ("metrics", HomeAssistant.Runtime.Metrics.metricsAction store rrdPath rrdtool)
, ("metrics-http", HomeAssistant.Runtime.Graphing.graphAction rrdPath rrdtool metricsPort)
]
as <- mapM (\(name, act) -> async (supervised name defaultBackoff act)) workers as <- mapM (\(name, act) -> async (supervised name defaultBackoff act)) workers
(_, v) <- waitAny as (_, v) <- waitAny as
absurd v absurd v
+14 -2
View File
@@ -6,6 +6,8 @@ module HomeAssistant.Runtime.Bus
, CallIdGen(..) , CallIdGen(..)
, mkCallIdGen , mkCallIdGen
, withBus , withBus
, recordInbound
, recordOutbound
, channelHassEval , channelHassEval
) where ) where
@@ -21,10 +23,12 @@ import Control.Concurrent.STM
import Data.Aeson (Value) import Data.Aeson (Value)
import Data.IORef (atomicModifyIORef', newIORef) import Data.IORef (atomicModifyIORef', newIORef)
import HomeAssistant.Controller (HASSEff (..), Service) import HomeAssistant.Controller (HASSEff (..), Service)
import HomeAssistant.Runtime.Metrics (AppMetrics (..))
import Network.WebSockets (Connection) import Network.WebSockets (Connection)
import Katip (LogEnv, closeScribes, mkHandleScribe, ColorStrategy (..), permitItem, Severity (..), Verbosity (V2), registerScribe, defaultScribeSettings, initLogEnv, ls, sl, logFM, katipAddContext, KatipContext) import Katip (LogEnv, closeScribes, mkHandleScribe, ColorStrategy (..), permitItem, Severity (..), Verbosity (V2), registerScribe, defaultScribeSettings, initLogEnv, ls, sl, logFM, katipAddContext, KatipContext)
import Control.Exception (bracket) import Control.Exception (bracket)
import System.IO (stdout) import System.IO (stdout)
import System.Metrics.Counter (inc)
import Data.UUID (toText) import Data.UUID (toText)
import AFRP (Request(..), Event(..)) import AFRP (Request(..), Event(..))
import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.IO.Class (MonadIO, liftIO)
@@ -38,10 +42,11 @@ data Bus = Bus
, busConn :: TVar (Maybe Connection) , busConn :: TVar (Maybe Connection)
, busGen :: CallIdGen , busGen :: CallIdGen
, busLogEnv :: LogEnv , busLogEnv :: LogEnv
, busMetrics :: AppMetrics
} }
withBus :: Severity -> (Bus -> IO a) -> IO a withBus :: Severity -> AppMetrics -> (Bus -> IO a) -> IO a
withBus severity callback = do withBus severity metrics callback = do
handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem severity) V2 handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem severity) V2
let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "hass-controller" "production" let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "hass-controller" "production"
-- closeScribes will stop accepting new logs, flush existing ones and clean up resources -- closeScribes will stop accepting new logs, flush existing ones and clean up resources
@@ -52,8 +57,15 @@ withBus severity callback = do
<*> newTVarIO Nothing <*> newTVarIO Nothing
<*> mkCallIdGen 0 <*> mkCallIdGen 0
<*> pure le <*> pure le
<*> pure metrics
callback bus callback bus
recordInbound :: Bus -> IO ()
recordInbound bus = inc (amTriggersIn (busMetrics bus))
recordOutbound :: Bus -> IO ()
recordOutbound bus = inc (amServicesOut (busMetrics bus))
channelHassEval :: (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a channelHassEval :: (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a
channelHassEval bus = \case channelHassEval bus = \case
CallService req svc -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ do CallService req svc -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ do
+9 -2
View File
@@ -6,6 +6,7 @@ module HomeAssistant.Runtime.Connection
, writerAction , writerAction
, encodeService , encodeService
, dedupeBatch , dedupeBatch
, isTriggerEvent
) where ) where
import Control.Concurrent.STM import Control.Concurrent.STM
@@ -23,7 +24,7 @@ import Control.Concurrent (threadDelay)
import Control.Exception (onException) import Control.Exception (onException)
import Control.Exception.Annotated (throw) import Control.Exception.Annotated (throw)
import Control.Lens ((^?)) import Control.Lens ((^?))
import Control.Monad (forever, forM_) import Control.Monad (forever, forM_, when)
import Data.Aeson (Value, eitherDecode, encode, object, (.=)) import Data.Aeson (Value, eitherDecode, encode, object, (.=))
import Data.Aeson.Lens (key, _String) import Data.Aeson.Lens (key, _String)
import Data.List (sort) import Data.List (sort)
@@ -69,6 +70,9 @@ expectType expected msg =
Just t | t == expected -> pure () Just t | t == expected -> pure ()
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg)) _ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
isTriggerEvent :: Value -> Bool
isTriggerEvent v = v ^? key "type" . _String == Just "event"
subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO () subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO ()
subscribe bus conn ents = subscribe bus conn ents =
forM_ (S.toList ents) $ \entityId -> do forM_ (S.toList ents) $ \entityId -> do
@@ -96,7 +100,9 @@ receiveLoop bus conn = forever $ do
Left () -> atomically $ writeTChan (busInbound bus) Tick Left () -> atomically $ writeTChan (busInbound bus) Tick
Right msg -> case eitherDecode msg of Right msg -> case eitherDecode msg of
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
Right v -> atomically $ writeTChan (busInbound bus) (Event v) Right v -> do
when (isTriggerEvent v) (recordInbound bus)
atomically $ writeTChan (busInbound bus) (Event v)
receiveJSON :: WS.Connection -> IO Value receiveJSON :: WS.Connection -> IO Value
receiveJSON conn = do receiveJSON conn = do
@@ -128,6 +134,7 @@ sendWithId bus conn request svc = do
let textData = encode $ encodeService callId svc let textData = encode $ encodeService callId svc
runKatipContextT (busLogEnv bus) (sl "traceId" (toText (requestTraceId request))) "connection" $ runKatipContextT (busLogEnv bus) (sl "traceId" (toText (requestTraceId request))) "connection" $
logFM DebugS (ls textData) logFM DebugS (ls textData)
recordOutbound bus
WS.sendTextData conn textData WS.sendTextData conn textData
writerAction :: Bus -> IO Void writerAction :: Bus -> IO Void
+163
View File
@@ -0,0 +1,163 @@
{-# LANGUAGE OverloadedStrings #-}
module HomeAssistant.Runtime.Graphing
( Range (..)
, parseRange
, rangeStart
, Series (..)
, GraphSpec (..)
, graphs
, lookupGraph
, parseNameParam
, buildGraphArgs
, readProcessBytes
, graphAction
) where
import Control.Exception (evaluate)
import Control.Monad (forever)
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Void (Void)
import Network.HTTP.Types.Status (Status, status400, status404, status500)
import Network.Wai.Handler.Warp (defaultSettings, setHost, setPort)
import System.Exit (ExitCode (..))
import System.IO (hGetContents)
import System.Process (CreateProcess (..), StdStream (..), proc, waitForProcess, withCreateProcess)
import Web.Scotty
( ActionM
, ScottyM
, defaultOptions
, get
, pathParam
, queryParamMaybe
, raw
, scottyOpts
, setHeader
, settings
, status
, text
)
data Range = TenMinutes | OneHour | OneDay | OneWeek | OneMonth
deriving (Eq, Show, Bounded, Enum)
rangeSuffix :: Range -> String
rangeSuffix TenMinutes = "10m"
rangeSuffix OneHour = "1h"
rangeSuffix OneDay = "24h"
rangeSuffix OneWeek = "7d"
rangeSuffix OneMonth = "30d"
parseRange :: Text -> Maybe Range
parseRange t = lookup t [(T.pack (rangeSuffix r), r) | r <- [minBound .. maxBound]]
rangeStart :: Range -> String
rangeStart r = "end-" ++ rangeSuffix r
data Series = Series
{ serDs :: String
, serAlias :: String
, serColor :: String
, serLabel :: String
}
deriving (Eq, Show)
data GraphSpec = GraphSpec
{ gsName :: Text
, gsTitle :: String
, gsSeries :: [Series]
}
deriving (Eq, Show)
graphs :: [GraphSpec]
graphs =
[ GraphSpec
"traffic"
"Hass triggers in / services out"
[ Series "hass_trigger_in" "trig" "#0066cc" "triggers in/min"
, Series "hass_service_out" "svc" "#cc0000" "services out/min"
]
]
lookupGraph :: [GraphSpec] -> Text -> Maybe GraphSpec
lookupGraph specs name = case [g | g <- specs, gsName g == name] of
(g : _) -> Just g
[] -> Nothing
parseNameParam :: [GraphSpec] -> Text -> Maybe GraphSpec
parseNameParam specs t = T.stripSuffix ".png" t >>= lookupGraph specs
buildGraphArgs :: FilePath -> GraphSpec -> Range -> [String]
buildGraphArgs rrdPath spec range =
[ "graph"
, "-"
, "--start", rangeStart range
, "--title", gsTitle spec
, "--width", "800"
, "--height", "250"
, "--imgformat", "PNG"
]
++ concatMap defArgs (gsSeries spec)
++ map line (gsSeries spec)
where
defArgs (Series ds alias _ _) =
[ "DEF:" ++ alias ++ "=" ++ rrdPath ++ ":" ++ ds ++ ":AVERAGE"
, "CDEF:" ++ alias ++ "_min=" ++ alias ++ ",60,*"
]
line (Series _ alias color label) =
"LINE2:" ++ alias ++ "_min" ++ color ++ ":" ++ label
readProcessBytes :: FilePath -> [String] -> IO (ExitCode, BS.ByteString, String)
readProcessBytes cmd args =
withCreateProcess (proc cmd args) { std_out = CreatePipe, std_err = CreatePipe } $
\_ mout merr ph ->
case (mout, merr) of
(Just hout, Just herr) -> do
out <- BS.hGetContents hout
err <- hGetContents herr
code <- waitForProcess ph
_ <- evaluate (length err)
pure (code, out, err)
_ -> pure (ExitFailure 1, BS.empty, "failed to create pipes")
respond :: Status -> TL.Text -> ActionM ()
respond st body = status st >> text body
serve :: FilePath -> FilePath -> GraphSpec -> Range -> ActionM ()
serve rrdPath rrdtool spec range = do
(code, out, err) <- liftIO (readProcessBytes rrdtool (buildGraphArgs rrdPath spec range))
case code of
ExitSuccess
| not (BS.null out) -> do
setHeader "Content-Type" "image/png"
raw (BL.fromStrict out)
| otherwise -> respond status500 "empty graph"
_ -> respond status500 (TL.pack (take 200 err))
handler :: FilePath -> FilePath -> ActionM ()
handler rrdPath rrdtool = do
name <- pathParam "name"
case parseNameParam graphs name of
Nothing -> respond status404 "unknown graph"
Just spec -> do
rangeArg <- queryParamMaybe "range"
case rangeArg of
Nothing -> serve rrdPath rrdtool spec OneDay
Just t -> case parseRange t of
Nothing -> respond status400 "invalid range"
Just range -> serve rrdPath rrdtool spec range
app :: FilePath -> FilePath -> ScottyM ()
app rrdPath rrdtool = get "/metrics/:name" (handler rrdPath rrdtool)
graphAction :: FilePath -> FilePath -> Int -> IO Void
graphAction rrdPath rrdtool port = forever $ scottyOpts opts (app rrdPath rrdtool)
where
opts = defaultOptions
{ settings = setHost "127.0.0.1" (setPort port defaultSettings)
}
+65 -7
View File
@@ -8,6 +8,10 @@ module HomeAssistant.Runtime.Metrics
, buildSchema , buildSchema
, buildCreateArgs , buildCreateArgs
, buildUpdateArgs , buildUpdateArgs
, parseInfoDs
, schemaMatches
, AppMetrics (..)
, registerAppMetrics
, ensureRrd , ensureRrd
, sampleAndUpdate , sampleAndUpdate
, metricsAction , metricsAction
@@ -17,19 +21,23 @@ import Control.Concurrent (threadDelay)
import Control.Monad (forever, unless) import Control.Monad (forever, unless)
import Data.Char (ord) import Data.Char (ord)
import Data.Int (Int64) import Data.Int (Int64)
import Data.List (intercalate, sortBy) import Data.List (intercalate, sort, sortBy, stripPrefix)
import Data.Maybe (mapMaybe)
import Data.Ord (comparing) import Data.Ord (comparing)
import Data.Text (Text) import Data.Text (Text)
import Data.Time (defaultTimeLocale, formatTime, getCurrentTime)
import Data.Void (Void) import Data.Void (Void)
import Numeric (showHex) import Numeric (showHex)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.HashMap.Strict as HM import qualified Data.HashMap.Strict as HM
import qualified System.Metrics as M (Value (..), Sample, Store, sampleAll) import qualified System.Metrics as M (Value (..), Sample, Store, createCounter, sampleAll)
import System.Directory (doesFileExist) import System.Metrics.Counter (Counter)
import System.Process (callProcess) import System.Directory (doesFileExist, renameFile)
import System.Exit (ExitCode (..))
import System.Process (callProcess, readProcessWithExitCode)
data DsType = Derive | Gauge data DsType = Derive | Gauge
deriving (Eq, Show) deriving (Eq, Ord, Show)
data DsSpec = DsSpec data DsSpec = DsSpec
{ dsEkgName :: Text { dsEkgName :: Text
@@ -62,6 +70,17 @@ buildSchema sample =
, Just dt <- [dsTypeOf val] , Just dt <- [dsTypeOf val]
] ]
data AppMetrics = AppMetrics
{ amTriggersIn :: Counter
, amServicesOut :: Counter
}
registerAppMetrics :: M.Store -> IO AppMetrics
registerAppMetrics store =
AppMetrics
<$> M.createCounter "hass.trigger.in" store
<*> M.createCounter "hass.service.out" store
buildCreateArgs :: FilePath -> Int -> [(String, DsType)] -> [String] buildCreateArgs :: FilePath -> Int -> [(String, DsType)] -> [String]
buildCreateArgs path step specs = buildCreateArgs path step specs =
["create", path, "--step", show step] ["create", path, "--step", show step]
@@ -89,18 +108,57 @@ buildUpdateArgs path names values =
renderValue Nothing = "U" renderValue Nothing = "U"
renderValue (Just n) = show n renderValue (Just n) = show n
dsTypeFromString :: String -> Maybe DsType
dsTypeFromString "DERIVE" = Just Derive
dsTypeFromString "GAUGE" = Just Gauge
dsTypeFromString _ = Nothing
parseInfoDs :: String -> [(String, DsType)]
parseInfoDs = mapMaybe parseLine . lines
where
parseLine line = do
rest0 <- stripPrefix "ds[" line
let (name, rest1) = break (== ']') rest0
rest2 <- stripPrefix "].type = \"" rest1
let (ty, _) = break (== '"') rest2
dt <- dsTypeFromString ty
pure (name, dt)
schemaMatches :: [DsSpec] -> [(String, DsType)] -> Bool
schemaMatches schema existing =
sort (map (\s -> (dsName s, dsType s)) schema) == sort existing
lookupValue :: M.Sample -> Text -> Maybe Int64 lookupValue :: M.Sample -> Text -> Maybe Int64
lookupValue sample name = case HM.lookup name sample of lookupValue sample name = case HM.lookup name sample of
Just (M.Counter n) -> Just n Just (M.Counter n) -> Just n
Just (M.Gauge n) -> Just n Just (M.Gauge n) -> Just n
_ -> Nothing _ -> Nothing
readExistingDs :: FilePath -> FilePath -> IO [(String, DsType)]
readExistingDs rrdtool rrdPath = do
(code, out, _) <- readProcessWithExitCode rrdtool ["info", rrdPath] ""
pure $ case code of
ExitSuccess -> parseInfoDs out
_ -> []
backupPath :: FilePath -> IO FilePath
backupPath path = do
now <- getCurrentTime
pure $ path ++ ".bak-" ++ formatTime defaultTimeLocale "%Y%m%dT%H%M%S" now
ensureRrd :: FilePath -> FilePath -> [DsSpec] -> IO () ensureRrd :: FilePath -> FilePath -> [DsSpec] -> IO ()
ensureRrd rrdtool rrdPath schema = do ensureRrd rrdtool rrdPath schema = do
exists <- doesFileExist rrdPath exists <- doesFileExist rrdPath
unless exists $ if not exists
callProcess rrdtool (buildCreateArgs rrdPath 10 (map toPair schema)) then create
else do
current <- readExistingDs rrdtool rrdPath
unless (schemaMatches schema current) $ do
backup <- backupPath rrdPath
renameFile rrdPath backup
create
where where
create = callProcess rrdtool (buildCreateArgs rrdPath 10 (map toPair schema))
toPair s = (dsName s, dsType s) toPair s = (dsName s, dsType s)
sampleAndUpdate :: M.Store -> FilePath -> FilePath -> [DsSpec] -> IO () sampleAndUpdate :: M.Store -> FilePath -> FilePath -> [DsSpec] -> IO ()
+23 -2
View File
@@ -10,16 +10,19 @@ import Control.Concurrent.STM
, writeTChan , writeTChan
) )
import Data.Aeson (Value (..)) import Data.Aeson (Value (..))
import qualified Data.HashMap.Strict as HM
import Data.Time (UTCTime (..), utc) import Data.Time (UTCTime (..), utc)
import Data.UUID (nil) import Data.UUID (nil)
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..)) import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
import HomeAssistant.Runtime.Bus import HomeAssistant.Runtime.Bus
import HomeAssistant.Runtime.Metrics (registerAppMetrics)
import Katip (Namespace (Namespace), runKatipContextT, Severity (..)) import Katip (Namespace (Namespace), runKatipContextT, Severity (..))
import qualified System.Metrics as Metrics
import Test.Hspec import Test.Hspec
spec :: Spec spec :: Spec
spec = describe "Bus" $ do spec = describe "Bus" $ do
it "broadcasts inbound messages to every dup'd channel in order" $ withBus InfoS $ \bus -> do it "broadcasts inbound messages to every dup'd channel in order" $ withTestBus $ \bus -> do
p1 <- atomically $ dupTChan (busInbound bus) p1 <- atomically $ dupTChan (busInbound bus)
p2 <- atomically $ dupTChan (busInbound bus) p2 <- atomically $ dupTChan (busInbound bus)
atomically $ writeTChan (busInbound bus) (Event (Number 1)) atomically $ writeTChan (busInbound bus) (Event (Number 1))
@@ -29,7 +32,7 @@ spec = describe "Bus" $ do
r1 `shouldBe` (Event (Number 1), Event (Number 2)) r1 `shouldBe` (Event (Number 1), Event (Number 2))
r2 `shouldBe` (Event (Number 1), Event (Number 2)) r2 `shouldBe` (Event (Number 1), Event (Number 2))
it "channelHassEval writes CallService to the outbound channel" $ withBus InfoS $ \bus -> do it "channelHassEval writes CallService to the outbound channel" $ withTestBus $ \bus -> do
let req = Request (UTCTime (toEnum 0) 0) utc nil let req = Request (UTCTime (toEnum 0) 0) utc nil
svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"] svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"]
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $ runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
@@ -43,3 +46,21 @@ spec = describe "Bus" $ do
a <- generateCallId gen a <- generateCallId gen
b <- generateCallId gen b <- generateCallId gen
(a, b) `shouldBe` (1, 2) (a, b) `shouldBe` (1, 2)
describe "recordInbound / recordOutbound" $ do
it "increments the trigger and service counters" $ do
store <- Metrics.newStore
m <- registerAppMetrics store
withBus InfoS m $ \bus -> do
recordInbound bus
recordInbound bus
recordOutbound bus
sample <- Metrics.sampleAll store
HM.lookup "hass.trigger.in" sample `shouldBe` Just (Metrics.Counter 2)
HM.lookup "hass.service.out" sample `shouldBe` Just (Metrics.Counter 1)
withTestBus :: (Bus -> IO a) -> IO a
withTestBus action = do
store <- Metrics.newStore
m <- registerAppMetrics store
withBus InfoS m action
+9 -1
View File
@@ -9,7 +9,7 @@ import Data.Text (Text)
import Data.Time (UTCTime (..), utc) import Data.Time (UTCTime (..), utc)
import Data.UUID (fromString) import Data.UUID (fromString)
import HomeAssistant.Controller (Service (..), Target(..)) import HomeAssistant.Controller (Service (..), Target(..))
import HomeAssistant.Runtime.Connection (encodeService, dedupeBatch) import HomeAssistant.Runtime.Connection (encodeService, dedupeBatch, isTriggerEvent)
import Test.Hspec import Test.Hspec
spec :: Spec spec :: Spec
@@ -36,6 +36,14 @@ spec = do
, "service_data" .= object ["brightness" .= (200 :: Int)] , "service_data" .= object ["brightness" .= (200 :: Int)]
] ]
describe "isTriggerEvent" $ do
it "is true for event frames" $
isTriggerEvent (object ["type" .= ("event" :: Text)]) `shouldBe` True
it "is false for result frames" $
isTriggerEvent (object ["type" .= ("result" :: Text)]) `shouldBe` False
it "is false when there is no type" $
isTriggerEvent (object ["id" .= (1 :: Int)]) `shouldBe` False
describe "dedupeBatch" $ do describe "dedupeBatch" $ do
it "collapses identical calls to one" $ it "collapses identical calls to one" $
let batch = [ (req 1, lightOn [AreaId "x"]) let batch = [ (req 1, lightOn [AreaId "x"])
+126
View File
@@ -0,0 +1,126 @@
{-# LANGUAGE OverloadedStrings #-}
module GraphingSpec (spec) where
import Control.Exception (SomeException, finally, try)
import qualified Data.ByteString as BS
import HomeAssistant.Runtime.Graphing
( GraphSpec (..)
, Range (..)
, buildGraphArgs
, graphs
, lookupGraph
, parseNameParam
, parseRange
, rangeStart
, readProcessBytes
)
import qualified HomeAssistant.Runtime.Metrics as Metrics
import System.Directory (findExecutable, getTemporaryDirectory, removeFile)
import System.Exit (ExitCode (..))
import System.Process (readProcessWithExitCode)
import Test.Hspec
traffic :: GraphSpec
traffic = case lookupGraph graphs "traffic" of
Just s -> s
Nothing -> error "traffic graph is missing from the table"
spec :: Spec
spec = do
describe "parseRange" $ do
it "accepts every suffix in the closed set" $
map parseRange ["10m", "1h", "24h", "7d", "30d"]
`shouldBe` [ Just TenMinutes
, Just OneHour
, Just OneDay
, Just OneWeek
, Just OneMonth
]
it "rejects everything outside the closed set" $
map parseRange
[ ""
, "1h "
, " 1h"
, "1h; rm -rf /"
, "end-1h"
, "../../etc/passwd"
, "1y"
, "30m"
, "1H"
]
`shouldBe` replicate 9 Nothing
describe "rangeStart" $ do
it "renders rrdtool end- relative starts" $
map rangeStart [TenMinutes, OneHour, OneDay, OneWeek, OneMonth]
`shouldBe` ["end-10m", "end-1h", "end-24h", "end-7d", "end-30d"]
describe "parseNameParam" $ do
it "resolves the traffic graph only with a .png suffix" $ do
fmap gsName (parseNameParam graphs "traffic.png") `shouldBe` Just "traffic"
parseNameParam graphs "traffic" `shouldBe` Nothing
it "rejects unknown, malformed, and traversing names" $
map (parseNameParam graphs)
[ "unknown.png"
, "../traffic.png"
, "traffic.png.png"
, ""
, "nope"
]
`shouldBe` replicate 5 Nothing
describe "buildGraphArgs" $ do
it "builds the traffic argv for a range" $
buildGraphArgs "hass-controller.rrd" traffic OneHour
`shouldBe`
[ "graph"
, "-"
, "--start", "end-1h"
, "--title", "Hass triggers in / services out"
, "--width", "800"
, "--height", "250"
, "--imgformat", "PNG"
, "DEF:trig=hass-controller.rrd:hass_trigger_in:AVERAGE"
, "CDEF:trig_min=trig,60,*"
, "DEF:svc=hass-controller.rrd:hass_service_out:AVERAGE"
, "CDEF:svc_min=svc,60,*"
, "LINE2:trig_min#0066cc:triggers in/min"
, "LINE2:svc_min#cc0000:services out/min"
]
describe "end-to-end (rrdtool-gated)" $ do
it "renders a PNG for the traffic graph" $ do
mRrdtool <- findExecutable "rrdtool"
case mRrdtool of
Nothing -> pendingWith "rrdtool not on PATH"
Just rrdtool -> do
tmp <- getTemporaryDirectory
let rrdPath = tmp ++ "/hass-controller-graph-test.rrd"
cleanup = try (removeFile rrdPath) :: IO (Either SomeException ())
schema =
[ ("hass_trigger_in", Metrics.Derive)
, ("hass_service_out", Metrics.Derive)
]
pngMagic = BS.pack [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
_ <- cleanup
( do
(createCode, _, _) <-
readProcessWithExitCode rrdtool (Metrics.buildCreateArgs rrdPath 10 schema) ""
createCode `shouldBe` ExitSuccess
(updateCode, _, _) <-
readProcessWithExitCode
rrdtool
(Metrics.buildUpdateArgs
rrdPath
["hass_trigger_in", "hass_service_out"]
[Just 5, Just 3])
""
updateCode `shouldBe` ExitSuccess
(code, out, _) <- readProcessBytes rrdtool (buildGraphArgs rrdPath traffic OneHour)
code `shouldBe` ExitSuccess
out `shouldSatisfy` BS.isPrefixOf pngMagic
)
`finally` cleanup
+2
View File
@@ -7,6 +7,7 @@ import qualified BackoffProp
import qualified BedroomSpec import qualified BedroomSpec
import qualified BusSpec import qualified BusSpec
import qualified ConnectionSpec import qualified ConnectionSpec
import qualified GraphingSpec
import qualified MetricsSpec import qualified MetricsSpec
import qualified RuntimeSpec import qualified RuntimeSpec
import qualified SupervisorSpec import qualified SupervisorSpec
@@ -18,6 +19,7 @@ main = hspec $ do
BedroomSpec.spec BedroomSpec.spec
BusSpec.spec BusSpec.spec
ConnectionSpec.spec ConnectionSpec.spec
GraphingSpec.spec
MetricsSpec.spec MetricsSpec.spec
RuntimeSpec.spec RuntimeSpec.spec
SupervisorSpec.spec SupervisorSpec.spec
+80 -4
View File
@@ -4,8 +4,9 @@ module MetricsSpec (spec) where
import Data.HashMap.Strict (HashMap) import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM import qualified Data.HashMap.Strict as HM
import Data.Int (Int64) import Data.List (isPrefixOf)
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T
import HomeAssistant.Runtime.Metrics import HomeAssistant.Runtime.Metrics
( DsType (..) ( DsType (..)
, DsSpec (..) , DsSpec (..)
@@ -16,12 +17,16 @@ import HomeAssistant.Runtime.Metrics
, buildUpdateArgs , buildUpdateArgs
, ensureRrd , ensureRrd
, sampleAndUpdate , sampleAndUpdate
, metricsAction , parseInfoDs
, schemaMatches
, AppMetrics (..)
, registerAppMetrics
) )
import qualified System.Metrics as M (Value (..)) import qualified System.Metrics as M (Value (..))
import Test.Hspec import Test.Hspec
import Control.Exception (try, SomeException) import Control.Exception (try, SomeException, finally)
import System.Directory (findExecutable, getTemporaryDirectory, removeFile) import Control.Monad (forM_)
import System.Directory (findExecutable, getTemporaryDirectory, listDirectory, removeFile)
import System.Exit (ExitCode (ExitSuccess)) import System.Exit (ExitCode (ExitSuccess))
import System.Process (readProcessWithExitCode) import System.Process (readProcessWithExitCode)
import qualified System.Metrics as Metrics import qualified System.Metrics as Metrics
@@ -126,6 +131,49 @@ spec = do
, "N:U" , "N:U"
] ]
describe "parseInfoDs" $ do
it "extracts DS names and types from rrdtool info output" $
let info = unlines
[ "filename = \"test.rrd\""
, "step = 10"
, "ds[foo].index = 0"
, "ds[foo].type = \"DERIVE\""
, "ds[bar].type = \"GAUGE\""
, "rra[0].cf = \"AVERAGE\""
]
in parseInfoDs info `shouldBe` [("foo", Derive), ("bar", Gauge)]
it "ignores lines that are not ds type declarations" $
parseInfoDs "step = 10\nrra[0].cf = \"AVERAGE\"\n" `shouldBe` []
describe "schemaMatches" $ do
let ds name ty = DsSpec (T.pack name) name ty
it "is true for identical schemas regardless of order" $
schemaMatches [ds "b" Gauge, ds "a" Derive] [("a", Derive), ("b", Gauge)]
`shouldBe` True
it "is false when the derived schema has an added DS" $
schemaMatches [ds "a" Derive, ds "b" Gauge] [("a", Derive)]
`shouldBe` False
it "is false when the derived schema has a removed DS" $
schemaMatches [ds "a" Derive] [("a", Derive), ("b", Gauge)]
`shouldBe` False
it "is false when a DS type changed" $
schemaMatches [ds "a" Derive] [("a", Gauge)]
`shouldBe` False
describe "registerAppMetrics" $ do
it "registers both counters with the store" $ do
store <- Metrics.newStore
m <- registerAppMetrics store
Counter.inc (amTriggersIn m)
Counter.inc (amServicesOut m)
sample <- Metrics.sampleAll store
HM.lookup "hass.trigger.in" sample `shouldBe` Just (M.Counter 1)
HM.lookup "hass.service.out" sample `shouldBe` Just (M.Counter 1)
describe "end-to-end (rrdtool-gated)" $ do describe "end-to-end (rrdtool-gated)" $ do
it "creates an rrd, samples, and updates it" $ do it "creates an rrd, samples, and updates it" $ do
mRrdtool <- findExecutable "rrdtool" mRrdtool <- findExecutable "rrdtool"
@@ -148,3 +196,31 @@ spec = do
length out `shouldSatisfy` (> 0) length out `shouldSatisfy` (> 0)
_ <- try (removeFile rrdPath) :: IO (Either SomeException ()) _ <- try (removeFile rrdPath) :: IO (Either SomeException ())
return () return ()
it "backs up and recreates the rrd when the schema changes" $ do
mRrdtool <- findExecutable "rrdtool"
case mRrdtool of
Nothing -> pendingWith "rrdtool not on PATH"
Just rrdtool -> do
tmp <- getTemporaryDirectory
let rrdPath = tmp ++ "/hass-controller-schema-change-test.rrd"
bakPrefix = "hass-controller-schema-change-test.rrd.bak-"
base = [ DsSpec "a" "a" Derive, DsSpec "b" "b" Gauge ]
expanded = base ++ [ DsSpec "c" "c" Derive ]
clean = do
stale <- filter (isPrefixOf bakPrefix) <$> listDirectory tmp
forM_ (rrdPath : map (\f -> tmp ++ "/" ++ f) stale) $ \f ->
try (removeFile f) :: IO (Either SomeException ())
clean
( do
ensureRrd rrdtool rrdPath base
ensureRrd rrdtool rrdPath base
noBackups <- filter (isPrefixOf bakPrefix) <$> listDirectory tmp
noBackups `shouldBe` []
ensureRrd rrdtool rrdPath expanded
backups <- filter (isPrefixOf bakPrefix) <$> listDirectory tmp
length backups `shouldBe` 1
(rc, out, _) <- readProcessWithExitCode rrdtool ["info", rrdPath] ""
rc `shouldBe` ExitSuccess
out `shouldContain` "ds[c].type"
) `finally` clean
+1 -1
View File
@@ -11,7 +11,7 @@ spec = pure ()
-- --
-- spec :: Spec -- spec :: Spec
-- spec = describe "runController" $ do -- spec = describe "runController" $ do
-- it "feeds inbound events through the machine and forwards service calls" $ withBus InfoS $ \bus -> do -- it "feeds inbound events through the machine and forwards service calls" $ withBus severity appMetrics $ \bus -> do
-- _ <- async (runController bus (Controller "test" lightController True)) -- _ <- async (runController bus (Controller "test" lightController True))
-- putStrLn "Before the delay" -- putStrLn "Before the delay"
-- threadDelay 100000 -- let the controller dup its inbound channel -- threadDelay 100000 -- let the controller dup its inbound channel