def test_create_periodic_report_with_group():
    collection = get_mongo_collection()
    collection.remove()
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 4, 7),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 4, 1),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 3, 7),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 3, 21),
        'params': {"url": "www.centrum.cz"}
    })
    report = Report(title="report group", description="test", db_query='db.%s.group({ key : { "params.url" : true }, condition : { event : "statistic", timestamp : {$gt : ${{d1}}, $lt: ${{d2}}} }, reduce : function( obj, prev ) { prev.total++; }, initial : { total : 0 } })' % (STATISTICS_MONGODB_COLLECTION,), interval='m')
    report.save()
    tools.assert_equals(True, create_reports())
    tools.assert_not_equals(ReportResult.objects.all().count(), 0)
    tools.assert_not_equals(Report.objects.all()[0].last_report, None)
    collection.remove()
def test_create_aperiodic_report_with_count():
    collection = get_mongo_collection()
    collection.remove()
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2010, 3, 1),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 2, 12),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "content_link",
        'timestamp': datetime(2011, 2, 12),
        'params': {"url": "www.atlas.cz"}
    })
    report = Report(title="report count", description="test", db_query='db.%s.find({event: "statistc"}).count()' % (STATISTICS_MONGODB_COLLECTION,), interval='n')
    report.save()
    report = Report(title="report now", description="test", db_query='db.%s.count()' % (STATISTICS_MONGODB_COLLECTION,), interval='n')
    report.save()
    tools.assert_equals(True, create_reports())
    tools.assert_equals(ReportResult.objects.all().count(), 2)
    tools.assert_not_equals(Report.objects.all()[0].last_report, None)
    tools.assert_equals(ReportResult.objects.all()[0].output, '{"count" : 2}')
    tools.assert_equals(ReportResult.objects.all()[1].output, '{"count" : 3}')
    collection.remove()
def test_create_aperiodic_report_with_mapreduce():
    collection = get_mongo_collection()
    collection.remove()
    collection.insert({
        'event': "statistic",
        'timestamp': datetime(2010, 4, 1),
        'params': {"url": "www.atlas.cz"}
    })
    collection.insert({
        'event': "statistic",
        'timestamp': datetime(2011, 3, 7),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistic",
        'timestamp': datetime(2011, 3, 21),
        'params': {"url": "www.centrum.cz"}
    })
    report = Report(title="report mapreduce", description="test", db_query='m = function() { if (this.event == "statistic") { emit(this.params.url, 1); }};r = function(url, nums) { var total=0; for (var i=0; i<nums.length; i++) { total += nums[i]; } return total; };res = db.%s.mapReduce(m, r);res.find().forEach(printjson);' % (STATISTICS_MONGODB_COLLECTION,), interval='n')
    report.save()
    tools.assert_equals(True, create_reports())
    tools.assert_not_equals(ReportResult.objects.all().count(), 0)
    tools.assert_not_equals(Report.objects.all()[0].last_report, None)
    tools.assert_equals(ReportResult.objects.all()[0].output, '{ "_id" : "www.atlas.cz", "value" : 1 }\n{ "_id" : "www.centrum.cz", "value" : 2 }\n')
    collection.remove()
    def handle(self, *args, **options):
	"""
	execute saved periodic reports   
	"""

	if not create_reports():
	    raise CommandError('error - in execute priodic reports')
	
	print'execute reports successfull'
def test_create_periodic_report_with_count():
    collection = get_mongo_collection()
    collection.remove()
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 4, 7),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 4, 5),
        'params': {"url": "www.centrum.cz"}
    })
    collection.insert({
        'event': "statistc",
        'timestamp': datetime(2011, 3, 7),
        'params': {"url": "www.centrum.cz"}
    })
    report = Report(title="report count", description="test", db_query='db.%s.find({event: "statistc", timestamp: {$gte: ${{d1}}, $lte: ${{d2}}}}).count()' % (STATISTICS_MONGODB_COLLECTION,), interval='w')
    report.save()
    tools.assert_equals(True, create_reports())
    tools.assert_not_equals(ReportResult.objects.all().count(), 0)
    tools.assert_not_equals(Report.objects.all()[0].last_report, None)
    collection.remove()