def ajax_create_task():
    # task_name forbid illegal enter
    task_number = request.args.get('task_number', 5)
    task_name = request.args.get('task_name','') # must
    create_by = request.args.get('create_by', 'admin') # 用户
    create_at = str(request.args.get('create_at', int(time.time())))
    stop_time = request.args.get('stop_time', "default") #timestamp, 1234567890
    keywords = request.args.get("keywords", "") # keywords_list, split with " "
    remark = request.args.get("remark", "")
    _id = create_by + "-" + task_name
    exist_es = es.exists(index=index_manage_sensing_task, doc_type=task_doc_type, id=_id)
    if exist_es:
        return json.dumps(["0"]) # 任务名不能重合
    query_body = {
        "query":{
            "filtered":{
                "filter":{
                    "bool":{
                        "must":[
                            {"term":{"processing_status": "1"}},
                            {"term":{"finish":"0"}}
                        ]
                    }
                }
            }
        }
    }
    
    unfinish_number =  es.count(index=index_manage_sensing_task, doc_type=task_doc_type, body=query_body)['count']
    if unfinish_number > (int(task_number)-1):
        return "more than limit"
    if task_name:
        task_detail = dict()
        task_detail["task_name"] = task_name
        task_detail["create_by"] = create_by # 创建任务, user
        task_detail["stop_time"] = stop_time
        task_detail["remark"] = remark
        task_detail["create_at"] = create_at
        task_detail["warning_status"] = '0'
        task_detail["finish"] = "0" # not end the task
        task_detail["history_status"] = json.dumps([]) # ts, keywords, warning_status
        task_detail['burst_reason'] = ''
        task_detail['processing_status'] = "1" #任务正在进行
        task_detail['keywords'] = keywords

    # store task detail into es
    es.index(index=index_manage_sensing_task, doc_type=task_doc_type, id=_id, body=task_detail)

    return json.dumps(["1"])
Exemple #2
0
def imagine(uid,
            query_fields_dict,
            index_name=portrait_index_name,
            doctype=portrait_index_type):
    default_setting_dict = query_fields_dict
    print query_fields_dict

    personal_info = es.get(index=portrait_index_name,
                           doc_type=portrait_index_type,
                           id=uid,
                           _source=True)['_source']

    # tag
    tag_dict = dict()
    tag_dict_value = 0
    if "tag" in query_fields_dict:
        tag_dict_value = query_fields_dict["tag"]
        query_fields_dict.pop("tag")
        for key, value in personal_info.iteritems():
            if "tag-" in key:
                tag_dict[key] = value
    print tag_dict, tag_dict_value

    # size
    sort_size = query_fields_dict["size"]
    query_fields_dict.pop("size")

    keys_list = []
    for k, v in query_fields_dict.iteritems():
        if v:
            keys_list.append(k)  #需要进行关联的键

    search_dict = {}  # 检索的属性字典
    iter_list = []
    tag_attri_vaule = []

    # 对搜索的键值进行过滤,去掉无用的键
    for iter_key in keys_list:
        if iter_key in personal_info:
            if not personal_info[iter_key] or not query_fields_dict[iter_key]:
                query_fields_dict.pop(iter_key)
                continue
            else:
                iter_list.append(iter_key)
                temp = personal_info[iter_key]
                search_dict[iter_key] = temp.split('&')
            """
            query_fields_dict.pop(iter_key)
            if tag_dict.get(iter_key,''):
                tag_attri_vaule.append(iter_key+"-"+tag_dict[iter_key])
            """

    if len(iter_list) == 0 and len(tag_dict) == 0:
        return []

    query_body = {
        'query': {
            'function_score': {
                'query': {
                    'bool': {
                        'must': []
                    }
                }
            }
        }
    }

    number = es.count(index=index_name, doc_type=doctype,
                      body=query_body)['count']
    query_body["size"] = sort_size + 100

    for (k, v) in query_fields_dict.items():
        temp = {}
        temp_list = []
        if k in personal_info and v != 0:
            for iter_key in search_dict[k]:
                temp_list.append({
                    'wildcard': {
                        k: {
                            'wildcard': '*' + iter_key + '*',
                            'boost': v
                        }
                    }
                })

            query_body['query']['function_score']['query']['bool'][
                'must'].append({'bool': {
                    'should': temp_list
                }})

    if tag_dict and tag_dict_value:
        temp_list = []
        for k, v in tag_dict.iteritems():
            temp = {"term": {k: v}}
            temp_list.append(temp)
        query_body['query']['function_score']['query']['bool']['must'].append(
            {'bool': {
                'should': temp_list
            }})

    result = es.search(index=index_name, doc_type=doctype,
                       body=query_body)['hits']['hits']
    field_list = [
        'uid', 'uname', 'activeness', 'importance', 'influence', 'sensitive'
    ]
    evaluate_index_list = [
        'activeness', 'importance', 'influence', 'sensitive'
    ]
    return_list = []
    count = 0

    if len(result) > 1 and result:
        if result[0]['_id'] != uid:
            top_score = result[0]['_score']
        else:
            top_score = result[1]['_score']

    #get evaluate max to normal
    evaluate_max_dict = get_evaluate_max()
    for item in result:
        if uid == item['_id']:
            score = item['_score']
            continue
        info = []
        for field in field_list:
            if field in evaluate_index_list:
                value = item['_source'][field]
                normal_value = math.log(
                    value / float(evaluate_max_dict[field]) * 9 + 1, 10) * 100
            else:
                normal_value = item['_source'][field]
                if not normal_value:
                    normal_value = item['_id']
            info.append(normal_value)
        info.append(item['_score'] / float(top_score) * 100)
        return_list.append(info)
        count += 1

        if count == sort_size:
            break

    return_list.append(number)

    temp_list = []
    for field in field_list:
        if field in evaluate_index_list:
            value = personal_info[field]
            normal_value = math.log(
                value / float(evaluate_max_dict[field]) * 9 + 1, 10) * 100
        else:
            normal_value = personal_info[field]
        temp_list.append(normal_value)

    results = []
    results.append(temp_list)
    results.extend(return_list)

    return results
def imagine(uid, query_fields_dict,index_name=portrait_index_name, doctype=portrait_index_type):
    default_setting_dict = query_fields_dict
    print query_fields_dict

    personal_info = es.get(index=portrait_index_name, doc_type=portrait_index_type, id=uid, _source=True)['_source']

    # tag
    tag_dict = dict()
    tag_dict_value = 0
    if "tag" in query_fields_dict:
        tag_dict_value = query_fields_dict["tag"]
        query_fields_dict.pop("tag")
        for key,value in personal_info.iteritems():
            if "tag-" in key:
                tag_dict[key] = value
    print tag_dict, tag_dict_value

    # size
    sort_size = query_fields_dict["size"]
    query_fields_dict.pop("size")

    keys_list = []
    for k, v in query_fields_dict.iteritems():
        if v:
            keys_list.append(k) #需要进行关联的键

    search_dict = {} # 检索的属性字典
    iter_list = []
    tag_attri_vaule = []

    # 对搜索的键值进行过滤,去掉无用的键
    for iter_key in keys_list:
        if iter_key in personal_info:
            if not personal_info[iter_key] or not query_fields_dict[iter_key]:
                query_fields_dict.pop(iter_key)
                continue
            else:
                iter_list.append(iter_key)
                temp = personal_info[iter_key]
                search_dict[iter_key] = temp.split('&')

            """
            query_fields_dict.pop(iter_key)
            if tag_dict.get(iter_key,''):
                tag_attri_vaule.append(iter_key+"-"+tag_dict[iter_key])
            """

    if len(iter_list) == 0 and len(tag_dict) == 0:
        return []

    query_body = {
        'query':{
            'function_score':{
                'query':{
                    'bool':{
                        'must':[
                        ]
                    }
                }
            }
        }
    }

    number = es.count(index=index_name, doc_type=doctype, body=query_body)['count']
    query_body["size"] = sort_size+100

    for (k,v) in query_fields_dict.items():
        temp = {}
        temp_list = []
        if k in personal_info and v != 0:
            for iter_key in search_dict[k]:
                temp_list.append({'wildcard':{k:{'wildcard':'*'+iter_key+'*', 'boost': v}}})

            query_body['query']['function_score']['query']['bool']['must'].append({'bool':{'should':temp_list}})

    if tag_dict and tag_dict_value:
        temp_list = []
        for k,v in tag_dict.iteritems():
            temp = {"term":{k:v}}
            temp_list.append(temp)
        query_body['query']['function_score']['query']['bool']['must'].append({'bool':{'should':temp_list}})

    result = es.search(index=index_name, doc_type=doctype, body=query_body)['hits']['hits']
    field_list = ['uid','uname', 'activeness','importance', 'influence', 'sensitive']
    evaluate_index_list = ['activeness', 'importance', 'influence', 'sensitive']
    return_list = []
    count = 0

    if len(result) > 1 and result:
        if result[0]['_id'] != uid:
            top_score = result[0]['_score']
        else:
            top_score = result[1]['_score']

    #get evaluate max to normal
    evaluate_max_dict = get_evaluate_max()
    for item in result:
        if uid == item['_id']:
            score = item['_score']
            continue
        info = []
        for field in field_list:
            if field in evaluate_index_list:
                value = item['_source'][field]
                normal_value = math.log(value / float(evaluate_max_dict[field] )* 9 + 1, 10) * 100
            else:
                normal_value = item['_source'][field]
                if not normal_value:
                    normal_value = item['_id']
            info.append(normal_value)
        info.append(item['_score']/float(top_score)*100)
        return_list.append(info)
        count += 1

        if count == sort_size:
            break

    return_list.append(number)

    temp_list = []
    for field in field_list:
        if field in evaluate_index_list:
            value = personal_info[field]
            normal_value = math.log(value / float(evaluate_max_dict[field]) * 9 + 1, 10) * 100
        else:
            normal_value = personal_info[field]
        temp_list.append(normal_value)

    results = []
    results.append(temp_list)
    results.extend(return_list)


    return results
