-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnjoy_Manager.py
144 lines (118 loc) · 6.01 KB
/
Enjoy_Manager.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
import sys
def print_exception ():
exc_type, exc_obj, exc_tb = sys.exc_info()
print exc_type
print exc_obj
print exc_tb.tb_lineno
import datetime
import time
start_cols = [
"start_time",
"latitude",
"longitude",
"fuel"
]
from DataBaseProxy import dbp
import pandas as pd
def pre_process (df):
df["longitude"] = df["lat"]
df["latitude"] = df["lon"]
return df.drop(["lat","lon"], axis=1)
class Enjoy_Manager ():
def __init__ (self, city):
self.name = "enjoy " + city
self.city = city
doc = dbp.find_last("snapshots",
{"provider":"enjoy","city":self.city}).next()
self.last_time = doc["timestamp"]
self.last = pd.DataFrame(doc["snapshot"]).set_index("car_plate")
self.last = pre_process(self.last)
self.fleet = self.last.index
self.active_parkings = pd.DataFrame(columns = start_cols, index=self.last.index)
self.active_parkings["start_time"] = datetime.datetime.now()
self.active_parkings["latitude"] = self.last.latitude
self.active_parkings["longitude"] = self.last.longitude
self.active_parkings["fuel"] = self.last.fuel
self.active_bookings = pd.DataFrame(columns = start_cols, index=[])
def check (self):
try:
doc = dbp.find_last("snapshots",
{"provider":"enjoy","city":self.city}).next()
self.current_time = doc["timestamp"]
self.current = pd.DataFrame(doc["snapshot"]).set_index("car_plate")
self.current = pre_process(self.current)
except:
print "Exception in " + self.name
print_exception()
if not self.current.equals(self.last):
# Detect new cars
# print "Fleet"
# print self.fleet.shape
self.new_cars = self.current.index.difference(self.fleet)
# print self.new_cars.shape
# Indexes creation
# print "Status"
self.still_parked = self.fleet.intersection(self.current.index)
self.just_booked = self.active_parkings.index.difference(self.current.index).intersection(self.fleet)
self.just_parked = self.current.index.intersection(self.fleet).difference(self.active_parkings.index)
# print self.still_parked.shape
# print self.just_booked.shape
# print self.just_parked.shape
# - New parkings
self.active_parkings = pd.concat([self.active_parkings, pd.DataFrame(index=self.just_parked)])
self.active_parkings.loc[self.just_parked, "start_time"] = \
self.current_time
self.active_parkings.loc[self.just_parked,"latitude"] = \
self.current.loc[self.just_parked, "latitude"].values
self.active_parkings.loc[self.just_parked,"longitude"] = \
self.current.loc[self.just_parked, "longitude"].values
self.active_parkings.loc[self.just_parked,"fuel"] = \
self.current.loc[self.just_parked, "fuel"].values
# - New bookings
self.active_bookings = pd.concat([self.active_bookings, pd.DataFrame(index=self.just_booked)])
self.active_bookings.loc[self.just_booked, "start_time"] = \
self.current_time
self.active_bookings.loc[self.just_booked,"latitude"] = \
self.active_parkings.loc[self.just_booked, "latitude"].values
self.active_bookings.loc[self.just_booked,"longitude"] = \
self.active_parkings.loc[self.just_booked, "longitude"].values
self.active_bookings.loc[self.just_booked,"start_fuel"] = \
self.active_parkings.loc[self.just_booked, "fuel"].values
# - Parkings terminated
def record_parking(parkings):
parkings["end_time"] = datetime.datetime.now()
recorded = parkings.T.to_dict()
if len(recorded.keys()):
record = {
"provider":"enjoy",
"city":self.city,
"timestamp":datetime.datetime.now(),
"parkings":recorded
}
dbp.insert("parkings", record)
record_parking(self.active_parkings.loc[self.just_booked])
self.active_parkings.drop(self.just_booked, inplace=True)
self.last = self.current
self.last_time = self.current_time
# - Bookings terminated
def record_booking(bookings):
bookings["end_time"] = datetime.datetime.now()
bookings["end_fuel"] = self.current.loc[bookings.index, "fuel"].values
bookings["end_latitude"] = self.current.loc[bookings.index, "latitude"].values
bookings["end_longitude"] = self.current.loc[bookings.index, "longitude"].values
recorded = bookings.T.to_dict()
if len(recorded.keys()):
record = {
"provider":"enjoy",
"city":self.city,
"timestamp":datetime.datetime.now(),
"bookings":recorded
}
dbp.insert("bookings", record)
record_booking(self.active_bookings.loc[self.just_parked.intersection(self.active_bookings.index)])
self.active_bookings.drop(self.just_parked.intersection(self.active_bookings.index), inplace=True)
# - Update fleet
self.fleet = self.fleet.union(self.current.index)
# print self.fleet.shape
#manager = Enjoy_Manager(sys.argv[1])
#manager.start()