Skip to content

Instantly share code, notes, and snippets.

@neumino
Last active December 25, 2015 22:49
Show Gist options
  • Save neumino/7053150 to your computer and use it in GitHub Desktop.
Save neumino/7053150 to your computer and use it in GitHub Desktop.
r.db('etdemo').table('et_metrics').groupedMapReduce(
// group -- we return the value that we are going to use to group
function(doc) {
// We group by logTime rouned to 5*60 (5 min)
return doc("logTime").sub( doc("logTime").mod(5*60) )
},
// map -- we return the value we are interested in
// count is to keep track of how many group we are
function(doc) {
return {
iowait: doc("cpu")("times")("iowait"),
system: doc("cpu")("times")("system"),
count: 1
}
},
// reduce -- here we just do the sum of iowait, system and count -- the left/right objects are the one mapped before so something like { iowait:..., system: ..., count:...}
function(left, right) {
return {
iowait: left("iowait").add( right("iowait")),
system: left("system").add( right("system")),
count: left("count").add( right("count"))
}
}
).map( function(reducedObject) {
// here reducedObject("count") is the number of doc we have in each group (we mapped each do to 1, then did the sum)
return {
group: reducedObject("group"),
iowait: reducedObject("reduction")("iowait").div( reducedObject("reduction")("count") ),
system: reducedObject("reduction")("system").div( reducedObject("reduction")("count") )
}
})
@wojons
Copy link

wojons commented Oct 20, 2013

RqlRuntimeError: Cannot perform get_field on a non-object non-sequence `0`. in:
r.db("etdemo").table("et_metrics").between(["y98sGsEHRQKocICOXoZhkg", "c3d00b6a-36a3-4110-8944-458747d8f2f4", 22872990], ["y98sGsEHRQKocICOXoZhkg", "c3d00b6a-36a3-4110-8944-458747d8f2f4", 22875000], {index: "nodeid_mType_logTime"}).groupedMapReduce(function(var_60) { return var_60("logTime").sub(var_60("logTime").mod(5)); }, function(var_61) { return {cpu: {times: {iowait: var_61("cpu")("times")("iowait").default(0), system: var_61("cpu")("times")("system").default(0), user: var_61("cpu")("times")("user").default(0), nice: var_61("cpu")("times")("nice").default(0), irq: var_61("cpu")("times")("irq").default(0), softirq: var_61("cpu")("times")("softirq").default(0), steal: var_61("cpu")("times")("steal").default(0), host: var_61("cpu")("times")("host").default(0)}, procsRunning: var_61("cpu")("procsRunning"), procsBlocked: var_61("cpu")("procsBlocked"), loadAvg: var_61("cpu")("loadAvg").default([0, 0, 0])}, count: 1}; }, function(var_62) { return {cpu: {times: {iowait: var_62("cpu")("times")("iowait").add(var_63("cpu")("times")("iowait")), system: var_62("cpu")("times")("system").add(var_63("cpu")("times")("system")), user: var_62("cpu")("times")("system").add(var_63("cpu")("times")("user")), nice: var_62("cpu")("times")("system").add(var_63("cpu")("times")("nice")), irq: var_62("cpu")("times")("system").add(var_63("cpu")("times")("irq")), softirq: var_62("cpu")("times")("system").add(var_63("cpu")("times")("softirq")), steal: var_62("cpu")("times")("system").add(var_63("cpu")("times")("steal")), host: var_62("cpu")("times")("system").add(var_63("cpu")("times")("host"))}, procsRunning: var_62("cpu")("procsRunning").add(var_63("cpu")("procsRunning")), procsBlocked: var_62("cpu")("procsBlocked").add(var_63("cpu")("procsBlocked")), loadAvg: [var_62("cpu")("loadAvg")(0).add(var_63("loadAvg")(0)), var_62("cpu")("loadAvg")(1).add(var_63("loadAvg")(1)), var_62("cpu")("loadAvg")(2).add(var_63("loadAvg")(2))]}, count: var_62("count").add(var_63("count"))}; }, {base: {cpu: {times: {iowait: 0, system: 0, user: 0, nice: 0, irq: 0, softirq: 0, steal: 0, host: 0}, procsRunning: 0, procsBlocked: 0, loadAvg: [0, 0, 0]}, count: 0}})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^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