from urllib import urlencode
import datetime

from django.conf import settings
from django.core.cache import cache

from treeherder.etl.mixins import JsonExtractorMixin
from treeherder.model.derived import RefDataManager


class BzApiBugProcess(JsonExtractorMixin):

    def _get_bz_source_url(self, last_fetched=None):
        hostname = settings.BZ_API_URL
        params = {
            'keywords': 'intermittent-failure',
            'include_fields': 'id,summary,status,resolution,op_sys,cf_crash_signature, keywords',
        }
        if last_fetched:
            params.update({'changed_after': last_fetched})
        endpoint = 'rest/bug'

        source_url = '{0}/{1}?{2}'.format(
            hostname, endpoint, urlencode(params)
        )
        return source_url

    def run(self):
        # this is the last day we fetched bugs from bugzilla
        last_fetched = cache.get('bz_last_fetched')
        curr_date = datetime.date.today()

        if last_fetched:
            # if we have a last_fetched timestamp available
            # we don't need pagination.
            source_url = self._get_bz_source_url(last_fetched)
            response = self.extract(source_url)
            if response:
                bug_list = response.get('bugs', [])
        else:
            offset = 0
            limit = 500

            bug_list = []
            while True:
                # fetch the bugzilla service until we have an empty result
                paginated_url = "{0}&offset={1}&limit={2}".format(
                    self._get_bz_source_url(),
                    offset,
                    limit
                )
                print "fetching bugs {0} to {1}".format(offset, offset+limit)
                response = self.extract(paginated_url)
                print "bugs fetched"
                temp_bug_list = response.get('bugs', [])
                bug_list += temp_bug_list
                if len(temp_bug_list) < limit:
                    break
                else:
                    offset += limit

        if bug_list:

            # store the new date for one day
            cache.set('bz_last_fetched', curr_date, 60 * 60 * 24)

            rdm = RefDataManager()
            rdm.update_bugscache(bug_list)

@@ -1637,86 +1637,8 @@
   )%0A
-                print %22fetching bugs %7B0%7D to %7B1%7D%22.format(offset, offset+limit)%0A
     
@@ -1692,45 +1692,8 @@
 rl)%0A
-                print %22bugs fetched%22%0A
     

remove print statements