Skip to content

Commit

Permalink
chore: fix server-timing metric order and doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla authored Nov 28, 2023
1 parent a72241a commit ca5eb64
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/PostgREST/Response/Performance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import Protolude
data ServerMetric =
SMJwt
| SMParse
| SMResp
| SMPlan
| SMTransaction
| SMResp
deriving (Show, Eq, Ord)
type ServerTimingData = Map ServerMetric (Maybe Double)

-- | Render the Server-Timing header from a ServerTimingData
--
-- >>> renderServerTimingHeader $ Map.fromList [(SMPlan, 0.1), (SMTransaction, 0.2), (SMResp, 0.3), (SMJwt, 0.4)]
-- ("Server-Timing","jwt;dur=400000.0, render;dur=300000.0, plan;dur=100000.0, query;dur=200000.0")
-- >>> renderServerTimingHeader $ Map.fromList [(SMPlan, Just 0.1), (SMTransaction, Just 0.2), (SMResp, Just 0.3), (SMJwt, Just 0.4), (SMParse, Just 0.5)]
-- ("Server-Timing","jwt;dur=400000.0, parse;dur=500000.0, plan;dur=100000.0, transaction;dur=200000.0, response;dur=300000.0")
renderServerTimingHeader :: ServerTimingData -> HTTP.Header
renderServerTimingHeader timingData =
("Server-Timing", BS.intercalate ", " $ map renderTiming $ Map.toList timingData)
renderTiming :: (ServerMetric, Maybe Double) -> BS.ByteString
renderTiming (metric, time) = maybe "" (\x -> BS.concat [renderMetric metric, BS.pack $ ";dur=" <> showFFloat (Just 1) (x * 1000000) ""]) time
where
renderMetric SMJwt = "jwt"
renderMetric SMParse = "parse"
renderMetric SMPlan = "plan"
renderMetric SMTransaction = "transaction"
renderMetric SMResp = "response"
renderMetric SMJwt = "jwt"
renderMetric SMParse = "parse"
1 change: 1 addition & 0 deletions test/doc/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ main =
, "src/PostgREST/Query/SqlFragment.hs"
, "src/PostgREST/ApiRequest/Preferences.hs"
, "src/PostgREST/ApiRequest/QueryParams.hs"
, "src/PostgREST/Response/Performance.hs"
, "src/PostgREST/Error.hs"
, "src/PostgREST/MediaType.hs"
, "src/PostgREST/Config.hs"
Expand Down

0 comments on commit ca5eb64

Please sign in to comment.