Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # map / reduce
- mapFunc = () ->
- days= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
- now = new Date().valueOf()
- key = "#{this.ts.getFullYear()}-#{this.ts.getMonth()+1}-#{this.ts.getDate()}"
- for i in [-9..0]
- date = new Date(now + i*86400000)
- formatted = "#{date.getFullYear()}-#{date.getMonth()+1}-#{date.getDate()}"
- if formatted is key then emit(formatted, {count: 1, day: days[date.getDay()]})
- else emit(formatted, {count: 0, day: days[date.getDay()]})
- reduceFunc = (key, values) ->
- total = 0
- for value in values
- total += value.count
- day = value.day
- return { count: total, day: day }
- # query
- query = {to:channel}
- params = { out: {inline:1}, query: query, sort: {ts:-1} }
- # result
- [ { _id: '2012-1-1', value: { count: 0, day: 'Sun' } },
- { _id: '2012-1-10', value: { count: 3, day: 'Tue' } },
- { _id: '2012-1-2', value: { count: 0, day: 'Mon' } },
- { _id: '2012-1-3', value: { count: 0, day: 'Tue' } },
- { _id: '2012-1-4', value: { count: 3, day: 'Wed' } },
- { _id: '2012-1-5', value: { count: 1, day: 'Thu' } },
- { _id: '2012-1-6', value: { count: 1, day: 'Fri' } },
- { _id: '2012-1-7', value: { count: 0, day: 'Sat' } },
- { _id: '2012-1-8', value: { count: 1, day: 'Sun' } },
- { _id: '2012-1-9', value: { count: 0, day: 'Mon' } } ]
Add Comment
Please, Sign In to add comment