from __future__ import absolute_import, with_statement, print_function, generators, nested_scopes, unicode_literals

import logging

import ckan.plugins as plugins
import ckan.logic
import ckan.lib.search
import ckan.lib.search.query
from ckan.common import _

log = logging.getLogger(__name__)

HOST = 'http://localhost:8983/solr'
QUERY = 'extras_TempCoverageBegin:[* TO {e}] AND extras_TempCoverageEnd:[{s} TO *]'


class TimelineAPIPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.interfaces.IActions, inherit=True)

    def get_actions(self):
        return {'timeline': timeline}

@ckan.logic.side_effect_free
def timeline(context, request_data):
    '''
    Return a list of points for a timeline plot

    :param start: the start point in time
    :type start: int
    :param end: the end point in time
    :type end: int
    :param method: the way to execute the queries
    :type method: str

    :rtype: list
    '''

    log.debug('context: {c}'.format(c=context))
    log.debug('request_data: {r}'.format(r=request_data))

    #ckan.logic.check_access('timeline', context, request_data)

    start = request_data.get('start')
    end = request_data.get('end')
    method = request_data.get('method', 't')

    if start is None:
        raise ckan.logic.ValidationError({'start': _('Missing value')})
    if end is None:
        raise ckan.logic.ValidationError({'end': _('Missing value')})
    if method not in ('s', 'p', 't'):
        raise ckan.logic.ValidationError({'method': _('Wrong value')})

    start = int(start)
    end = int(end)
    if end <= start:
        raise ckan.logic.ValidationError({'end': _('Smaller or equal to start')})

    delta = float(end - start)
    log.debug('delta: {d}'.format(d=delta))

    interval = delta / 100
    log.debug('interval: {i}'.format(i=interval))

    ls = set()
    for a in range(100):
        s = int(start + interval * a)
        e = int(start + interval * (a + 1))
        m = (s + e) / 2
        if s != e:
            ls.add((s, e, m))

    if len(ls) != 100:
        log.warning('{l} not 100 elements'.format(l=len(ls)))

    ls = list(ls)
    # log.debug('ls: {l}'.format(l=ls))

    solr = ckan.lib.search.make_connection()

    rl = []
    for s, e, m in ls:
        r = solr.select(QUERY.format(s=s, e=e),
                        fields=['id'],
                        rows=0)
        rl.append((s, e, m, r._numFound))

    solr.close()

    return sorted(rl)

@@ -2169,94 +2169,145 @@
     
-solr = ckan.lib.search.make_connection()%0A%0A    rl = %5B%5D%0A    for s, e, m in ls:%0A        r
+rl = %5B%5D%0A    %5Brl.append(ps(l)) for l in ls%5D%0A%0A    return sorted(rl)%0A%0A%0Adef ps((s, e, m)):%0A    solr = ckan.lib.search.make_connection()%0A    n
  = s
@@ -2361,20 +2361,16 @@
         
-    
 fields=%5B
@@ -2400,20 +2400,16 @@
     
-    
 rows=0)%0A
     
@@ -2408,51 +2408,8 @@
 =0)%0A
-        rl.append((s, e, m, r._numFound))%0A%0A
     
@@ -2421,17 +2421,16 @@
 close()%0A
-%0A
     retu
@@ -2421,30 +2421,40 @@
 close()%0A    return s
-orted(rl)
+, e, m, n._numFound
 %0A

Extract query code to function