diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index 08c2305267..5c4f5986a6 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -24,7 +24,8 @@ from loop_ivr.models import PriceInfoLog, PriceInfoIncoming from loop_ivr.outliers.removal import remove_crop_outliers - +import logging +logger = logging.getLogger(__name__) def make_market_info_call(caller_number, dg_number, incoming_time, incoming_call_id, call_source): app_request_url = APP_REQUEST_URL%(EXOTEL_ID,EXOTEL_TOKEN,EXOTEL_ID) @@ -172,7 +173,7 @@ def get_crop_code_list(number_of_crop, selling_crop_window): def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag): price_info_list = [] - price_info_log_list = [] + # price_info_log_list = [] crop_mandi_comb = [] crop_map = dict() mandi_map = dict() @@ -190,98 +191,93 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_list.append(agg_sms_initial_line) price_info_list.append(AGGREGATOR_SMS_NO) price_info_list.append('\n') - # today_date = datetime.now() - # raw_query = raw_sql.last_three_trans.format('(%s)'%(crop_list[0],) if len(crop_list) == 1 else crop_list, '(%s)'%(mandi_list[0],) if len(mandi_list) == 1 else mandi_list, tuple((today_date-timedelta(days=day)).strftime('%Y-%m-%d') for day in range(0,3))) - # query_result = run_query(raw_query) query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) - if (not result) or dataframe == None or dataframe.empty: - if not all_crop_flag and not all_mandi_flag: - crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) - mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) - no_price_message = (agg_sms_no_price_crop_mandi)%(crop_name_list, mandi_name_list) - # If query for all Mandi - elif all_mandi_flag: - crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) - no_price_message = (agg_sms_no_price_all_mandi)%(crop_name_list,) - # If query for all crops + try: + if (not result) or dataframe.empty or dataframe is None: + if not all_crop_flag and not all_mandi_flag: + crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) + mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) + no_price_message = (agg_sms_no_price_crop_mandi)%(crop_name_list, mandi_name_list) + # If query for all Mandi + elif all_mandi_flag: + crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) + no_price_message = (agg_sms_no_price_all_mandi)%(crop_name_list,) + # If query for all crops + else: + no_price_message = agg_sms_no_price_available + price_info_list.append('\n') + price_info_list.append(no_price_message) + crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) + price_info_list.append(('\n\n%s')%(crop_code_list,)) else: - no_price_message = agg_sms_no_price_available - price_info_list.append('\n') - price_info_list.append(no_price_message) - crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) - price_info_list.append(('\n\n%s')%(crop_code_list,)) - else: - prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' - for index, row in dataframe.iterrows(): - crop, mandi, date, Av_Rate, STD, PriceMax, PriceMin = row['Crop'], row['Market_Real'], row['Date'], row['Av_Ratemean'], row['STDmean'], row['Pricemax'], row['Pricemin'] - delta = PriceMax - PriceMin - if crop != prev_crop or mandi != prev_mandi: - if not all_crop_flag and not all_mandi_flag: - crop_mandi_comb.append((crop,mandi)) - price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, - crop_id=crop, mandi_id=mandi) - price_info_log_list.append(price_info_log_obj) - crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") - mandi_name = mandi_map[mandi].encode("utf-8") - if crop != prev_crop: - temp_str = ('\n%s: %s (%s: %s)\n\n%s %s\n')%(agg_sms_crop_line,crop_name,code_hi,str(crop),mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) - else: - temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) - price_info_list.append(temp_str) - prev_crop, prev_mandi = crop, mandi - if delta < 1: - Av_Rate = round(Av_Rate) - temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) - elif 1 <= delta <=2: - if STD <= delta * 0.4: - Av_Rate = round(Av_Rate) - temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) - else: - max_price = round(PriceMax) - min_price = round(PriceMin) - temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) - elif delta > 2: - min_price = round(PriceMin) - 1 - max_price = round(PriceMax) + 1 - temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) - # if STD == 0: - # temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) - # elif STD < 3: - # min_price = Av_Rate - STD - # max_price = Av_Rate + STD - # temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) - price_info_list.append(temp_str) - # Save combination of crop and mandi for which data is not present in query on if query not for all mandi and crops. - if not all_crop_flag and not all_mandi_flag: - prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' - for crop, mandi in itertools.product(crop_list, mandi_list): - if (crop,mandi) not in crop_mandi_comb: - price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, - crop_id=crop, mandi_id=mandi) - price_info_log_list.append(price_info_log_obj) - if result and (not dataframe.empty): + prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' + for index, row in dataframe.iterrows(): + crop, mandi, date, Av_Rate, STD, PriceMax, PriceMin = row['Crop'], row['Market_Real'], row['Date'], row['Av_Ratemean'], row['STDmean'], row['Pricemax'], row['Pricemin'] + delta = PriceMax - PriceMin + if crop != prev_crop or mandi != prev_mandi: + if not all_crop_flag and not all_mandi_flag: + crop_mandi_comb.append((crop,mandi)) + # price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, + # crop_id=crop, mandi_id=mandi) + # price_info_log_list.append(price_info_log_obj) crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") mandi_name = mandi_map[mandi].encode("utf-8") if crop != prev_crop: - prev_crop = crop temp_str = ('\n%s: %s (%s: %s)\n\n%s %s\n')%(agg_sms_crop_line,crop_name,code_hi,str(crop),mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) else: - prev_mandi = mandi temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) price_info_list.append(temp_str) - price_info_list.append(agg_sms_no_price_for_combination) - price_info_list.append(('\n%s: %s')%(helpline_hi, EXOTEL_HELPLINE_NUMBER)) - final_result = ''.join(price_info_list) - price_info_incoming_obj.price_result = final_result + prev_crop, prev_mandi = crop, mandi + if delta < 1: + Av_Rate = round(Av_Rate) + temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) + elif 1 <= delta <=2: + if STD <= delta * 0.4: + Av_Rate = round(Av_Rate) + temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) + else: + max_price = round(PriceMax) + min_price = round(PriceMin) + temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) + elif delta > 2: + min_price = round(PriceMin) - 1 + max_price = round(PriceMax) + 1 + temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) + price_info_list.append(temp_str) + # Save combination of crop and mandi for which data is not present in query on if query not for all mandi and crops. + if not all_crop_flag and not all_mandi_flag: + prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' + for crop, mandi in itertools.product(crop_list, mandi_list): + if (crop,mandi) not in crop_mandi_comb: + price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, + crop_id=crop, mandi_id=mandi) + # price_info_log_list.append(price_info_log_obj) + if result and dataframe is not None and (not dataframe.empty): + crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") + mandi_name = mandi_map[mandi].encode("utf-8") + if crop != prev_crop: + prev_crop = crop + temp_str = ('\n%s: %s (%s: %s)\n\n%s %s\n')%(agg_sms_crop_line,crop_name,code_hi,str(crop),mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) + else: + prev_mandi = mandi + temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) + price_info_list.append(temp_str) + price_info_list.append(agg_sms_no_price_for_combination) - price_info_incoming_obj.return_result_to_app = 0 - price_info_incoming_obj.info_status = 1 - price_info_incoming_obj.save() - send_info_using_textlocal(from_number, final_result, price_info_incoming_obj) + price_info_list.append(('\n%s: %s')%(helpline_hi, EXOTEL_HELPLINE_NUMBER)) + final_result = ''.join(price_info_list) + + price_info_incoming_obj.price_result = final_result + price_info_incoming_obj.return_result_to_app = 0 + price_info_incoming_obj.info_status = 1 + price_info_incoming_obj.save() + send_info_using_textlocal(from_number, final_result, price_info_incoming_obj) + except Exception as e: + logger.debug(e) # If caller is calling first time then send crop code to them. if PriceInfoIncoming.objects.filter(from_number=from_number).count() == 1: @@ -292,11 +288,8 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, #send_sms(AGGREGATOR_SMS_NO, from_number, first_time_caller_message) send_info_using_textlocal(from_number, first_time_caller_message) else: - pass #send_sms(AGGREGATOR_SMS_NO, from_number, crop_code_list) send_info_using_textlocal(from_number, crop_code_list) - # PriceInfoLog.objects.bulk_create(price_info_log_list) - def send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) : # Send No code entered message to user diff --git a/loop_ivr/outliers/removal.py b/loop_ivr/outliers/removal.py index 602b5cdf29..64464d2339 100644 --- a/loop_ivr/outliers/removal.py +++ b/loop_ivr/outliers/removal.py @@ -15,8 +15,6 @@ def remove_crop_outliers(ct_data=None): combined_transactions_data = call_methods(combined_transactions_data) - # combined_transactions_data.to_csv("final_data_after_outliers.csv") - for recursion_counter in range(0,3): combined_transactions_data.fillna(0,inplace=True) ct_data = combined_transactions_data[(combined_transactions_data['D/STD'] > 1.3)] @@ -55,7 +53,6 @@ def call_methods(combined_transactions_data): def get_statistics(combined_transactions_data): - ct_data_with_mean = combined_transactions_data.groupby(group_by_list).apply(compute_mean).reset_index(name='Av_Rate') combined_transactions_data = combined_transactions_data.merge(ct_data_with_mean,how='left',on=group_by_list) diff --git a/loop_ivr/views.py b/loop_ivr/views.py index e5709adbd8..cb1a3d3057 100644 --- a/loop_ivr/views.py +++ b/loop_ivr/views.py @@ -136,59 +136,59 @@ def crop_price_query(request): def handle_query_code(query_code, price_info_incoming_obj, farmer_number): - if query_code == '' or query_code == 'None': - if price_info_incoming_obj.call_source == 3: - sms_content = [no_code_entered,'\n\n'] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif query_code == '0': - if price_info_incoming_obj.call_source == 3: - sms_content = [] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif re.search(PATTERN_REGEX, query_code) is None: - # send wrong query code + if query_code == '' or query_code == 'None': + if price_info_incoming_obj.call_source == 3: + sms_content = [no_code_entered,'\n\n'] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif query_code == '0': + if price_info_incoming_obj.call_source == 3: + sms_content = [] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif re.search(PATTERN_REGEX, query_code) is None: + # send wrong query code + if price_info_incoming_obj.call_source == 3: + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + else: + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() + return HttpResponse(status=404) + else : + # send corresponding response + query_code = query_code.split('**') + all_crop_flag = False + all_mandi_flag = False + + if len(query_code) >= 2: + crop_info, mandi_info = query_code[0], query_code[1] + elif len(query_code) == 1: + crop_info = query_code[0] + mandi_info = '' + if re.search(CONTAINS_ZERO,crop_info) is not None: + all_crop_flag = True + if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': + all_mandi_flag = True + + crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) + mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) + if len(crop_list) == 0: if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code) else: price_info_incoming_obj.info_status = 2 price_info_incoming_obj.save() return HttpResponse(status=404) - else : - # send corresponding response - query_code = query_code.split('**') - all_crop_flag = False - all_mandi_flag = False - - if len(query_code) >= 2: - crop_info, mandi_info = query_code[0], query_code[1] - elif len(query_code) == 1: - crop_info = query_code[0] - mandi_info = '' - if re.search(CONTAINS_ZERO,crop_info) is not None: - all_crop_flag=True - if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': - all_mandi_flag=True - - crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) - mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) - if len(crop_list) == 0: - if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) - else: - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() - return HttpResponse(status=404) - else: - Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() - return HttpResponse(status=200) - return HttpResponse(status=403) + else: + Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() + return HttpResponse(status=200) + return HttpResponse(status=403) @csrf_exempt def market_info_response(request): @@ -227,65 +227,6 @@ def market_info_response(request): price_info_incoming_obj.save() return HttpResponse(status=200) -# def crop_price_sms_content(request): -# if request.method == 'HEAD': -# return HttpResponse(status=200, content_type='text/plain') -# if request.method == 'GET': -# call_id = str(request.GET.getlist('CallSid')[0]) -# farmer_number = str(request.GET.getlist('From')[0]) -# dg_number = str(request.GET.getlist('To')[0]) -# try: -# price_info_obj = PriceInfoIncoming.objects.get(call_id=call_id, from_number=farmer_number, -# to_number=dg_number) -# if price_info_obj.return_result_to_app == 1: -# sms_content = price_info_obj.price_result -# price_info_obj.info_status = 1 -# price_info_obj.save() -# response = HttpResponse(sms_content, content_type='text/plain') -# else: -# response = HttpResponse(status=200, content_type='text/plain') -# except Exception as e: -# logger.debug(e) -# response = HttpResponse(status=200, content_type='text/plain') -# return response -# return HttpResponse(status=403) -# -# def no_code_message(request): -# if request.method == 'HEAD': -# return HttpResponse(status=200, content_type='text/plain') -# if request.method == 'GET': -# crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) -# sms_content = [no_code_entered,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] -# sms_content = ''.join(sms_content) -# response = HttpResponse(sms_content, content_type='text/plain') -# return response -# return HttpResponse(status=403) -# -# def wrong_code_message(request): -# if request.method == 'HEAD': -# return HttpResponse(status=200, content_type='text/plain') -# if request.method == 'GET': -# crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) -# call_id = str(request.GET.getlist('CallSid')[0]) -# farmer_number = str(request.GET.getlist('From')[0]) -# dg_number = str(request.GET.getlist('To')[0]) -# try: -# price_info_obj = PriceInfoIncoming.objects.get(call_id=call_id, from_number=farmer_number, -# to_number=dg_number) -# wrong_query_code = str(price_info_obj.query_code) if price_info_obj.query_code else '' -# except Exception as e: -# wrong_query_code = '' -# wrong_code_entered_message = wrong_code_entered -# if wrong_query_code == '': -# wrong_code_entered_message = wrong_code_entered_message%(wrong_query_code,) -# else: -# wrong_code_entered_message = wrong_code_entered_message%((' (%s:%s)')%(code_hi,wrong_query_code),) -# sms_content = [wrong_code_entered_message,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] -# sms_content = ''.join(sms_content) -# response = HttpResponse(sms_content, content_type='text/plain') -# return response -# return HttpResponse(status=403) - @csrf_exempt def push_message_sms_response(request): if request.method == 'POST':