From bed51da47499ed2669ac9833c89e5574edd6cef9 Mon Sep 17 00:00:00 2001 From: Daniel Woelfel Date: Tue, 26 Nov 2024 13:17:52 -0500 Subject: [PATCH] move drop-refresh-spam to flag --- server/flags-config/instant.perms.ts | 140 +++++++++++++----------- server/flags-config/instant.schema.ts | 46 ++++---- server/src/instant/config.clj | 2 - server/src/instant/flags.clj | 34 +++++- server/src/instant/reactive/session.clj | 44 ++++---- 5 files changed, 153 insertions(+), 113 deletions(-) diff --git a/server/flags-config/instant.perms.ts b/server/flags-config/instant.perms.ts index 8633cc1cf..313425078 100644 --- a/server/flags-config/instant.perms.ts +++ b/server/flags-config/instant.perms.ts @@ -1,79 +1,91 @@ -export default { - "attrs": { - "allow": { - "create": "false" - } +// Docs: https://www.instantdb.com/docs/permissions + +const rules = { + attrs: { + allow: { + create: "false", + }, }, "storage-whitelist": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, "friend-emails": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, "view-checks": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } - }, - "app-users-to-triples-migration": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, "power-user-emails": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, "test-emails": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, "promo-emails": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, + }, + hazelcast: { + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, - "hazelcast": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } + "drop-refresh-spam": { + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, + }, + custodian: { + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, }, "team-emails": { - "allow": { - "view": "false", - "create": "false", - "delete": "false", - "update": "false" - } - } -}; \ No newline at end of file + allow: { + view: "false", + create: "false", + delete: "false", + update: "false", + }, + }, +}; + +export default rules; diff --git a/server/flags-config/instant.schema.ts b/server/flags-config/instant.schema.ts index 46cb94575..2b9460535 100644 --- a/server/flags-config/instant.schema.ts +++ b/server/flags-config/instant.schema.ts @@ -1,50 +1,50 @@ -// instant-config -// https://instantdb.com/dash?s=main&t=home&app=24a4d71b-7bb2-4630-9aee-01146af26239 +// http://localhost:3000/dash?s=main&t=home&app=24a4d71b-7bb2-4630-9aee-01146af26239 +// Docs: https://www.instantdb.com/docs/schema import { i } from "@instantdb/core"; const graph = i.graph( { - "$users": i.entity({ - "email": i.string().unique().indexed(), + $users: i.entity({ + email: i.string().unique().indexed(), }), - "app-users-to-triples-migration": i.entity({ - "appId": i.string(), - "processId": i.string(), + "drop-refresh-spam": i.entity({ + "default-value": i.boolean(), + "disabled-apps": i.any(), + "enabled-apps": i.any(), }), "friend-emails": i.entity({ - "email": i.string().unique(), + email: i.string().unique(), }), - "hazelcast": i.entity({ + hazelcast: i.entity({ "default-value": i.boolean(), - "disabled": i.boolean(), + disabled: i.boolean(), "disabled-apps": i.any(), "enabled-apps": i.any(), }), "power-user-emails": i.entity({ - "email": i.string().unique(), + email: i.string().unique(), }), "promo-emails": i.entity({ - "email": i.string(), + email: i.string(), }), "storage-whitelist": i.entity({ - "appId": i.string().unique().indexed(), - "email": i.string(), - "isEnabled": i.boolean(), + appId: i.string().unique().indexed(), + email: i.string(), + isEnabled: i.boolean(), }), "team-emails": i.entity({ - "email": i.string(), + email: i.string(), }), "test-emails": i.entity({ - "email": i.string(), - }), - "view-checks": i.entity({ - "default-value": i.boolean(), - "disabled-apps": i.any(), - "enabled-apps": i.any(), + email: i.string(), }), }, - {} + // You can define links here. + // For example, if `posts` should have many `comments`. + // More in the docs: + // https://www.instantdb.com/docs/schema#defining-links + {}, ); export default graph; diff --git a/server/src/instant/config.clj b/server/src/instant/config.clj index 06a3c4d11..57ed122c1 100644 --- a/server/src/instant/config.clj +++ b/server/src/instant/config.clj @@ -67,8 +67,6 @@ (when-let [app-id (System/getenv "INSTANT_ON_INSTANT_APP_ID")] (parse-uuid app-id))) -(def drop-refresh-spam? (= "true" (System/getenv "DROP_REFRESH_SPAM"))) - (defn db-url->config [url] (cond (string/starts-with? url "jdbc") {:jdbcUrl url} diff --git a/server/src/instant/flags.clj b/server/src/instant/flags.clj index 79c48763f..46a9f5387 100644 --- a/server/src/instant/flags.clj +++ b/server/src/instant/flags.clj @@ -12,8 +12,8 @@ :storage-whitelist {} :team-emails {} :test-emails {} - :view-checks {} :hazelcast {} + :drop-refresh-spam {} :promo-emails {}}) (defn transform-query-result @@ -60,11 +60,26 @@ :disabled? disabled?})) promo-code-emails (set (keep (fn [o] (get o "email")) - (get result "promo-emails")))] + (get result "promo-emails"))) + drop-refresh-spam (when-let [hz-flag (-> (get result "drop-refresh-spam") + first)] + (let [disabled-apps (-> hz-flag + (get "disabled-apps") + (#(map parse-uuid %)) + set) + enabled-apps (-> hz-flag + (get "enabled-apps") + (#(map parse-uuid %)) + set) + default-value (get hz-flag "default-value" false)] + {:disabled-apps disabled-apps + :enabled-apps enabled-apps + :default-value default-value}))] {:emails emails :storage-enabled-whitelist storage-enabled-whitelist :hazelcast hazelcast - :promo-code-emails promo-code-emails})) + :promo-code-emails promo-code-emails + :drop-refresh-spam drop-refresh-spam})) (def queries [{:query query :transform #'transform-query-result}]) @@ -109,3 +124,16 @@ (defn hazelcast-disabled? [] (get-in (query-result) [:hazelcast :disabled?] false)) + +(defn drop-refresh-spam? [app-id] + (if-let [flag (get (query-result) :drop-refresh-spam)] + (let [{:keys [disabled-apps enabled-apps default-value]} flag] + (cond (contains? disabled-apps app-id) + false + + (contains? enabled-apps app-id) + true + + :else default-value)) + ;; Default false + false)) diff --git a/server/src/instant/reactive/session.clj b/server/src/instant/reactive/session.clj index 5f9249624..337752a79 100644 --- a/server/src/instant/reactive/session.clj +++ b/server/src/instant/reactive/session.clj @@ -7,36 +7,36 @@ Each connection has their own `session` worker, that can understand these commands." (:require - [lambdaisland.uri :as uri] - [instant.config :as config] - [instant.util.async :as ua] - [instant.jdbc.aurora :as aurora] - [instant.jdbc.sql :as sql] - [instant.reactive.store :as rs] - [instant.reactive.query :as rq] - [instant.db.transaction :as tx] - [instant.util.tracer :as tracer] + [clojure.main :refer [root-cause]] [instant.db.datalog :as d] - [instant.util.json :refer [<-json]] - [instant.util.delay :as delay] - [instant.model.app :as app-model] [instant.db.model.attr :as attr-model] + [instant.db.permissioned-transaction :as permissioned-tx] [instant.db.pg-introspect :as pg-introspect] + [instant.db.transaction :as tx] + [instant.flags :as flags] + [instant.grouped-queue :as grouped-queue] + [instant.jdbc.aurora :as aurora] + [instant.jdbc.sql :as sql] + [instant.model.app :as app-model] + [instant.model.app-admin-token :as app-admin-token-model] [instant.model.app-user :as app-user-model] [instant.model.instant-user :as instant-user-model] - [instant.model.app-admin-token :as app-admin-token-model] - [instant.db.permissioned-transaction :as permissioned-tx] [instant.model.rule :as rule-model] - [clojure.main :refer [root-cause]] [instant.reactive.ephemeral :as eph] + [instant.reactive.query :as rq] + [instant.reactive.receive-queue :as receive-queue :refer [receive-q]] + [instant.reactive.store :as rs] + [instant.util.async :as ua] + [instant.util.delay :as delay] [instant.util.exception :as ex] + [instant.util.json :refer [<-json]] + [instant.util.tracer :as tracer] [instant.util.uuid :as uuid-util] - [instant.grouped-queue :as grouped-queue] - [instant.reactive.receive-queue :as receive-queue :refer [receive-q]]) + [lambdaisland.uri :as uri]) (:import + (java.time Duration Instant) (java.util.concurrent CancellationException) - (java.util.concurrent.atomic AtomicLong) - (java.time Duration Instant))) + (java.util.concurrent.atomic AtomicLong))) ;; ------ ;; Setup @@ -188,13 +188,15 @@ num-spam (count spam) num-computations (count computations) num-recomputations (count recompute-results) - computations (if config/drop-refresh-spam? + drop-spam? (flags/drop-refresh-spam? app-id) + computations (if drop-spam? computations recompute-results)] (tracer/with-span! {:name "handle-refresh/send-event!" :attributes {:num-recomputations num-recomputations :num-spam num-spam - :num-computations num-computations}} + :num-computations num-computations + :dropped-spam? drop-spam?}} (when (seq computations) (rs/send-event! store-conn app-id sess-id {:op :refresh-ok :processed-tx-id processed-tx-id