From 4117fa0429aa6ae4ad0f1f7002700d6f4a0b26cc Mon Sep 17 00:00:00 2001 From: Pan Xiong Date: Mon, 6 Nov 2023 21:11:31 +1100 Subject: [PATCH] add index to origin transactions and snapshot --- app/models/origin_transaction.rb | 13 +++++++++---- app/models/transactions_snapshot_info.rb | 16 ++++++++-------- app/models/transactions_snapshot_record.rb | 2 +- ...106093031_add_index_to_origin_transactions.rb | 6 ++++++ ...add_index_to_transactions_snapshot_records.rb | 9 +++++++++ db/schema.rb | 9 ++++++++- 6 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20231106093031_add_index_to_origin_transactions.rb create mode 100644 db/migrate/20231106095236_add_index_to_transactions_snapshot_records.rb diff --git a/app/models/origin_transaction.rb b/app/models/origin_transaction.rb index 1da654f..654dfdc 100644 --- a/app/models/origin_transaction.rb +++ b/app/models/origin_transaction.rb @@ -17,9 +17,9 @@ def self.total_summary(user_id=nil) total_estimated_revenue = records.where(trade_type: 'buy').sum(&:revenue) total_roi = total_cost.zero? ? 0 : total_estimated_revenue / total_cost date = Date.yesterday - infos = TransactionsSnapshotInfo.includes(:snapshot_records).where("event_date <= ?", date) + infos = TransactionsSnapshotInfo.where("event_date <= ?", date) - { + summary = { profit_count: profit_records.count, profit_amount: calculate_field(profit_records), loss_count: loss_records.count, @@ -27,7 +27,10 @@ def self.total_summary(user_id=nil) total_cost: total_cost, total_revenue: records.where(trade_type: 'sell').sum(&:revenue), total_estimated_revenue: total_estimated_revenue, - total_roi: total_roi, + total_roi: total_roi + } + + summary.merge!({ max_profit: infos.max_profit(user_id: user_id), max_profit_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_profit_date"), max_loss: infos.max_loss(user_id: user_id), @@ -44,7 +47,9 @@ def self.total_summary(user_id=nil) max_profit_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_profit_roi_date"), max_loss_roi: infos.max_loss_roi(user_id: user_id), max_loss_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_loss_roi_date") - } + }) if user_id.nil? + + summary end def revenue_ratio(total_revenue) diff --git a/app/models/transactions_snapshot_info.rb b/app/models/transactions_snapshot_info.rb index d0a9c92..a56c61b 100644 --- a/app/models/transactions_snapshot_info.rb +++ b/app/models/transactions_snapshot_info.rb @@ -34,7 +34,7 @@ def self.max_profit(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_max_profit" total_profit = $redis.get(redis_key).to_f if total_profit == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all max_profit = infos.max {|a, b| a.total_profit <=> b.total_profit} if max_profit total_profit = max_profit.total_profit @@ -49,7 +49,7 @@ def self.max_loss(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_max_loss" total_loss = $redis.get(redis_key).to_f if total_loss == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all max_loss = infos.min {|a, b| a.total_loss <=> b.total_loss} if max_loss total_loss = max_loss.total_loss @@ -64,7 +64,7 @@ def self.max_revenue(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_max_revenue" total_revenue = $redis.get(redis_key).to_f if total_revenue == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all max_revenue = infos.max {|a, b| a.total_revenue <=> b.total_revenue} if max_revenue total_revenue = max_revenue.total_revenue @@ -79,7 +79,7 @@ def self.min_revenue(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_min_revenue" total_revenue = $redis.get(redis_key).to_f if total_revenue == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all min_revenue = infos.min {|a, b| a.total_revenue <=> b.total_revenue} if min_revenue total_revenue = min_revenue.total_revenue @@ -94,7 +94,7 @@ def self.max_roi(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_max_roi" total_roi = $redis.get(redis_key).to_f if total_roi == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all max_roi = infos.max {|a, b| a.total_roi <=> b.total_roi} if max_roi total_roi = max_roi.total_roi @@ -109,7 +109,7 @@ def self.min_roi(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_min_roi" total_roi = $redis.get(redis_key).to_f if total_roi == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all min_roi = infos.min {|a, b| a.total_roi <=> b.total_roi} if min_roi total_roi = min_roi.total_roi @@ -124,7 +124,7 @@ def self.max_profit_roi(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_max_profit_roi" total_roi = $redis.get(redis_key).to_f if total_roi == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all max_roi = infos.max {|a, b| a.total_profit_roi <=> b.total_profit_roi} if max_roi total_roi = max_roi.total_profit_roi @@ -139,7 +139,7 @@ def self.max_loss_roi(user_id: nil, date: Date.yesterday) redis_key = "user_#{user_id}_#{date.to_s}_spots_max_loss_roi" total_roi = $redis.get(redis_key).to_f if total_roi == 0 - infos = TransactionsSnapshotInfo.joins(:snapshot_records) + infos = TransactionsSnapshotInfo.all max_roi = infos.min {|a, b| a.total_loss_roi <=> b.total_loss_roi} if max_roi total_roi = max_roi.total_loss_roi diff --git a/app/models/transactions_snapshot_record.rb b/app/models/transactions_snapshot_record.rb index 8b394d7..fa6dbfa 100644 --- a/app/models/transactions_snapshot_record.rb +++ b/app/models/transactions_snapshot_record.rb @@ -21,7 +21,7 @@ def self.total_summary(user_id: nil, date: Date.yesterday) total_cost = calculate_field(records, :amount) total_estimated_revenue = records.where(trade_type: 'buy').sum(&:revenue) total_roi = total_cost.zero? ? 0 : total_estimated_revenue / total_cost - infos = TransactionsSnapshotInfo.includes(:snapshot_records).where("event_date <= ?", date) + infos = TransactionsSnapshotInfo.where("event_date <= ?", date) { profit_count: profit_records.count, diff --git a/db/migrate/20231106093031_add_index_to_origin_transactions.rb b/db/migrate/20231106093031_add_index_to_origin_transactions.rb new file mode 100644 index 0000000..71590d3 --- /dev/null +++ b/db/migrate/20231106093031_add_index_to_origin_transactions.rb @@ -0,0 +1,6 @@ +class AddIndexToOriginTransactions < ActiveRecord::Migration[6.1] + def change + add_index :origin_transactions, :trade_type + add_index :origin_transactions, [:user_id, :trade_type] + end +end diff --git a/db/migrate/20231106095236_add_index_to_transactions_snapshot_records.rb b/db/migrate/20231106095236_add_index_to_transactions_snapshot_records.rb new file mode 100644 index 0000000..f0b56b4 --- /dev/null +++ b/db/migrate/20231106095236_add_index_to_transactions_snapshot_records.rb @@ -0,0 +1,9 @@ +class AddIndexToTransactionsSnapshotRecords < ActiveRecord::Migration[6.1] + def change + add_index :transactions_snapshot_records, :from_symbol + add_index :transactions_snapshot_records, :amount + add_index :transactions_snapshot_records, :event_time + add_index :transactions_snapshot_records, :revenue + add_index :transactions_snapshot_records, :trade_type + end +end diff --git a/db/schema.rb b/db/schema.rb index 820750e..8951d95 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_11_04_080315) do +ActiveRecord::Schema.define(version: 2023_11_06_095236) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -226,6 +226,8 @@ t.index ["campaign"], name: "index_origin_transactions_on_campaign" t.index ["order_id"], name: "index_origin_transactions_on_order_id" t.index ["source"], name: "index_origin_transactions_on_source" + t.index ["trade_type"], name: "index_origin_transactions_on_trade_type" + t.index ["user_id", "trade_type"], name: "index_origin_transactions_on_user_id_and_trade_type" t.index ["user_id"], name: "index_origin_transactions_on_user_id" end @@ -422,6 +424,11 @@ t.datetime "event_time" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.index ["amount"], name: "index_transactions_snapshot_records_on_amount" + t.index ["event_time"], name: "index_transactions_snapshot_records_on_event_time" + t.index ["from_symbol"], name: "index_transactions_snapshot_records_on_from_symbol" + t.index ["revenue"], name: "index_transactions_snapshot_records_on_revenue" + t.index ["trade_type"], name: "index_transactions_snapshot_records_on_trade_type" t.index ["transactions_snapshot_info_id"], name: "index_snapshot_info_id" end