Skip to content

Commit 3cfeddb

Browse files
committed
Ensures we generate the automation name based on the port we parse from the websocket URL vs. hardcoding it to 8080
1 parent 5ff960d commit 3cfeddb

File tree

6 files changed

+42
-32
lines changed

6 files changed

+42
-32
lines changed

generate-allure-site/app/Main.hs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE OverloadedStrings #-}
2-
31
module Main (main) where
42

53
import Control.Lens ((^?), to)
@@ -72,8 +70,8 @@ localizedTimestamp branch suite ts idx' = H.td $ do
7270
-- and I need to check the data or make this more robust. For now it's
7371
-- not worth the effort.
7472
--
75-
branchRunRowHtml :: String -> String -> String -> Int -> ByteString -> Maybe (Integer, [Html])
76-
branchRunRowHtml rootPrefix branch suite idx fileData = do
73+
branchRunHtml :: String -> String -> String -> Int -> ByteString -> Maybe (Integer, [Html])
74+
branchRunHtml rootPrefix branch suite idx fileData = do
7775
runUniqueId <- fileData ^? nth idx . key "runUniqueId" . _String
7876

7977
let
@@ -112,7 +110,7 @@ branchRunRowHtml rootPrefix branch suite idx fileData = do
112110
--
113111
-- Does the majority of the heavy lifting with stuffing branch rows
114112
-- into Html (tr) and extracting the most recent timestamp from each
115-
-- branch so we can compare later for sorting.
113+
-- branch -> suite run so we can compare later for sorting.
116114
--
117115
branchesRunsHtml :: String -> [String] -> IO [(Integer, String, [(String, [Html])])]
118116
branchesRunsHtml rootPrefix branches =
@@ -140,7 +138,7 @@ branchesRunsHtml rootPrefix branches =
140138
let
141139
(newTimestamp, branchSuiteRowHtml) =
142140
fromMaybe (0, []) $
143-
branchRunRowHtml rootPrefix branch suite idx fileData
141+
branchRunHtml rootPrefix branch suite idx fileData
144142
in
145143
(max newTimestamp maxTimestamp, branchSuiteHtml <> branchSuiteRowHtml)
146144

@@ -223,14 +221,6 @@ branchesPage branches = do
223221
H.h2 ! A.class_ "m-2 p-1" $ "automation-service - Branch Test Runs"
224222
branches
225223

226-
getRootPrefix :: IO String
227-
getRootPrefix = do
228-
args <- getArgs
229-
pure $
230-
case args of
231-
(rp:_) -> rp
232-
_ -> ""
233-
234224
--
235225
-- this is hacky test page generation code and I'm not being very
236226
-- careful about configuration, so keep in mind how many assumptions
@@ -255,6 +245,15 @@ generateSite = do
255245
branchesRuns'
256246
writeFile "test-runs/index.html" (renderMarkup $ branchesPage $ fold branchesRunsOutput)
257247

248+
where
249+
getRootPrefix :: IO String
250+
getRootPrefix = do
251+
args <- getArgs
252+
pure $
253+
case args of
254+
(rp:_) -> rp
255+
_ -> ""
256+
258257
serve :: IO ()
259258
serve = do
260259
args <- getArgs

ui/src/AutomationService/DeviceView.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,19 @@ init = pure <<< initState
6969
update
7070
:: forall ws. WebSocket ws
7171
=> Maybe ws
72+
-> String
7273
-> State
7374
-> Message
7475
-> Transition Message State
75-
update ws s = case _ of
76+
update ws wsPort s = case _ of
7677
LoadDevices newDevices -> do
7778
forkVoid $ liftEffect $ debug $ "loaded devices: " <> show newDevices
7879
forkVoid $ do
7980
liftEffect $ for_ newDevices $ \d -> do
8081
let
8182
-- this needs to get passed in from parent state as config, or something
8283
subscribeMsg =
83-
MQTT.subscribe (deviceTopic (_.name <<< details $ d)) "HTTP 8080"
84+
MQTT.subscribe (deviceTopic (_.name <<< details $ d)) $ "HTTP " <> wsPort
8485
pingStateMsg =
8586
MQTT.publish (getTopic (_.name <<< details $ d)) $ MQTT.state ""
8687

@@ -338,7 +339,7 @@ view { devices, deviceStates } dispatch =
338339

339340
Just cap
340341
| not (canSet cap.access) ->
341-
H.div "" $ H.text "Not allowed to turn this one on chief"
342+
H.div "" $ H.text "Not allowed to turn this one on"
342343

343344
| otherwise ->
344345
H.div "form-check form-switch"

ui/src/AutomationService/Message.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Data.String.Pattern (Pattern(..), Replacement(..))
1717
data Message ws
1818
= SetPage Page
1919
| DeviceMsg Devices.Message
20-
| InitWS ws
20+
| InitWS String ws
2121
| PublishMsgChanged String
2222
| Publish
2323

