Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions loop/crop_price_structure.py
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']

46 changes: 46 additions & 0 deletions loop/mi_data_structure.py
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):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why these methods?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK

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


153 changes: 153 additions & 0 deletions loop/mi_views.py
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')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are category and distance hardcoded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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']
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
3 changes: 3 additions & 0 deletions loop/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from loop.views import *
from loop.dashboard_views import *
from loop.mi_views import *
from loop.utils.send_log.loop_data_log import send_updated_log
from loop.utils.send_log.send_sms import send_sms, sms_receipt_from_txtlcl, deprecated_send_sms
from loop.utils.send_log.loop_admin_log import send_updated_admin_log
Expand Down Expand Up @@ -109,4 +110,6 @@
url(r'^get_partners_list/', get_partners_list),
url(r'^admin/logout/?$', 'django.contrib.auth.views.logout', {'next_page': '/loop/admin/'}),
url(r'^admin/', include(loop_admin.urls)),
url(r'^get_agg_mi_data/', get_aggregator_mi_related_data),
url(r'^get_crop_prices/', get_crop_prices)
)
109 changes: 109 additions & 0 deletions loop/utils/Transport Cost & Capacity Data - Sheet1.csv
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
4 changes: 4 additions & 0 deletions loop/utils/mi_pilot_var.py
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"
2 changes: 1 addition & 1 deletion loop_ivr/utils/marketinfo/get_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime, timedelta

def query_for_rates(crop_list, mandi_list, date_range=3):
# today_date = datetime(2018, 03, 21)
# today_date = datetime(2018, 05, 22)
today_date = datetime.now()

query = query_for_transactions.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,date_range)))
Expand Down