def execute_query(db_query, report, run_date): """ execute query from report in mongo db and save result """ print db_query+"\n" try: process = Popen(["mongo","--host", string.join(settings.MONGODB_HOSTS), "--eval", db_query, settings.MONGODB_DB], stdout=PIPE) output_shell = process.communicate()[0] if process.returncode != 0: print "error - can not execute mongo query " return False except e: print "error - mongo error: ", e[0] return False print output_shell index = string.find(output_shell, "{") if index == -1: analyse_list = string.split(output_shell, "\n") try: analyse_element = int(analyse_list[-2]) output = '{"count" : %s}' % (analyse_element) except ValueError: output = '{"data" : "no data"}' else: output = output_shell[index:] #mongo_output = OutputMongo(output_shell) #full_analyse = mongo_output.getoutput() report.last_report = run_date report.save() result = ReportResult(report=report, output=output, run_date=run_date) result.save() return True
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_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_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_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))