Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/controllers/api/v1/matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ def monthly
daily_metrics[day.strftime("%Y-%m-%d")] = {
matches: matches_json(day_matches),
total_matches: day_matches.count,
total_energy: calculations.sum { |c| c[:energyUsed] }.round(2),
total_energy: calculations.sum { |c| c[:energyUsed] }.round(3),
total_energy_cost: calculations.sum { |c| c[:energyCost] }.round(2),
total_bft: {
amount: day_matches.sum(&:totalToken).to_f.round(2),
amount: day_matches.sum(&:totalToken).to_f.round(3),
value: calculations.sum { |c| c[:tokenValue] }.round(2)
},
total_flex: {
Expand Down Expand Up @@ -174,11 +174,11 @@ def monthly_summary
summary: {
matchesCount: @matches.count,
energyUsed: {
amount: calculations.sum { |c| c[:energyUsed] }.round(2),
amount: calculations.sum { |c| c[:energyUsed] }.round(3),
cost: calculations.sum { |c| c[:energyCost] }.round(2)
},
totalBft: {
amount: @matches.sum(&:totalToken).to_f.round(2),
amount: @matches.sum(&:totalToken).to_f.round(3),
value: calculations.sum { |c| c[:tokenValue] }.round(2)
},
totalFlex: {
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/v1/summaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def daily
render json: {
matchesCount: matches.count,
energyUsed: {
amount: total_calculations.sum { |c| c[:energyUsed] }.round(2),
amount: total_calculations.sum { |c| c[:energyUsed] }.round(3),
cost: total_calculations.sum { |c| c[:energyCost] }.round(2)
},
totalBft: {
amount: matches.sum(&:totalToken).to_f.round(2),
amount: matches.sum(&:totalToken).to_f.round(3),
value: total_calculations.sum { |c| c[:tokenValue] }.round(2)
},
totalFlex: {
Expand Down Expand Up @@ -47,10 +47,10 @@ def monthly

render json: {
total_matches: matches.count,
total_energy: total_calculations.sum { |c| c[:energyUsed] }.round(2),
total_energy: total_calculations.sum { |c| c[:energyUsed] }.round(3),
total_energy_cost: total_calculations.sum { |c| c[:energyCost] }.round(2),
total_bft: {
amount: matches.sum(&:totalToken).to_f.round(2),
amount: matches.sum(&:totalToken).to_f.round(3),
value: total_calculations.sum { |c| c[:tokenValue] }.round(2)
},
total_flex: {
Expand Down
6 changes: 3 additions & 3 deletions app/models/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Match < ApplicationRecord
validates :time, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :energyUsed, presence: true, numericality: { greater_than: 0 }
validates :result, inclusion: { in: %w[win loss draw] }, allow_nil: true
validates :totalToken, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true
validates :totalToken, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
validates :totalPremiumCurrency, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true

private
Expand All @@ -29,13 +29,13 @@ def normalize_map
def calculate_energy_used
# Recalculer energyUsed si le temps a changé ou si energyUsed est nil
if time.present? && (energyUsed.nil? || time_changed?)
self.energyUsed = (time.to_f / 10.0).round(2)
self.energyUsed = energyUsed.to_f
end
end

def calculate_values
# Valeurs par défaut
self.energyCost = (energyUsed * 1.49).round(2)
self.energyCost = (energyUsed.to_f * 1.49).round(2)
self.tokenValue = ((totalToken || 0) * 0.01).round(2)
self.premiumCurrencyValue = ((totalPremiumCurrency || 0) * 0.00744).round(2)
self.profit = (tokenValue + premiumCurrencyValue - energyCost).round(2)
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20240711000000_change_total_token_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ChangeTotalTokenToDecimal < ActiveRecord::Migration[7.1]
def up
change_column :matches, :totalToken, :decimal, precision: 10, scale: 3
end

def down
change_column :matches, :totalToken, :integer
end
end
9 changes: 9 additions & 0 deletions db/migrate/20240711000001_change_energy_used_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ChangeEnergyUsedToDecimal < ActiveRecord::Migration[7.1]
def up
change_column :matches, :energyUsed, :decimal, precision: 10, scale: 3
end

def down
change_column :matches, :energyUsed, :integer
end
end
22 changes: 2 additions & 20 deletions db/schema.rb

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

Loading