Skip to content

Instantly share code, notes, and snippets.

@olgen
Created April 18, 2016 16:49
Show Gist options
  • Save olgen/369694648e8acfda236709ee5baf2d3a to your computer and use it in GitHub Desktop.
Save olgen/369694648e8acfda236709ee5baf2d3a to your computer and use it in GitHub Desktop.
perf analysis script
class PerformanceReportRow
include ActiveModel::Model
ATTRIBUTES = %w(bids wins impressions clicks conversions buying_price cost app_opens profit)
attr_accessor *ATTRIBUTES
def initialize(attributes = {})
ATTRIBUTES.each do |attr|
self.send("#{attr}=", 0)
end
super(attributes)
end
def +(other)
ATTRIBUTES.each do |attr|
self.send("#{attr}=", self.send(attr) + other.send(attr))
end
return self
end
def compare_to(other)
result = {}
ATTRIBUTES.each do |attr|
change_factor = self.send(attr).to_f / other.send(attr).to_f
change_in_percent = (change_factor -1)*100.0
puts "#{attr}: #{fi other.send(attr)}->#{fi self.send(attr)} #{change_in_percent.round(2)} %"
end
puts "margin: #{other.margin.round(2)}->#{self.margin.round(2)} %"
puts "cr: #{other.cr.round(2)}->#{self.cr.round(2)} %"
return result
end
def fi(i)
i.to_s
end
def profit
self.buying_price - self.cost
end
def margin
1 - (self.cost.to_f/self.buying_price.to_f)
end
def cr
self.conversions.to_f / self.clicks.to_f
end
end
class PerformanceReport
include ActiveModel::Model
attr_accessor :org_ids, :start_date, :end_date
def run
r = SimpleReport.new()
r.query = query()
r.run()
@result = r.result
end
def total
total = PerformanceReportRow.new()
@result.map do |r|
puts "row", r
total = total + PerformanceReportRow.new(r["event"])
end
return total
end
def query
query = {
dataSource: 'bids',
queryType: 'groupBy',
granularity: 'all',
intervals: ["#{@start_date}/#{@end_date}"],
aggregations: [
{ type: 'longSum', name: 'bids', fieldName: 'bids' },
{ type: 'longSum', name: 'wins', fieldName: 'wins' },
{ type: 'longSum', name: 'impressions', fieldName: 'impressions' },
{ type: 'longSum', name: 'clicks', fieldName: 'clicks' },
{ type: 'longSum', name: 'buying_price', fieldName: 'buying_price_eur' },
{ type: 'longSum', name: 'cost', fieldName: 'win_price_eur' },
{ type: 'longSum', name: 'conversions', fieldName: 'conversions' },
{ type: 'longSum', name: 'app_opens', fieldName: 'app_opens' },
],
postAggregations: [ ],
filter: Druid::BooleanFilter.new(type: 'or', fields: add_orglist_filter),
}
end
def add_orglist_filter
@org_ids.map { |org_id| { type: 'selector', dimension: 'organization', value: org_id } }
end
end
default_before = {
start_date: "2016-03-19",
end_date: "2016-03-21"
}
default_after = {
start_date: "2016-04-16",
end_date: "2016-04-18"
}
default_dates = [default_before, default_after ]
orgs_and_dates = {
default_dates => [
# ACTIVE in default dates:
# ORG ID, ORG NAME, SWITCH DATES:
10, #HelloFood, 2016-04-01, 2016-04-13
176, #Talabat, 2016-04-01
26, #Lieferheld, 2016-03-29, 2016-04-05
34, # Die Welt, 2016-03-24
35,# pizza.de, 2016-04-01
41,# Delivery Club, 2016-03-24
43,# Zalora, 2016-04-01, 2016-04-17
97,# Doubledown Interactive, 2016-03-29
175,# # CJMall, 2016-04-06, 2016-04-13, 2016-04-15
5, #Funstage, 2016-03-24, 2016-04-11
53,# CBS Interactive, 2016-03-30
67,# LaModa, 2016-04-01
7,# KaufDa, 2016-04-04, 2016-04-17
149,# Gilt, 2016-04-05
183,# MachineZone, 2016-04-05
19,# Aviasales, 2016-04-05, 2016-04-06
47,# wego, 2016-04-05
86, #Xyrality, 2016-04-05
54,# Product Madness, 2016-04-05
94,# Foodora, 2016-04-05, 2016-04-12
92,# Kama Games, 2016-04-05
74,# Bravofly, 2016-04-05
127,# Linio, 2016-04-05
129,# NetShoes, 2016-04-05
143,# Electronic Arts, 2016-04-06
158,# Yoshirt, 2016-04-06
145,# Ponos US, 2016-04-06
142,# AGS RocketPlay, 2016-04-06
159,# Jump Ramp Games, 2016-04-06
16,# GSN Games, 2016-04-06
91,# Redmart, 2016-04-06
104,# Dena, 2016-04-06
122,# ATeam, 2016-04-06
123,# Clover Lab.,inc., 2016-04-06
134,# Rocket Games, 2016-04-06
154,# Seatgeek, 2016-04-08, 2016-04-15
117,# Konami, 2016-04-12
138,# Storm8, 2016-04-17
# QUESTION IF TO INCLUDE?
8,# ??? HRS, 2016-04-05, slowed down?
9, # ??? Foodpanda, 2016-03-24, 2016-04-05, 2016-04-13, slowed down?
21,# ??? Game Insight, 2016-04-05, slowed down?
# BLACKLISTED for various reasons
# 171,# Retailmenot, 2016-03-31, stopped on 2016-04-02
# 148, Otlob, 2016-04-01, was not running before
#55, Yemeksepeti, 2016-04-01, was not running before
# 128, Knip, 2016-04-01, # Knip stopped on 2016-04-12
# 102, One Two Trip, 2016-04-05, Did not have significant scale before 2016-04-12
# 156, G5 Games, 2016-04-06, stopped after 2016-04-11
# 161, Ponos JP, 2016-04-06, stopped after 2016-04-11
# 136, PeixeUrbano, 2016-04-06, 2016-04-17, did not run until 2016-04-06
# 192, TodayTix, 2016-04-07, did not run until 2016-04-08
# 15, GameSys, 2016-04-07, did not run until 2016-04-12
# 165, Hailo, 2016-04-08, did not run until 2016-04-12
],
[{
start_date: "2016-03-26",
end_date: "2016-03-28"
}, default_after] => [
101,# Gumi, 2016-04-04
85,# Innogames, 2016-03-31
],
[{
start_date: "2016-04-02",
end_date: "2016-04-04"
}, default_after] => [
33, #PedidosYa, 2016-04-05
],
}
# empty results
before_result = PerformanceReportRow.new()
after_result = PerformanceReportRow.new()
orgs_and_dates.each do |dates, org_ids|
before, after = dates
r_before = PerformanceReport.new(
{ org_ids: org_ids }.merge(before)
)
r_before.run
before_result += r_before.total
r_after = PerformanceReport.new(
{ org_ids: org_ids }.merge(after)
)
r_after.run
after_result += r_after.total
end
diff = after_result.compare_to(before_result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment