Skip to content

Commit 51c183a

Browse files
Reintroduce “Random” Sorting in DRep Directory with Pagination Support - BACKEND
1 parent 01c9b4b commit 51c183a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

govtool/backend/src/VVA/API.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import qualified Data.Text.Lazy.Encoding as TL
2828
import Data.Time (TimeZone, localTimeToUTC)
2929
import Data.Time.LocalTime (TimeZone, getCurrentTimeZone)
3030
import qualified Data.Vector as V
31+
import Data.Hashable (hash, hashWithSalt)
3132

3233
import Numeric.Natural (Natural)
3334

@@ -64,6 +65,7 @@ type VVAApi =
6465
:> QueryParam "sort" DRepSortMode
6566
:> QueryParam "page" Natural
6667
:> QueryParam "pageSize" Natural
68+
:> QueryParam "seed" Text
6769
:> Get '[JSON] ListDRepsResponse
6870
:<|> "drep" :> "get-voting-power" :> Capture "drepId" HexText :> Get '[JSON] Integer
6971
:<|> "drep" :> "getVotes"
@@ -175,8 +177,8 @@ delegationToResponse Types.Delegation {..} =
175177
}
176178

177179

178-
drepList :: App m => Maybe Text -> [DRepStatus] -> Maybe DRepSortMode -> Maybe Natural -> Maybe Natural -> m ListDRepsResponse
179-
drepList mSearchQuery statuses mSortMode mPage mPageSize = do
180+
drepList :: App m => Maybe Text -> [DRepStatus] -> Maybe DRepSortMode -> Maybe Natural -> Maybe Natural -> Maybe Text -> m ListDRepsResponse
181+
drepList mSearchQuery statuses mSortMode mPage mPageSize mSeed = do
180182
CacheEnv {dRepListCache} <- asks vvaCache
181183
dreps <- cacheRequest dRepListCache (fromMaybe "" mSearchQuery) (DRep.listDReps mSearchQuery)
182184

@@ -193,17 +195,22 @@ drepList mSearchQuery statuses mSortMode mPage mPageSize = do
193195
Types.DRep ->
194196
True
195197

196-
197198
let filterDRepsByStatus = case statuses of
198199
[] -> id
199200
_ -> filter $ \Types.DRepRegistration {..} ->
200201
mapDRepStatus dRepRegistrationStatus `elem` statuses
201202

202-
randomizedOrderList <- mapM (\_ -> randomRIO (0, 1 :: Double)) dreps
203+
let seedInt :: Int
204+
seedInt =
205+
maybe 0 (hash . Text.toCaseFold) mSeed
206+
207+
randomKey :: Types.DRepRegistration -> Int
208+
randomKey Types.DRepRegistration{..} =
209+
hashWithSalt seedInt dRepRegistrationDRepHash
203210

204211
let sortDReps = case mSortMode of
205212
Nothing -> id
206-
Just Random -> fmap snd . sortOn fst . Prelude.zip randomizedOrderList
213+
Just Random -> sortOn randomKey
207214
Just VotingPower -> sortOn $ \Types.DRepRegistration {..} ->
208215
Down dRepRegistrationVotingPower
209216
Just Activity -> sortOn $ \Types.DRepRegistration {..} ->

0 commit comments

Comments
 (0)