Skip to content

Instantly share code, notes, and snippets.

@abhishekgupta5
Last active February 8, 2019 13:16
Show Gist options
  • Select an option

  • Save abhishekgupta5/0225281749acad0248421c9103d0c6ca to your computer and use it in GitHub Desktop.

Select an option

Save abhishekgupta5/0225281749acad0248421c9103d0c6ca to your computer and use it in GitHub Desktop.

Revisions

  1. Abhishek Gupta revised this gist Feb 8, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions tv_squared.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    D = cts.split("\n") # To create(spot level)
    C = saleshouse.split("\n") # spot level
    B = tv_channels.split("\n") # campaign level
    A = account_ids.split("\n") #account level


    values = A.zip(B, C, D)
    # values = [['A', 'B', 'C', 'D'], ['A2', 'B2', 'C2', 'D2'],.....]
    keys = ["account_id", "channel", "saleshouse", "Correct TV Saleshouse"]
  2. Abhishek Gupta created this gist Feb 8, 2019.
    50 changes: 50 additions & 0 deletions tv_squared.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    values = A.zip(B, C, D)
    # values = [['A', 'B', 'C', 'D'], ['A2', 'B2', 'C2', 'D2'],.....]
    keys = ["account_id", "channel", "saleshouse", "Correct TV Saleshouse"]

    def create_rule(dimension, channel, rule_name, filters)
    puts "... starting #{rule_name} ..."
    rule = dimension.rules_for_user_annotators.find_or_initialize_by(name: rule_name, channel: channel)
    rule.assign_attributes(filters: filters, status: 'ACTIVE', level: 'spot')
    rule.save! if rule.changed?
    puts "... ended #{rule_name} ..."
    end

    def construct_filter(channel, row)
    filter = []
    #account_id = row[0]
    account = TvSquaredAdAccount.where(company_id: 411, account_id: row[0]).active.first
    raise "Account not found with ID #{account_id}" if account.blank?
    filter << {
    dimension: 'custom_tags.Account ID.name',
    operator: '$in',
    value: [row[0]]
    }
    #channel = row[1]
    #campaign = TvSquaredCampaign.where(tv_squared_ad_account_id: account_id, channel: channel).active.first
    filter << {
    dimension: 'custom_tags.TV Channel.name',
    operator: '$in',
    value: [row[1]]
    }
    #saleshouse = row[2]
    #spot = TvSquaredSpot.where(tv_squared_ad_account_id: account_id, tv_squared_campaign_id: campaign.id, saleshouse: saleshouse).active.first
    filter << {
    dimension: 'custom_tags.Saleshouse.name',
    operator: '$in',
    value: [row[2]]
    }
    return filter
    end

    dimension = UserTagGroupAnnotator.find(606)
    channel = 'tv_squared'

    values.each do |row|
    begin
    filters = [construct_filter(channel, row)]
    create_rule(dimension, channel, row[3], filters)
    rescue => e
    puts "Couldn't create rule: #{rule_name}, row: #{row}, error: #{e.inspect}"
    end
    end