-
Notifications
You must be signed in to change notification settings - Fork 0
/
pml_storage.py
175 lines (156 loc) · 4.03 KB
/
pml_storage.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
173
174
175
import os
from dotenv import load_dotenv
import pymongo
class DB(object):
APP_ROOT = os.path.join(os.path.dirname(__file__), '') # refers to application_top
dotenv_path = os.path.join(APP_ROOT, '.env')
load_dotenv(dotenv_path)
@staticmethod
def init():
client = pymongo.MongoClient(os.getenv('MONGO_URI'))
DB.DATABASE = client[os.getenv('MONGO_DATABASE')]
@staticmethod
def insert(collection, data):
DB.DATABASE[collection].insert(data)
@staticmethod
def find_one(collection, query):
return DB.DATABASE[collection].find_one(query)
@staticmethod
def find(collection, query):
return DB.DATABASE[collection].find() if query is None else DB.DATABASE[collection].find(query)
class PML(object):
def __init__(self, clave, nombre, proceso, sistema, area, fecha, hora, datetime, pml, pml_ene, pml_per, pml_cng, inegi_estado, inegi_municipio, zona_carga):
self.clave = clave
self.nombre = nombre
self.proceso = proceso
self.sistema = sistema
self.area = area
self.fecha = fecha
self.hora = hora
self.datetime = datetime
self.pml = pml
self.pml_ene = pml_ene
self.pml_per = pml_per
self.pml_cng = pml_cng
self.inegi_estado = inegi_estado
self.inegi_municipio = inegi_municipio
self.zona_carga = zona_carga
@staticmethod
def all():
return list(DB.find('pml_data'))
@staticmethod
def where(query):
return DB.find('pml_data', query=query)
@staticmethod
def findByClave(clave, proccess):
return PML.where({'clave': clave, 'proceso': proccess})
@staticmethod
def getOneByClavel(clave, proccess):
query = DB.find_one('pml_data', query={'clave': clave, 'proceso': proccess})
return PML(
clave = query['clave'],
nombre = query['nombre'],
proceso = query['proceso'],
sistema = query['sistema'],
area = query['area'],
fecha = query['fecha'],
hora = query['hora'],
datetime = query['datetime'],
pml = query['pml'],
pml_ene = query['pml_ene'],
pml_per = query['pml_per'],
pml_cng = query['pml_cng']
) if query else False
@staticmethod
def validateNodesAndDate(date_str, nodes):
request = [
{
'$match': {
'$and': [
{'fecha':date_str},
{'clave': {
'$in': nodes
}
}
]
}
},
{ '$group': { '_id': '$clave' } }
]
pml_collection = DB.DATABASE['pml_data']
nodes_cvl = list(pml_collection.aggregate(request))
return list(map(lambda node: node['_id'], nodes_cvl))
@staticmethod
def groupClaveAndCount():
request = [{
"$group": {
"_id": "$clave",
"total": {
"$sum": 1
}
}
}]
pml_collection = DB.DATABASE['pml_data']
elements = list(pml_collection.aggregate(request))
return elements
@staticmethod
def allCount():
pml_collection = DB.DATABASE['pml_data']
return pml_collection.count()
@staticmethod
def nodosM(inegi_estado, inegi_municipio, proceso, inicio, fin):
request = [
{
"$match":
{
"inegi_estado": inegi_estado,
"inegi_municipio": inegi_municipio,
"proceso": proceso,
"datetime": {
"$gte": inicio,
"$lt": fin
}
}
},
{
"$group":
{
"_id": {
"fecha": "$datetime"
},
"pml": { "$avg": "$pml" },
"pml_ene": { "$avg": "$pml_ene" },
"pml_per": { "$avg": "$pml_per" },
"pml_cng": { "$avg": "$pml_cng" },
"datetime": { "$first": "$datetime" },
"fecha": { "$first": "$fecha" },
"hora": { "$first": "$hora" }
}
},
{
"$sort": { "datetime": 1 }
}
]
pml_collection = DB.DATABASE['pml_data']
nodosM = list(pml_collection.aggregate(request))
return nodosM
def save(self):
DB.insert('pml_data', data = self.json())
def json(self):
return {
'clave': self.clave,
'nombre': self.nombre,
'proceso': self.proceso,
'sistema': self.sistema,
'area': self.area,
'fecha': self.fecha,
'hora': self.hora,
'datetime': self.datetime,
'pml': self.pml,
'pml_ene': self.pml_ene,
'pml_per': self.pml_per,
'pml_cng': self.pml_cng,
'inegi_estado': self.inegi_estado,
'inegi_municipio': self.inegi_municipio,
'zona_carga': self.zona_carga
}