Skip to content

Instantly share code, notes, and snippets.

@manveru
Created March 13, 2011 12:36
Show Gist options
  • Save manveru/868055 to your computer and use it in GitHub Desktop.
Save manveru/868055 to your computer and use it in GitHub Desktop.
Preview behaviour of a map function over a subset of _all_docs.
http = require 'http'
p = console.log
options = {
host: 'kyle',
port: 5984,
path: '/tiny_cdr/_all_docs?include_docs=true&limit=100'
}
map_results = []
emit = (key, value) ->
map_results.push([key, value])
mapred_map = (doc) ->
profile = doc.callflow.caller_profile
username = profile.username
if /^\d{4}$/.test(username)
variables = doc.variables
utime = parseInt(doc.callflow.times.created_time / 1000)
key = [utime, profile.username]
value = {
start_time: utime
caller_id_name: profile.caller_id_name,
caller_id_number: profile.caller_id_number,
destination_number: profile.destination_number,
context: profile.context,
duration: variables.duration,
billsec: variables.billsec
}
emit(key, value)
http.get(options, (res) ->
p "Got response: #{res.statusCode}"
chunks = []
res.on 'data', (chunk) ->
chunks.push chunk
res.on 'end', ->
map_results = []
json = JSON.parse(chunks.join(''))
for row in json.rows
mapred_map(row.doc)
p map_results
p map_results.length
p "\nMap Function:"
p mapred_map.toString()
).on 'error', (e) ->
p "Got error: #{e.message}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment