示例#1
0
 def test_parse_field(self):
     valid_fields = ["ip_address", "referrer", "user_agent"]
     invalid_fields = ["password", "id", "admin"]
     for field in valid_fields:
         self.assertEqual(field, Analytics.parse_field(field))
     for field in invalid_fields:
         self.assertEqual("1", Analytics.parse_field(field))
示例#2
0
    def test_hits_by_ip(self):
        analytics = Analytics.hits_by_ip(self.ip_address, 'referrer')
        self.assertEqual(len(analytics['referrer']), 2)
        referrers = analytics['referrer']
        referrer_first, referrer_second = referrers[0], referrers[1]
        self.assertEqual(referrer_first, 'Ryan')
        self.assertEqual(referrer_second, 'Vinai')

        referrer_third = Analytics.hits_by_ip(self.ip_address2,
                                              'referrer')['referrer'][0]
        self.assertEqual(referrer_third, "Ryan")
示例#3
0
def checkAnalyticsDate():
    t = datetime.now()
    t2 = int(time.time()) - t.minute * 60 - t.second

    if not Analytics.query.filter_by(time=t2).first():
        db.session.add(Analytics(time=t2, requests=0))
        db.session.commit()
示例#4
0
def analytics():
    # TODO: move this to different module
    ua = UserAgent(request.headers.get('User-Agent'))
    os = ua.platform
    browser = ua.browser + ' ' + ua.version
    lat = request.args.get('lat')
    lon = request.args.get('lon')
    dev = request.args.get('dev')
    dur = request.args.get('dur')
    seconds = request.args.get('time')
    t = datetime.fromtimestamp(int(seconds) / 1e3).time()
    db.session.add(
        Analytics(device=dev,
                  os=os,
                  browser=browser,
                  duration=dur,
                  traffic=t,
                  lat=lat,
                  lon=lon))
    db.session.commit()
    return make_response('ok')
示例#5
0
 def test_count_by_col(self):
     count1 = Analytics.count_by_col('referrer')
     self.assertEqual(count1, 3)
     count2 = Analytics.count_by_col('created_at')
     self.assertEqual(count2, 3)