<commit_before># -*- coding: utf-8 -*-
# @Date    : 2014-06-25 15:43:36
# @Author  : <EMAIL>
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from BeautifulSoup import BeautifulSoup
from ..models.pe_models import PEUser
from ..models.tice_cache import TiceCache
from sqlalchemy.orm.exc import NoResultFound
import tornado.web
import tornado.gen
import urllib
import random
import json,socket,base64,traceback
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime,mktime,strptime
from datetime import datetime, timedelta, date
import functools
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
last_refresh = 0
all_count = {}
all_sum = 0

class PEHandler(tornado.web.RequestHandler):
    # 为查跑操的 TCP Client 建立线程池
    executor = ThreadPoolExecutor(10)

    @property
    def db(self):
        return self.application.db

    def get(self):
        self.write('Herald Web Service')

    def on_finish(self):
        self.db.close()


    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        state = 'success'
        cardnum = self.get_argument('cardnum', default=None)
        pwd = self.get_argument('pwd', default=cardnum)
        retjson = {'code':200, 'content':''}
        # pwd 缺省为 cardnum

        if not cardnum:
            retjson['code'] = 400
            retjson['content'] = 'params lack'
        else:
            result = yield self.get_pe_count(cardnum)

            if result == -1:
                state = 'time_out'
                try:
                    # 超时取出缓存
                    user = self.db.query(PEUser).filter(
                        PEUser.cardnum == int(cardnum)).one()
                    retjson['content'] = user.count
		    retjson['rank'] = self.get_rank(user.count)
                    retjson['remain'] = self.get_remain_day()
                except NoResultFound:
                    retjson['remain'] = self.get_remain_day()
                    retjson['code'] = 408
                    retjson['content'] = 'time out'
            else:
                retjson['content'] = str(result)
		retjson['rank'] = self.get_rank(result)
                retjson['remain'] = self.get_remain_day()
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()

        if state == 'success':
            # 更新缓存
            try:
                user = self.db.query(PEUser).filter(
                    PEUser.cardnum == int(cardnum)).one()
                user.count = int(result)
            except NoResultFound:
                user = PEUser(cardnum=int(cardnum), count=int(result))
                self.db.add(user)
            finally:
                self.db.commit()
        self.db.close()
    def get_rank(self, count):
        global last_refresh
        global all_count
        global all_sum
        try:
            if last_refresh + 300 < int(time()):
                last_refresh = int(time())
                db_instance = self.db.execute("select count, count(*) as all_sum from pe group by count").fetchall()
                all_count = {}
                all_sum = 0
                for item in db_instance:
                    all_count[item.count] = int(item.all_sum)
                    all_sum += int(item.all_sum)
            temp_sum = 0
            for i, j in all_count.items():
                if int(i) >= int(count):
                    temp_sum += all_count[i]
            return '%.2f' % ((float(all_sum)-temp_sum)/all_sum*100)
        except Exception,e:
            return '0'

    # 把查跑操的 TCP 连接放在线程池中运行
    @run_on_executor
    def get_pe_count(self,cardnum):
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.settimeout(3)
        try:
            s.connect((API_SERVER_HOST, API_SERVER_PORT))
            s.send('\x00'+A+'\x09'+cardnum+'\x00')
            recv = s.recv(1024).split(',')
            return int(recv[0])+int(recv[1])
        except socket.timeout:
            return -1
        except:
            return -1

    def get_remain_day(self):
        def weekends_between(start, end):
            days_between = (end - start).days
            weekends, leftover = divmod(days_between, 7)
            if leftover:
                start_day = (end-timedelta(leftover)).isoweekday()
                end_day = start_day + leftover
                if start_day <= 6 and end_day > 6:
                    weekends += .5
                if start_day <= 7 and end_day > 7:
                    weekends += .5
            return weekends
        
        now = datetime.now()
        current_day = date(now.year, now.month, now.day)
        final = final_day + timedelta(days=1)
        return (final - current_day).days - weekends_between(current_day, final)*2
        
        """
        finay_day_strp = strptime(finay_day,"%Y-%m-%d")
        final = datetime.datetime(finay_day_strp[0],finay_day_strp[1],finay_day_strp[2])
        current_day_strp = strptime(strftime('%Y-%m-%d', localtime(time())),"%Y-%m-%d")
        current = datetime.datetime(current_day_strp[0],current_day_strp[1],current_day_strp[2])
        alldays = (final-current).days
        current_date = daymap[strftime('%a', localtime(time()))]
        front_remain = 7-current_date
        front_day = 6-current_date
        front_day = front_day if front_day>0 else 0
        back_remain = 5
        all_week = (alldays-5-front_remain)/7
        workday_count = (all_week+1)*5+front_day
        if current_date<=6:
            workday_count = workday_count-1
        return workday_count if workday_count > 0  else 0
        """
#体测信息
class ticeInfoHandler(tornado.web.RequestHandler):<commit_msg>当天过了8点按第二天计算<commit_after># -*- coding: utf-8 -*-
# @Date    : 2014-06-25 15:43:36
# @Author  : <EMAIL>
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from BeautifulSoup import BeautifulSoup
from ..models.pe_models import PEUser
from ..models.tice_cache import TiceCache
from sqlalchemy.orm.exc import NoResultFound
import tornado.web
import tornado.gen
import urllib
import random
import json,socket,base64,traceback
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime,mktime,strptime
from datetime import datetime, timedelta, date
import functools
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
last_refresh = 0
all_count = {}
all_sum = 0

