#-*- coding:utf-8 -*-
from rrd.store import graph_db_conn as db_conn

class EndpointCounter(object):
    def __init__(self, id, endpoint_id, counter, step, type_):
        self.id = str(id)
        self.endpoint_id = str(endpoint_id)
        self.counter = counter
        self.step = step
        self.type_ = type_

    def __repr__(self):
        return "<EndpointCounter id=%s, endpoint_id=%s, counter=%s>" %(self.id, self.endpoint_id, self.counter)
    __str__ = __repr__

    @classmethod
    def search_in_endpoint_ids(cls, qs, endpoint_ids, start=0, limit=100):
        if not endpoint_ids:
            return []

        holders = ["%s" for x in endpoint_ids]
        placeholder = ",".join(holders)

        args = endpoint_ids
        for q in qs:
            args.append("%"+q+"%")
        args += [start, limit]

        sql = '''select id, endpoint_id, counter, step, type from endpoint_counter where endpoint_id in (''' +placeholder+ ''') '''
        for q in qs:
            sql += ''' and counter like %s'''
        sql += ''' limit %s,%s'''

        cursor = db_conn.execute(sql, args)
        rows = cursor.fetchall()
        cursor and cursor.close()

        return [cls(*row) for row in rows]

    @classmethod
    def gets_by_endpoint_ids(cls, endpoint_ids, start=0, limit=100):
        if not endpoint_ids:
            return []

        holders = ["%s" for x in endpoint_ids]
        placeholder = ",".join(holders)
        args = endpoint_ids + [start, limit]

        cursor = db_conn.execute('''select id, endpoint_id, counter, step, type from endpoint_counter where endpoint_id in ('''+placeholder+''') limit %s, %s''', args)
        rows = cursor.fetchall()
        cursor and cursor.close()

        return [cls(*row) for row in rows]

    @classmethod
    def gets(cls, ids, deadline=0):
        if not ids:
            return []

        holders = ["%s" for x in ids]
        placeholder = ",".join(holders)
        args = ids + [start, limit]

        cursor = db_conn.execute('''select id, endpoint, ts from endpoint where id in ('''+placeholder+''') and ts > %s''', args)
        rows = cursor.fetchall()
        cursor and cursor.close()

        return [cls(*row) for row in rows]

@@ -61,16 +61,26 @@
  db_conn
+%0Aimport re
 %0A%0Aclass 
@@ -744,95 +744,8 @@
 _ids
-%0A        for q in qs:%0A            args.append(%22%25%22+q+%22%25%22)%0A        args += %5Bstart, limit%5D
 %0A%0A  
@@ -870,24 +870,43 @@
 r+ ''') '''%0A
+        tags = %7B%7D%0A%0A
         for 
@@ -930,41 +930,1445 @@
     
-sql += ''' and counter like %25s'''
+matchObj = re.match('(%5B%5E%5C0%5D+)=(%5B%5E%5C0%5D+)', q)%0A            if matchObj:%0A                tags%5BmatchObj.group(1)%5D = matchObj.group(2).split(',')%0A            else:%0A                args.append(%22%25%22+q+%22%25%22)%0A                sql += ''' and counter like %25s'''%0A%0A        if 'isp' in tags:%0A            sql += ''' and ('''%0A            for isp in tags%5B'isp'%5D%5B:-1%5D:%0A                args.append(%22%25%22+isp+%22%25%22)%0A                sql += ''' counter like %25s or'''%0A            args.append(%22%25%22+tags%5B'isp'%5D%5B-1%5D+%22%25%22)%0A            sql += ''' counter like %25s )'''%0A        if 'province' in tags:%0A            sql += ''' and ('''%0A            for province in tags%5B'province'%5D%5B:-1%5D:%0A                args.append(%22%25%22+province+%22%25%22)%0A                sql += ''' counter like %25s or'''%0A            args.append(%22%25%22+tags%5B'province'%5D%5B-1%5D+%22%25%22)%0A            sql += ''' counter like %25s )'''%0A        if 'city' in tags:%0A            sql += ''' and ('''%0A            for city in tags%5B'city'%5D%5B:-1%5D:%0A                args.append(%22%25%22+city+%22%25%22)%0A                sql += ''' counter like %25s or'''%0A            args.append(%22%25%22+tags%5B'city'%5D%5B-1%5D+%22%25%22)%0A            sql += ''' counter like %25s )'''%0A        if 'tag' in tags:%0A            sql += ''' and ('''%0A            for tag in tags%5B'tag'%5D%5B:-1%5D:%0A                args.append(%22%25%22+tag+%22%25%22)%0A                sql += ''' counter like %25s or'''%0A            args.append(%22%25%22+tags%5B'tag'%5D%5B-1%5D+%22%25%22)%0A            sql += ''' counter like %25s )'''%0A%0A        args += %5Bstart, limit%5D
 %0A   

Modify the way of searching counters