-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_deliver.py
172 lines (139 loc) · 6.38 KB
/
create_deliver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
import numpy as np
import pandas as pd
import psycopg2
from config import config
def createDeliver():
conn = None
try:
print('Connecting to the PostgreSQL database...')
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
df = pd.read_csv('data.csv', usecols=[
'loadsmart_id',
'lane',
'quote_date',
'book_date',
'source_date',
'pickup_date',
'delivery_date',
'book_price',
'source_price',
'pnl',
'mileage',
'carrier_name',
'shipper_name',
'carrier_on_time_to_pickup',
'carrier_on_time_to_delivery',
'carrier_on_time_overall',
'pickup_appointment_time',
'delivery_appointment_time',
'has_mobile_app_tracking',
'has_macropoint_tracking',
'has_edi_tracking',
'contracted_load',
'load_booked_autonomously',
'load_sourced_autonomously',
'load_was_cancelled'
])
deliveries = df.as_matrix()
sqls = np.array([])
deliveries = deliveries[1:2]
for deliveriesArray in deliveries:
loadsmartId = str(deliveriesArray[0])
# ----------------------------------------------------------
addressesPair = deliveriesArray[1].split(' -> ')
# ----------------------------------------------------------
cityFrom = addressesPair[0].split(',')[0]
sqlFromId = ''.join([
'SELECT ad.id from address ad where ad.city = ', "'", cityFrom, "'", ';'])
cur.execute(sqlFromId)
fromId = cur.fetchone()[0]
# ----------------------------------------------------------
cityTo = addressesPair[1].split(',')[0]
sqlToId = ''.join([
'SELECT ad.id from address ad where ad.city = ', "'", cityTo, "'", ';'])
cur.execute(sqlToId)
toId = cur.fetchone()[0]
# ----------------------------------------------------------
quoteDate = deliveriesArray[2]
bookDate = deliveriesArray[3]
sourceDate = deliveriesArray[4]
pickupDate = deliveriesArray[5]
deliveryDate = deliveriesArray[6]
bookPrice = str(deliveriesArray[7])
sourcePrice = str(deliveriesArray[8])
pnl = str(deliveriesArray[9])
mileage = str(deliveriesArray[10])
# ----------------------------------------------------------
carrierSourceId = deliveriesArray[11].split(' ')[1]
print(carrierSourceId)
sqlCarrierId = ''.join([
'SELECT ca.id from carrier ca where ca.source_id = ', "", carrierSourceId, "", ';'])
cur.execute(sqlCarrierId)
carrierId = cur.fetchone()[0]
print(carrierId)
# ----------------------------------------------------------
shipperSourceId = deliveriesArray[12].split(' ')[1]
sqlShipperId = ''.join([
'SELECT sp.id from shipper sp where sp.source_id = ', "", shipperSourceId, "", ';'])
cur.execute(sqlShipperId)
shipperId = cur.fetchone()[0]
# ----------------------------------------------------------
carrierOnTimeToPickup = str(deliveriesArray[13])
carrierOnTimeToDelivery = str(deliveriesArray[14])
carrierOnTimeOverall = str(deliveriesArray[15])
pickupAppointmentTime = deliveriesArray[16]
deliveryAppointmentTime = deliveriesArray[17]
hasMobileAppTracking = str(deliveriesArray[18])
hasMacropointTracking = str(deliveriesArray[19])
hasEdiTracking = str(deliveriesArray[20])
contractedLoad = str(deliveriesArray[21])
loadBookedAutonomously = str(deliveriesArray[22])
loadSourcedAutonomously = str(deliveriesArray[23])
loadWasCancelled = str(deliveriesArray[24])
sql = ''.join([
'INSERT INTO public.deliver (loadsmart_id,from_id,to_id,quote_date,book_date,source_date,pickup_date,delivery_date,book_price,source_price,pnl,mileage,carrier_id,shipper_id,carrier_on_time_to_pickup,carrier_on_time_to_delivery,carrier_on_time_overall,pickup_appointment_time,delivery_appointment_time,has_mobile_app_tracking,has_macropoint_tracking,has_edi_tracking,contracted_load,load_booked_autonomously,load_sourced_autonomously,load_was_cancelled) VALUES (',
"'", loadsmartId, "'", ",",
"'", fromId, "'", ",",
"'", toId, "'", ",",
"'", quoteDate, "'", ",",
"'", bookDate, "'", ",",
"'", sourceDate, "'", ",",
"'", pickupDate, "'", ",",
"'", deliveryDate, "'", ",",
"'", bookPrice, "'", ",",
"'", sourcePrice, "'", ",",
"'", pnl, "'", ",",
"'", mileage, "'", ",",
"'", carrierId, "'", ",",
"'", shipperId, "'", ",",
"'", carrierOnTimeToPickup, "'::boolean", ",",
"'", carrierOnTimeToDelivery, "'::boolean", ",",
"'", carrierOnTimeOverall, "'::boolean", ",",
"'", pickupAppointmentTime, "'::date", ",",
"'", deliveryAppointmentTime, "'::date", ",",
"'", hasMobileAppTracking, "'::boolean", ",",
"'", hasMacropointTracking, "'::boolean", ",",
"'", hasEdiTracking, "'::boolean", ",",
"'", contractedLoad, "'::boolean", ",",
"'", loadBookedAutonomously, "'::boolean", ",",
"'", loadSourcedAutonomously, "'::boolean", ",",
"'", loadWasCancelled, "'::boolean"
');'])
print(sql)
sqls = np.append(sqls, [sql])
sqls = list(dict.fromkeys(sqls))
for sql in sqls:
cur.execute(sql)
conn.commit()
print("Record inserted successfully in table.")
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print('Database failed.', error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
print('Delivers created.')