Skip to content

Commit

Permalink
Update transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 7, 2023
1 parent 1dd85a1 commit a0fcb69
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
25 changes: 15 additions & 10 deletions app/controllers/origin_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ def index
sort_type = params[:sort_type].presence || "desc"
txs = OriginTransaction.available.year_to_date.where(user_id: nil).order("#{sort} #{sort_type}")
@total_txs = txs
@symbol = params[:search]
@campaign = params[:campaign]
@source = params[:source]
txs = filter_txs(txs)
@event_date = Date.parse(params[:event_date]) rescue nil
txs = txs.where(campaign: @campaign) if @campaign.present?
txs = txs.where(source: @source) if @source.present?
txs = txs.where(original_symbol: @symbol) if @symbol.present?
txs = txs.where(event_time: @event_date.all_day) if @event_date.present?
@txs = txs.page(params[:page]).per(20)
@total_summary = txs.total_summary
Expand All @@ -23,12 +18,9 @@ def users
@page_index = 19
sort = params[:sort].presence || "event_time"
sort_type = params[:sort_type].presence || "desc"
@symbol = params[:search]
txs = OriginTransaction.available.year_to_date.where(user_id: current_user.id).order("#{sort} #{sort_type}")
@total_txs = txs
txs = txs.where(campaign: params[:campaign]) if params[:campaign].present?
txs = txs.where(source: params[:source]) if params[:source].present?
txs = txs.where(original_symbol: @symbol) if @symbol.present?
txs = filter_txs(txs)
@txs = txs.page(params[:page]).per(20)
@total_summary = txs.total_summary(current_user.id)
end
Expand Down Expand Up @@ -102,4 +94,17 @@ def period_date
else Date.today.last_month.to_date
end
end

def filter_txs(txs)
@symbol = params[:search]
@campaign = params[:campaign]
@source = params[:source]
@trade_type = params[:trade_type]

txs = txs.where(campaign: @campaign) if @campaign.present?
txs = txs.where(source: @source) if @source.present?
txs = txs.where(original_symbol: @symbol) if @symbol.present?
txs = txs.where(trade_type: @trade_type) if @trade_type.present?
txs
end
end
9 changes: 3 additions & 6 deletions app/controllers/snapshot_infos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ def positions_graphs
end

def export_roi
infos = SnapshotInfo.includes(:snapshot_positions).where(user_id: nil, event_date: [Date.today.last_quarter.to_date..Date.yesterday]).order(event_date: :desc)
infos = SnapshotInfo.where(user_id: nil, event_date: [Date.today.last_quarter.to_date..Date.yesterday]).includes(:snapshot_positions).order(event_date: :desc)

file = CSV.generate(force_quotes: true) do |csv|
csv << ['日期', '总投入', '总收益', '收益占比']
infos.each do |info|
total_summary = info.snapshot_positions.total_summary
total_cost = total_summary[:total_cost]
total_revenue = total_summary[:total_revenue]
roi = ((total_revenue / total_cost) * 100).round(4).to_s + "%"
csv << [info.event_date, total_cost.round(4), total_revenue.round(4), roi]
roi = info.total_roi.to_s + "%"
csv << [info.event_date, info.total_cost.round(4), info.total_revenue.round(4), roi]
end
end

Expand Down
5 changes: 3 additions & 2 deletions app/models/origin_transaction.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class OriginTransaction < ApplicationRecord
SKIP_SYMBOLS = %w(USDC BTC ETH).freeze
SKIP_SYMBOLS = %w(BTC ETH).freeze
FILTER_SYMBOL = %w(USDC).freeze

def self.available
OriginTransaction.where('(from_symbol IN (?) AND amount >= 50) OR from_symbol NOT IN (?)', SKIP_SYMBOLS, SKIP_SYMBOLS)
OriginTransaction.where('from_symbol NOT IN (?) and ((from_symbol IN (?) AND amount >= 50) OR from_symbol NOT IN (?))', FILTER_SYMBOL, SKIP_SYMBOLS, SKIP_SYMBOLS)
end

def self.year_to_date
Expand Down
2 changes: 1 addition & 1 deletion app/models/snapshot_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.total_summary(user_id: nil, is_synced: false, date: Date.yesterday)
records = SnapshotPosition.available
profit_records = records.profit
loss_records = records.loss
infos = SnapshotInfo.includes(:snapshot_positions).where("event_date <= ?", date)
infos = SnapshotInfo.where("event_date <= ?", date)
redis_key = is_synced ? "user_#{user_id}_#{date.to_s}_synced_positions" : "user_#{user_id}_#{date.to_s}_positions"

{
Expand Down
3 changes: 2 additions & 1 deletion app/models/transactions_snapshot_record.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class TransactionsSnapshotRecord < ApplicationRecord
SKIP_SYMBOLS = %w(USDC BTC ETH).freeze
FILTER_SYMBOL = %w(USDC).freeze

belongs_to :snapshot_info, class_name: 'TransactionsSnapshotInfo', foreign_key: :transactions_snapshot_info_id

scope :profit, -> { where("revenue > 0") }
scope :loss, -> { where("revenue < 0") }

def self.available
TransactionsSnapshotRecord.where('(from_symbol IN (?) AND amount >= 50) OR from_symbol NOT IN (?)', SKIP_SYMBOLS, SKIP_SYMBOLS)
TransactionsSnapshotRecord.where('from_symbol NOT IN (?) and ((from_symbol IN (?) AND amount >= 50) OR from_symbol NOT IN (?))', FILTER_SYMBOL, SKIP_SYMBOLS, SKIP_SYMBOLS)
end

def self.year_to_date
Expand Down
2 changes: 1 addition & 1 deletion app/models/user_synced_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def self.total_summary(user_id=nil)
profit_records = records.select{|r| r.revenue > 0}
loss_records = records.select{|r| r.revenue < 0}
date = Date.yesterday
infos = SnapshotInfo.synced.includes(:snapshot_positions).where("event_date <= ?", date)
infos = SnapshotInfo.synced.where("event_date <= ?", date)
{
profit_count: profit_records.count,
profit_amount: profit_records.sum(&:revenue),
Expand Down
8 changes: 5 additions & 3 deletions app/views/origin_transactions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
<div class="input-group mb-3 position-filter">
<%= select_tag(:search, options_for_select(@total_txs.pluck(:original_symbol).uniq, @symbol), { prompt: '请选择币种...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:campaign, options_for_select(@total_txs.pluck(:campaign).compact.uniq, params[:campaign]),{ prompt: '请选择campaign...', class: 'select2-dropdown form-control' }) %>
<%= select_tag(:campaign, options_for_select(@total_txs.pluck(:campaign).compact.uniq, @campaign),{ prompt: '请选择campaign...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:source, options_for_select(@total_txs.pluck(:source).compact.uniq, params[:source]),{ prompt: '请选择来源...', class: 'select2-dropdown form-control' }) %>
<%= select_tag(:trade_type, options_for_select([['买入', 'buy'], ['卖出', 'sell']], @trade_type),{ prompt: '请选择交易类别...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= text_field_tag :event_date, params[:event_date], class: "form-control datepicker", type: "text", placeholder: "请选择交易日期", autocomplete: "off" %>
<%= select_tag(:source, options_for_select(@total_txs.pluck(:source).compact.uniq, @source),{ prompt: '请选择来源...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= text_field_tag :event_date, @event_date, class: "form-control datepicker", type: "text", placeholder: "请选择交易日期", autocomplete: "off" %>
<span class='ms-3'></span>
<button type="submit" class="btn btn-primary mx-3">确定</button>
<%= link_to "Reset", origin_transactions_path, class: 'btn btn-warning me-3' %>
Expand Down
6 changes: 4 additions & 2 deletions app/views/origin_transactions/users.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
<div class="input-group mb-3 position-filter">
<%= select_tag(:search, options_for_select(@total_txs.pluck(:original_symbol).uniq, @symbol), { prompt: '请选择币种...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:campaign, options_for_select(@total_txs.pluck(:campaign).compact.uniq, params[:campaign]),{ prompt: '请选择campaign...', class: 'select2-dropdown form-control', onchange: 'this.form.submit()' }) %>
<%= select_tag(:campaign, options_for_select(@total_txs.pluck(:campaign).compact.uniq, @campaign),{ prompt: '请选择campaign...', class: 'select2-dropdown form-control', onchange: 'this.form.submit()' }) %>
<span class='ms-3'></span>
<%= select_tag(:source, options_for_select(@total_txs.pluck(:source).compact.uniq, params[:source]),{ prompt: '请选择来源...', class: 'select2-dropdown form-control', onchange: 'this.form.submit()' }) %>
<%= select_tag(:trade_type, options_for_select([['买入', 'buy'], ['卖出', 'sell']], @trade_type),{ prompt: '请选择交易类别...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:source, options_for_select(@total_txs.pluck(:source).compact.uniq, @source),{ prompt: '请选择来源...', class: 'select2-dropdown form-control', onchange: 'this.form.submit()' }) %>
<button type="submit" class="btn btn-primary mx-3">确定</button>
<%= link_to "Reset", users_origin_transactions_path, class: 'btn btn-warning me-3' %>
<%= link_to "Refresh", refresh_origin_transactions_path(user_id: current_user.id), class: 'btn btn-success' %>
Expand Down

0 comments on commit a0fcb69

Please sign in to comment.