Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9fda48f589 | ||
|
|
319104641b | ||
|
|
9861266c1e | ||
|
|
0893862f3a |
No files matched your search
+9
-9
@@ -1,8 +1,8 @@
|
||||
{ mkDerivation, aeson, annotated-exception, async, base, bytestring
|
||||
, cereal, cereal-conduit, conduit, containers, directory, ekg-core
|
||||
, filepath, hedgehog, hspec, hspec-hedgehog, katip, lens
|
||||
, lens-aeson, lib, network, process, stm, text, time
|
||||
, unordered-containers, uuid, websockets
|
||||
, filepath, hedgehog, hspec, hspec-hedgehog, http-types, katip
|
||||
, lens, lens-aeson, lib, network, process, scotty, stm, text, time
|
||||
, unordered-containers, uuid, wai, warp, websockets
|
||||
}:
|
||||
mkDerivation {
|
||||
pname = "home-assistant-controller";
|
||||
@@ -12,15 +12,15 @@ mkDerivation {
|
||||
isExecutable = true;
|
||||
libraryHaskellDepends = [
|
||||
aeson annotated-exception async base bytestring cereal
|
||||
cereal-conduit conduit containers directory ekg-core filepath katip
|
||||
lens lens-aeson network process stm text time unordered-containers
|
||||
uuid websockets
|
||||
cereal-conduit conduit containers directory ekg-core filepath
|
||||
http-types katip lens lens-aeson network process scotty stm text
|
||||
time unordered-containers uuid wai warp websockets
|
||||
];
|
||||
executableHaskellDepends = [ base ];
|
||||
testHaskellDepends = [
|
||||
aeson annotated-exception async base cereal containers directory
|
||||
ekg-core hedgehog hspec hspec-hedgehog katip process stm text time
|
||||
unordered-containers uuid
|
||||
aeson annotated-exception async base bytestring cereal containers
|
||||
directory ekg-core hedgehog hspec hspec-hedgehog katip process stm
|
||||
text time unordered-containers uuid
|
||||
];
|
||||
license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause";
|
||||
mainProgram = "home-assistant-controller";
|
||||
|
||||
@@ -68,6 +68,7 @@ library
|
||||
, HomeAssistant.Runtime
|
||||
, HomeAssistant.Runtime.Bus
|
||||
, HomeAssistant.Runtime.Connection
|
||||
, HomeAssistant.Runtime.Graphing
|
||||
, HomeAssistant.Runtime.Metrics
|
||||
, HomeAssistant.Runtime.Supervisor
|
||||
|
||||
@@ -102,6 +103,10 @@ library
|
||||
, filepath
|
||||
, conduit
|
||||
, cereal-conduit
|
||||
, scotty
|
||||
, http-types
|
||||
, warp
|
||||
, wai
|
||||
|
||||
-- Directories containing source files.
|
||||
hs-source-dirs: src
|
||||
@@ -148,6 +153,7 @@ test-suite home-assistant-controller-test
|
||||
, BedroomSpec
|
||||
, BusSpec
|
||||
, ConnectionSpec
|
||||
, GraphingSpec
|
||||
, MetricsSpec
|
||||
, RuntimeSpec
|
||||
, SupervisorSpec
|
||||
@@ -169,6 +175,7 @@ test-suite home-assistant-controller-test
|
||||
build-depends:
|
||||
base ^>=4.20.2.0,
|
||||
home-assistant-controller,
|
||||
bytestring,
|
||||
hspec,
|
||||
stm,
|
||||
aeson,
|
||||
|
||||
@@ -36,7 +36,9 @@ import HomeAssistant.Controller.Children (schoolLightController)
|
||||
import Data.Maybe (fromMaybe)
|
||||
import qualified System.Metrics
|
||||
import qualified HomeAssistant.Runtime.Metrics
|
||||
import qualified HomeAssistant.Runtime.Graphing
|
||||
import System.FilePath ((</>))
|
||||
import Text.Read (readMaybe)
|
||||
import HomeAssistant.Controller.Kitchen (kitchenMotionController)
|
||||
|
||||
step :: (MonadIO m) => FilePath -> UUID -> Auto m a b -> a -> m (b, Auto m a b)
|
||||
@@ -79,6 +81,15 @@ runController rootDir bus (Controller name machine _enabled) = do
|
||||
(_, next) <- step path uuid f msg
|
||||
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 = withSocketsDo $ do
|
||||
severity <- maybe InfoS (const DebugS) <$> lookupEnv "HA_DEBUG"
|
||||
@@ -91,13 +102,16 @@ defaultMain = withSocketsDo $ do
|
||||
rootPath <- fromMaybe "/tmp/" <$> lookupEnv "HA_LIB_DIR"
|
||||
rrdPath <- fromMaybe "hass-controller.rrd" <$> lookupEnv "HA_RRD_PATH"
|
||||
rrdtool <- fromMaybe "rrdtool" <$> lookupEnv "HA_RRDTOOL"
|
||||
metricsPort <- lookupPort
|
||||
let active = [c | c@(Controller _ _ True) <- controllers]
|
||||
ents = foldMap (\(Controller _ m _) -> entities m) active
|
||||
workers =
|
||||
[ ("reader", readerAction host 8123 token ents bus)
|
||||
, ("writer", writerAction bus)
|
||||
] ++ [ (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
|
||||
(_, v) <- waitAny as
|
||||
absurd v
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
@@ -7,6 +7,7 @@ import qualified BackoffProp
|
||||
import qualified BedroomSpec
|
||||
import qualified BusSpec
|
||||
import qualified ConnectionSpec
|
||||
import qualified GraphingSpec
|
||||
import qualified MetricsSpec
|
||||
import qualified RuntimeSpec
|
||||
import qualified SupervisorSpec
|
||||
@@ -18,6 +19,7 @@ main = hspec $ do
|
||||
BedroomSpec.spec
|
||||
BusSpec.spec
|
||||
ConnectionSpec.spec
|
||||
GraphingSpec.spec
|
||||
MetricsSpec.spec
|
||||
RuntimeSpec.spec
|
||||
SupervisorSpec.spec
|
||||
|
||||
Reference in new issue
Block a user