Skip to content

Commit

Permalink
add index to origin transactions and snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 6, 2023
1 parent 37ba88d commit 4117fa0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
13 changes: 9 additions & 4 deletions app/models/origin_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ 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,
loss_amount: calculate_field(loss_records),
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),
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions app/models/transactions_snapshot_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/transactions_snapshot_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20231106093031_add_index_to_origin_transactions.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4117fa0

Please sign in to comment.