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: No attribute times in object:
{
"loadAvg": [0.5, 0.26000000000000000888, 0.089999999999999996669],
"procsBlocked": 0,
"procsRunning": 1
} in:
r.limit(r.db("etdemo").table("et_metrics").groupedMapReduce(function(var_0) { return var_0("logTime").sub(var_0("logTime").mod(5)); }, function(var_1) { return {iowait: var_1("cpu")("times")("iowait"), system: var_1("cpu")("times")("system"), count: 1}; }, function(var_2) { return {iowait: "var_2("iowait")var_3("iowait")", system: "var_2("system")var_3("system")", count: "var_2("count")var_3("count")"}; }, {base: {iowait: 0, system: 0, count: 0}}), 100)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@wojons
Copy link

wojons commented Oct 20, 2013

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(
    // group -- we return the value that we are going to use to group
    function(doc) {
       // We group by logTime rouned to 5 (5 min)
       //return doc
       return doc("logTime").sub( doc("logTime").mod(5) )
       //return doc("logTime")
    },
    // map -- we return the value we are interested in
    // count is to keep track of how many group we are
    function(doc) {
        return {
            cpu: {
                times: {
                    iowait: doc("cpu")("times")("iowait").default(0),
                    system: doc("cpu")("times")("system").default(0),
                    user: doc("cpu")("times")("user").default(0),
                    nice: doc("cpu")("times")("nice").default(0),
                    irq: doc("cpu")("times")("irq").default(0),
                    softirq: doc("cpu")("times")("softirq").default(0),
                    steal: doc("cpu")("times")("steal").default(0),
                    host: doc("cpu")("times")("host").default(0)
                },
                procsRunning: doc("cpu")("procsRunning"),
                procsBlocked: doc("cpu")("procsBlocked"),
                loadAvg: doc("cpu")("loadAvg").default([0,0,0])
            },
            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 {
            cpu: {
                times: {
                    iowait: left("cpu")("times")("iowait").add(right("cpu")("times")("iowait")),
                    system: left("cpu")("times")("system").add(right("cpu")("times")("system")),
                    user: left("cpu")("times")("system").add(right("cpu")("times")("user")),
                    nice: left("cpu")("times")("system").add(right("cpu")("times")("nice")),
                    irq: left("cpu")("times")("system").add(right("cpu")("times")("irq")),
                    softirq: left("cpu")("times")("system").add(right("cpu")("times")("softirq")),
                    steal: left("cpu")("times")("system").add(right("cpu")("times")("steal")),
                    host: left("cpu")("times")("system").add(right("cpu")("times")("host"))
                },
                procsRunning: left("cpu")("procsRunning").add(right("cpu")("procsRunning")),
                procsBlocked: left("cpu")("procsBlocked").add(right("cpu")("procsBlocked")),
                loadAvg:      [left("cpu")("loadAvg")(0).add(right("loadAvg")(0)), left("cpu")("loadAvg")(1).add(right("loadAvg")(1)), left("cpu")("loadAvg")(2).add(right("loadAvg")(2))]
            },
            count: left("count").add(right("count"))
        }
    },
    {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}
)

@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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