Skip to content

Instantly share code, notes, and snippets.

@tsyber1an
Created March 21, 2013 11:17
Show Gist options
  • Save tsyber1an/5212302 to your computer and use it in GitHub Desktop.
Save tsyber1an/5212302 to your computer and use it in GitHub Desktop.
class @LogsMapReduce extends MapReduce
constructor: ->
super("logs", "statistics")
@init()
init: ->
@before()
@map_reduce()
@after()
before: ->
@stats_setting = db.stats_settings.findOne {key: 'last_logs_map_reduce'}
@lastTimeStated = @stats_setting["created_at"]
after: ->
db.stats_settings.save {_id: @stats_setting._id, key: @stats_setting.key, created_at: ISODate()}
map: ->
# FIXME: simplify by removing extra fields
year = NumberInt(@created_at.getFullYear())
month = NumberInt(@created_at.getMonth())
day = NumberInt(@created_at.getDate())
hour = NumberInt(@created_at.getHours())
date = new Date(year, month, day, hour+4, 0, 0)
path = if @path == '/connect/booking.json' && @agent && @agent == 'ghe056' then '/connect/booking-aviasales.json' else @path
key =
path: path
year: year
month: month
day: day
hour: hour
datetime: date
emit key,
count: 1
reduce: (k, v) ->
sum = 0
v.forEach (e) ->
sum += e.count
count: sum
# Abs class
class MapReduce
constructor: (@in, @out) ->
map: ->
reduce: ->
query: ->
before: ->
after: ->
map_reduce: ->
db.runCommand
mapreduce: @in
map: @map
reduce: @reduce
query:
created_at:
$gt: @lastTimeStated
out:
merge: @out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment