-
Notifications
You must be signed in to change notification settings - Fork 8
Loop mi v5 pilot #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Loop mi v5 pilot #931
Changes from all commits
461b393
e73afab
acf6700
3bb8786
153c818
d0a2863
943fb7b
d6c0148
5c54394
4f9cae9
a26eec6
26d9c11
a6be6d2
f141e2f
4cf1e45
df92494
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class CropMandiData: | ||
| def __init__(self, **kwargs): | ||
| self.crop_id = kwargs['crop_id'] | ||
| self.mandi_id = kwargs['mandi_id'] | ||
| self.price_details = [] | ||
| self.latest_price_date = None | ||
|
|
||
| class PriceDetails: | ||
| def __init__(self, **kwargs): | ||
| self.date = kwargs['date'] | ||
| self.min_price = kwargs['min_price'] | ||
| self.max_price = kwargs['max_price'] | ||
| self.avg_price = kwargs['avg_price'] | ||
| self.std = kwargs['std'] | ||
| self.delta = kwargs['delta'] | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| class AggregatorDetail: | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.id = kwargs['aggregator_id'] | ||
| self.name = kwargs['aggregator_name'] | ||
| self.mandi_list = [] | ||
|
|
||
| def getAggData(self): | ||
| pass | ||
|
|
||
| class MandiDetail: | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.id =kwargs['mandi_id'] | ||
| self.name = kwargs['mandi_name'] | ||
| self.category = kwargs['mandi_category'] | ||
| self.distance = kwargs['mandi_distance'] | ||
| self.transport_list = [] | ||
| self.gaddidar_list = [] | ||
|
|
||
| def getMandiDetail(self): | ||
| pass | ||
|
|
||
| class TransportDetail: | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.id = kwargs['transport_id'] | ||
| self.name = kwargs['transport_name'] | ||
| self.cost = kwargs['transport_cost'] | ||
| self.capacity = kwargs['transport_capacity'] | ||
|
|
||
| def getTransportDetail(self): | ||
| pass | ||
|
|
||
|
|
||
| class GaddidarDetail: | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.id = kwargs['gaddidar_id'] | ||
| self.name = kwargs['gaddidar_name'] | ||
| self.phone_no = kwargs['gaddidar_phone_no'] | ||
|
|
||
| def getGaddidarDetail(self): | ||
| pass | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import os, sys | ||
| import json | ||
|
|
||
| import requests | ||
| import pandas as pd | ||
|
|
||
| from django.http import HttpResponse | ||
|
|
||
| from loop_ivr.outliers.removal import remove_crop_outliers | ||
| from loop_ivr.helper_function import run_query | ||
| from loop_ivr.utils.marketinfo import get_query | ||
| from loop.models import * | ||
| from tastypie.models import ApiKey | ||
|
|
||
| from mi_data_structure import * | ||
| from crop_price_structure import * | ||
| from loop.utils.mi_pilot_var import mandi_list, agg_list, transport_detail_filepath | ||
| from dg.base_settings import PROJECT_PATH | ||
|
|
||
| def get_aggregator_mi_related_data(request): | ||
|
|
||
| agg_list_requested = is_authenticated(request) | ||
|
|
||
| if agg_list_requested : | ||
|
|
||
| # Create Objects | ||
| agg_data_obj = LoopUser.objects.filter(user__in=agg_list_requested).values('user', 'name_en', 'preferred_language_id') | ||
|
|
||
| # get Dict from Single Obj | ||
| agg_obj = agg_data_obj[0] | ||
| preferred_lang_suffix = '_en' if agg_obj['preferred_language_id'] == 2 else '' | ||
|
|
||
| # Mandi Name in preferred lang of aggregator | ||
| mandi_name_label = 'mandi_name' + preferred_lang_suffix | ||
| mandi_data_obj = Mandi.objects.filter(id__in=mandi_list).values('id', mandi_name_label) | ||
|
|
||
| #Gaddidar Name in preferred lang of aggregator | ||
| gaddidar_name_label = 'gaddidar_name' + preferred_lang_suffix | ||
| gaddidar_data_obj = Gaddidar.objects.filter(mandi__id__in=mandi_list) | ||
|
|
||
| # Read CSV file and filter for requested Aggreagator | ||
| filepath = os.path.abspath(os.path.join(PROJECT_PATH, '..', transport_detail_filepath)) | ||
| transport_dataframe = pd.read_csv(filepath) | ||
|
|
||
| transport_dataframe = transport_dataframe[transport_dataframe['Aggregator Id']==agg_list_requested[0]] | ||
|
|
||
| # Store Json Result | ||
| agg_res = [] | ||
|
|
||
| # Mandi Detail | ||
| mandi_res = [] | ||
| for mandiobj in mandi_data_obj: | ||
| mandiobjdetail = MandiDetail(mandi_id=mandiobj['id'], mandi_name=mandiobj[mandi_name_label], mandi_category='Chhoti Mandi',\ | ||
| mandi_distance='590') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are category and distance hardcoded?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For Initially, testing. |
||
| gaddidar_obj= gaddidar_data_obj.filter(mandi__id=mandiobj['id']).values('id', gaddidar_name_label, 'gaddidar_phone') | ||
| for gaddidarobj in gaddidar_obj: | ||
| gaddidarobj = GaddidarDetail(gaddidar_id=gaddidarobj['id'], gaddidar_name=gaddidarobj[gaddidar_name_label],\ | ||
| gaddidar_phone_no=gaddidarobj['gaddidar_phone']) | ||
| mandiobjdetail.gaddidar_list.append(gaddidarobj.__dict__) | ||
|
|
||
| transport_dataframe_obj = transport_dataframe[transport_dataframe['Mandi Id'] == mandiobj['id']] | ||
| for index, row in transport_dataframe_obj.iterrows(): | ||
| transportobj = TransportDetail(transport_id=row['Vehicle Id'], transport_name=row['Vehicle Name'], transport_cost=str(row['Cost']),\ | ||
| transport_capacity=row['Capacity']) | ||
|
|
||
| mandiobjdetail.distance = row['Mandi Distance'] | ||
| mandiobjdetail.category = row['Mandi Category'] | ||
| mandiobjdetail.transport_list.append(transportobj.__dict__) | ||
|
|
||
| mandi_res.append(mandiobjdetail.__dict__) | ||
|
|
||
| # Aggregator Details | ||
| for aggobj in agg_data_obj: | ||
| aggobj = AggregatorDetail(aggregator_id=aggobj['user'], aggregator_name=aggobj['name_en']) | ||
| aggobj.mandi_list = mandi_res | ||
| agg_res.append(aggobj.__dict__) | ||
|
|
||
| data = json.dumps(agg_res) | ||
| return HttpResponse(data) | ||
| else : | ||
| return HttpResponse(status=401) | ||
|
|
||
|
|
||
| def get_crop_prices(request): | ||
|
|
||
| agg_list_requested = is_authenticated(request) | ||
|
|
||
| if agg_list_requested : | ||
| # Handling Request Params | ||
| kwargs = read_params(request) | ||
|
|
||
| cropobj = Crop.objects.values_list('id', flat=True) | ||
| cropobj = map(int, cropobj) | ||
| # Prepare data | ||
| crop_list = tuple(cropobj) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if only 1 crop is requested, will this tuple method work? |
||
|
|
||
| query = get_query.query_for_rates(crop_list , mandi_list, date_range=kwargs['day_limit']) | ||
|
|
||
| result = run_query(query) | ||
| dataframe = remove_crop_outliers(ct_data = result) | ||
|
|
||
| # Grouping Raw Data to Crop & Mandi wise. | ||
| df = dataframe.groupby(['Crop', 'Market_Real']) | ||
|
|
||
| # Creation of JSON | ||
| res = [] | ||
| for i, r in df: | ||
| # Assigning min date | ||
| latest_price_date = pd.Timestamp('2015-01-01') | ||
| cropmandidata = CropMandiData(crop_id=i[0], mandi_id= i[1]) | ||
| for index, row in r.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'] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure that avg_price and std are always returned from the function?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You doubt that? :<, BTW yes. |
||
| delta = PriceMax - PriceMin | ||
| latest_price_date = max(latest_price_date, date) | ||
| priceobj = PriceDetails(date=str(date), std=round(STD, 2), min_price=round(PriceMin, 2), max_price=round(PriceMax, 2),\ | ||
| delta=round(delta, 2), avg_price=round(Av_Rate, 2)) | ||
| cropmandidata.price_details.append(priceobj.__dict__) | ||
| cropmandidata.latest_price_date = str(latest_price_date) | ||
| res.append(cropmandidata.__dict__) | ||
|
|
||
| data = json.dumps(res) | ||
| return HttpResponse(data) | ||
| else: | ||
| return HttpResponse(status=401) | ||
|
|
||
| def read_params(request): | ||
| kwargs = {} | ||
|
|
||
| kwargs['day_limit'] = int(request.GET.get('day_limit', 3)) | ||
| kwargs['start_date'] = request.GET.get('start_date', None) | ||
| kwargs['end_date'] = request.GET.get('end_date', None) | ||
| kwargs['crop_id'] = tuple(request.GET.getlist('crop_id', None)) | ||
| kwargs['mandi_id'] = tuple(request.GET.getlist('mandi_id', None)) | ||
| kwargs['range'] = request.GET.get('range', None) | ||
| kwargs['absolute'] = request.GET.get('absolute', None) | ||
|
|
||
| return kwargs | ||
|
|
||
|
|
||
| def is_authenticated(request): | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add function doc string to explain the working of function |
||
| try: | ||
| # Mobile App Authentication | ||
| request_data = request.META.get('HTTP_AUTHORIZATION', '') | ||
| username, apikey = request_data.split(':', 1) | ||
| # Get User Id | ||
| apikeyobj = ApiKey.objects.get(key=apikey) | ||
| aggregator_id = [apikeyobj.user_id] | ||
| agg_list_requested = list(set(aggregator_id) & set(agg_list)) | ||
| except Exception: | ||
| agg_list_requested = [] | ||
|
|
||
| return agg_list_requested | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| Mandi Id,Mandi Name,Mandi Category,Mandi Distance,Aggregator Id,Aggregator Name,Vehicle Id,Vehicle Name,Cost,Capacity | ||
| 134,Darhara,2,7,4846,सतेन्दर कुमार,2,Tempo,300,600 | ||
| 134,Darhara,2,7,4846,सतेन्दर कुमार,5,ace,300,1500 | ||
| 134,Darhara,2,7,4846,सतेन्दर कुमार,4,pick up,500,2500 | ||
| 134,Darhara,2,8,4844,बिशेशवर प्रसाद,2,Tempo,400,600 | ||
| 134,Darhara,2,8,4844,बिशेशवर प्रसाद,5,ace,400,1500 | ||
| 134,Darhara,2,8,4844,बिशेशवर प्रसाद,4,pick up,600,2500 | ||
| 134,Darhara,2,20,4992,धनराज सिंह,2,Tempo,600,600 | ||
| 134,Darhara,2,20,4992,धनराज सिंह,5,ace,600,1500 | ||
| 134,Darhara,2,20,4992,धनराज सिंह,4,pick up,800,2500 | ||
| 134,Darhara,2,10,5025,रवि रंजन,2,Tempo,400,600 | ||
| 134,Darhara,2,10,5025,रवि रंजन,5,ace,400,1500 | ||
| 134,Darhara,2,10,5025,रवि रंजन,4,pick up,600,2500 | ||
| 133,Buxar,2,31,4846,सतेन्दर कुमार,2,Tempo,600,600 | ||
| 133,Buxar,2,31,4846,सतेन्दर कुमार,5,ace,700,1500 | ||
| 133,Buxar,2,31,4846,सतेन्दर कुमार,4,pick up,1200,2500 | ||
| 133,Buxar,2,17,4844,बिशेशवर प्रसाद,2,Tempo,500,600 | ||
| 133,Buxar,2,17,4844,बिशेशवर प्रसाद,5,ace,500,1500 | ||
| 133,Buxar,2,17,4844,बिशेशवर प्रसाद,4,pick up,750,2500 | ||
| 133,Buxar,2,5,4992,धनराज सिंह,2,Tempo,300,600 | ||
| 133,Buxar,2,5,4992,धनराज सिंह,5,ace,300,1500 | ||
| 133,Buxar,2,5,4992,धनराज सिंह,4,pick up,500,2500 | ||
| 133,Buxar,2,17,5025,रवि रंजन,2,Tempo,500,600 | ||
| 133,Buxar,2,17,5025,रवि रंजन,5,ace,500,1500 | ||
| 133,Buxar,2,17,5025,रवि रंजन,4,pick up,800,2500 | ||
| 152,Bramhpur,2,15,4846,सतेन्दर कुमार,2,Tempo,500,600 | ||
| 152,Bramhpur,2,15,4846,सतेन्दर कुमार,5,ace,500,1500 | ||
| 152,Bramhpur,2,15,4846,सतेन्दर कुमार,4,pick up,700,2500 | ||
| 152,Bramhpur,2,17,4844,बिशेशवर प्रसाद,2,Tempo,500,600 | ||
| 152,Bramhpur,2,17,4844,बिशेशवर प्रसाद,5,ace,500,1500 | ||
| 152,Bramhpur,2,17,4844,बिशेशवर प्रसाद,4,pick up,750,2500 | ||
| 152,Bramhpur,2,30,4992,धनराज सिंह,2,Tempo,600,600 | ||
| 152,Bramhpur,2,30,4992,धनराज सिंह,5,ace,700,1500 | ||
| 152,Bramhpur,2,30,4992,धनराज सिंह,4,pick up,1200,2500 | ||
| 152,Bramhpur,2,20,5025,रवि रंजन,2,Tempo,600,600 | ||
| 152,Bramhpur,2,20,5025,रवि रंजन,5,ace,600,1500 | ||
| 152,Bramhpur,2,20,5025,रवि रंजन,4,pick up,1100,2500 | ||
| 16,Arra,3,65,4846,सतेन्दर कुमार,2,Tempo,900,600 | ||
| 16,Arra,3,65,4846,सतेन्दर कुमार,5,ace,900,1500 | ||
| 16,Arra,3,65,4846,सतेन्दर कुमार,4,pick up,1500,2500 | ||
| 16,Arra,3,65,4844,बिशेशवर प्रसाद,2,Tempo,800,600 | ||
| 16,Arra,3,65,4844,बिशेशवर प्रसाद,5,ace,900,1500 | ||
| 16,Arra,3,65,4844,बिशेशवर प्रसाद,4,pick up,1500,2500 | ||
| 16,Arra,3,80,4992,धनराज सिंह,2,Tempo,1500,600 | ||
| 16,Arra,3,80,4992,धनराज सिंह,5,ace,1500,1500 | ||
| 16,Arra,3,80,4992,धनराज सिंह,4,pick up,2500,2500 | ||
| 16,Arra,3,70,5025,रवि रंजन,2,Tempo,800,600 | ||
| 16,Arra,3,70,5025,रवि रंजन,5,ace,900,1500 | ||
| 16,Arra,3,70,5025,रवि रंजन,4,pick up,2000,2500 | ||
| 150,Nayabhojpur,2,15,4846,सतेन्दर कुमार,2,Tempo,500,600 | ||
| 150,Nayabhojpur,2,15,4846,सतेन्दर कुमार,5,ace,500,1500 | ||
| 150,Nayabhojpur,2,15,4846,सतेन्दर कुमार,4,pick up,700,2500 | ||
| 150,Nayabhojpur,2,4,4844,बिशेशवर प्रसाद,2,Tempo,350,600 | ||
| 150,Nayabhojpur,2,4,4844,बिशेशवर प्रसाद,5,ace,350,1500 | ||
| 150,Nayabhojpur,2,4,4844,बिशेशवर प्रसाद,4,pick up,500,2500 | ||
| 150,Nayabhojpur,2,12,4992,धनराज सिंह,2,Tempo,500,600 | ||
| 150,Nayabhojpur,2,12,4992,धनराज सिंह,5,ace,500,1500 | ||
| 150,Nayabhojpur,2,12,4992,धनराज सिंह,4,pick up,700,2500 | ||
| 150,Nayabhojpur,2,3,5025,रवि रंजन,2,Tempo,350,600 | ||
| 150,Nayabhojpur,2,3,5025,रवि रंजन,5,ace,350,1500 | ||
| 150,Nayabhojpur,2,3,5025,रवि रंजन,4,pick up,500,2500 | ||
| 14,Patna,3,116,4846,सतेन्दर कुमार,2,Tempo,NA,600 | ||
| 14,Patna,3,116,4846,सतेन्दर कुमार,5,ace,2000,1500 | ||
| 14,Patna,3,116,4846,सतेन्दर कुमार,4,pick up,3000,2500 | ||
| 14,Patna,3,130,4844,बिशेशवर प्रसाद,2,Tempo,NA,600 | ||
| 14,Patna,3,130,4844,बिशेशवर प्रसाद,5,ace,1500,1500 | ||
| 14,Patna,3,130,4844,बिशेशवर प्रसाद,4,pick up,3000,2500 | ||
| 14,Patna,3,150,4992,धनराज सिंह,2,Tempo,NA,600 | ||
| 14,Patna,3,150,4992,धनराज सिंह,5,ace,NA,1500 | ||
| 14,Patna,3,150,4992,धनराज सिंह,4,pick up,4000,2500 | ||
| 14,Patna,3,140,5025,रवि रंजन,2,Tempo,NA,600 | ||
| 14,Patna,3,140,5025,रवि रंजन,5,ace,1500,1500 | ||
| 14,Patna,3,140,5025,रवि रंजन,4,pick up,3500,2500 | ||
| 5,Chapra,3,65,4846,सतेन्दर कुमार,2,Tempo,1200,600 | ||
| 5,Chapra,3,65,4846,सतेन्दर कुमार,5,ace,1200,1500 | ||
| 5,Chapra,3,65,4846,सतेन्दर कुमार,4,pick up,1700,2500 | ||
| 5,Chapra,3,75,4844,बिशेशवर प्रसाद,2,Tempo,1200,600 | ||
| 5,Chapra,3,75,4844,बिशेशवर प्रसाद,5,ace,1200,1500 | ||
| 5,Chapra,3,75,4844,बिशेशवर प्रसाद,4,pick up,2000,2500 | ||
| 5,Chapra,3,90,4992,धनराज सिंह,2,Tempo,1300,600 | ||
| 5,Chapra,3,90,4992,धनराज सिंह,5,ace,1400,1500 | ||
| 5,Chapra,3,90,4992,धनराज सिंह,4,pick up,2500,2500 | ||
| 5,Chapra,3,80,5025,रवि रंजन,2,Tempo,1200,600 | ||
| 5,Chapra,3,80,5025,रवि रंजन,5,ace,1200,1500 | ||
| 5,Chapra,3,80,5025,रवि रंजन,4,pick up,2000,2500 | ||
| 151,Dumaraon,2,65,4846,सतेन्दर कुमार,2,Tempo,500,600 | ||
| 151,Dumaraon,2,65,4846,सतेन्दर कुमार,5,ace,500,1500 | ||
| 151,Dumaraon,2,65,4846,सतेन्दर कुमार,4,pick up,700,2500 | ||
| 151,Dumaraon,2,75,4844,बिशेशवर प्रसाद,2,Tempo,350,600 | ||
| 151,Dumaraon,2,75,4844,बिशेशवर प्रसाद,5,ace,350,1500 | ||
| 151,Dumaraon,2,75,4844,बिशेशवर प्रसाद,4,pick up,600,2500 | ||
| 151,Dumaraon,2,90,4992,धनराज सिंह,2,Tempo,500,600 | ||
| 151,Dumaraon,2,90,4992,धनराज सिंह,5,ace,500,1500 | ||
| 151,Dumaraon,2,90,4992,धनराज सिंह,4,pick up,700,2500 | ||
| 151,Dumaraon,2,80,5025,रवि रंजन,2,Tempo,400,600 | ||
| 151,Dumaraon,2,80,5025,रवि रंजन,5,ace,400,1500 | ||
| 151,Dumaraon,2,80,5025,रवि रंजन,4,pick up,700,2500 | ||
| 188,Dumri,1,21,4846,सतेन्दर कुमार,2,Tempo,700,600 | ||
| 188,Dumri,1,21,4846,सतेन्दर कुमार,5,ace,700,1500 | ||
| 188,Dumri,1,21,4846,सतेन्दर कुमार,4,pick up,1000,2500 | ||
| 188,Dumri,1,6,4844,बिशेशवर प्रसाद,2,Tempo,350,600 | ||
| 188,Dumri,1,6,4844,बिशेशवर प्रसाद,5,ace,400,1500 | ||
| 188,Dumri,1,6,4844,बिशेशवर प्रसाद,4,pick up,600,2500 | ||
| 188,Dumri,1,18,4992,धनराज सिंह,2,Tempo,500,600 | ||
| 188,Dumri,1,18,4992,धनराज सिंह,5,ace,500,1500 | ||
| 188,Dumri,1,18,4992,धनराज सिंह,4,pick up,700,2500 | ||
| 188,Dumri,1,10,5025,रवि रंजन,2,Tempo,400,600 | ||
| 188,Dumri,1,10,5025,रवि रंजन,5,ace,400,1500 | ||
| 188,Dumri,1,10,5025,रवि रंजन,4,pick up,700,2500 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| mandi_list = (134, 133, 152, 16, 150, 14, 5, 151, 188) | ||
| agg_list = (4846, 4844, 4992, 5025) | ||
|
|
||
| transport_detail_filepath = "loop/utils/Transport Cost & Capacity Data - Sheet1.csv" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why these methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK