Skip to content

Commit

Permalink
Aggregations are now stored on the database.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmiguelv committed Feb 21, 2018
1 parent da7aea4 commit 2320333
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 124 deletions.
81 changes: 0 additions & 81 deletions chirp/aggregations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from bson.code import Code

sentiment_avg = [{
'$group': {
'_id': 'null',
Expand All @@ -8,82 +6,3 @@
}
}
}]

sentiment_count_mr = {
'mapper': Code('''
function() {
var polarity = this.chirp.sentiment.polarity;
if (polarity > 0) {
emit('positive', 1);
} else if (polarity < 0) {
emit('negative', 1);
} else {
emit('neutral', 1);
}
}
'''),
'reducer': Code('''
function(key, values) {
return Array.sum(values);
}
''')
}

sentiment_country_mr = {
'mapper': Code('''
function() {
emit(this.place.country_code, this.chirp.sentiment.polarity);
}
'''),
'reducer': Code('''
function(key, values) {
return Array.avg(values);
}
'''),
'query': {'place.country_code': {'$exists': True}}
}

sentiment_date_mr = {
'mapper': Code('''
function() {
var date = new Date(this.created_at);
var label = 'neutral';
var polarity = this.chirp.sentiment.polarity;
if (polarity > 0) {
label = 'positive';
} else if (polarity < 0) {
label = 'negative';
}
emit({'year': date.getFullYear(),
'month': date.getMonth(),
'day': date.getDate(),
'sentiment': label}, 1);
}
'''),
'reducer': Code('''
function(key, values) {
return Array.sum(values);
}
'''),
'query': {'$and': [
{'chirp.sentiment': {'$exists': True}},
{'created_at': {'$exists': True}}
]}
}

tweets_total_country_mr = {
'mapper': Code('''
function() {
emit(this.place.country_code, 1);
}
'''),
'reducer': Code('''
function(key, values) {
return Array.sum(values);
}
'''),
'query': {"place.country_code": {"$exists": True}}
}
50 changes: 7 additions & 43 deletions chirp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ def uid(self):
return '{}_{}_{}'.format(
self._meta.app_label, self.__class__.__name__, self.id).lower()

@property
def number_of_tweets(self):
collection = self.get_mongo_collection()
return collection.count()

def get_mongo_collection(self):
client = MongoClient(s.MONGO_DB_URI)
db = client[s.MONGO_DB_NAME]
collection = db[self.uid]
return collection

@property
def number_of_tweets(self):
collection = self.get_mongo_collection()
return collection.count()

@property
def sentiment(self):
s = self.sentiment_avg
Expand All @@ -117,41 +117,5 @@ def sentiment_avg(self):

return r[0]['sentiment_avg']

def get_sentiment_count(self):
collection = self.get_mongo_collection()
options = aggregations.sentiment_count_mr

r = collection.map_reduce(
options['mapper'], options['reducer'],
'{}_sentiment_count'.format(self.uid))

return list(r.find())

def get_sentiment_by_country(self):
collection = self.get_mongo_collection()
options = aggregations.sentiment_country_mr

r = collection.map_reduce(
options['mapper'], options['reducer'],
'{}_sentiment_country'.format(self.uid), query=options['query'])

return list(r.find())

def get_sentiment_by_date(self):
collection = self.get_mongo_collection()
options = aggregations.sentiment_date_mr

r = collection.map_reduce(
options['mapper'], options['reducer'],
'{}_sentiment_date'.format(self.uid), query=options['query'])

return list(r.find())

def get_tweets_total_by_country(self):
collection = self.get_mongo_collection()
options = aggregations.tweets_total_country_mr

r = collection.map_reduce(
options['mapper'], options['reducer'],
'{}_tweets_total_country'.format(self.uid), query=options['query'])
return list(r.find())
def get_aggregations(self):
return self.aggregations.all()

0 comments on commit 2320333

Please sign in to comment.