Skip to content

Commit

Permalink
add columns to transactions snapshot infos
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 4, 2023
1 parent 227b679 commit db947dc
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/jobs/generate_origin_transactions_snapshots_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def perform(date: Date.today)
# 生成快照记录
generate_snapshot(transactions_snapshot_info)

GetTransactionsSnapshotInfoSummaryJob.perform_later(snapshot_info, date)

# 触发垃圾回收任务
ForceGcJob.perform_later
end
Expand Down
36 changes: 36 additions & 0 deletions app/jobs/get_transactions_snapshot_info_summary_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class GetTransactionsSnapshotInfoSummaryJob < ApplicationJob
queue_as :default

def perform(snapshot_info, date)
total_summary = date == Date.today ? OriginTransaction.total_summary : snapshot_info.snapshot_records.total_summary(date: snapshot_info.event_date)
total_revenue = total_summary[:total_revenue].to_f
total_cost = total_summary[:total_cost].to_f
total_roi = total_cost == 0 ? 0 : ((total_revenue / total_cost) * 100).round(4)

snapshot_info.update(
profit_count: total_summary[:profit_count],
profit_amount: total_summary[:profit_amount],
loss_count: total_summary[:loss_count],
loss_amount: total_summary[:loss_amount],
total_cost: total_cost,
total_revenue: total_revenue,
total_roi: total_roi,
max_profit: total_summary[:max_profit],
max_profit_date: total_summary[:max_profit_date],
max_loss: total_summary[:max_loss],
max_loss_date: total_summary[:max_loss_date],
max_revenue: total_summary[:max_revenue],
max_revenue_date: total_summary[:max_revenue_date],
min_revenue: total_summary[:min_revenue],
min_revenue_date: total_summary[:min_revenue_date],
max_roi: total_summary[:max_roi],
max_roi_date: total_summary[:max_roi_date],
max_profit_roi: total_summary[:max_profit_roi],
max_profit_roi_date: total_summary[:max_profit_roi_date],
max_loss_roi: total_summary[:max_loss_roi],
max_loss_roi_date: total_summary[:max_loss_roi_date],
min_roi: total_summary[:min_roi],
min_roi_date: total_summary[:min_roi_date]
)
end
end
43 changes: 36 additions & 7 deletions app/models/transactions_snapshot_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,40 @@ def self.year_to_date
TransactionsSnapshotRecord.where('event_time >= ?', DateTime.parse('2023-01-01'))
end

def self.total_summary
records = TransactionsSnapshotRecord.available.year_to_date.where(trade_type: 'buy')
def self.total_summary(user_id: nil, date: Date.yesterday)
records = TransactionsSnapshotRecord.available.year_to_date
profit_records = records.select{|r| r.revenue > 0}
loss_records = records.select{|r| r.revenue < 0}
result = {
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)

{
profit_count: profit_records.count,
profit_amount: profit_records.sum(&:revenue),
profit_amount: calculate_field(profit_records),
loss_count: loss_records.count,
loss_amount: loss_records.sum(&:revenue),
total_cost: records.sum(&:amount),
total_revenue: records.sum(&:revenue)
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,
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),
max_loss_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_loss_date"),
max_revenue: infos.max_revenue(user_id: user_id),
max_revenue_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_revenue_date"),
min_revenue: infos.min_revenue(user_id: user_id),
min_revenue_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_min_revenue_date"),
max_roi: infos.max_roi(user_id: user_id),
max_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_max_roi_date"),
min_roi: infos.min_roi(user_id: user_id),
min_roi_date: $redis.get("user_#{user_id}_#{date.to_s}_spots_min_roi_date"),
max_profit_roi: infos.max_profit_roi(user_id: user_id),
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")
}
end

Expand All @@ -35,4 +58,10 @@ def revenue_ratio(total_cost)
def cost_ratio(total_cost)
amount / total_cost
end

private
def self.calculate_field(records, field_name = :revenue)
buys, sells = records.partition { |record| record.trade_type == "buy" }
buys.sum { |record| record.send(field_name) } - sells.sum { |record| record.send(field_name) }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class AddColumnsToTransactionsSnapshotInfos < ActiveRecord::Migration[6.1]
def change
add_column :transactions_snapshot_infos, :total_cost, :decimal
add_column :transactions_snapshot_infos, :total_revenue, :decimal
add_column :transactions_snapshot_infos, :total_roi, :decimal
add_column :transactions_snapshot_infos, :profit_count, :integer
add_column :transactions_snapshot_infos, :profit_amount, :decimal
add_column :transactions_snapshot_infos, :loss_count, :integer
add_column :transactions_snapshot_infos, :loss_amount, :decimal
add_column :transactions_snapshot_infos, :max_profit, :decimal
add_column :transactions_snapshot_infos, :max_loss, :decimal
add_column :transactions_snapshot_infos, :max_revenue, :decimal
add_column :transactions_snapshot_infos, :min_revenue, :decimal
add_column :transactions_snapshot_infos, :max_profit_date, :datetime
add_column :transactions_snapshot_infos, :max_loss_date, :datetime
add_column :transactions_snapshot_infos, :max_revenue_date, :datetime
add_column :transactions_snapshot_infos, :min_revenue_date, :datetime
add_column :transactions_snapshot_infos, :max_roi, :decimal
add_column :transactions_snapshot_infos, :max_roi_date, :datetime
add_column :transactions_snapshot_infos, :max_profit_roi, :decimal
add_column :transactions_snapshot_infos, :max_profit_roi_date, :datetime
add_column :transactions_snapshot_infos, :max_loss_roi, :decimal
add_column :transactions_snapshot_infos, :max_loss_roi_date, :datetime
add_column :transactions_snapshot_infos, :min_roi, :decimal
add_column :transactions_snapshot_infos, :min_roi_date, :datetime
end
end
25 changes: 24 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 db947dc

Please sign in to comment.