#! /usr/bin/env python
# --*-- coding:utf-8 --*--

import os
import sys
sys.path.append(os.path.split(os.path.abspath(sys.path[0]))[0])

from datetime import datetime
import time
import json
import traceback
from bs4 import BeautifulSoup
import requests
import logging
import logging.config
from decimal import Decimal
from config.setting import *


get_datatime = lambda x : time.strftime(TIME_FORMAT, time.localtime(x))
get_now = lambda : time.strftime('%Y-%m-%dT%H:%M:%S',time.localtime(time.time()))
datetime_timestamp = lambda x : int(time.mktime(time.strptime(x, '%Y-%m-%d %H:%M:%S')))
getTaskFlag = lambda : 'tf{0}'.format(int(time.time()))

def get_unixtime_range():
     start = int(time.mktime(time.strptime(get_now(), '%Y-%m-%dT%H:%M:%S'))) - 120
     end = start + unix_end_offset_seconds
     return start, end

def get_clickid(content):
    soup = BeautifulSoup(content)
    for link in soup.find_all('a'):
        return link.get('href').split('=')[1]

def getLogger():
    logging.config.fileConfig(os.path.join(os.path.split(sys.path[0])[0],'config/logging.conf'))
    logger = logging.getLogger("eredar")
    return logger

def getDruidDetailResult(start_time, end_time, transaction_id_list, realDataCount):
    logger = getLogger()
    param = param_template % (start_time, end_time, transaction_id_list)
    if "'" in param:
        param = param.replace("'",'"')
    geturl = query_url + param
    isEnvOK()
    logger.info('get detail query param: {0}'.format(param))
    logger.info('wait {0} seconds that flow data to druid'.format(timewaitquerydetail))
    time.sleep(timewaitquerydetail)
    try:
        r = requests.get(geturl, timeout=60)
        data = json.loads(r.text).get('data').get('data')
        if realDataCount == len(data[1:]) == len(offer_aff_combination) * cycletimes * 2:
            logger.info('imitate data count: {0} equal to query detail data count: {1}'.format(realDataCount, len(data[1:])))
        else:
            logger.error('{0} {1} {2} not equal'.format(realDataCount, len(data[1:]), len(offer_aff_combination) * cycletimes * 2))
            sys.exit()
        return data[0], data[1:]
    except Exception as ex:
        logger.error('get druid detail result failed: {0}'.format(ex))
        sys.exit()
    else:
        return None

def getResponse(param):
    logger = getLogger()
    url = query_url + param
    try:
        r = requests.get(url)
    except Exception as ex:
        logger.error('send query url get a exception: {0}'.format(ex))
    else:
        return r.text


def unicode2str(unicodeList):
    strList = list()
    if unicodeList:
        for u in unicodeList:
            if not isinstance(u, basestring):
                strList.append(u)
            else:
                strList.append(u.encode())
    return tuple(strList)


def getUtcHour():
    currentHour = datetime.now().hour
    if currentHour >= 8:
        return currentHour - 8
    else:
        return 16 + currentHour

def getStrList(valueList):
    strValueList = list()
    for v in valueList:
        if v == datetime.now().year or v == getUtcHour():
            strValueList.append(str(v))
        elif isinstance(v, Decimal):
            if str(v).find('.') != -1:
                strValueList.append(float(v))
            else:
                strValueList.append(v)
        else:
            strValueList.append(v)
    return strValueList

def getVaildColumn(column):
    if isinstance(column, list):
        columnString = ','.join(column)
    return '({0})'.format(columnString)

def setCache():
    logger = getLogger()
    result = os.popen(cachecmd).read()
    if result.startswith("success"):
        logger.info('reset l1 cache and l2 cache false success')
    result = os.popen(routercmd).read()
    if result.startswith("successfully"):
        logger.info('reset router to druid success')

def portScanner(machine):
    import socket
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.settimeout(10)
    try:
        sk.connect(machine)
    except Exception as ex:
        return ex
    sk.close()
    return "OK"

def isEnvOK():
    logger = getLogger()
    for machine in machineList:
        if portScanner(machine) != "OK":
            logger.error('telnet {0} on port {1} failed'.format(machine[0], machine[1]))
            sys.exit()
    logger.info('check envirment success')

@@ -641,16 +641,44 @@
 ime()))%0A
+add = lambda x, y : x + y%0A%0A%0A
 %0Adef get
@@ -709,16 +709,20 @@
 start = 
+add(
 int(time
@@ -780,14 +780,36 @@
 ')))
- - 120
+, unix_start_offset_seconds)
 %0A   
@@ -1798,17 +1798,17 @@
 taCount 
-=
+!
 = len(da
@@ -1818,114 +1818,67 @@
 1:%5D)
- == len(offer_aff_combination) * cycletimes * 2:%0A            logger.info('imitate data count: %7B0%7D equal to
+:%0A            logger.error('set click and conv data: %7B0%7D !=
  que
@@ -1895,14 +1895,8 @@
 data
- count
 : %7B1
@@ -1940,154 +1940,8 @@
 )))%0A
-        else:%0A            logger.error('%7B0%7D %7B1%7D %7B2%7D not equal'.format(realDataCount, len(data%5B1:%5D), len(offer_aff_combination) * cycletimes * 2))%0A
     
@@ -2756,16 +2756,20 @@
  return 
+add(
 currentH
@@ -2775,12 +2775,13 @@
 Hour
+,
  -
- 
 8
+)
 %0A   
@@ -2806,13 +2806,12 @@
 urn 
-16 + 
+add(
 curr
@@ -2817,16 +2817,21 @@
 rentHour
+, 16)
 %0A%0Adef ge

add start_offset add add() method