

from BaseDataMaintenance.dataSource.source import getConnect_ots
from BaseDataMaintenance.common.multiThread import MultiThreadHandler
from BaseDataMaintenance.common.Utils import *
from tablestore import *

import requests
from queue import Queue
from threading import Thread



class TestEAS():

    def __init__(self):

        self.ots_client = getConnect_ots()
        self.task_queue = Queue(3000)

    def producer(self):
        columns = ["dochtmlcon","page_time","doctitle","docchannel"]

        boolQuery = BoolQuery(must_queries=[TermQuery("page_time","2021-11-25")])
        rows,next_token,total_count,is_all_succeed = self.ots_client.search("document","document_index",
                                                                            SearchQuery(boolQuery,sort=Sort(sorters=[FieldSort("crtime",SortOrder.DESC)]),get_total_count=True,limit=100),
                                                                                        ColumnsToGet(column_names=columns,return_type=ColumnReturnType.SPECIFIED))
        while rows:
            dict_rows = getRow_ots(rows)
            for _row in dict_rows:
                self.task_queue.put(_row)
            rows,next_token,total_count,is_all_succeed = self.ots_client.search("document","document_index",
                                                                                SearchQuery(boolQuery,next_token=next_token,get_total_count=True,limit=100),
                                                                            ColumnsToGet(column_names=columns,return_type=ColumnReturnType.SPECIFIED))

    def comsumer(self):
        def _handle(item,result_queue):
            content = item.get("dochtmlcon")
            # _url = "http://1255640119316927.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/article_extract"
            _url = "http://1255640119316927.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/other_extract"
            # _url = "http://192.168.2.102:15000/article_extract"
            data = item
            header = {'Content-Type': 'application/json',"Authorization":"NzZmOWZlMmU2MGY3YmQ4MDBjM2E5MDAyZjhjNjQ0MzZlMmE0NTMwZg=="}
            resp = requests.post(_url,json=data,headers=header)
            if resp.status_code=="200":
                print("%s==200"%getCurrent_date())
            else:
                print("%s==%s"%(getCurrent_date(),str(resp.status_code)))
            # print(resp.status_code,item)
            # print(resp.content.decode("utf8"))

        mt = MultiThreadHandler(task_queue=self.task_queue,task_handler=_handle,result_queue=None,thread_count=80)
        mt.run()

    def start(self):
        t_producer = Thread(target=self.producer)
        t_comsumer = Thread(target=self.comsumer)
        t_producer.start()
        t_comsumer.start()
        print("start_time:%s"%getCurrent_date())

if __name__ == '__main__':
    # testEAS = TestEAS()
    # testEAS.start()
    _url = "http://1255640119316927.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/content_extract"
    # _url = "http://192.168.2.102:15000/article_extract"
    data = {"partitionkey":1,"doc_id":33,"docchannel":52,"title":"baddf","content":"招标人： 广州比地数据科技有限公司","page_time":"2021-01-01"}
    header = {'Content-Type': 'application/json',"Authorization":"NzZmOWZlMmU2MGY3YmQ4MDBjM2E5MDAyZjhjNjQ0MzZlMmE0NTMwZg=="}
    resp = requests.post(_url,json=data,headers=header)
    print(resp.content.decode())
    if resp.status_code==200:
        print(resp.content.decode())
        print("%s==200"%getCurrent_date())
    else:
        print("%s==%s"%(getCurrent_date(),str(resp.status_code)))