ui/src/AutomationService/WebSocket.purs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Effect.Class (liftEffect)
2020
import Elmish.Component (Command)
2121
import Foreign (unsafeFromForeign)
2222
import Parsing (runParser)
23-
import Prelude (Unit, (<<<), ($), (<>), (=<<), bind, const, flip, identity, pure)
23+
import Prelude (Unit, (<<<), ($), (<>), (=<<), bind, const, discard, flip, identity, pure)
2424
import URI.Authority (_hosts)
2525
import URI.HierarchicalPart (_authority)
2626
import URI.Host as Host
@@ -61,32 +61,40 @@ instance WebSocket WS.WebSocket where
6161

6262
connectToWS :: Command Aff (Main.Message WS.WebSocket)
6363
connectToWS { dispatch } = do
64-
wsUrl <- liftEffect getWsUrl
64+
{port, wsUrl} <- liftEffect getWsUrl
6565
ws <- liftEffect $ create wsUrl []
66-
liftEffect $ dispatch (Main.InitWS ws)
66+
liftEffect $ dispatch (Main.InitWS port ws)
6767

6868
-- URI parsing util
6969

70-
getWsUrl :: Effect String
70+
getWsUrl :: Effect ({ port :: String, wsUrl :: String })
7171
getWsUrl = do
7272
htmlDocument <- Window.document =<< HTML.window
7373
uriStr <- Document.documentURI <<< HTMLDocument.toDocument $ htmlDocument
7474
let
7575
protocolStr = "ws://"
7676
localhostStr = "localhost"
77-
defaultWsUrl = protocolStr <> localhostStr
77+
defaultPortAndWsUrl = { port: "", wsUrl: protocolStr <> localhostStr }
7878
parseUriResult = runParser uriStr $ URI.parser options
7979
-- this is all very tedious
80-
pure $ flip (either $ const defaultWsUrl) parseUriResult $ \uri ->
80+
pure $ flip (either $ const defaultPortAndWsUrl) parseUriResult $ \uri ->
8181
let
8282
hosts = uri ^? _hierPart <<< _authority <<< _hosts <<< _Just
8383
in
84-
flip (maybe defaultWsUrl) hosts $ case _ of
85-
This hostName -> protocolStr <> Host.print hostName
84+
flip (maybe defaultPortAndWsUrl) hosts $ case _ of
85+
This hostName ->
86+
{ port: ""
87+
, wsUrl: (protocolStr <> Host.print hostName)
88+
}
8689
-- this would be strange ¯\_(ツ)_/¯
87-
That port -> defaultWsUrl <> Port.print port
90+
That port ->
91+
{ port: Port.print port
92+
, wsUrl: (defaultPortAndWsUrl.wsUrl <> Port.print port)
93+
}
8894
Both hostName port ->
89-
protocolStr <> Host.print hostName <> Port.print port
95+
{ port: Port.print port
96+
, wsUrl: (protocolStr <> Host.print hostName <> Port.print port)
97+
}
9098

9199
options :: Record (URIOptions UserInfo (HostPortPair Host Port) Path HierPath Query Fragment)
92100
options =

ui/src/Main.purs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type State ws =
4040
, publishMsg :: String
4141
, lastSentMsg :: Maybe String
4242
, websocket :: Maybe ws
43+
, websocketPort :: String
4344
}
4445

4546
init
@@ -58,6 +59,7 @@ init newDsUpdateTimers connectToWS = do
5859
, publishMsg: "{}"
5960
, lastSentMsg: Nothing
6061
, websocket: Nothing
62+
, websocketPort: ""
6163
}
6264

6365
update
@@ -70,10 +72,10 @@ update s = case _ of
7072
pure $ s { currentPage = newPage }
7173

7274
DeviceMsg deviceMsg -> do
73-
Devices.update s.websocket s.devices deviceMsg #
75+
Devices.update s.websocket s.websocketPort s.devices deviceMsg #
7476
bimap DeviceMsg (s { devices = _ })
7577

76-
InitWS ws -> do
78+
InitWS wsPort ws -> do
7779
forks $ \{ dispatch: msgSink } -> do
7880
let
7981
msgSink' = msgSink <<< DeviceMsg
@@ -136,7 +138,7 @@ update s = case _ of
136138

137139
liftEffect $ addWSEventListener ws messageHandler
138140

139-
pure $ s { websocket = Just ws }
141+
pure $ s { websocket = Just ws, websocketPort = wsPort }
140142

141143
PublishMsgChanged msg -> do
142144
pure $ s { publishMsg = msg }

ui/test/src/Test/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ instance WebSocket TestWS where
7373

7474
connectToWS :: TestWS -> Command Aff (Message TestWS)
7575
connectToWS wsState { dispatch: msgSink } =
76-
liftEffect <<< msgSink <<< InitWS $ wsState
76+
liftEffect <<< msgSink <<< InitWS "" $ wsState
7777

7878
sendMessage :: EventTarget -> String -> Effect Unit
7979
sendMessage ws msg = do

0 commit comments

Comments
 (0)