-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseProxy.py
91 lines (64 loc) · 3.01 KB
/
DataBaseProxy.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
import datetime
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
import logging
logging.basicConfig(filename=datetime.datetime.now().strftime("%Y-%m-%d") + ".log",
level=logging.DEBUG)
class DataBaseProxy (object):
def __init__ (self):
self.db = client['UMAP']
def log_message (self, record, scope, status):
return '[{}] -> {} {}: {} {}'\
.format(datetime.datetime.now(),\
record["provider"],\
record["city"],\
scope,\
status)
def insert (self, collection, record):
collection = self.db[collection]
try:
collection.insert_one(record)
logging.debug(self.log_message(record, "insert", "success"))
except:
logging.debug(self.log_message(record, "insert", "error"))
def query (self, collection, query):
return self.db[collection].find(query)
def find_last (self, collection, query):
return self.db[collection].find \
(query).sort([("_id", -1)]).limit(1)
dbp = DataBaseProxy()
import datetime
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
#cursor = dbp.query("bookings", {"city":"vancouver"})
#df = pd.concat([pd.DataFrame(doc["bookings"]).T for doc in cursor])
#df["car_id"] = df.index.values
#df = df.set_index("start_time")
start = datetime.datetime(2017, 10, 31)
cursor = dbp.query("bookings", {"city":"madrid", "timestamp":{"$gt": start}})
df = pd.concat([pd.DataFrame(doc["bookings"]).T for doc in cursor])
df["car_id"] = df.index.values
df["hour"] = df["start_time"].apply(lambda d: d.hour)
df = df.set_index("start_time")
df["fuel_consumption"] = df.end_fuel - df.start_fuel
plt.figure()
df.loc[df.fuel_consumption > 0].groupby("hour").fuel_consumption.count().plot(marker="o")
plt.figure()
df.loc[df.fuel_consumption <= 0].groupby("hour").fuel_consumption.count().plot(marker="o")
plt.figure()
df.loc[df.fuel_consumption > 0].groupby("hour").fuel_consumption.sum().plot(marker="o")
df.loc[df.fuel_consumption <= 0].groupby("hour").fuel_consumption.sum().apply(np.abs).plot(marker="o")
plt.figure()
df.loc[df.fuel_consumption > 0].groupby("hour").fuel_consumption.apply(np.mean).plot(marker="o")
df.loc[df.fuel_consumption <= 0].groupby("hour").fuel_consumption.apply(np.mean).apply(np.abs).plot(marker="o")
plt.figure()
df.loc[df.fuel_consumption > 0].groupby("hour").fuel_consumption.apply(np.sum).hist(bins=50, cumulative=True)
plt.figure()
df.loc[df.fuel_consumption <= 0].groupby("hour").fuel_consumption.apply(np.sum).hist(bins=50, cumulative=True)
plt.figure()
df.loc[df.fuel_consumption > 0].groupby("hour").fuel_consumption.apply(np.mean).hist(bins=50, cumulative=True)
plt.figure()
df.loc[df.fuel_consumption <= 0].groupby("hour").fuel_consumption.apply(np.mean).hist(bins=50, cumulative=True)