I have a CouchDB view with the following view/reduce
function. After working over 2 hours on it, I have a result I am happy with, but I believe there is a lot of room for improvement. Basically, I would like the code a bit more slim.
map: function (doc) {
if (doc.type == 'ticket') {
emit([doc._id, doc.applications, doc.clients, doc.release, doc.departments], doc)
} else if (doc.type == 'application') {
emit([doc._id, 0], doc)
} else if (doc.type == 'client') {
emit([doc._id, 1], doc)
} else if (doc.type == 'release') {
emit([doc._id, 2], doc)
} else if (doc.type == 'department') {
emit([doc._id, 3], doc)
}
},
reduce: function (keys, values, rereduce) {
var result = null
var applications = []
var clients = []
var release
var departments = []
for (var i = 0; i < values.length; i++) {
if (values[i]) {
if (values[i].type == 'ticket') {
result = values[i]
} else if (values[i].type == 'application') {
applications.push(values[i])
} else if (values[i].type == 'client') {
clients.push(values[i])
} else if (values[i].type == 'release') {
release = values[i]
} else if (values[i].type == 'department') {
departments.push(values[i])
}
}
}
if (result != null) {
var apps = []
var cls = []
var deps = []
if (result.applications) {
for (var i = 0; i < applications.length; i++) {
if (result.applications.indexOf(applications[i]._id) > -1) {
apps.push(applications[i])
}
}
}
if (result.clients) {
for (var i = 0; i < clients.length; i++) {
if (result.clients.indexOf(clients[i]._id) > -1) {
cls.push(clients[i])
}
}
}
if (result.departments) {
for (var i = 0; i < departments.length; i++) {
if (result.departments.indexOf(departments[i]._id) > -1) {
deps.push(departments[i])
}
}
}
result.applications = apps
result.clients = cls
result.departments = deps
result.release = release
return result
}
}
}
To answer the question, what my code is doing, I have documents in my database, which contain ticket data. These look like this:
{
"_id": "12345",
"_rev": "33-bb1ad1ec8429b638f1cb8ec2c592c8f6",
"priority": "200",
"description": "Ja, ist was",
"deadline": "",
"minutesperweek": "125",
"ordervolume": "125",
"impactdescription": "Irgendwas geht nicht mehr",
"applications": [
"6817eeda32184183e792011df10013e6"
],
"departments": [
"966f9e33d4d0233d967e0e3d3a000a90"
],
"type": "ticket",
"assignee": {
"_id": "theo",
"firstname": "Theo",
"lastname": "Test",
"emailaddress": "[email protected]",
"phonenumber": "123456"
},
"assigneehistory": [
{
"_id": "theo",
"firstname": "Theo",
"lastname": "Test",
"emailaddress": "[email protected]",
"phonenumber": "123456"
}
],
"comments": [
{
"commentvalue": "asd",
"creator": {
"_id": "theo",
"firstname": "Theo",
"lastname": "Test",
"emailaddress": "[email protected]",
"phonenumber": "123456"
},
"created": "2014-11-27T15:03:00.342Z"
}
],
"clients": [
"1152b87a2c7650ba9f8a7992b10009a1",
"1152b87a2c7650ba9f8a7992b1000c4e"
],
"reviewcomment": [
{
"commentvalue": "fdg",
"creator": {
"firstname": "Theo",
"_id": "test",
"lastname": "Test",
"emailaddress": "[email protected]",
"phonenumber": "123456"
},
"created": "2014-12-02T13:50:47.218Z"
}
],
"reviewed": true
}
With the posted reduce
function I would like to replace the arrays of clients
, applications
and departments
with the items from the database. I think that the reduce
is not very efficient.