コード例 #1
0
def execute_past_reports(report, db_query, date_from, date_to, date_now):
    """
    excuted reports results for past periods for given report
    """
    if report.last_report == None and report.interval != 'n' and date_from != None:
	date_from_var = date_from
	date_to_var = date_to
	run_date = date_now
	#report.date_from must be found in db if date_form is not set
	if report.date_from == None:
	    collection = get_mongo_collection()
	    for record in collection.find().sort("timestamp", pymongo.ASCENDING).limit(1):
		report_date_from = record["timestamp"]
	else:
	    report_date_from = report.date_from
	while True:
	    if date_from_var <= report_date_from:
		break
	    # replace date patterns
	    db_query_var = db_query
	    if date_from_var != None:
		date_from_var = date_from_var - timedelta( seconds=PERIOD_CHOICES[report.interval])
		db_query_var = string.replace(db_query_var, "${{d1}}", "new Date(%s,%s,%s)" % (date_from_var.year, date_from_var.month - 1, date_from_var.day))
	    if date_to_var != None:
		date_to_var = date_to_var - timedelta( seconds=PERIOD_CHOICES[report.interval])
		db_query_var = string.replace(db_query_var, "${{d2}}", "new Date(%s,%s,%s)" % (date_to_var.year, date_to_var.month - 1, date_to_var.day))
		    
	    run_date = run_date - timedelta( seconds=PERIOD_CHOICES[report.interval])
	    if not execute_query(db_query_var, report, run_date):
		print "error in past generate - unsupported query: report title: %s, id: " % (report.title, report.id)
		break

    return True
コード例 #2
0
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()
コード例 #3
0
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()
コード例 #4
0
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()
コード例 #5
0
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()