module Ecluse.Core.Registry.Cargo.Metadata (
cargoReadWith,
cargoChargeFactors,
prepareCargoRead,
prepareCargoReadAs,
finishCargoFull,
selectCargoVersionDoc,
) where
import Data.Aeson (Value (Object))
import Ecluse.Core.Package (PackageInfo)
import Ecluse.Core.Registry (UrlFormationError (UnparseableUrl))
import Ecluse.Core.Registry.CachedDocument (CachedDoc, cargoCached)
import Ecluse.Core.Registry.Cargo.Credential (cargoCredential)
import Ecluse.Core.Registry.Cargo.Discovery (CargoDiscovery, cargoDiscovery, discoveryIndex)
import Ecluse.Core.Registry.Cargo.Document (cargoDocument, cargoRecords)
import Ecluse.Core.Registry.Cargo.Reader (CargoIndexResult (..), CargoRead (..), readCargoIndexCharged)
import Ecluse.Core.Registry.Cargo.Request (cargoIndexRequest)
import Ecluse.Core.Registry.JsonStream (StreamResult (..))
import Ecluse.Core.Registry.Metadata (MetadataError)
import Ecluse.Core.Registry.Metadata.EntryFloor (noRetainedEntries)
import Ecluse.Core.Registry.Metadata.Fetch.Preparation (PrepareRead (..), bindToOrigin, presentedRead)
import Ecluse.Core.Registry.Metadata.Fetch.Types (EcosystemRead (..))
import Ecluse.Core.Registry.Metadata.Projection (finishSelectedVersion, streamError)
import Ecluse.Core.Registry.Origin (OriginClient (ocChargeFullReadWorkspace, ocChargeSelectedRead))
import Ecluse.Core.Registry.Request (CredentialMapping)
import Ecluse.Core.Registry.ServedDocument (stringField)
import Ecluse.Core.Security (Limits)
import Ecluse.Core.Server.Admission.Types (ChargeFactors (..))
import Ecluse.Core.Version (Version, renderVersion)
cargoChargeFactors :: ChargeFactors
cargoChargeFactors :: ChargeFactors
cargoChargeFactors = ChargeFactors{cfFullReadPermille :: Int
cfFullReadPermille = Int
1700, cfKeptPermille :: Int
cfKeptPermille = Int
1500, cfOutputPermille :: Int
cfOutputPermille = Int
2600}
prepareCargoRead :: PrepareRead
prepareCargoRead :: PrepareRead
prepareCargoRead = CredentialMapping -> PrepareRead
readAfterDiscovery CredentialMapping
cargoCredential
prepareCargoReadAs :: CredentialMapping -> PrepareRead
prepareCargoReadAs :: CredentialMapping -> PrepareRead
prepareCargoReadAs CredentialMapping
mapping = CredentialMapping -> PrepareRead -> PrepareRead
presentedRead CredentialMapping
mapping (CredentialMapping -> PrepareRead
readAfterDiscovery CredentialMapping
mapping)
readAfterDiscovery :: CredentialMapping -> PrepareRead
readAfterDiscovery :: CredentialMapping -> PrepareRead
readAfterDiscovery CredentialMapping
mapping = (OriginClient -> IO (Either MetadataError EcosystemRead))
-> PrepareRead
PrepareRead (\OriginClient
origin -> (CargoDiscovery -> EcosystemRead)
-> Either MetadataError CargoDiscovery
-> Either MetadataError EcosystemRead
forall a b.
(a -> b) -> Either MetadataError a -> Either MetadataError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Int -> IO ()) -> (Int -> IO ()) -> CargoDiscovery -> EcosystemRead
cargoReadWith (OriginClient -> Int -> IO ()
ocChargeFullReadWorkspace OriginClient
origin) (OriginClient -> Int -> IO ()
ocChargeSelectedRead OriginClient
origin)) (Either MetadataError CargoDiscovery
-> Either MetadataError EcosystemRead)
-> IO (Either MetadataError CargoDiscovery)
-> IO (Either MetadataError EcosystemRead)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CredentialMapping
-> OriginClient -> IO (Either MetadataError CargoDiscovery)
cargoDiscovery CredentialMapping
mapping OriginClient
origin)
cargoReadWith :: (Int -> IO ()) -> (Int -> IO ()) -> CargoDiscovery -> EcosystemRead
cargoReadWith :: (Int -> IO ()) -> (Int -> IO ()) -> CargoDiscovery -> EcosystemRead
cargoReadWith Int -> IO ()
fullPay Int -> IO ()
selectedPay CargoDiscovery
discovery =
UrlFormationError -> Text -> EcosystemRead -> EcosystemRead
bindToOrigin
(Text -> UrlFormationError
UnparseableUrl Text
"Cargo discovery belongs to another index root")
(CargoDiscovery -> Text
discoveryIndex CargoDiscovery
discovery)
EcosystemRead
{ erRequest :: BaseRequest
-> Maybe ClientCredential
-> PackageName
-> Either UrlFormationError Request
erRequest = BaseRequest
-> Maybe ClientCredential
-> PackageName
-> Either UrlFormationError Request
cargoIndexRequest
, erUniqueFields :: [Text]
erUniqueFields = []
, erWalkFull :: Limits -> PackageName -> Text -> DocumentWalk CargoIndexResult
erWalkFull = \Limits
limits PackageName
name Text
_ -> (Int -> IO ())
-> CargoDiscovery
-> Limits
-> PackageName
-> CargoRead
-> DocumentWalk CargoIndexResult
readCargoIndexCharged Int -> IO ()
fullPay CargoDiscovery
discovery Limits
limits PackageName
name CargoRead
FullRead
, erFinishFull :: Limits
-> PackageName
-> Text
-> StreamResult CargoIndexResult
-> Either MetadataError (PackageInfo, CachedDoc)
erFinishFull = \Limits
limits PackageName
_ Text
_ -> Limits
-> StreamResult CargoIndexResult
-> Either MetadataError (PackageInfo, CachedDoc)
finishCargoFull Limits
limits
, erRetainedEntries :: CargoIndexResult -> RetainedEntries
erRetainedEntries = RetainedEntries -> CargoIndexResult -> RetainedEntries
forall a b. a -> b -> a
const RetainedEntries
noRetainedEntries
, erWalkSelected :: Limits -> PackageName -> Version -> DocumentWalk CargoIndexResult
erWalkSelected = \Limits
limits PackageName
name Version
version -> (Int -> IO ())
-> CargoDiscovery
-> Limits
-> PackageName
-> CargoRead
-> DocumentWalk CargoIndexResult
readCargoIndexCharged Int -> IO ()
selectedPay CargoDiscovery
discovery Limits
limits PackageName
name (Version -> CargoRead
SelectedRead Version
version)
, erFinishSelected :: Limits
-> PackageName
-> Text
-> Version
-> StreamResult CargoIndexResult
-> Either MetadataError VersionRead
erFinishSelected = \Limits
limits PackageName
_ Text
_ -> (CargoIndexResult -> PackageInfo)
-> (Version -> CargoIndexResult -> Maybe CachedDoc)
-> Limits
-> Version
-> StreamResult CargoIndexResult
-> Either MetadataError VersionRead
forall a.
(a -> PackageInfo)
-> (Version -> a -> Maybe CachedDoc)
-> Limits
-> Version
-> StreamResult a
-> Either MetadataError VersionRead
finishSelectedVersion CargoIndexResult -> PackageInfo
cargoIndexInfo (\Version
version CargoIndexResult
result -> Version -> CachedDoc -> Maybe CachedDoc
selectCargoVersionDoc Version
version (CargoIndexResult -> CachedDoc
cargoIndexDocument CargoIndexResult
result)) Limits
limits
}
finishCargoFull :: Limits -> StreamResult CargoIndexResult -> Either MetadataError (PackageInfo, CachedDoc)
finishCargoFull :: Limits
-> StreamResult CargoIndexResult
-> Either MetadataError (PackageInfo, CachedDoc)
finishCargoFull Limits
limits StreamResult CargoIndexResult
streamed = do
result <- (ParseError -> MetadataError)
-> Either ParseError CargoIndexResult
-> Either MetadataError CargoIndexResult
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Limits -> ParseError -> MetadataError
streamError Limits
limits) (StreamResult CargoIndexResult -> Either ParseError CargoIndexResult
forall a. StreamResult a -> Either ParseError a
streamValue StreamResult CargoIndexResult
streamed)
pure (cargoIndexInfo result, cargoIndexDocument result)
selectCargoVersionDoc :: Version -> CachedDoc -> Maybe CachedDoc
selectCargoVersionDoc :: Version -> CachedDoc -> Maybe CachedDoc
selectCargoVersionDoc Version
version CachedDoc
document = do
source <- (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
-> CachedDoc -> Maybe CargoDocument
forall a b. (a, b) -> b
snd (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached CachedDoc
document
let selected = ((EntryKey, Value) -> Bool)
-> [(EntryKey, Value)] -> [(EntryKey, Value)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Value -> Bool
hasVersion (Value -> Bool)
-> ((EntryKey, Value) -> Value) -> (EntryKey, Value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EntryKey, Value) -> Value
forall a b. (a, b) -> b
snd) (CargoDocument -> [(EntryKey, Value)]
cargoRecords CargoDocument
source)
case selected of
[(EntryKey, Value)
row] -> CachedDoc -> Maybe CachedDoc
forall a. a -> Maybe a
Just ((CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
-> CargoDocument -> CachedDoc
forall a b. (a, b) -> a
fst (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached ([(EntryKey, Value)] -> CargoDocument
cargoDocument [(EntryKey, Value)
row]))
[(EntryKey, Value)]
_ -> Maybe CachedDoc
forall a. Maybe a
Nothing
where
hasVersion :: Value -> Bool
hasVersion Value
value = Value -> Maybe Text
recordVersion Value
value Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just (Version -> Text
renderVersion Version
version)
recordVersion :: Value -> Maybe Text
recordVersion :: Value -> Maybe Text
recordVersion = \case
Object Object
fields -> Key -> Object -> Maybe Text
stringField Key
"vers" Object
fields
Value
_ -> Maybe Text
forall a. Maybe a
Nothing