Last active
November 1, 2017 14:56
-
-
Save tkuldeep/21a0a3b09143d3f5ec5974cc78f4b8b5 to your computer and use it in GitHub Desktop.
use-reduce-mango db
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
/** | |
* Retun campaign stats with use reduce-map approach. | |
* | |
* @TODO This method is not used anywhere, just adding, so that in future we can have refrence to reduce-map approach. | |
* @param $campaign_id | |
* @return mixed | |
*/ | |
public function countEmailreportByCampaignMap($campaign_id) | |
{ | |
$map = 'function() { | |
emit(this.campaign, { | |
opened : this.opened, | |
clicked : this.clicked, | |
status : this.status, | |
openCount : 1, | |
clickCount : 1, | |
sentCount : 1, | |
bounceCount : 1, | |
unsubscribeCount : 1, | |
spamCount : 1, | |
} | |
); | |
}'; | |
$reduce = 'function(k, values) { | |
var result = { | |
opened : this.opened, | |
clicked : this.clicked, | |
status : this.status, | |
openCount : 0, | |
clickCount : 0, | |
sentCount : 0, | |
bounceCount : 0, | |
unsubscribeCount : 0, | |
spamCount : 0, | |
}; | |
values.forEach(function(value) { | |
result.opened = value.opened; | |
result.clicked = value.clicked; | |
result.status = value.status; | |
if (value.opened) { | |
result.openCount += value.openCount; | |
} | |
if (value.clicked) { | |
result.clickCount += value.clickCount; | |
} | |
if (value.status === "Delivered" || value.status === "Accepted") { | |
result.sentCount += value.sentCount; | |
} | |
if (value.status === "Self") { | |
result.unsubscribeCount += value.unsubscribeCount; | |
} | |
if (value.status === "Spam") { | |
result.spamCount += value.spamCount; | |
} | |
if (value.status === "Bounce") { | |
result.bounceCount += value.bounceCount; | |
} | |
}); | |
return result; | |
}'; | |
$query = $this->createQueryBuilder(); | |
$query->field('campaign')->equals($campaign_id) | |
->map($map) | |
->reduce($reduce); | |
return $query->getQuery()->execute(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment