@@ -23,11 +23,12 @@ import Data.Maybe (fromMaybe, isJust, isNothin
2323import Data.Scientific
2424import Data.String (fromString )
2525import Data.Text (Text , pack , unpack , intercalate , pack , isPrefixOf , drop )
26+ import qualified Data.Text as T
2627import qualified Data.Text.Encoding as Text
2728import Data.Time
2829
2930import qualified Database.PostgreSQL.Simple as SQL
30- import Database.PostgreSQL.Simple.Types (In (.. ))
31+ import Database.PostgreSQL.Simple.Types (In (.. ), PGArray ( .. ) )
3132import Database.PostgreSQL.Simple.FromRow
3233
3334import VVA.Config
@@ -268,7 +269,6 @@ getDRepsVotingPowerList identifiers = withPool $ \conn -> do
268269 ]
269270
270271
271- -- 1. Fetch from 3rd party
272272fetchDRepAiData ::
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
303302manipulateDRepAiData :: SearchAiResponse -> SearchAiResponse
304303manipulateDRepAiData 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
317369drepAiSearch ::
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
320372drepAiSearch query page limit = do
321373 aiData <- fetchDRepAiData query page limit
322374 let manipulated = manipulateDRepAiData aiData
323- enrichDRepDataWithDb manipulated
375+ enrichDRepAiDataWithDb manipulated
0 commit comments