Skip to content
This repository was archived by the owner on Nov 24, 2019. It is now read-only.

Commit dfe59e4

Browse files
finish up APIs
1 parent ff77a43 commit dfe59e4

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

config/routes

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
/heartbeat HeartbeatR GET
88
/traces TraceR GET
99
/trace PutTraceR PUT
10-
/start-trace StartTraceR POST
11-
/remove-trace RemoveTraceR POST
10+
/trace/start StartTraceR POST
11+
/trace/remove RemoveTraceR POST
12+
/trace/running RunningTraceR GET
13+
/traces/running/all RunningTracesR GET

src/Handler/Lambda.hs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
{-# LANGUAGE TemplateHaskell #-}
66
{-# LANGUAGE TypeFamilies #-}
77

8-
module Handler.Lambda (getHeartbeatR, getTraceR, putPutTraceR, postStartTraceR, postRemoveTraceR) where
8+
module Handler.Lambda (
9+
getHeartbeatR,
10+
getTraceR,
11+
putPutTraceR,
12+
postStartTraceR,
13+
postRemoveTraceR,
14+
getRunningTracesR,
15+
getRunningTraceR
16+
) where
917
import Lambda.Endpoint
1018
import Data.Aeson
1119
import qualified Data.Text as T
1220
import Import
1321
import Lambda.Trace
1422
import Database.Persist.Sql
15-
23+
import Lambda.Trace.Unsafe
1624
data RemoveTrace =
1725
RemoveTrace {
1826
removeType :: Text,
@@ -46,6 +54,12 @@ data InfoWithId =
4654
traceNo :: Int64
4755
} deriving (Generic, Show)
4856

57+
data InfoWithPath =
58+
InfoWithPath {
59+
info' :: TraceInfo,
60+
path :: String
61+
} deriving (Generic, Show)
62+
4963
instance ToJSON HeartbeatReply where
5064
toEncoding = genericToEncoding defaultOptions
5165
instance FromJSON HeartbeatReply
@@ -67,6 +81,10 @@ instance ToJSON RemoveTrace where
6781
toEncoding = genericToEncoding defaultOptions
6882
instance FromJSON RemoveTrace
6983

84+
instance ToJSON InfoWithPath where
85+
toEncoding = genericToEncoding defaultOptions
86+
instance FromJSON InfoWithPath
87+
7088
getHeartbeatR :: Handler Value
7189
getHeartbeatR = withVerification $ do
7290
t <- liftIO getCurrentTime
@@ -135,6 +153,24 @@ postRemoveTraceR = withVerification $ do
135153
return "deleted"
136154
_ -> do
137155
invalidArgs ["removeType"]
156+
157+
getRunningTracesR :: Handler Value
158+
getRunningTracesR = withVerification $ do
159+
xs <- liftIO getAllRunning
160+
return $ toJSONList (map (\(x, y)-> InfoWithPath y x) xs)
161+
162+
getRunningTraceR :: Handler Value
163+
getRunningTraceR = withVerification $ do
164+
parm <- lookupGetParam "path"
165+
liftIO $ print parm
166+
case parm of
167+
Just a -> do
168+
i <- liftIO $ getRunningTrace (T.unpack a)
169+
case i of
170+
Nothing -> return (toJSON ())
171+
Just x -> return (toJSON x)
172+
Nothing ->
173+
invalidArgs ["path"]
138174

139175

140176

src/Lambda/Communication.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ submitStart :: String -> IO ()
4141
submitStart x = do
4242
request <- parseRequest $ "PUT " <> platformUrl-- <> "/submit"
4343
hash <- hashedSecret
44-
res <- httpLBS
44+
_ <- httpLBS
4545
$ setRequestBodyJSON (Info x "start")
4646
$ setRequestHeader "Authorization" [BSS.append (BSS.pack endpointUUID) hash] request
47-
BS8.putStrLn $ getResponseBody res
4847
return ()
4948

5049
submitFinished :: String -> Handle -> ProcessHandle -> IO ()
5150
submitFinished x herr process = do
5251
body <- buildFinishedBody x herr process
5352
request <- parseRequest $ "PUT " <> platformUrl-- <> "/submit"
5453
hash <- hashedSecret
55-
res <- httpLBS
54+
_ <- httpLBS
5655
$ setRequestBodyLBS body $ setRequestHeader "Content-Type" ["application/json"]
5756
$ setRequestHeader "Authorization" [BSS.append (BSS.pack endpointUUID) hash] request
58-
BS8.putStrLn $ getResponseBody res <> "\nfinished"
5957
delRunningTrace x
6058
return ()
6159

@@ -72,12 +70,11 @@ runSubmit identity hout herr process chunkNo = do
7270
jsonData <- buildProgressBody identity field chunkNo
7371
request <- parseRequest $ "PUT " <> platformUrl-- <> "/submit"
7472
hash <- hashedSecret
75-
res <-
73+
_ <-
7674
httpLBS $
7775
setRequestBodyLBS jsonData $
7876
setRequestHeader "Content-Type" ["application/json"] $
7977
setRequestHeader "Authorization" [BSS.append (BSS.pack endpointUUID) hash] request
80-
BS8.putStrLn $ getResponseBody res
8178
runSubmit identity hout herr process (chunkNo + 1)
8279

8380
buildProgressBody :: String -> BS.ByteString -> Int -> IO (BS.ByteString)

src/Lambda/Trace.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ generateFile :: Trace a => a -> Int -> IO String
1919
generateFile x t = do
2020
text <- return $ generate x t
2121
uuid <- nextRandom
22-
let filePath = "/tmp/lambda-" <> show uuid <> ".stap"
22+
let filePath = "/tmp/lambda-" <> show uuid <> ".trace"
2323
withFile filePath WriteMode (`TIO.hPutStr` text)
2424
return filePath
2525

src/Lambda/Trace/Unsafe.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ delRunningTrace :: String -> IO ()
1616
delRunningTrace path = H.delete runningTraces path
1717

1818
getRunningTrace :: String -> IO (Maybe TraceInfo)
19-
getRunningTrace path = H.lookup runningTraces path
19+
getRunningTrace path = H.lookup runningTraces path
20+
21+
getAllRunning :: IO [(String, TraceInfo)]
22+
getAllRunning = H.toList runningTraces
23+

0 commit comments

Comments
 (0)