From 5b97c3166b20a90e446f2ced9bc712c20675eb8c Mon Sep 17 00:00:00 2001 From: Tsvetozar Penov Date: Mon, 20 Jan 2025 15:07:37 +0100 Subject: [PATCH 1/6] Remove incomplete_expired from active subs --- lib/sanbase/billing/subscription/query.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sanbase/billing/subscription/query.ex b/lib/sanbase/billing/subscription/query.ex index 3363722b41..de4f0a234c 100644 --- a/lib/sanbase/billing/subscription/query.ex +++ b/lib/sanbase/billing/subscription/query.ex @@ -32,7 +32,7 @@ defmodule Sanbase.Billing.Subscription.Query do def all_active_and_trialing_incomplete_subscriptions(query) do from(q in query, - where: q.status in ["active", "past_due", "trialing", "incomplete", "incomplete_expired"] + where: q.status in ["active", "past_due", "trialing", "incomplete"] ) end From 69f77c5f5b6d491f451d9c42a2d05aca1745d8dd Mon Sep 17 00:00:00 2001 From: Tsvetozar Penov Date: Mon, 20 Jan 2025 16:02:16 +0100 Subject: [PATCH 2/6] Revert "Remove incomplete_expired from active subs" --- lib/sanbase/billing/subscription/query.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sanbase/billing/subscription/query.ex b/lib/sanbase/billing/subscription/query.ex index de4f0a234c..3363722b41 100644 --- a/lib/sanbase/billing/subscription/query.ex +++ b/lib/sanbase/billing/subscription/query.ex @@ -32,7 +32,7 @@ defmodule Sanbase.Billing.Subscription.Query do def all_active_and_trialing_incomplete_subscriptions(query) do from(q in query, - where: q.status in ["active", "past_due", "trialing", "incomplete"] + where: q.status in ["active", "past_due", "trialing", "incomplete", "incomplete_expired"] ) end From c72bf502b1133c56ada57f8f23983ba0948c62d4 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Mon, 20 Jan 2025 16:59:39 +0200 Subject: [PATCH 3/6] Use application/json content type for wordsSocialVolume --- lib/sanbase/social_data/social_volume.ex | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/sanbase/social_data/social_volume.ex b/lib/sanbase/social_data/social_volume.ex index 7842e7cf72..8fe41201ff 100644 --- a/lib/sanbase/social_data/social_volume.ex +++ b/lib/sanbase/social_data/social_volume.ex @@ -170,15 +170,8 @@ defmodule Sanbase.SocialData.SocialVolume do } |> Jason.encode_to_iodata!() - options = [ - recv_timeout: @recv_timeout - ] - - # metricshub reads data from request.query_string and request.form - # Put this header so the data is read from the body properly - headers = [ - {"Content-Type", "application/x-www-form-urlencoded"} - ] + options = [recv_timeout: @recv_timeout] + headers = [{"Content-Type", "application/json"}] http_client().post(url, body, headers, options) end From 179db668ec468e31e1be27dfab668392b46aed4a Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 17 Jan 2025 14:36:15 +0200 Subject: [PATCH 4/6] Add livebook that compiles data about alert type --- livebooks/gather_alerts_info.livemd | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 livebooks/gather_alerts_info.livemd diff --git a/livebooks/gather_alerts_info.livemd b/livebooks/gather_alerts_info.livemd new file mode 100644 index 0000000000..7794ae367a --- /dev/null +++ b/livebooks/gather_alerts_info.livemd @@ -0,0 +1,34 @@ +# Untitled notebook + +## Section + +```elixir +type = "metric_signal" +alerts = Sanbase.Alert.UserTrigger.get_all_triggers_by_type(type) + +IO.puts("Got #{length(alerts)} alerts") +``` + +```elixir +data = + alerts + |> Enum.take(2) + |> Enum.map(fn alert -> + %{ + alert_id: alert.id, + user_id: alert.user_id, + inserted_at: alert.inserted_at, + updated_at: alert.updated_at, + metric: alert.trigger.settings.metric, + target: alert.trigger.settings.target, + channels: alert.trigger.settings.channel |> List.wrap() + } + end) +``` + +```elixir +json = Jason.encode!(data) + +Path.expand("~/alerts_data.json") +|> File.write!(json) +``` From 26367d8151896fd7f85a63c9b510ce9f211c2f01 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 21 Jan 2025 14:04:04 +0200 Subject: [PATCH 5/6] Fix typo in queriesExecutionsInfo --- lib/sanbase_web/graphql/resolvers/user/user_resolver.ex | 2 +- lib/sanbase_web/graphql/schema/types/user_types.ex | 2 +- test/sanbase_web/graphql/queries/queries_api_test.exs | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/sanbase_web/graphql/resolvers/user/user_resolver.ex b/lib/sanbase_web/graphql/resolvers/user/user_resolver.ex index 43356ed84a..d34cffc4b5 100644 --- a/lib/sanbase_web/graphql/resolvers/user/user_resolver.ex +++ b/lib/sanbase_web/graphql/resolvers/user/user_resolver.ex @@ -158,7 +158,7 @@ defmodule SanbaseWeb.Graphql.Resolvers.UserResolver do Sanbase.Queries.Authorization.query_executions_limit(subscription_product, plan_name) result = %{ - credits_availalbe_month: credits_limit, + credits_available_month: credits_limit, credits_spent_month: details.monthly_credits_spent, credits_remaining_month: credits_limit - details.monthly_credits_spent, queries_executed_month: details.queries_executed_month, diff --git a/lib/sanbase_web/graphql/schema/types/user_types.ex b/lib/sanbase_web/graphql/schema/types/user_types.ex index dd0e2ee32d..24127b9eb7 100644 --- a/lib/sanbase_web/graphql/schema/types/user_types.ex +++ b/lib/sanbase_web/graphql/schema/types/user_types.ex @@ -104,7 +104,7 @@ defmodule SanbaseWeb.Graphql.UserTypes do end object :queries_executions_info do - field(:credits_availalbe_month, non_null(:integer)) + field(:credits_available_month, non_null(:integer)) field(:credits_spent_month, non_null(:integer)) field(:credits_remaining_month, non_null(:integer)) field(:queries_executed_month, non_null(:integer)) diff --git a/test/sanbase_web/graphql/queries/queries_api_test.exs b/test/sanbase_web/graphql/queries/queries_api_test.exs index 7cdcf57440..70cf878943 100644 --- a/test/sanbase_web/graphql/queries/queries_api_test.exs +++ b/test/sanbase_web/graphql/queries/queries_api_test.exs @@ -285,7 +285,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do |> get_in(["data", "currentUser", "queriesExecutionsInfo"]) assert stats == %{ - "creditsAvailalbeMonth" => 500, + "creditsAvailableMonth" => 500, "creditsRemainingMonth" => 499, "creditsSpentMonth" => 1, "queriesExecutedDay" => 1, @@ -334,7 +334,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do assert Process.get(:queries_dynamic_repo) == Sanbase.ClickhouseRepo.BusinessMaxUser assert stats == %{ - "creditsAvailalbeMonth" => 500_000, + "creditsAvailableMonth" => 500_000, "creditsRemainingMonth" => 499_999, "creditsSpentMonth" => 1, "queriesExecutedDay" => 1, @@ -357,7 +357,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do currentUser { queriesExecutionsInfo { # credits info - creditsAvailalbeMonth + creditsAvailableMonth creditsSpentMonth creditsRemainingMonth # queries executed From f5793c0c539d19a349cc5b6289e62aef0bfde7bc Mon Sep 17 00:00:00 2001 From: Tsvetozar Penov Date: Tue, 21 Jan 2025 16:10:42 +0100 Subject: [PATCH 6/6] Cleanup annual discounts and warnings --- lib/sanbase/accounts/email_jobs.ex | 21 -------- .../billing_event_subscriber.ex | 49 +++++++------------ lib/sanbase/email/mailer.ex | 14 ------ lib/sanbase/email/template.ex | 8 --- lib/sanbase/transfers/erc20_transfers.ex | 2 - lib/sanbase_web.ex | 8 +-- lib/sanbase_web/components/core_components.ex | 2 +- lib/sanbase_web/gettext.ex | 4 +- test/sanbase/email/emails_test.exs | 36 -------------- 9 files changed, 25 insertions(+), 119 deletions(-) diff --git a/lib/sanbase/accounts/email_jobs.ex b/lib/sanbase/accounts/email_jobs.ex index 6fe523739a..d39c9b8df6 100644 --- a/lib/sanbase/accounts/email_jobs.ex +++ b/lib/sanbase/accounts/email_jobs.ex @@ -81,27 +81,6 @@ defmodule Sanbase.Accounts.EmailJobs do add_email_job(user.id, end_of_trial_template(), vars, scheduled_at: days_after(11)) end - def schedule_annual_discount_emails(subscription) do - user = Sanbase.Accounts.User.by_id!(subscription.user_id) - name = Sanbase.Accounts.User.get_name(user) - - common_vars = %{ - name: name, - username: name - } - - vars_50 = Map.put(common_vars, :end_subscription_date, days_after(14) |> format_date()) - vars_35 = Map.put(common_vars, :date, days_after(30) |> format_date()) - - add_email_job(subscription.user_id, during_trial_annual_discount_template(), vars_50, - scheduled_at: days_after(12) - ) - - add_email_job(subscription.user_id, after_trial_annual_discount_template(), vars_35, - scheduled_at: days_after(24) - ) - end - def send_pro_started_email(subscription) do user = Sanbase.Accounts.User.by_id!(subscription.user_id) name = Sanbase.Accounts.User.get_name(user) diff --git a/lib/sanbase/billing/subscription/event_handling/billing_event_subscriber.ex b/lib/sanbase/billing/subscription/event_handling/billing_event_subscriber.ex index 3d2ad4fd41..8ba98d54d8 100644 --- a/lib/sanbase/billing/subscription/event_handling/billing_event_subscriber.ex +++ b/lib/sanbase/billing/subscription/event_handling/billing_event_subscriber.ex @@ -75,32 +75,16 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do when event_type == :create_subscription do subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id) - Sanbase.Accounts.get_user(subscription.user_id) - |> case do - {:ok, user} -> - {:ok, _settings} = - Sanbase.Accounts.UserSettings.update_settings(user, %{ - is_subscribed_metric_updates: true - }) - - _ -> - :ok - end + toggle_settings(subscription.user_id, %{ + is_subscribed_metric_updates: true + }) cond do Subscription.trialing_sanbase_pro?(subscription) -> EmailJobs.send_trial_started_email(subscription) EmailJobs.schedule_trial_will_end_email(subscription) - case subscription.plan.interval do - "month" -> - # comment temporarily until we decide to bring back the annual discounts - # Sanbase.Accounts.EmailJobs.schedule_annual_discount_emails(subscription) - :ok - - _ -> - :ok - end + :ok Subscription.active_sanbase_pro?(subscription) -> Sanbase.Accounts.EmailJobs.send_pro_started_email(subscription) @@ -114,17 +98,9 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do when event_type == :cancel_subscription_at_period_end do subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id) - Sanbase.Accounts.get_user(subscription.user_id) - |> case do - {:ok, user} -> - {:ok, _settings} = - Sanbase.Accounts.UserSettings.update_settings(user, %{ - is_subscribed_metric_updates: false - }) - - _ -> - :ok - end + toggle_settings(subscription.user_id, %{ + is_subscribed_metric_updates: false + }) end defp do_handle(:send_discord_notification, event_type, event) @@ -150,4 +126,15 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do defp do_handle(_type, _event_type, _event) do :ok end + + defp toggle_settings(user_id, settings) do + Sanbase.Accounts.get_user(user_id) + |> case do + {:ok, user} -> + Sanbase.Accounts.UserSettings.update_settings(user, settings) + + _ -> + :ok + end + end end diff --git a/lib/sanbase/email/mailer.ex b/lib/sanbase/email/mailer.ex index 2d8e3a1348..1be942a20f 100644 --- a/lib/sanbase/email/mailer.ex +++ b/lib/sanbase/email/mailer.ex @@ -9,8 +9,6 @@ defmodule Sanbase.Mailer do alias Sanbase.Billing.{Subscription, Product} @edu_templates ~w(first-edu-email second-edu-email) - @during_trial_annual_discount_template during_trial_annual_discount_template() - @after_trial_annual_discount_template after_trial_annual_discount_template() @end_of_trial_template end_of_trial_template() @trial_started_template trial_started_template() @@ -75,18 +73,6 @@ defmodule Sanbase.Mailer do not is_excluded_email?(user.email) end - defp can_send?(user, template, _params) - when template == @during_trial_annual_discount_template do - res = Sanbase.Billing.Subscription.annual_discount_eligibility(user.id) - res.is_eligible and res.discount.percent_off == 50 - end - - defp can_send?(user, template, _params) - when template == @after_trial_annual_discount_template do - res = Sanbase.Billing.Subscription.annual_discount_eligibility(user.id) - res.is_eligible and res.discount.percent_off == 35 - end - defp can_send?(_user, _template, _params), do: true defp has_card?(user) do diff --git a/lib/sanbase/email/template.ex b/lib/sanbase/email/template.ex index 330678f3a3..475934f792 100644 --- a/lib/sanbase/email/template.ex +++ b/lib/sanbase/email/template.ex @@ -164,12 +164,6 @@ defmodule Sanbase.Email.Template do # Send 3 days before trial ends @end_of_trial_template "trial-end-mail" - # Send 2 days before trial ends - @during_trial_annual_discount_template "50-percent-discount-offer" - - # Send once - 1 week before monthly Sanbase Pro ends - @after_trial_annual_discount_template "35-percent-discount-offer" - @automatic_renewal_template "automatic_renewal" # Send after 2 weeks of inactivity. @@ -190,8 +184,6 @@ defmodule Sanbase.Email.Template do def post_cancellation_template2, do: @post_cancellation_template2 def end_of_trial_template, do: @end_of_trial_template def trial_started_template, do: @trial_started_template - def during_trial_annual_discount_template, do: @during_trial_annual_discount_template - def after_trial_annual_discount_template, do: @after_trial_annual_discount_template def automatic_renewal_template, do: @automatic_renewal_template def comment_notification_template, do: @comment_notification_template diff --git a/lib/sanbase/transfers/erc20_transfers.ex b/lib/sanbase/transfers/erc20_transfers.ex index f10d4ec757..37681eec63 100644 --- a/lib/sanbase/transfers/erc20_transfers.ex +++ b/lib/sanbase/transfers/erc20_transfers.ex @@ -9,8 +9,6 @@ defmodule Sanbase.Transfers.Erc20Transfers do alias Sanbase.ClickhouseRepo alias Sanbase.Project - alias Sanbase.Utils.Config - defguard is_non_neg_integer(int) when is_integer(int) and int > 0 @spec top_wallet_transfers( diff --git a/lib/sanbase_web.ex b/lib/sanbase_web.ex index 684295a32c..41e63c2799 100644 --- a/lib/sanbase_web.ex +++ b/lib/sanbase_web.ex @@ -33,7 +33,7 @@ defmodule SanbaseWeb do def channel do quote do use Phoenix.Channel - import SanbaseWeb.Gettext + use Gettext, backend: SanbaseWeb.Gettext unquote(verified_routes()) end @@ -46,7 +46,7 @@ defmodule SanbaseWeb do layouts: [html: SanbaseWeb.Layouts] import Plug.Conn - import SanbaseWeb.Gettext + use Gettext, backend: SanbaseWeb.Gettext unquote(verified_routes()) end end @@ -111,7 +111,7 @@ defmodule SanbaseWeb do import Phoenix.View import SanbaseWeb.ErrorHelpers - import SanbaseWeb.Gettext + use Gettext, backend: SanbaseWeb.Gettext alias SanbaseWeb.Router.Helpers, as: Routes unquote(verified_routes()) @@ -124,7 +124,7 @@ defmodule SanbaseWeb do import Phoenix.HTML # Core UI components and translation import SanbaseWeb.CoreComponents - import SanbaseWeb.Gettext + use Gettext, backend: SanbaseWeb.Gettext # Shortcut for generating JS commands alias Phoenix.LiveView.JS diff --git a/lib/sanbase_web/components/core_components.ex b/lib/sanbase_web/components/core_components.ex index 426d1951ea..591e49fcf0 100644 --- a/lib/sanbase_web/components/core_components.ex +++ b/lib/sanbase_web/components/core_components.ex @@ -17,7 +17,7 @@ defmodule SanbaseWeb.CoreComponents do use Phoenix.Component alias Phoenix.LiveView.JS - import SanbaseWeb.Gettext + use Gettext, backend: SanbaseWeb.Gettext @doc """ Renders a modal. diff --git a/lib/sanbase_web/gettext.ex b/lib/sanbase_web/gettext.ex index 3745a24646..b301fb927b 100644 --- a/lib/sanbase_web/gettext.ex +++ b/lib/sanbase_web/gettext.ex @@ -5,7 +5,7 @@ defmodule SanbaseWeb.Gettext do By using [Gettext](https://hexdocs.pm/gettext), your module gains a set of macros for translations, for example: - import SanbaseWeb.Gettext + use Gettext, backend: SanbaseWeb.Gettext # Simple translation gettext "Here is the string to translate" @@ -20,5 +20,5 @@ defmodule SanbaseWeb.Gettext do See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage. """ - use Gettext, otp_app: :sanbase + use Gettext.Backend, otp_app: :sanbase end diff --git a/test/sanbase/email/emails_test.exs b/test/sanbase/email/emails_test.exs index 6d3cbeed69..e6abea9262 100644 --- a/test/sanbase/email/emails_test.exs +++ b/test/sanbase/email/emails_test.exs @@ -257,42 +257,6 @@ defmodule Sanbase.EmailsTest do refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_)) end - - test "do not send discount 50% email", context do - assert :ok = - perform_job(Sanbase.Mailer, %{ - "user_id" => context.user.id, - "template" => during_trial_annual_discount_template() - }) - - refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_)) - end - - test "do not send discount 35% email", context do - assert :ok = - perform_job(Sanbase.Mailer, %{ - "user_id" => context.user.id, - "template" => after_trial_annual_discount_template() - }) - - refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_)) - end - - test "do not send discount 35% email if more than 30 days passed", context do - insert(:subscription_pro_sanbase, - user: context.user, - status: "trialing", - trial_end: days_after(-20) - ) - - assert :ok = - perform_job(Sanbase.Mailer, %{ - "user_id" => context.user.id, - "template" => after_trial_annual_discount_template() - }) - - refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_)) - end end defp email_login_verify_mutation(user) do