-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMongo_class.py
More file actions
79 lines (70 loc) · 2.61 KB
/
Mongo_class.py
File metadata and controls
79 lines (70 loc) · 2.61 KB
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
import pymongo
import datetime
import ssl
class Mongo_pizza():
def __init__(self, user="Peter", pwd="1234", table="Pizza_db"):
client = pymongo.MongoClient(
"mongodb+srv://" + user + ":" + pwd +
"@cluster0.x2yjr.gcp.mongodb.net/" + table +
"?retryWrites=true&w=majority",
ssl_cert_reqs=ssl.CERT_NONE)
self.db = client.Pizza_db
def classify_pizza(self, ingredients):
"""
Classify Pizza
ingredients: list of ingredients
"""
for document in self.db.Pizza.find():
for ingredient in ingredients:
if ingredient in document["Key_ingredients"]:
return document["name"]
break
return document["name"]
def get_ingredient(self, ingredient_name):
"""
Get ingredient element by name
ingredient_name: string
"""
aux = list(self.db.Ingredient.find({"name":ingredient_name}))
if len(aux) == 0:
print("ERROR: Ingredient not found")
print(ingredient_name)
self.get_error("Error Mongo db, Ingredient not found "
+ ingredient_name)
return None
return aux[0]
def get_pizza(self, pizza_name):
"""
Get ingredient element by name
ingredient_name: string
"""
aux = list(self.db.Pizza.find({"name":pizza_name}))
if len(aux) == 0:
print("ERROR: Pizza not found")
print(pizza_name)
self.get_error("Error Mongo db, Pizza not found "
+ pizza_name)
return None
return aux[0]
def insert_log(self, pizza_name, ingredients, co2, user, kitchen, action):
"""
Historical record from the query CO2
"""
time = datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
if co2 > -1:
log = {"Data": time, "Pizza": pizza_name, "co2": co2,
"user": user, "Kitchen": kitchen,
"ingredients": ingredients, "action": action}
else:
log = {"Data": time, "Pizza": pizza_name,
"user": user, "Kitchen": kitchen,
"ingredients": ingredients, "action": action}
self.db.Stats.insert_one(log)
def get_error(self, error):
"""
Historiacal record from the erros
"""
print("Recording Error")
time = datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
log = {"Data": time, "Error": error}
self.db.Error_log.insert_one(log)