From db947dc365b68809d432da010179f207f12b226f Mon Sep 17 00:00:00 2001 From: Pan Xiong Date: Sat, 4 Nov 2023 19:30:52 +1100 Subject: [PATCH] add columns to transactions snapshot infos --- ...erate_origin_transactions_snapshots_job.rb | 2 + ..._transactions_snapshot_info_summary_job.rb | 36 ++++++++++++++++ app/models/transactions_snapshot_record.rb | 43 ++++++++++++++++--- ..._columns_to_transactions_snapshot_infos.rb | 27 ++++++++++++ db/schema.rb | 25 ++++++++++- 5 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 app/jobs/get_transactions_snapshot_info_summary_job.rb create mode 100644 db/migrate/20231104080315_add_columns_to_transactions_snapshot_infos.rb diff --git a/app/jobs/generate_origin_transactions_snapshots_job.rb b/app/jobs/generate_origin_transactions_snapshots_job.rb index 44b66f7..dd43dac 100644 --- a/app/jobs/generate_origin_transactions_snapshots_job.rb +++ b/app/jobs/generate_origin_transactions_snapshots_job.rb @@ -8,6 +8,8 @@ def perform(date: Date.today) # 生成快照记录 generate_snapshot(transactions_snapshot_info) + GetTransactionsSnapshotInfoSummaryJob.perform_later(snapshot_info, date) + # 触发垃圾回收任务 ForceGcJob.perform_later end diff --git a/app/jobs/get_transactions_snapshot_info_summary_job.rb b/app/jobs/get_transactions_snapshot_info_summary_job.rb new file mode 100644 index 0000000..5631a8e --- /dev/null +++ b/app/jobs/get_transactions_snapshot_info_summary_job.rb @@ -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 diff --git a/app/models/transactions_snapshot_record.rb b/app/models/transactions_snapshot_record.rb index 84a6129..8b394d7 100644 --- a/app/models/transactions_snapshot_record.rb +++ b/app/models/transactions_snapshot_record.rb @@ -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 @@ -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 diff --git a/db/migrate/20231104080315_add_columns_to_transactions_snapshot_infos.rb b/db/migrate/20231104080315_add_columns_to_transactions_snapshot_infos.rb new file mode 100644 index 0000000..3cecb2e --- /dev/null +++ b/db/migrate/20231104080315_add_columns_to_transactions_snapshot_infos.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 912c808..820750e 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_10_21_091525) do +ActiveRecord::Schema.define(version: 2023_11_04_080315) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -376,6 +376,29 @@ t.date "event_date" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.decimal "total_cost" + t.decimal "total_revenue" + t.decimal "total_roi" + t.integer "profit_count" + t.decimal "profit_amount" + t.integer "loss_count" + t.decimal "loss_amount" + t.decimal "max_profit" + t.decimal "max_loss" + t.decimal "max_revenue" + t.decimal "min_revenue" + t.datetime "max_profit_date" + t.datetime "max_loss_date" + t.datetime "max_revenue_date" + t.datetime "min_revenue_date" + t.decimal "max_roi" + t.datetime "max_roi_date" + t.decimal "max_profit_roi" + t.datetime "max_profit_roi_date" + t.decimal "max_loss_roi" + t.datetime "max_loss_roi_date" + t.decimal "min_roi" + t.datetime "min_roi_date" t.index ["event_date"], name: "index_transactions_snapshot_infos_on_event_date" end