#!/usr/bin/env python

# Copyright (C) 2012 Lukas Lalinsky
# Distributed under the MIT license, see the LICENSE file for details.

import re
import urllib
import urllib2
from contextlib import closing
from acoustid.script import run_script
from acoustid.data.stats import update_lookup_stats


def call_internal_api(func, **kwargs):
    url = script.config.cluster.base_master_url.rstrip('/') + '/v2/internal/' + func
    data = dict(kwargs)
    data['secret'] = script.config.cluster.secret
    urllib2.urlopen(url, urllib.urlencode(data))


def main(script, opts, args):
    db = script.engine.connect()
    redis = script.redis
    for key, count in redis.hgetall('lookups').iteritems():
        count = int(count)
        date, hour, application_id, type = key.split(':')
        if not count:
            # the only way this could be 0 is if we already processed it and
            # nothing touched it since then, so it's safe to delete
            redis.hdel('lookups', key)
        else:
            if script.config.cluster.role == 'master':
                update_lookup_stats(db, application_id, date, hour, type, count)
            else:
                call_internal_api('update_lookup_stats', date=date, hour=hour,
                    application_id=application_id, type=type, count=count)
            redis.hincrby('lookups', key, -count)


run_script(main)


#!/usr/bin/env python

# Copyright (C) 2012 Lukas Lalinsky
# Distributed under the MIT license, see the LICENSE file for details.

import re
from contextlib import closing
from acoustid.script import run_script
from acoustid.data.stats import update_lookup_stats


def main(script, opts, args):
    db = script.engine.connect()
    redis = script.redis
    for key, count in redis.hgetall('lookups').iteritems():
        count = int(count)
        date, hour, application_id, type = key.split(':')
        if not count:
            # the only way this could be 0 is if we already processed it and
            # nothing touched it since then, so it's safe to delete
            redis.hdel('lookups', key)
        else:
            update_lookup_stats(db, application_id, date, hour, type, count)
            redis.hincrby('lookups', key, -count)


run_script(main)


--- 
+++ 
@@ -4,9 +4,18 @@
 # Distributed under the MIT license, see the LICENSE file for details.
 
 import re
+import urllib
+import urllib2
 from contextlib import closing
 from acoustid.script import run_script
 from acoustid.data.stats import update_lookup_stats
+
+
+def call_internal_api(func, **kwargs):
+    url = script.config.cluster.base_master_url.rstrip('/') + '/v2/internal/' + func
+    data = dict(kwargs)
+    data['secret'] = script.config.cluster.secret
+    urllib2.urlopen(url, urllib.urlencode(data))
 
 
 def main(script, opts, args):
@@ -20,7 +29,11 @@
             # nothing touched it since then, so it's safe to delete
             redis.hdel('lookups', key)
         else:
-            update_lookup_stats(db, application_id, date, hour, type, count)
+            if script.config.cluster.role == 'master':
+                update_lookup_stats(db, application_id, date, hour, type, count)
+            else:
+                call_internal_api('update_lookup_stats', date=date, hour=hour,
+                    application_id=application_id, type=type, count=count)
             redis.hincrby('lookups', key, -count)
 
 
Handle lookup stats update on a slave server