def ajax_create_task():
    # task_name forbid illegal enter
    task_number = request.args.get('task_number', 5)
    task_name = request.args.get('task_name', '')  # must
    create_by = request.args.get('create_by', 'admin')  # 用户
    create_at = str(request.args.get('create_at', int(time.time())))
    stop_time = request.args.get('stop_time',
                                 "default")  #timestamp, 1234567890
    keywords = request.args.get("keywords",
                                "")  # keywords_list, split with " "
    remark = request.args.get("remark", "")
    _id = create_by + "-" + task_name
    exist_es = es.exists(index=index_manage_sensing_task,
                         doc_type=task_doc_type,
                         id=_id)
    if exist_es:
        return json.dumps(["0"])  # 任务名不能重合
    query_body = {
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "must": [{
                            "term": {
                                "processing_status": "1"
                            }
                        }, {
                            "term": {
                                "finish": "0"
                            }
                        }]
                    }
                }
            }
        }
    }

    unfinish_number = es.count(index=index_manage_sensing_task,
                               doc_type=task_doc_type,
                               body=query_body)['count']
    if unfinish_number > (int(task_number) - 1):
        return "more than limit"
    if task_name:
        task_detail = dict()
        task_detail["task_name"] = task_name
        task_detail["create_by"] = create_by  # 创建任务, user
        task_detail["stop_time"] = stop_time
        task_detail["remark"] = remark
        task_detail["create_at"] = create_at
        task_detail["warning_status"] = '0'
        task_detail["finish"] = "0"  # not end the task
        task_detail["history_status"] = json.dumps(
            [])  # ts, keywords, warning_status
        task_detail['burst_reason'] = ''
        task_detail['processing_status'] = "1"  #任务正在进行
        task_detail['keywords'] = keywords

    # store task detail into es
    es.index(index=index_manage_sensing_task,
             doc_type=task_doc_type,
             id=_id,
             body=task_detail)

    return json.dumps(["1"])