# -*- coding: utf-8 -*-
"""
Created on Fri Jun  1 18:03:03 2018

@author: DONG
"""
import sys
import os
os.environ['KERAS_BACKEND']='tensorflow'
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
sys.path.append(os.path.abspath(".."))
from flask import Flask, jsonify
from flask import request
import logging
import time
import traceback
import base64
from BaseDataMaintenance.dataSource.source import getConnect_ots
from BaseDataMaintenance.common.Utils import log
from BaseDataMaintenance.model.ots.document_extract2 import *

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


def rollbackStatus(ots_client,docids):
    for _docid in docids:
        docid = int(_docid)
        partitionkey = docid%500+1
        _dict = {document_extract2_partitionkey:partitionkey,
                 document_extract2_docid:docid,
                 document_extract2_save:1,
                 document_extract2_already_storage:1,
                 document_extract2_status:69}
        _extract2 = Document_extract2(_dict)
        _extract2.update_row(ots_client)
        log("process %d done"%docid)

ots_client = getConnect_ots()

@app.route('/rollbackStatus', methods=['POST'])
def turn_status():
    # 初始化待返回结果
    try:
        s_docids = request.form.get('docids')
        docids = s_docids.split(",")
        rollbackStatus(ots_client,docids)

        log("rollback status succeed %s"%(str(s_docids)))
    except Exception as e:
        log("rollback status failed %s"%(str(s_docids)))
        traceback.print_exc()
        return jsonify({"sucess":False,"docids":s_docids}), 500

    return jsonify({"sucess":True,"docids":s_docids}), 201

def start_rollback_service():
    app.run(host='0.0.0.0', port=17050, threaded=True, debug=True)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=17050, threaded=True, debug=True)
    # text_predict()