Skip to content

Commit 6fd01e1

Browse files
committed
Minor adjustments to the code.
1 parent 8083fbd commit 6fd01e1

5 files changed

+32
-30
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
# Crealytics
2-
Technical test - Emanuel Comsa
2+
Technical test - Emanuel Comsa
3+
4+
#Explanation
5+
The current script reads a CSV with multiple columns. The columns are defined in the class constants. From the fact that it uses a combiner class, I assumed that at any point a bach of csv's need to be merged using a unique column. I think you have multiple CSV types for each customer/campaign and you need to make a single CSV with all the data. Maybe multiple exports from a third party source.
6+
The script checks a folder for the files that matche the name inputed and then sorts them by the date parsed from the file name.
7+
Since your code initially had only one file as a param for the combine method from the Combiner class, I assumed that i could work for now with just one CSV source, so i created one for tests.
8+
You create a hash with the keys being the headers of the CSV files and the values being an array of all the values.
9+
You now have an enumerator with as many elements as there are lines in the CSV.
10+
Then, for each of the elements in the enumerator, you output it into the destination CSV, making sure you divide the total rows using the LINES_PER_FILE value.
11+
You used the combine_values method to change some of the values depending on calculations needed by the business logic.
12+

app.rb

+14-28
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Modifier
3939

4040
LINES_PER_FILE = 120000
4141

42+
DEFAULT_CSV_OPTIONS = { col_sep: "\t", headers: :first_row }
43+
4244
def initialize(saleamount_factor, cancellation_factor)
4345
@saleamount_factor = saleamount_factor
4446
@cancellation_factor = cancellation_factor
@@ -52,33 +54,29 @@ def modify(output, input)
5254
end.combine(input_enumerator)
5355

5456
merger = Enumerator.new do |yielder|
55-
while true
56-
begin
57-
list_of_rows = combiner.next
57+
combiner.each do |comb|
58+
list_of_rows = comb
5859
merged = combine_hashes(list_of_rows)
5960
yielder.yield(combine_values(merged))
60-
rescue StopIteration
61-
break
62-
end
6361
end
6462
end
6563
done = false
6664
file_index = 0
6765
file_name = output.gsub('.txt', '')
68-
while not done do
69-
CSV.open(file_name + "_#{file_index}.txt", "wb", col_sep: "\t", headers: :first_row, row_sep: "\r\n") do |csv|
66+
while !done do
67+
CSV.open("#{file_name}_#{file_index}.txt", "wb", col_sep: "\t", headers: :first_row, row_sep: "\r\n") do |csv|
7068
headers_written = false
7169
line_count = 0
7270
while line_count < LINES_PER_FILE
7371
begin
7472
merged = merger.next
75-
if not headers_written
73+
unless headers_written
7674
csv << merged.keys
7775
headers_written = true
78-
line_count +=1
76+
line_count += 1
7977
end
8078
csv << merged
81-
line_count +=1
79+
line_count += 1
8280
rescue StopIteration
8381
done = true
8482
break
@@ -95,27 +93,19 @@ def sort(file)
9593
content_as_table = parse(file)
9694
headers = content_as_table.headers
9795
index_of_key = headers.index('Clicks')
98-
content = content_as_table.sort_by { |a| -a[index_of_key].to_i }
96+
content = content_as_table.sort_by { |a| - a[index_of_key].to_i }
9997
write(content, headers, output)
100-
return output
98+
output
10199
end
102100

103101
private
104102

105-
def combine(merged)
106-
result = []
107-
merged.each do |_, hash|
108-
result << combine_values(hash)
109-
end
110-
result
111-
end
112-
113103
def combine_values(hash)
114104
LAST_VALUE_WINS.each do |key|
115105
hash[key] = hash[key].last
116106
end
117107
LAST_REAL_VALUE_WINS.each do |key|
118-
hash[key] = hash[key].select {|v| !v.nil? && v != 0 && v != '0' && !v.empty?}.last
108+
hash[key] = hash[key].reverse.find { |v| !v.nil? && v != 0 && v != '0' && !v.empty? }
119109
end
120110
INT_VALUES.each do |key|
121111
hash[key] = hash[key][0].to_s
@@ -150,8 +140,6 @@ def combine_hashes(list_of_rows)
150140
result
151141
end
152142

153-
DEFAULT_CSV_OPTIONS = { :col_sep => "\t", :headers => :first_row }
154-
155143
# Reads the csv file with the default options
156144
def parse(file)
157145
CSV.read(file, DEFAULT_CSV_OPTIONS)
@@ -168,15 +156,13 @@ def lazy_read(file)
168156

169157
# Writes content and headers to the output csv file
170158
def write(content, headers, output)
171-
CSV.open(output, "wb", { :col_sep => "\t", :headers => :first_row, :row_sep => "\r\n" }) do |csv|
159+
CSV.open(output, 'wb', col_sep: "\t", headers: :first_row, row_sep: "\r\n") do |csv|
172160
csv << headers
173161
content.each do |row|
174162
csv << row
175163
end
176164
end
177165
end
178-
179-
180166
end
181167

