Created
May 16, 2012 02:54
-
-
Save andymorris/2706927 to your computer and use it in GitHub Desktop.
CS vs JS map/reduce
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
# With coffeescript design doc: | |
$ time curl http://192.168.1.7:5984/drillin/_design/drillin/_view/bha_stats?group=true\&limit=1 | |
... | |
real 33m40.725s | |
user 0m0.029s | |
sys 0m0.044s | |
# With javascript design doc: | |
$ time curl http://192.168.1.7:5984/drillin/_design/drillin/_view/bha_stats?group=true\&limit=1 | |
... | |
real 2m19.669s | |
user 0m0.015s | |
sys 0m0.007s |
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
# bha_stats map | |
(d) -> | |
#return unless d.bha_id < "04" | |
if d.type == "bha" | |
len = 0 | |
d.items.forEach (i) -> | |
len += 3 # not available right now | |
emit d._id, | |
pick_up: null | |
lay_down: null | |
used: null | |
drilling_footage: 0 | |
rotating_footage: 0 | |
sliding_footage: 0 | |
rotating_footage_percent: 0 | |
sliding_footage_percent: 0 | |
drilling_hours: 0 | |
rotating_hours: 0 | |
sliding_hours: 0 | |
rotating_rop: 0 | |
sliding_rop: 0 | |
min_weight_on_bit: 99999999 | |
max_weight_on_bit: -99999999 | |
min_motor_rpm: 99999999 | |
max_motor_rpm: -99999999 | |
min_table_rpm: 99999999 | |
max_table_rpm: -99999999 | |
md_in: 99999999 | |
inclination_in: null | |
md_out: -99999999 | |
inclination_out: null | |
flow_rate_sum: 0 | |
flow_rate_count: 0 | |
bha_length: len | |
else if d.type == "activity" | |
return unless d.bha_id | |
is_sliding = d.activity_type is "Directional Work (Sliding)" | |
is_rotating = d.activity_type is "Drilling (Rotating)" | |
is_drilling = is_sliding || is_rotating | |
is_pick_up = d.activity_type is "BHA Handling - Pickup BHA" | |
is_lay_down = d.activity_type is "BHA Handling - Laydown BHA" | |
is_survey = d.activity_type is "Survey" | |
depth_change = d.depth_end - d.depth_start | |
# extra quotes put in on accident | |
start = d.start.replace('"', '').replace('"', '') | |
end = d.end.replace('"', '').replace('"', '') | |
hours = (new Date(end) - new Date(start)) / 1000 / 60 / 60 | |
rop = depth_change / hours | |
flow = 0 | |
if is_drilling | |
d.pump_records.forEach (p) -> | |
strokeLength = 2 # not available | |
cubincInchesPerStroke = Math.PI * Math.pow(p.liner_size / 2, 2) * strokeLength * 3 | |
gallonsPerStroke = cubincInchesPerStroke / 231 | |
flow += gallonsPerStroke * p.strokes_per_minute * p.efficiency | |
emit d.bha_id, | |
pick_up: if is_pick_up then start else null | |
lay_down: if is_lay_down then end else null | |
used: is_pick_up | |
drilling_footage: if is_drilling then depth_change else 0 | |
rotating_footage: if is_rotating then depth_change else 0 | |
sliding_footage: if is_sliding then depth_change else 0 | |
rotating_footage_percent: 0 | |
sliding_footage_percent: 0 | |
drilling_hours: if is_drilling then hours else 0 | |
rotating_hours: if is_rotating then hours else 0 | |
sliding_hours: if is_sliding then hours else 0 | |
rotating_rop: if is_rotating then rop else 0 | |
sliding_rop: if is_sliding then rop else 0 | |
min_weight_on_bit: d.weight_on_bit || 99999999 | |
max_weight_on_bit: d.weight_on_bit || -99999999 | |
min_motor_rpm: d.motor_rpm || 99999999 | |
max_motor_rpm: d.motor_rpm || -99999999 | |
min_table_rpm: d.table_rpm || 99999999 | |
max_table_rpm: d.table_rpm || -99999999 | |
md_in: if is_survey then d.drillpoint.md else null | |
inclination_in: if is_survey then d.drillpoint.inclination else null | |
md_out: if is_survey then d.drillpoint.md else null | |
inclination_out: if is_survey then d.drillpoint.inclination else null | |
flow_rate_sum: if is_drilling then flow else 0 | |
flow_rate_count: if is_drilling then 1 else 0 | |
bha_length: null | |
# bha_stats reduce | |
(keys, values, rereduce) -> | |
r = | |
pick_up: null | |
lay_down: null | |
used: null | |
drilling_footage: 0 | |
rotating_footage: 0 | |
sliding_footage: 0 | |
rotating_footage_percent: 0 | |
sliding_footage_percent: 0 | |
drilling_hours: 0 | |
rotating_hours: 0 | |
sliding_hours: 0 | |
rotating_rop: 0 | |
sliding_rop: 0 | |
min_weight_on_bit: 99999999 | |
max_weight_on_bit: -99999999 | |
min_motor_rpm: 99999999 | |
max_motor_rpm: -99999999 | |
min_table_rpm: 99999999 | |
max_table_rpm: -999999990 | |
md_in: 99999999 | |
inclination_in: null | |
md_out: -99999999 | |
inclination_out: null | |
flow_rate_sum: 0 | |
flow_rate_count: 0 | |
bha_length: null | |
values.forEach (v) -> | |
r.pick_up ||= v.pick_up | |
r.lay_down ||= v.lay_down | |
r.used ||= v.used | |
r.drilling_footage += v.rotating_footage + v.sliding_footage | |
r.rotating_footage += v.rotating_footage | |
r.sliding_footage += v.sliding_footage | |
r.drilling_hours += v.drilling_hours | |
r.rotating_hours += v.rotating_hours | |
r.sliding_hours += v.sliding_hours | |
r.min_weight_on_bit = Math.min(r.min_weight_on_bit, v.min_weight_on_bit) | |
r.max_weight_on_bit = Math.max(r.max_weight_on_bit, v.max_weight_on_bit) | |
r.min_motor_rpm = Math.min(r.min_motor_rpm, v.min_motor_rpm) | |
r.max_motor_rpm = Math.max(r.max_motor_rpm, v.max_motor_rpm) | |
r.min_table_rpm = Math.min(r.min_table_rpm, v.min_table_rpm) | |
r.max_table_rpm = Math.max(r.max_table_rpm, v.max_table_rpm) | |
r.md_in = Math.min(r.md_in, v.md_in) | |
r.inclination_in = v.inclination_in if r.md_in is v.md_in | |
r.md_out = Math.max(r.md_out, v.md_out) | |
r.inclination_out = v.inclination_out if r.md_out is v.md_out | |
r.flow_rate_sum += v.flow_rate_sum | |
r.flow_rate_count += v.flow_rate_count | |
r.bha_length ||= v.bha_length | |
r.rotating_footage_percent = r.rotating_footage / r.drilling_footage | |
r.sliding_footage_percent = r.sliding_footage / r.drilling_footage | |
r.rotating_rop = r.rotating_footage / r.rotating_hours | |
r.sliding_rop = r.sliding_footage / r.sliding_hours | |
r.flow_rate = r.flow_rate_sum / r.flow_rate_count | |
r |
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
function(d) { | |
var depth_change, end, flow, hours, is_drilling, is_lay_down, is_pick_up, is_rotating, is_sliding, is_survey, len, rop, start; | |
if (d.type === "bha") { | |
len = 0; | |
d.items.forEach(function(i) { | |
return len += 3; | |
}); | |
emit(d._id, { | |
pick_up: null, | |
lay_down: null, | |
used: null, | |
drilling_footage: 0, | |
rotating_footage: 0, | |
sliding_footage: 0, | |
rotating_footage_percent: 0, | |
sliding_footage_percent: 0, | |
drilling_hours: 0, | |
rotating_hours: 0, | |
sliding_hours: 0, | |
rotating_rop: 0, | |
sliding_rop: 0, | |
min_weight_on_bit: 99999999, | |
max_weight_on_bit: -99999999, | |
min_motor_rpm: 99999999, | |
max_motor_rpm: -99999999, | |
min_table_rpm: 99999999, | |
max_table_rpm: -99999999, | |
md_in: 99999999, | |
inclination_in: null, | |
md_out: -99999999, | |
inclination_out: null, | |
flow_rate_sum: 0, | |
flow_rate_count: 0, | |
bha_length: len | |
}); | |
} else if (d.type === "activity") { | |
if (!d.bha_id) return; | |
is_sliding = d.activity_type === "Directional Work (Sliding)"; | |
is_rotating = d.activity_type === "Drilling (Rotating)"; | |
is_drilling = is_sliding || is_rotating; | |
is_pick_up = d.activity_type === "BHA Handling - Pickup BHA"; | |
is_lay_down = d.activity_type === "BHA Handling - Laydown BHA"; | |
is_survey = d.activity_type === "Survey"; | |
depth_change = d.depth_end - d.depth_start; | |
start = d.start.replace('"', '').replace('"', ''); | |
end = d.end.replace('"', '').replace('"', ''); | |
hours = (new Date(end) - new Date(start)) / 1000 / 60 / 60; | |
rop = depth_change / hours; | |
flow = 0; | |
if (is_drilling) { | |
d.pump_records.forEach(function(p) { | |
var cubincInchesPerStroke, gallonsPerStroke, strokeLength; | |
strokeLength = 2; | |
cubincInchesPerStroke = Math.PI * Math.pow(p.liner_size / 2, 2) * strokeLength * 3; | |
gallonsPerStroke = cubincInchesPerStroke / 231; | |
flow += gallonsPerStroke * p.strokes_per_minute * p.efficiency; | |
}); | |
} | |
emit(d.bha_id, { | |
pick_up: is_pick_up ? start : null, | |
lay_down: is_lay_down ? end : null, | |
used: is_pick_up, | |
drilling_footage: is_drilling ? depth_change : 0, | |
rotating_footage: is_rotating ? depth_change : 0, | |
sliding_footage: is_sliding ? depth_change : 0, | |
rotating_footage_percent: 0, | |
sliding_footage_percent: 0, | |
drilling_hours: is_drilling ? hours : 0, | |
rotating_hours: is_rotating ? hours : 0, | |
sliding_hours: is_sliding ? hours : 0, | |
rotating_rop: is_rotating ? rop : 0, | |
sliding_rop: is_sliding ? rop : 0, | |
min_weight_on_bit: d.weight_on_bit || 99999999, | |
max_weight_on_bit: d.weight_on_bit || -99999999, | |
min_motor_rpm: d.motor_rpm || 99999999, | |
max_motor_rpm: d.motor_rpm || -99999999, | |
min_table_rpm: d.table_rpm || 99999999, | |
max_table_rpm: d.table_rpm || -99999999, | |
md_in: is_survey ? d.drillpoint.md : null, | |
inclination_in: is_survey ? d.drillpoint.inclination : null, | |
md_out: is_survey ? d.drillpoint.md : null, | |
inclination_out: is_survey ? d.drillpoint.inclination : null, | |
flow_rate_sum: is_drilling ? flow : 0, | |
flow_rate_count: is_drilling ? 1 : 0, | |
bha_length: null | |
}); | |
} | |
} | |
function(keys, values, rereduce) { | |
var r = { | |
pick_up: null, | |
lay_down: null, | |
used: null, | |
drilling_footage: 0, | |
rotating_footage: 0, | |
sliding_footage: 0, | |
rotating_footage_percent: 0, | |
sliding_footage_percent: 0, | |
drilling_hours: 0, | |
rotating_hours: 0, | |
sliding_hours: 0, | |
rotating_rop: 0, | |
sliding_rop: 0, | |
min_weight_on_bit: 99999999, | |
max_weight_on_bit: -99999999, | |
min_motor_rpm: 99999999, | |
max_motor_rpm: -99999999, | |
min_table_rpm: 99999999, | |
max_table_rpm: -999999990, | |
md_in: 99999999, | |
inclination_in: null, | |
md_out: -99999999, | |
inclination_out: null, | |
flow_rate_sum: 0, | |
flow_rate_count: 0, | |
bha_length: null | |
}; | |
values.forEach(function(v) { | |
r.pick_up || (r.pick_up = v.pick_up); | |
r.lay_down || (r.lay_down = v.lay_down); | |
r.used || (r.used = v.used); | |
r.drilling_footage += v.rotating_footage + v.sliding_footage; | |
r.rotating_footage += v.rotating_footage; | |
r.sliding_footage += v.sliding_footage; | |
r.drilling_hours += v.drilling_hours; | |
r.rotating_hours += v.rotating_hours; | |
r.sliding_hours += v.sliding_hours; | |
r.min_weight_on_bit = Math.min(r.min_weight_on_bit, v.min_weight_on_bit); | |
r.max_weight_on_bit = Math.max(r.max_weight_on_bit, v.max_weight_on_bit); | |
r.min_motor_rpm = Math.min(r.min_motor_rpm, v.min_motor_rpm); | |
r.max_motor_rpm = Math.max(r.max_motor_rpm, v.max_motor_rpm); | |
r.min_table_rpm = Math.min(r.min_table_rpm, v.min_table_rpm); | |
r.max_table_rpm = Math.max(r.max_table_rpm, v.max_table_rpm); | |
r.md_in = Math.min(r.md_in, v.md_in); | |
if (r.md_in === v.md_in) { | |
r.inclination_in = v.inclination_in; | |
} | |
r.md_out = Math.max(r.md_out, v.md_out); | |
if (r.md_out === v.md_out) { | |
r.inclination_out = v.inclination_out; | |
} | |
r.flow_rate_sum += v.flow_rate_sum; | |
r.flow_rate_count += v.flow_rate_count; | |
return r.bha_length || (r.bha_length = v.bha_length); | |
}); | |
r.rotating_footage_percent = r.rotating_footage / r.drilling_footage; | |
r.sliding_footage_percent = r.sliding_footage / r.drilling_footage; | |
r.rotating_rop = r.rotating_footage / r.rotating_hours; | |
r.sliding_rop = r.sliding_footage / r.sliding_hours; | |
r.flow_rate = r.flow_rate_sum / r.flow_rate_count; | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment