Skip to content

Commit 012114d

Browse files
committed
kinda-working-something
1 parent 9f03cb9 commit 012114d

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

govtool/backend/sql/list-dreps.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Keep it in sync with the list-dreps-for-ai-search.sql as those two queries
2+
-- are used in the same context, but with different parameters.
3+
14
WITH DRepDistr AS (
25
SELECT DISTINCT ON (drep_distr.hash_id) drep_distr.*
36
FROM drep_distr

govtool/backend/src/VVA/DRep.hs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import Data.Maybe (fromMaybe, isJust, isNothin
2323
import Data.Scientific
2424
import Data.String (fromString)
2525
import Data.Text (Text, pack, unpack, intercalate, pack, isPrefixOf, drop)
26+
import qualified Data.Text as T
2627
import qualified Data.Text.Encoding as Text
2728
import Data.Time
2829

2930
import qualified Database.PostgreSQL.Simple as SQL
30-
import Database.PostgreSQL.Simple.Types (In(..))
31+
import Database.PostgreSQL.Simple.Types (In(..), PGArray(..))
3132
import Database.PostgreSQL.Simple.FromRow
3233

3334
import VVA.Config
@@ -268,7 +269,6 @@ getDRepsVotingPowerList identifiers = withPool $ \conn -> do
268269
]
269270

270271

271-
-- 1. Fetch from 3rd party
272272
fetchDRepAiData ::
273273
(Has VVAConfig r, MonadReader r m, MonadIO m, MonadError AppError m) =>
274274
Maybe Text -- ^ search query
@@ -299,25 +299,77 @@ fetchDRepAiData query page limit = do
299299
Left err -> throwError $ CriticalError ("Could not parse the dReps: " <> pack err)
300300
Right val -> return val
301301

302-
-- 2. Manipulate/transform
303302
manipulateDRepAiData :: SearchAiResponse -> SearchAiResponse
304303
manipulateDRepAiData resp = resp { elements = fmap (map cutDrepId) (elements resp) }
305304
where
306305
cutDrepId d =
307306
let dId = drepId d
308307
in d { drepId = if Data.Text.isPrefixOf "22" dId then Data.Text.drop 2 dId else dId }
309308

310-
-- 3. Enrich with SQL data
311-
enrichDRepDataWithDb ::
312-
(Monad m) =>
309+
listDRepsForAiSearchSql :: SQL.Query
310+
listDRepsForAiSearchSql = sqlFrom $(embedFile "sql/list-dreps-for-ai-search.sql")
311+
312+
enrichDRepAiDataWithDb ::
313+
(Has ConnectionPool r, MonadReader r m, MonadIO m) =>
313314
SearchAiResponse -> m SearchAiResponse
314-
enrichDRepDataWithDb = return
315+
enrichDRepAiDataWithDb aiResponse = withPool $ \conn -> do
316+
317+
let drepIds =
318+
case elements aiResponse of
319+
Just ds -> filter (not . T.null) $ map drepId ds
320+
Nothing -> []
321+
322+
liftIO $ print drepIds
323+
324+
results <- liftIO $
325+
if null drepIds
326+
then return []
327+
else SQL.query conn listDRepsForAiSearchSql (SQL.Only (PGArray drepIds)) :: IO [DRepQueryResult]
328+
329+
-- TODO: merge/enrich aiResponse.elements with results as needed
330+
return aiResponse
331+
332+
timeZone <- liftIO getCurrentTimeZone
333+
return
334+
[ DRepRegistration
335+
(queryDrepHash result)
336+
(queryDrepView result)
337+
(queryIsScriptBased result)
338+
(queryUrl result)
339+
(queryDataHash result)
340+
(floor @Scientific $ queryDeposit result)
341+
(queryVotingPower result)
342+
status
343+
drepType
344+
(queryTxHash result)
345+
(localTimeToUTC timeZone $ queryDate result)
346+
(queryMetadataError result)
347+
(queryPaymentAddress result)
348+
(queryGivenName result)
349+
(queryObjectives result)
350+
(queryMotivations result)
351+
(queryQualifications result)
352+
(queryImageUrl result)
353+
(queryImageHash result)
354+
(queryIdentityReferences result)
355+
(queryLinkReferences result)
356+
| result <- results
357+
, let status = case (queryIsActive result, queryDeposit result) of
358+
(_, d) | d < 0 -> Retired
359+
(isActive, d) | d >= 0 && isActive -> Active
360+
| d >= 0 && not isActive -> Inactive
361+
, let latestDeposit' = floor @Scientific (queryLatestDeposit result) :: Integer
362+
, let drepType | latestDeposit' >= 0 && isNothing (queryUrl result) = SoleVoter
363+
| latestDeposit' >= 0 && isJust (queryUrl result) = DRep
364+
| latestDeposit' < 0 && not (queryLatestNonDeregisterVotingAnchorWasNotNull result) = SoleVoter
365+
| latestDeposit' < 0 && queryLatestNonDeregisterVotingAnchorWasNotNull result = DRep
366+
| Data.Maybe.isJust (queryUrl result) = DRep
367+
]
315368

316-
-- 4. Compose them in your endpoint/handler
317369
drepAiSearch ::
318-
(Has VVAConfig r, MonadReader r m, MonadIO m, MonadError AppError m) =>
370+
(Has ConnectionPool r, Has VVAConfig r, MonadReader r m, MonadIO m, MonadError AppError m) =>
319371
Maybe Text -> Int -> Int -> m SearchAiResponse
320372
drepAiSearch query page limit = do
321373
aiData <- fetchDRepAiData query page limit
322374
let manipulated = manipulateDRepAiData aiData
323-
enrichDRepDataWithDb manipulated
375+
enrichDRepAiDataWithDb manipulated

0 commit comments

Comments
 (0)