182168
modified = input = latest('test')
@@ -186,4 +172,4 @@ def write(content, headers, output)
186172
modifier = Modifier.new(modification_factor, cancellaction_factor)
187173
modifier.modify(modified, input)
188174

189-
puts "DONE modifying"
175+
puts 'DONE modifying'
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Account ID Account Name Campaign Ad Group Keyword Keyword Type Subid Paused Max CPC Keyword Unique ID ACCOUNT CAMPAIGN BRAND BRAND+CATEGORY ADGROUP KEYWORD Last Avg CPC Last Avg Pos Clicks Impressions ACCOUNT - Clicks CAMPAIGN - Clicks BRAND - Clicks BRAND+CATEGORY - Clicks ADGROUP - Clicks KEYWORD - Clicks Avg CPC CTR Est EPC newBid Costs Avg Pos number of commissions Commission Value ACCOUNT - Commission Value CAMPAIGN - Commission Value BRAND - Commission Value BRAND+CATEGORY - Commission Value ADGROUP - Commission Value KEYWORD - Commission Value
22
1 first camp1 ad1 keyword1 keytype1 subid1 no 4 1 acc camp1 brand1 brandcamp1 adgr1 keyword1 2.3 5 235 4343 33 34 45 454 54 576 3.2 1 3 5 232 1 42 1 3 5 6 9 6 4
3-
2 second camp2 ad2 keyword2 keytype2 subid2 no 3 2 acc1 camp2 brand2 brandcamp2 adgr2 keyword2 3.3 2 2435 23343 73 58 89 236 47 238 1.25 2 5 8 343 3 7 4 7 1 3 5 3 8
3+
2 second camp2 ad2 keyword2 keytype2 subid2 no 3 2 acc1 camp2 brand2 brandcamp2 adgr2 keyword2 3.3 2 2435 23343 73 58 89 236 47 238 1.25 2 5 8 343 3 7 4 7 1 3 5 3 8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Account ID Account Name Campaign Ad Group Keyword Keyword Type Subid Paused Max CPC Keyword Unique ID ACCOUNT CAMPAIGN BRAND BRAND+CATEGORY ADGROUP KEYWORD Last Avg CPC Last Avg Pos Clicks Impressions ACCOUNT - Clicks CAMPAIGN - Clicks BRAND - Clicks BRAND+CATEGORY - Clicks ADGROUP - Clicks KEYWORD - Clicks Avg CPC CTR Est EPC newBid Costs Avg Pos number of commissions Commission Value ACCOUNT - Commission Value CAMPAIGN - Commission Value BRAND - Commission Value BRAND+CATEGORY - Commission Value ADGROUP - Commission Value KEYWORD - Commission Value
2+
2 second camp2 ad2 keyword2 keytype2 subid2 no 3 2 acc1 camp2 brand2 brandcamp2 adgr2 keyword2 3.3 2 2435 23343 73 58 89 236 47 238 1.25 2 5 8 343 3 7 4 7 1 3 5 3 8
3+
1 first camp1 ad1 keyword1 keytype1 subid1 no 4 1 acc camp1 brand1 brandcamp1 adgr1 keyword1 2.3 5 235 4343 33 34 45 454 54 576 3.2 1 3 5 232 1 42 1 3 5 6 9 6 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Account ID Account Name Campaign Ad Group Keyword Keyword Type Subid Paused Max CPC Keyword Unique ID ACCOUNT CAMPAIGN BRAND BRAND+CATEGORY ADGROUP KEYWORD Last Avg CPC Last Avg Pos Clicks Impressions ACCOUNT - Clicks CAMPAIGN - Clicks BRAND - Clicks BRAND+CATEGORY - Clicks ADGROUP - Clicks KEYWORD - Clicks Avg CPC CTR Est EPC newBid Costs Avg Pos number of commissions Commission Value ACCOUNT - Commission Value CAMPAIGN - Commission Value BRAND - Commission Value BRAND+CATEGORY - Commission Value ADGROUP - Commission Value KEYWORD - Commission Value
2+
2 second camp2 ad2 keyword2 keytype2 subid2 no 3 2 acc1 camp2 brand2 brandcamp2 adgr2 keyword2 3.3 2 2435 23343 73 58 89 236 47 238 1,25 2,0 5,0 8,0 343,0 3,0 2,8000000000000003 1,6 2,8000000000000003 0,4 1,2000000000000002 2,0 1,2000000000000002 3,2
3+
1 first camp1 ad1 keyword1 keytype1 subid1 no 4 1 acc camp1 brand1 brandcamp1 adgr1 keyword1 2.3 5 235 4343 33 34 45 454 54 576 3,2 1,0 3,0 5,0 232,0 1,0 16,8 0,4 1,2000000000000002 2,0 2,4000000000000004 3,6 2,4000000000000004 1,6

0 commit comments

Comments
 (0)