app/services/data_point_evaluation.rb
def perform
    result = GeographicDatabase.connection.execute(to_sql).first
    puts "result: #{result.inspect}"
    agg_variables = @data_point.field_array.map(&:to_sym)

    # Collect fields, ordered to send as arguments
    # into the aggregator function.
    arguments = agg_variables.collect { |var|
      Array(
        parse_pg_array( result[var.to_s] )
      ).map(&:to_i)
    }

    aggregation = AggregatorFunctions.send(
      @data_point.aggregator.name,
      *arguments
    )
    attributes = {
      title:      data_point.name,
      modifier:   data_point.aggregator.modifier,
      aggregator: data_point.aggregator.name,
      units:      data_point.units
    }.merge(aggregation)

    # TODO: Create a non-ActiveRecord model for DataPoint evaluations,
    # and serialize them using a JSONAPI Resources wrapper.
    data = {
      id: data_point.id.to_s,
      type: 'evaluated-data-point',
      attributes: attributes
    }
  rescue StandardError => e
    # Separate out as a method to call.
    {
      id:   data_point.try(:id),
      type: "evaluated-data-point",
      attributes: {
        value: 0,
        margin: 0,
        units: "error",
        error: e.to_s,
        message:  "Data point #{data_point.try(:name)} could not be evaluated.",
        sql: to_sql,
        data_point: data_point.try(:attributes)
      }
    }
  end
# This method is too long. The attributes and data hashes
# could easily be refactored into their own methods, as could
# the parsing of arguments.
This method is too long. The attributes and data hashes
could easily be refactored into their own methods, as could
the parsing of arguments.
This method is too long . The attributes and data hashes could easily be refactored into their own methods as could the parsing of arguments .
def perform
    result = GeographicDatabase.connection.execute(to_sql).first
    puts "result: #{result.inspect}"
    agg_variables = @data_point.field_array.map(&:to_sym)
    arguments = agg_variables.collect { |var|
      Array(
        parse_pg_array( result[var.to_s] )
      ).map(&:to_i)
    }
    aggregation = AggregatorFunctions.send(
      @data_point.aggregator.name,
      *arguments
    )
    attributes = {
      title:      data_point.name,
      modifier:   data_point.aggregator.modifier,
      aggregator: data_point.aggregator.name,
      units:      data_point.units
    }.merge(aggregation)
    data = {
      id: data_point.id.to_s,
      type: 'evaluated-data-point',
      attributes: attributes
    }
  rescue StandardError => e
    {
      id:   data_point.try(:id),
      type: "evaluated-data-point",
      attributes: {
        value: 0,
        margin: 0,
        units: "error",
        error: e.to_s,
        message:  "Data point #{data_point.try(:name)} could not be evaluated.",
        sql: to_sql,
        data_point: data_point.try(:attributes)
      }
    }
  end
def perform result = GeographicDatabase . connection . execute ( to_sql ) . first puts "result: #{result.inspect}" agg_variables = @data_point . field_array . map ( & :to_sym ) arguments = agg_variables . collect { | var | Array ( parse_pg_array ( result [ var . to_s ] ) ) . map ( & :to_i ) } aggregation = AggregatorFunctions . send ( @data_point . aggregator . name , * arguments ) attributes = { title : data_point . name , modifier : data_point . aggregator . modifier , aggregator : data_point . aggregator . name , units : data_point . units } . merge ( aggregation ) data = { id : data_point . id . to_s , type : 'evaluated-data-point' , attributes : attributes } rescue StandardError => e { id : data_point . try ( :id ) , type : "evaluated-data-point" , attributes : { value : 0 , margin : 0 , units : "error" , error : e . to_s , message : "Data point #{data_point.try(:name)} could not be evaluated." , sql : to_sql , data_point : data_point . try ( :attributes ) } } end
This method is too long.
This method is too long .
# Collect fields, ordered to send as arguments # into the aggregator function. # TODO: Create a non-ActiveRecord model for DataPoint evaluations, # and serialize them using a JSONAPI Resources wrapper. # Separate out as a method to call.