Created
January 8, 2018 14:56
-
-
Save crova/92a55c23ae46b5bf0e5089d8d8e790c1 to your computer and use it in GitHub Desktop.
Can't figure why I end up with multiple records for the same date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reate_table "sib_transacs", force: :cascade do |t| | |
t.integer "requests" | |
t.integer "delivered" | |
t.integer "bounces" | |
t.integer "spamreports" | |
t.integer "blocked" | |
t.integer "invalid_sent" | |
t.integer "deferred" | |
t.integer "opens" | |
t.integer "unique_opens" | |
t.integer "clicks" | |
t.integer "unique_clicks" | |
t.string "tag" | |
t.float "cost" | |
t.float "turnover" | |
t.float "margin" | |
t.float "cpm" | |
t.float "ecpm" | |
t.string "partner" | |
t.string "name" | |
t.date "date" | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
t.float "result" | |
t.float "orate" | |
t.float "crate" | |
t.float "roi" | |
t.string "campaign_name" | |
t.float "payout" | |
t.string "deal" | |
t.string "theme" | |
t.string "scenario" | |
t.string "sender" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sib | |
@sib ||= Mailin.new("URL","APIKEY") | |
end | |
def shoot_criteria_gmail_e_cia | |
{ "aggregate" => 0, "tag" => "WM-ABD-2018-GMAIL-E-CIA-COMBUSTIVEL", "end_date" => "2018-01-08", "start_date" => "2018-01-01" } | |
end | |
def shoots_gmail_e_cia | |
@shoots_gmail_e_cia ||= sib.get_statistics(shoot_criteria_gmail_e_cia) | |
end | |
def needed_records_gmail_e_cia | |
first_cut = shoots_gmail_e_cia.dig('data') | |
end | |
def campaign_records_gmail_e_cia | |
@campaign_records_gmail_e_cia ||= needed_records_gmail_e_cia.map | |
end | |
def transac_gmail_e_cia_fetch | |
def self.find_or_create_by(attributes) | |
SibTransac.where(attributes).first || SibTransac.create(attributes) | |
end | |
def invalid=(value) | |
write_attribute(:invalid_sent, value) | |
end | |
def invalid | |
read_attribute(:invalid_sent) | |
end | |
# update/create | |
campaign_records_gmail_e_cia.each do |record| | |
shoot = SibTransac.find_or_create_by(record) | |
unless shoot.update(record) | |
# the data was invalid, so the shoot wasn't saved, do something about that here | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment