Harry
Harry

Reputation: 4835

Rails reporting objects

I am currently attempting to implement a reporting module in a rails app. Thanks to some assistance provided here: Ruby on Rails object reporting, I have decided to go down the road of coding common metrics and populating reports with these.

What I have to work out is how to create the metrics - essentially I need to have a metric object that I can use within my targeting framework (e.g. if i have a target object where target.value is 0.5, I can have target.metric_id to know which metric is being targeted, and thus report on it).

My problem is how to store the formula for the metric within the model structure. A simple example of a metric would be profit, where I could do sales.selling_prices.sum - sales.cost_prices.sum. How can I set up some columns that allow this formula to be stored? All formulas will be calculated using other objects, as in the profit example.

Any assistance would be greatly appreciated.

Thanks!

Upvotes: 0

Views: 150

Answers (1)

sigre
sigre

Reputation: 371

Depending on how ambitious your formulas get, you could start with something like this for metric:

operation_type:string, one of %w(add sub mult div)
left_operand:decimal
right_operand:decimal

Then, to calculate, you might have a method on metric like:

def result
  if operation_type == 'add'
    left_operand + right_operand
  elsif operation_type == 'sub'
    left_operand - right_operand
  ...
end

When you create your metrics (maybe an admin panel of some kind) you could have ways of selecting the source inputs (for instance, left_operand is set to sales.selling_prices.sum, etc).

Upvotes: 1

Related Questions