def test_count(self): """ Send count request to StatHat API """ responses.add( responses.POST, 'http://stathatapi.example/ez', body='', status=200, content_type='application/json' ) instance = StatHat('*****@*****.**') instance.count('a_stat', 10) self.assertEqual(len(responses.calls), 1) self.assertDictEqual( json.loads(responses.calls[0].request.body), { 'ezkey': '*****@*****.**', 'data': [ {'stat': 'a_stat', 'count': 10} ] } )
def stat_log(statistic): stats = StatHat(MY_STATHAT_USER) app.logger.info(statistic) try: stats.count(statistic, 1) except Exception: app.logger.info('push to stathat failed') pass
def main(): parser = argparse.ArgumentParser() parser.add_argument("--last_id", type=int, default=1) parser.add_argument("--index", type=str, default='sunrise3') parser.add_argument("--force", action="store_true") parser.add_argument("--cached", action="store_true") parser.add_argument("--stathat", action="store_true") args = parser.parse_args() print args db = MongoClient()['test'] es = Elasticsearch() stats = StatHat('hq08Ng2ujA8o3VPe') lastfm_url = "http://ws.audioscrobbler.com/2.0/?api_key=048cd62229f507d6d577393a6d7ac972&format=json" factory = Factory(db, lastfm_url) factory.cached = args.cached last_id = args.last_id while True: where = {'_id': {'$gt': last_id}, 'end_ts': {'$gt': 0}} #where = {'_id': {'$gt': last_id}} if not args.force: where['pub_ts'] = 0 print where oid = last_id for air in db.air.find(where).sort('ts').limit(100): oid = air['_id'] audio = factory.build_audio_from_air(air) es.index(index=args.index, doc_type='audio', id=oid, body=audio) if not args.force: db.air.update({'_id': oid}, {'$set': { 'pub_ts': int(time.time()) }}) print '---' * 10, oid pp(audio) if args.stathat: stats.count('index.audio', 1) if audio.get('is_track'): stats.count('index.track', 1) if oid == last_id: if args.force: continue else: print 'wait for new tracks...' time.sleep(10) else: last_id = oid
class StatHatLogger(NoOpLogger): 'stathat.com logger' arguments = [ ( ['--stathat-key'], dict(type=None, help="Key to use for StatHat (only required if using StatHatLogger"), ), ( ['--stathat-prefix'], dict(type=str, help="Prefix for stathat stats"), ), ( ['--stathat-presence-only'], dict(action="store_true", help="Only store presence of a line, not it's content (requires --stathat-prefix)") ), ] def __init__(self, options): 'initialize this logger' super(StatHatLogger, self).__init__(options) # stathat library is required if StatHat is None: print 'stathat module is required for this logger' sys.exit(3) if options.stathat_key is None: print 'stathat-key is a required argument when using the stathat logger' sys.exit(2) # initialize a stathat instance self.stathat = StatHat(options.stathat_key) print 'Initialized stathat for %s' % options.stathat_key # deal with what kind of data to send self.presence_only = options.stathat_presence_only if self.presence_only: self.prefix = options.stathat_prefix if self.prefix is None: print 'stathat-prefix is a required argument when using presence only' sys.exit(2) print '\t... logging presence only, with key %s' % self.prefix else: if options.stathat_prefix is not None: self.prefix = '%s.' % options.stathat_prefix print '\t... with prefix %s' % options.stathat_prefix else: self.prefix = '' print '\t... with no prefix' def log(self, values): 'send a stat off to stathat' if len(values) > 1: print 'stathat logger can only use parsers with single arguments' sys.exit(2) key = self.prefix + values[0] if not self.presence_only else self.prefix result = self.stathat.count(key, 1) print 'Logging "%s". Successful: %s' % (key, result)
def index(request): stats = StatHat(settings.STATHAT_ACCOUNT) stats.count('user.visited', 1) return render(request, 'index.html')