Skip to content

Commit

Permalink
Use a single aggregate query for readTotalProduction.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Sep 4, 2020
1 parent 0f2b0cb commit 767a4ff
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/core/src/Cardano/Pool/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,20 @@ newDBLayer trace fp timeInterpreter = do

pure (foldl' toMap Map.empty production)

readTotalProduction = do
production <- fmap entityVal <$>
selectList ([] :: [Filter PoolProduction]) []

let toMap m (PoolProduction{poolProductionPoolId}) =
Map.insertWith (+) poolProductionPoolId 1 m

pure $ Map.map Quantity $ foldl' toMap Map.empty production
readTotalProduction =
Map.fromList . rights . fmap parseRow <$> rawSql query []
where
query = T.unwords
[ "SELECT pool_id, count(pool_id) as block_count"
, "FROM pool_production"
, "GROUP BY pool_id;"
]
parseRow
( Single fieldPoolId
, Single fieldBlockCount
) = (,)
<$> fromPersistValue fieldPoolId
<*> (Quantity <$> fromPersistValue fieldBlockCount)

putStakeDistribution epoch@(EpochNo ep) distribution = do
deleteWhere [StakeDistributionEpoch ==. fromIntegral ep]
Expand Down

0 comments on commit 767a4ff

Please sign in to comment.