Last active
August 29, 2015 14:08
-
-
Save itayw/78114771dd0a77bbf138 to your computer and use it in GitHub Desktop.
rethinkdb - early query - simple
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
r.db('joola').table('reads').filter(r.row('timestamp').during(r.time(2010,1,1,'Z'), r.time(2015,1,1,'Z'))).group('workspace', 'username').sum('readCount'); |
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
joola.insert('logins', [{userid: '123', username: 'itay', attempts: 1, success: true}, {userid: '1234', username: 'or', attempts: 2, success: true}]); |
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
joola.insert('visits', [{userid: '123', username: 'itay', device: 'android', visits: 1}, {userid: '1234', username: 'or', device: 'iphone', visits: 2}, {userid: '123', username: 'itay', device: 'iphone', visits: 1}]); |
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
joola.fetch({ | |
dimensions: ['username', 'device'], | |
metrics: [ | |
'visits', | |
{ | |
key: 'login_attempts', | |
dependsOn: 'attempts', | |
collection: 'logins' | |
}, | |
{ | |
key: 'avg_visits', | |
dependsOn: 'visits', | |
collection: 'visits', | |
aggregation: 'avg' | |
}, | |
{ | |
key: 'unique_users', | |
dependsOn: 'userid', | |
aggregation: 'ucount' | |
} | |
], | |
collection: 'visits', | |
filter: [ | |
['username', 'eq', 'itay'] | |
] | |
}) |
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
var sort = function(){ | |
return null; | |
//return r.asc('Id'); | |
}; | |
var filter = function(doc){ | |
return {}; //doc('attribute').eq('test3'); | |
}; | |
var group = function(){ | |
return 'attribute'; | |
}; | |
var map = function(doc){ | |
return { | |
attribute: doc('attribute'), | |
value: doc('value'), | |
max_value: doc('value'), | |
sum_value: doc('value'), | |
doccount: 1 | |
}; | |
}; | |
var reduce = function(left, right){ | |
return { | |
attribute: left('attribute'), | |
sum_value: left('sum_value').add(right('sum_value')), | |
max_value: r.branch(left('max_value').gt(right('max_value')), left('max_value'), right('max_value')), | |
doccount: left('doccount').add(right('doccount')) | |
}; | |
}; | |
var final = function(doc){ | |
return { | |
attribute: doc('reduction')('attribute'), | |
sum_value: doc('reduction')('sum_value'), | |
avg_value: doc('reduction')('sum_value').div(doc('reduction')('doccount')), | |
max_value: doc('reduction')('max_value') | |
} | |
}; | |
r.db('joola').table('testforor').filter(filter).map(map).group(group()).reduce(reduce).ungroup().map(final); |
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
joola.fetch({ | |
timeframe: 'last_30_days', | |
interval: 'day', | |
dimensions: ['timestamp'], | |
metrics: [ | |
'visits' | |
], | |
collection: 'visits', | |
filter:[['username','eq','itay']] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment