Skip to content

Release 11.0.0: Comparable Data Items and Editable Comments

Josh Schwartzman edited this page Jun 4, 2019 · 8 revisions

Dedupe Question Answers

qa_response_ids = []
dupe_qa_ids = []
# Honor the last answer saved
QuestionAnswer.order(created_at: :desc).each do |question_answer|
  qa_response_id = "#{question_answer.question_id}-#{question_answer.survey_response_id}"
  if qa_response_ids.include?(qa_response_id)
    dupe_qa_ids.push(question_answer.id)
  else
    qa_response_ids.push(qa_response_id)
  end
end; false
"#{dupe_qa_ids.size} Dupe QuestionAnswers found"
QuestionAnswer.where(id: dupe_qa_ids).delete_all

Create Audiences

# also in seeds.rb
Audience.create(name: 'Share via Link', price_per_response: 0)
Audience.create(name: 'All People (No Filters)', price_per_response: 3.75)

Migrate ChartItem to DataItem

Collection::TestCollection.all.each { |coll|
  chart_items = coll.items.where(type: 'Item::ChartItem')
  next if chart_items.blank?
  chart_items.update_all(type: 'Item::DataItem', report_type: Item::DataItem.report_types[:report_type_question_item])
  data_items = Item.where(id: chart_items.map(&:id))

  data_items.each do |data_item|
    dataset = Dataset::Question.create(
      data_source: data_item.data_source,
      timeframe: data_item.d_timeframe,
      identifier: data_item.d_measure,
      chart_type: :bar,
    )
    puts "ERR: #{dataset.errors.full_messages.join('. ')}" unless dataset.persisted?
    puts "Created dataset #{dataset.id} for Data Item #{data_item.id}" if dataset.persisted?
    next unless dataset.persisted?
    dataset.data_items_datasets.create(
      data_item: data_item,
    )
  end

  first = data_items.to_a.shift
  first.create_legend_item
  legend_item = first.legend_item
  data_items.each { |ci| ci.update(legend_item: legend_item) }
}

Record / C∆ Data Items

Item::DataItem.report_type_record.includes(:legend_item).each do |data_item|
  next if data_item.data_content['datasets'].blank?
  legend_item = data_item.legend_item
  legend_item.update(legend_search_source: :select_from_datasets) if legend_item
  data_item.data_content['datasets'].each do |dataset_hash|
    next if dataset_hash['data'].blank?
    dataset = Dataset.create(
      cached_data: dataset_hash['data'],
      identifier: dataset_hash['measure'],
      measure: data_item.name,
      timeframe: dataset_hash['timeframe'],
      chart_type: dataset_hash['chart_type'],
      max_domain: dataset_hash['max_domain'],
      style: dataset_hash['style'],
    )
    puts "ERR: #{dataset.errors.full_messages.join('. ')}" unless dataset.persisted?
    puts "Created dataset #{dataset.id} for Data Item #{data_item.id}" if dataset.persisted?
    next unless dataset.persisted?
    selected = true
    selected = false if dataset_hash['order'] != 0 && legend_item && !legend_item.selected_measures.include?(dataset.measure)
    dataset.data_items_datasets.create(
      data_item: data_item,
      selected: selected
    )
  end
end

Network App Metrics

Item::DataItem.report_type_network_app_metric.includes(:data_source).each do |data_item|    
  dataset = Dataset::NetworkAppMetric.create(
    url: data_item.url,
    identifier: data_item.d_measure,
    timeframe: data_item.d_timeframe,
    chart_type: :area,
  )
  dataset.data_items_datasets.create(
    data_item: data_item,
  )
end

Collections and Items

Item::DataItem.report_type_collections_and_items.each do |data_item|
  data_source = nil
  if data_item.d_filters.present?
    data_source_type = data_item.d_filters[0]['type']&.safe_constantize
    data_source_id = data_item.d_filters[0]['target']
    data_source = data_source_type.find(data_source_id) if data_source_type.present? && data_source_id.positive?
  end
  dataset = Dataset::CollectionsAndItems.create(
    data_source: data_source,
    measure: data_item.d_measure,
    timeframe: data_item.d_timeframe,
    chart_type: :area,
  )
  puts "ERR: #{dataset.errors.full_messages.join('. ')}" unless dataset.persisted?
  puts "Created dataset #{dataset.id} for Data Item #{data_item.id}" if dataset.persisted?
  next unless dataset.persisted?
  dataset.data_items_datasets.create(
    data_item: data_item,
  )
end
Clone this wiki locally