class PEHandler(tornado.web.RequestHandler):
    # 为查跑操的 TCP Client 建立线程池
    executor = ThreadPoolExecutor(10)

    @property
    def db(self):
        return self.application.db

    def get(self):
        self.write('Herald Web Service')

    def on_finish(self):
        self.db.close()


    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        state = 'success'
        cardnum = self.get_argument('cardnum', default=None)
        pwd = self.get_argument('pwd', default=cardnum)
        retjson = {'code':200, 'content':''}
        # pwd 缺省为 cardnum

        if not cardnum:
            retjson['code'] = 400
            retjson['content'] = 'params lack'
        else:
            result = yield self.get_pe_count(cardnum)

            if result == -1:
                state = 'time_out'
                try:
                    # 超时取出缓存
                    user = self.db.query(PEUser).filter(
                        PEUser.cardnum == int(cardnum)).one()
                    retjson['content'] = user.count
		    retjson['rank'] = self.get_rank(user.count)
                    retjson['remain'] = self.get_remain_day()
                except NoResultFound:
                    retjson['remain'] = self.get_remain_day()
                    retjson['code'] = 408
                    retjson['content'] = 'time out'
            else:
                retjson['content'] = str(result)
		retjson['rank'] = self.get_rank(result)
                retjson['remain'] = self.get_remain_day()
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()

        if state == 'success':
            # 更新缓存
            try:
                user = self.db.query(PEUser).filter(
                    PEUser.cardnum == int(cardnum)).one()
                user.count = int(result)
            except NoResultFound:
                user = PEUser(cardnum=int(cardnum), count=int(result))
                self.db.add(user)
            finally:
                self.db.commit()
        self.db.close()
    def get_rank(self, count):
        global last_refresh
        global all_count
        global all_sum
        try:
            if last_refresh + 300 < int(time()):
                last_refresh = int(time())
                db_instance = self.db.execute("select count, count(*) as all_sum from pe group by count").fetchall()
                all_count = {}
                all_sum = 0
                for item in db_instance:
                    all_count[item.count] = int(item.all_sum)
                    all_sum += int(item.all_sum)
            temp_sum = 0
            for i, j in all_count.items():
                if int(i) >= int(count):
                    temp_sum += all_count[i]
            return '%.2f' % ((float(all_sum)-temp_sum)/all_sum*100)
        except Exception,e:
            return '0'

    # 把查跑操的 TCP 连接放在线程池中运行
    @run_on_executor
    def get_pe_count(self,cardnum):
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.settimeout(3)
        try:
            s.connect((API_SERVER_HOST, API_SERVER_PORT))
            s.send('\x00'+A+'\x09'+cardnum+'\x00')
            recv = s.recv(1024).split(',')
            return int(recv[0])+int(recv[1])
        except socket.timeout:
            return -1
        except:
            return -1

    def get_remain_day(self):
        def weekends_between(start, end):
            days_between = (end - start).days
            weekends, leftover = divmod(days_between, 7)
            if leftover:
                start_day = (end-timedelta(leftover)).isoweekday()
                end_day = start_day + leftover
                if start_day <= 6 and end_day > 6:
                    weekends += .5
                if start_day <= 7 and end_day > 7:
                    weekends += .5
            return weekends
        
        final = final_day + timedelta(days=1)
        now = datetime.now()
        current_day = date(now.year, now.month, now.day)
        """
        如果超过八点，按后天计算
        """
        if now.hour > 8:
            current_day = current_day + timedelta(days=1)
        else:
            pass
        """
        跑操最后一次操的那天-今天-所有之间的双休日
        """
        return (final - current_day).days - weekends_between(current_day, final)*2
        
        """
        finay_day_strp = strptime(finay_day,"%Y-%m-%d")
        final = datetime.datetime(finay_day_strp[0],finay_day_strp[1],finay_day_strp[2])
        current_day_strp = strptime(strftime('%Y-%m-%d', localtime(time())),"%Y-%m-%d")
        current = datetime.datetime(current_day_strp[0],current_day_strp[1],current_day_strp[2])
        alldays = (final-current).days
        current_date = daymap[strftime('%a', localtime(time()))]
        front_remain = 7-current_date
        front_day = 6-current_date
        front_day = front_day if front_day>0 else 0
        back_remain = 5
        all_week = (alldays-5-front_remain)/7
        workday_count = (all_week+1)*5+front_day
        if current_date<=6:
            workday_count = workday_count-1
        return workday_count if workday_count > 0  else 0
        """
#体测信息
class ticeInfoHandler(tornado.web.RequestHandler):
{'fraction_non_alphanumeric': 0.10920172691103022, 'fraction_numerical': 0.014644882756285449, 'mean_word_length': 1.9646173149309911, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5585575', 'n_tokens_mistral': 3877, 'n_tokens_neox': 3670, 'n_words': 739}