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 test_create_periodic_graph_analysis_for_mapreduce():
    report = Report(title="report6", 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.events.mapReduce(m, r);res.find().forEach(printjson);', interval='m')
    report.save()
    report_result1 = ReportResult(report=report, output ='{ "_id" : "www.centrum.cz", "value" : 100 }\n{ "_id" : "www.amapy.cz", "value" : 41 }\n', run_date = datetime(2011, 6, 13))
    report_result1.save()
    report_result2 = ReportResult(report=report, output ='{ "_id" : "www.atlas.cz", "value" : 6 }\n{ "_id" : "www.centrum.cz", "value" : 29 }\n', run_date = datetime(2011, 6, 12))
    report_result2.save()
    analysis = Analysis(id=6, title="analysis6", description="test analysis", plug_in='output_bar_graph_file', interval='d')
    analysis.queries.add(report)
    analysis.save()
    tools.assert_equals(True, create_analysis())
    tools.assert_equals(AnalysisResult.objects.all().count(), 1)
    tools.assert_not_equals(Analysis.objects.all()[0].last_report, None)
def test_create_periodic_csv_analysis_for_group():
    report = Report(title="report4", description="test", db_query='db.events.group({ key : { event : true }, condition : { timestamp : {$gt : ${{d1}}, $lt: ${{d2}}} }, reduce : function( obj, prev ) { prev.total++; }, initial : { total : 0 } })', interval='m')
    report.save()
    report_result1 = ReportResult(report=report, output ='{ "event" : "services_list", "total" : 13 }\n{ "event" : "main_event6_1", "total" : 4 }\n', run_date = datetime(2011, 6, 13))
    report_result1.save()
    report_result2 = ReportResult(report=report, output ='{ "event" : "main_event7_1", "total" : 60 }\n{ "event" : "content_link", "total" : 20 }\n', run_date = datetime(2010, 5, 13))
    report_result2.save()
    analysis = Analysis(id=4, title="analysis4", description="test analysis", plug_in='output_csv_file', interval='y')
    analysis.queries.add(report)
    analysis.save()
    tools.assert_equals(True, create_analysis())
    tools.assert_equals(AnalysisResult.objects.all().count(), 1)
    tools.assert_not_equals(Analysis.objects.all()[0].last_report, None)
def test_create_aperiodic_graph_analysis_for_count():
    report = Report(title="report2", description="test", db_query='db.events.find({event: "statistc"}).count()', interval='w')
    report.save()
    report_result1 = ReportResult(report=report, output ='{"count" : 15}', run_date = datetime(2010, 5, 13))
    report_result1.save()
    report_result2 = ReportResult(report=report, output ='{"count" : 97}', run_date = datetime(2010, 5, 6))
    report_result2.save()
    analysis = Analysis(id=2, title="analysis2", description="test analysis", plug_in='output_bar_graph_file', interval='n')
    analysis.queries.add(report)
    analysis.save()
    tools.assert_equals(True, create_analysis())
    tools.assert_equals(AnalysisResult.objects.all().count(), 1)
    tools.assert_not_equals(Analysis.objects.all()[0].last_report, None)
def test_create_periodic_csv_analysis_for_count():
    report = Report(title="report1", description="test", db_query="db.events.count()", interval='m')
    report.save()
    report_result1 = ReportResult(report=report, output ='{"count" : 5}', run_date = datetime(2010, 5, 6))
    report_result1.save()
    report_result2 = ReportResult(report=report, output ='{"count" : 7}', run_date = datetime(2010, 4, 6))
    report_result2.save()
    analysis = Analysis(id=1, title="analysis1", description="test analysis", plug_in='output_csv_file', interval='m')
    analysis.queries.add(report)
    analysis.save()
    tools.assert_equals(True, create_analysis())
    tools.assert_equals(AnalysisResult.objects.all().count(), 1)
    tools.assert_not_equals(Analysis.objects.all()[0].last_report, None)
    analysis_result1 = AnalysisResult.objects.all()[0]
    os.remove('%s/%s' % (STATISTICS_REPORT_PATH, analysis_result1.output))
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()