Created
March 16, 2016 13:16
-
-
Save marians/da65fd201e89b149ead2 to your computer and use it in GitHub Desktop.
CouchDB reduce_overflow_error problem
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
function(doc) { | |
if (doc.request_context.user_name | |
&& doc.timestamp | |
&& doc.day | |
&& doc.request_context.activity | |
&& typeof doc.request_context.activity !== "undefined") { | |
var is_error = 0; | |
if (typeof doc.response_status !== "undefined" && doc.response_status > 400) { | |
is_error = 1; | |
} | |
out = { | |
num_requests: 1, | |
last_seen: doc.timestamp, | |
days: {}, | |
activities: {}, | |
user_agents: {}, | |
num_errors: is_error | |
}; | |
out.days[doc.day] = 1; | |
out.activities[doc.request_context.activity] = 1; | |
out.user_agents[doc.user_agent] = 1; | |
emit(doc.request_context.user_name, out); | |
} | |
} |
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
function(keys, values) { | |
var ret = { | |
num_requests: 0, | |
num_errors: 0, | |
last_seen: 0, | |
days: {}, | |
activities: {}, | |
user_agents: {} | |
} | |
values.forEach(function(val){ | |
ret.num_requests += val.num_requests; | |
ret.last_seen = Math.max(ret.last_seen, val.last_seen); | |
for (var key in val.activities) { | |
if (typeof ret.activities[key] === "undefined") { | |
ret.activities[key] = val.activities[key]; | |
} else { | |
ret.activities[key] += val.activities[key]; | |
} | |
} | |
for (var key in val.days) { | |
if (typeof ret.activities[key] === "undefined") { | |
ret.days[key] = val.days[key]; | |
} else { | |
ret.days[key] += val.days[key]; | |
} | |
} | |
for (var key in val.user_agents) { | |
if (typeof ret.user_agents[key] === "undefined") { | |
ret.user_agents[key] = val.user_agents[key]; | |
} else { | |
ret.user_agents[key] += val.user_agents[key]; | |
} | |
} | |
}); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment