-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental daml assistant support for metering report [DPP-816] #12485
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
language-support/hs/bindings/src/DA/Ledger/Services/MeteringReportService.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
|
||
{-# LANGUAGE DuplicateRecordFields #-} | ||
|
||
module DA.Ledger.Services.MeteringReportService ( | ||
getMeteringReport, | ||
MeteringReport(..), | ||
isoTimeToTimestamp, | ||
) where | ||
|
||
|
||
import Data.Aeson ( KeyValue((.=)), ToJSON(..), object) | ||
import DA.Ledger.Convert | ||
import DA.Ledger.GrpcWrapUtils | ||
import DA.Ledger.LedgerService | ||
import DA.Ledger.Types | ||
import qualified Data.Text.Lazy as TL | ||
import Network.GRPC.HighLevel.Generated | ||
import qualified Com.Daml.Ledger.Api.V1.Admin.MeteringReportService as LL | ||
import Data.Maybe (maybeToList) | ||
import qualified Data.Time.Clock.System as System | ||
import qualified Data.Time.Format.ISO8601 as ISO8601 | ||
import GHC.Int (Int64) | ||
import GHC.Word (Word32) | ||
|
||
data MeteredApplication = MeteredApplication { | ||
application :: ApplicationId | ||
, events :: Int64 | ||
} deriving (Show) | ||
|
||
instance ToJSON MeteredApplication where | ||
toJSON (MeteredApplication application events) = | ||
object | ||
[ "application" .= unApplicationId application | ||
, "events" .= events | ||
] | ||
|
||
data MeteringReport = MeteringReport { | ||
participant :: ParticipantId | ||
, from :: Timestamp | ||
, toRequested :: Maybe Timestamp | ||
, toActual :: Timestamp | ||
, applications :: [MeteredApplication] | ||
} deriving (Show) | ||
|
||
instance ToJSON MeteringReport where | ||
toJSON (MeteringReport participant from toRequested toActual applications) = | ||
object ( | ||
[ "participant" .= unParticipantId participant | ||
, "from" .= timestampToIso8601 from | ||
, "toActual" .= timestampToIso8601 toActual | ||
, "applications" .= applications | ||
] | ||
++ maybeToList (fmap (("toRequested" .=) . timestampToIso8601) toRequested) | ||
) | ||
|
||
timestampToSystemTime :: Timestamp -> System.SystemTime | ||
timestampToSystemTime ts = st | ||
where | ||
s = fromIntegral (seconds ts) :: Int64 | ||
n = fromIntegral (nanos ts) :: Word32 | ||
st = System.MkSystemTime s n | ||
|
||
systemTimeToTimestamp :: System.SystemTime -> Timestamp | ||
systemTimeToTimestamp st = ts | ||
where | ||
s = fromIntegral (System.systemSeconds st) :: Int | ||
n = fromIntegral (System.systemNanoseconds st) :: Int | ||
ts = Timestamp s n | ||
|
||
timestampToIso8601 :: Timestamp -> String | ||
simonmaxen-da marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timestampToIso8601 ts = ISO8601.iso8601Show ut | ||
where | ||
st = timestampToSystemTime ts | ||
ut = System.systemToUTCTime st | ||
|
||
isoTimeToTimestamp :: IsoTime -> Timestamp | ||
isoTimeToTimestamp iso = systemTimeToTimestamp $ System.utcToSystemTime $ unIsoTime iso | ||
|
||
raiseApplicationMeteringReport :: LL.ApplicationMeteringReport -> Perhaps MeteredApplication | ||
raiseApplicationMeteringReport (LL.ApplicationMeteringReport llApp events) = do | ||
application <- raiseApplicationId llApp | ||
return MeteredApplication {..} | ||
|
||
raiseParticipantMeteringReport :: LL.GetMeteringReportRequest -> LL.ParticipantMeteringReport -> Perhaps MeteringReport | ||
raiseParticipantMeteringReport (LL.GetMeteringReportRequest (Just llFrom) llTo _) (LL.ParticipantMeteringReport llParticipantId (Just llToActual) llAppReports) = do | ||
participant <- raiseParticipantId llParticipantId | ||
from <- raiseTimestamp llFrom | ||
toRequested <- traverse raiseTimestamp llTo | ||
toActual <- raiseTimestamp llToActual | ||
applications <- raiseList raiseApplicationMeteringReport llAppReports | ||
return MeteringReport{..} | ||
|
||
raiseParticipantMeteringReport _ response = Left $ Unexpected ("raiseParticipantMeteringReport unable to parse response: " <> show response) | ||
|
||
raiseGetMeteringReportResponse :: LL.GetMeteringReportResponse -> Perhaps MeteringReport | ||
raiseGetMeteringReportResponse (LL.GetMeteringReportResponse (Just request) (Just report) (Just _)) = | ||
raiseParticipantMeteringReport request report | ||
|
||
raiseGetMeteringReportResponse response = Left $ Unexpected ("raiseMeteredReport unable to parse response: " <> show response) | ||
|
||
getMeteringReport :: Timestamp -> Maybe Timestamp -> Maybe ApplicationId -> LedgerService MeteringReport | ||
getMeteringReport from to applicationId = | ||
makeLedgerService $ \timeout config mdm -> | ||
withGRPCClient config $ \client -> do | ||
service <- LL.meteringReportServiceClient client | ||
let | ||
LL.MeteringReportService {meteringReportServiceGetMeteringReport=rpc} = service | ||
gFrom = Just $ lowerTimestamp from | ||
gTo = fmap lowerTimestamp to | ||
gApp = maybe TL.empty unApplicationId applicationId | ||
request = LL.GetMeteringReportRequest gFrom gTo gApp | ||
rpc (ClientNormalRequest request timeout mdm) | ||
>>= unwrap | ||
>>= either (fail . show) return . raiseGetMeteringReportResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other flags default to a human readable output and then have a
--json
flag. i think it would be nice to keep that here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say this is a special case as the purpose of this helper is to output a report in json format as per the "Design: Ledger Metering" spec. By default the json is not pretty printed. I could add a
--pretty
flag to output the JSON in a more human readable format (or make pretty output the default and add a--nopretty
flag) ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about pretty print by default and add
--compact-output
(matchingjq
’s flag name) to not pretty print?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: