def search_top_skilled_problems_for_user(user_id, sort_field, size, heavy=False): try: rs = requests.session() must = [{'term': {'user_id': user_id}}] must.append({'term': {'status': 'UNSOLVED'}}) query_json = {'query': {'bool': {'must': must}}} query_json['from'] = 0 query_json['size'] = size query_json['sort'] = [{sort_field: {'order': 'desc'}}] search_url = 'http://{}/{}/{}/_search'.format(app.config['ES_HOST'], _es_index_problem_user, _es_type) response = rs.post(url=search_url, json=query_json, headers=_http_headers).json() item_list = [] if 'hits' in response: rank = 1 for hit in response['hits']['hits']: item = hit['_source'] item['relevant_score'] = "{:.2f}".format( item['relevant_score']) if heavy: item['problem_info'] = get_problem_details( item['problem_id']) item['rank'] = rank item_list.append(item) rank += 1 return item_list except Exception as e: raise e
def get(self, problem_id): try: app.logger.info('Get problem_details api called') response = get_problem_details(problem_id) return response except Exception as e: return {'message': str(e)}, 500
def find_problem_set_for_contest(contest_id): try: rs = requests.session() query_json = { 'query': { 'bool': { 'must': [{ 'term': { 'contest_id': contest_id } }] } } } query_json['size'] = _es_size search_url = 'http://{}/{}/{}/_search'.format( app.config['ES_HOST'], _es_index_contest_problem_edges, _es_type) response = rs.post(url=search_url, json=query_json, headers=_http_headers).json() item_list = [] if 'hits' in response: for hit in response['hits']['hits']: data = hit['_source'] problem_details = get_problem_details(data['problem_id']) problem_details['problem_order'] = data['problem_order'] item_list.append(problem_details) return item_list app.logger.error('Elasticsearch down, response: ' + str(response)) raise Exception('Elasticsearch down') except Exception as e: raise e
def post(self): try: app.logger.info('Create problem api called') rs = requests.session() data = request.get_json() categories = [] if 'category_dependency_list' in data: category_dependency_list = data['category_dependency_list'] data.pop('category_dependency_list', None) for cat in category_dependency_list: category_id = cat.get('category_id', None) if category_id is None: category_id = get_category_id_from_name( cat['category_name']) category_details = get_category_details(category_id) edge = { 'category_id': category_id, 'dependency_factor': cat['factor'], 'category_root': category_details['category_root'], 'category_name': category_details['category_name'], } categories.append(edge) data['categories'] = categories data['created_at'] = int(time.time()) data['updated_at'] = int(time.time()) problem_id = create_problem_id(data['oj_name'] + '-' + data['problem_id']) existing_problem = get_problem_details(problem_id) if existing_problem is not None: return { 'message': 'problem already exists with same problem id' }, 409 post_url = 'http://{}/{}/{}/{}'.format(app.config['ES_HOST'], _es_index, _es_type, problem_id) response = rs.post(url=post_url, json=data, headers=_http_headers).json() app.logger.info('Problem Created Successfully') if 'result' in response: app.logger.info('Create problem api completed') return response['_id'], 201 app.logger.error('Elasticsearch down, response: ' + str(response)) return response, 500 except Exception as e: return {'message': str(e)}, 500