Created
January 14, 2011 19:18
-
-
Save nwb/780070 to your computer and use it in GitHub Desktop.
This file contains 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
map = function(){ | |
customer = this; | |
function fresh_data(){ | |
return { | |
'order_value' : 0, | |
'order_sales' : 0, | |
'order_count' : 0, | |
'customer_count' : 1, | |
'item_count' : 0, | |
'sku_count' : 0, | |
'orders/customer' : 0, | |
'value/order' : 0, | |
'value/customer' : 0, | |
'sale/order' : 0, | |
'sale/customer' : 0, | |
'item_count/customer' : 0, | |
'item_count/order' : 0, | |
'sku_count/order' : 0 | |
}; | |
} | |
function add_data (arr, field, value) { | |
arr[field] += value; | |
} | |
var customer = this, | |
first_year = 100000; | |
//merged data is not identical to the original so we account for it. | |
if ("value" in customer) { | |
customer = customer.value; | |
}; | |
for ( var i in customer.years ){ | |
if( customer.years.hasOwnProperty( i ) && parseInt(i, 10) < first_year){ | |
first_year = parseInt( i, 10); | |
} | |
}; | |
data = {}; | |
all_data = fresh_data(); | |
for ( var year in customer.years ){ | |
if( customer.years.hasOwnProperty( year ) ){ | |
year_orders = customer.years[ year ]; | |
offset = parseInt( year, 10) - first_year; | |
year_data = fresh_data(); | |
for (var on = year_orders.length - 1; on >= 0; on--){ | |
order = customer.orders[year_orders[on]]; | |
quant = 0; | |
for (var li = order.line_items.length - 1; li >= 0; li--){ | |
quant += order.line_items[li].quantity; | |
}; | |
add_data( year_data, "order_value", Math.round(order.total * 100)); | |
add_data( year_data, "order_sales", Math.round(order.line_total * 100)); | |
add_data( year_data, "order_count", 1); | |
add_data( year_data, "sku_count", order.line_items.length); | |
add_data( year_data, "item_count", quant); | |
add_data( all_data, "order_value", Math.round(order.total * 100)); | |
add_data( all_data, "order_sales", Math.round(order.line_total * 100)); | |
add_data( all_data, "order_count", 1); | |
add_data( all_data, "sku_count", order.line_items.length); | |
add_data( all_data, "item_count", quant); | |
}; | |
data[offset] = year_data; | |
} | |
}; | |
data["all time"] = all_data; | |
emit( 'all', data ); | |
emit( first_year, data); | |
}; | |
reduce = function( first_year, report_data ){ | |
function fresh_data() { | |
return { | |
'order_value' : 0, | |
'order_sales' : 0, | |
'order_count' : 0, | |
'customer_count' : 0, | |
'item_count' : 0, | |
'sku_count' : 0, | |
'orders/customer' : 0, | |
'value/order' : 0, | |
'value/customer' : 0, | |
'sale/order' : 0, | |
'sale/customer' : 0, | |
'item_count/customer' : 0, | |
'item_count/order' : 0, | |
'sku_count/order' : 0 | |
}; | |
} | |
function add_field( dst, src, fld ){ | |
dst[fld] += src[fld]; | |
} | |
merged_report = {}; | |
for (var i = report_data.length - 1; i >= 0; i--){ | |
report = report_data[i]; | |
for ( var offset in report ){ | |
if( report.hasOwnProperty( offset ) ){ | |
merged_report[ offset ] = merged_report[ offset ] || fresh_data(); | |
mr = merged_report[ offset ]; | |
or = report[ offset ]; | |
add_field( mr, or,'order_value'); | |
add_field( mr, or,'order_sales'); | |
add_field( mr, or,'order_count'); | |
add_field( mr, or,'customer_count'); | |
add_field( mr, or,'item_count'); | |
add_field( mr, or,'sku_count'); | |
} | |
}; | |
}; | |
for ( var offet in merged_report ){ | |
if( merged_report.hasOwnProperty( offet ) ){ | |
mr = merged_report[ offet ]; | |
orders = mr[ 'order_count' ]; | |
customers = mr[ 'customer_count' ]; | |
mr[ 'orders/customer' ] = orders/customers; | |
mr[ 'value/order' ] = Math.round( mr[ 'order_value' ]/orders ); | |
mr[ 'value/customer' ] = Math.round( mr[ 'order_value' ]/customers ); | |
mr[ 'sale/order' ] = Math.round( mr[ 'order_sales' ]/orders ); | |
mr[ 'sale/customer' ] = Math.round( mr[ 'order_sales' ]/customers ); | |
mr[ 'item_count/customer' ] = mr[ 'item_count' ]/customers; | |
mr[ 'item_count/order' ] = mr[ 'item_count' ]/orders; | |
mr[ 'sku_count/order' ] = mr[ 'sku_count' ]/orders; | |
mr[ 'items/sku' ] = mr[ 'item_count'] / mr[ 'sku_count' ]; | |
} | |
}; | |
return merged_report; | |
}; | |
db.nwb_old.mapReduce( map, reduce, {out: 'nwb_old_ltv'} ); | |
db.pwb_old.mapReduce( map, reduce, {out: 'pwb_old_ltv'} ); | |
db.all_old.mapReduce( map, reduce, {out: 'all_old_ltv'} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment