From 77c0595d7e8a67f7ec3e8939fc22761fab48dfef Mon Sep 17 00:00:00 2001 From: Vijay Gupta Date: Wed, 18 Sep 2024 20:31:24 +0530 Subject: [PATCH] backend: kv-configs-reverse to read all from kv --- .../src/Kernel/Beam/Functions.hs | 27 ++++++------------- lib/mobility-core/src/Kernel/Types/Common.hs | 24 +++-------------- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/lib/mobility-core/src/Kernel/Beam/Functions.hs b/lib/mobility-core/src/Kernel/Beam/Functions.hs index 017bdb1b7..4de2149d5 100644 --- a/lib/mobility-core/src/Kernel/Beam/Functions.hs +++ b/lib/mobility-core/src/Kernel/Beam/Functions.hs @@ -33,6 +33,7 @@ where import Data.Aeson import Data.Default.Class +import qualified Data.HashMap.Strict as HM import qualified Data.Serialize as Serialize import Database.Beam hiding (timestamp) import Database.Beam.MySQL () @@ -53,7 +54,6 @@ import Kernel.Types.Error import Kernel.Utils.Error.Throwing (throwError) import Kernel.Utils.Logging (logDebug) import Sequelize -import System.Random -- classes for converting from beam types to ttypes and vice versa class @@ -117,25 +117,14 @@ runInMasterDb m = do setMeshConfig :: (L.MonadFlow m, HasCallStack) => Text -> Maybe Text -> MeshConfig -> m MeshConfig setMeshConfig modelName mSchema meshConfig' = do - schema <- maybe (L.throwException $ InternalError "Schema not found") pure mSchema + schema <- maybe (L.throwException $ InternalError "Schema not found in setMeshConfig") pure mSchema let redisStream = if schema == "atlas_driver_offer_bpp" then "driver-db-sync-stream" else "rider-db-sync-stream" -- lets change when we enable for dashboards - tables <- L.getOption KBT.Tables - randomIntV <- L.runIO (randomRIO (1, 100) :: IO Int) - case tables of - Nothing -> L.throwException $ InternalError "Tables not found" - Just tables' -> do - let enableKVForWriteAlso = tables'.enableKVForWriteAlso - enableKVForRead = tables'.enableKVForRead - tableObject = find (\table' -> nameOfTable table' == modelName) enableKVForWriteAlso - updatedMeshConfig <- case tableObject of - Nothing -> pure $ meshConfig' {meshEnabled = False, kvHardKilled = modelName `notElem` enableKVForRead, ecRedisDBStream = redisStream} - Just table' -> do - let redisTtl' = fromMaybe (meshConfig'.redisTtl) (table'.redisTtl) - if fromIntegral (percentEnable table') >= randomIntV - then pure $ meshConfig' {meshEnabled = True, kvHardKilled = modelName `notElem` enableKVForRead, ecRedisDBStream = redisStream, redisTtl = redisTtl'} - else pure $ meshConfig' {meshEnabled = False, kvHardKilled = modelName `notElem` enableKVForRead, ecRedisDBStream = redisStream} - L.logDebug ("setMeshConfig" :: Text) $ "meshConfig for table: " <> modelName <> " : " <> show updatedMeshConfig - pure updatedMeshConfig + tables' <- L.getOption KBT.Tables >>= maybe (L.throwException $ InternalError "Tables not found in setMeshConfig") pure + if modelName `elem` tables'.disableForKV + then pure $ meshConfig' {ecRedisDBStream = redisStream} + else do + let redisTtl' = HM.lookupDefault meshConfig'.redisTtl modelName tables'.kvTablesTtl + pure $ meshConfig' {meshEnabled = True, kvHardKilled = False, ecRedisDBStream = redisStream, redisTtl = redisTtl'} withUpdatedMeshConfig :: forall table m a. (L.MonadFlow m, HasCallStack, ModelMeta table) => Proxy table -> (MeshConfig -> m a) -> m a withUpdatedMeshConfig _ mkAction = do diff --git a/lib/mobility-core/src/Kernel/Types/Common.hs b/lib/mobility-core/src/Kernel/Types/Common.hs index 89ea92528..4699bcaf4 100644 --- a/lib/mobility-core/src/Kernel/Types/Common.hs +++ b/lib/mobility-core/src/Kernel/Types/Common.hs @@ -30,6 +30,7 @@ import Data.Aeson import Data.ByteString.Internal (ByteString) import Data.Fixed (Centi, Fixed (MkFixed)) import Data.Generics.Labels () +import qualified Data.HashMap.Strict as HM import Data.OpenApi hiding (value) import Data.Text as T import qualified Data.Vector as V @@ -57,7 +58,7 @@ import Kernel.Types.Logging as Common import Kernel.Types.MonadGuid as Common import Kernel.Types.Price as Common import Kernel.Types.Time as Common -import Kernel.Utils.Dhall (FromDhall, Natural) +import Kernel.Utils.Dhall (FromDhall) newtype IdObject = IdObject { id :: Text @@ -65,26 +66,9 @@ newtype IdObject = IdObject deriving stock (Generic, Show) deriving anyclass (ToJSON, FromJSON, ToSchema) -data KVTable = KVTable - { nameOfTable :: Text, - percentEnable :: Natural, - redisTtl :: Maybe Integer - } - deriving (Generic, Eq, Show, ToJSON, FromJSON, FromDhall, Ord) - -instance HasSqlValueSyntax be String => HasSqlValueSyntax be KVTable where - sqlValueSyntax = autoSqlValueSyntax - -instance BeamSqlBackend be => B.HasSqlEqualityCheck be KVTable - -instance FromBackendRow Postgres KVTable - -instance FromField KVTable where - fromField = fromFieldJSON - data Tables = Tables - { enableKVForWriteAlso :: [KVTable], - enableKVForRead :: [Text], + { disableForKV :: [Text], + kvTablesTtl :: HM.HashMap Text Integer, useCAC :: [Text], useCACForFrontend :: Bool, readFromMasterDb :: [Text]