Browse Source

add total match + avg weight analytics

David 9 năm trước cách đây
mục cha
commit
61147e630d
2 tập tin đã thay đổi với 17 bổ sung1 xóa
  1. 5 1
      analytics.py
  2. 12 0
      db.py

+ 5 - 1
analytics.py

@@ -23,7 +23,11 @@ def post_data(field, keys, value):
 
 if __name__ == '__main__':
     from db import db
-    errors = db().get_sources_with_error()
+    from pprint import pprint
+    d = db()
+    errors = d.get_sources_with_error()
     for e in errors:
         val = e['_id'].replace(" ", r"\ ")
         post_data('error', {'source': '%s' % val}, e['count'])
+
+    pprint(d.get_sources_with_count())

+ 12 - 0
db.py

@@ -189,3 +189,15 @@ class db():
         r = self.c_articles.aggregate([{'$match': {'error': True}},
                                        {'$group': {'_id': '$source_name', 'count': {'$sum': 1}}}])
         return r
+
+    def get_sources_with_count(self):
+        r = self.c_articles.aggregate([{'$match': {'matches': True}},
+                                       {'$group':
+                                           {'_id': '$source_name',
+                                            'count': {'$sum': 1},
+                                            'weight': {'$avg': '$weight'}
+                                            }
+                                        }])
+
+        # t = self.c_articles.aggregate([{'$group': {'_id': '$source_name', 'count': {'$sum': 1}}}])
+        return r