mirror of
https://github.com/ThomasGsp/HyperProxmox.git
synced 2025-02-14 12:12:16 +00:00
MongoIndex
This commit is contained in:
parent
22479eeded
commit
199a72b4e4
2 changed files with 73 additions and 16 deletions
|
@ -17,6 +17,7 @@ import threading
|
|||
import time
|
||||
import base64
|
||||
import hashlib
|
||||
import pymongo
|
||||
|
||||
def RunAnalyse(generalconf, logger):
|
||||
play = Analyse(generalconf, logger)
|
||||
|
@ -56,6 +57,9 @@ class Core:
|
|||
if self.mongo.client and self.redis_msg.connect() and self.redis_cache.connect():
|
||||
self.mongo.db = self.mongo.client.db
|
||||
|
||||
""" Create indexes"""
|
||||
self.setupindexes()
|
||||
|
||||
""" Others """
|
||||
# value
|
||||
self.concurrencydeploy = generalconf["deploy"]["concurrencydeploy"]
|
||||
|
@ -528,6 +532,30 @@ class Core:
|
|||
|
||||
return data
|
||||
|
||||
|
||||
def setupindexes(self):
|
||||
self.mongo.set_indexes("disks",
|
||||
[('date', pymongo.ASCENDING), ("cluster", pymongo.ASCENDING),
|
||||
("node", pymongo.ASCENDING), ("vmid", pymongo.ASCENDING)])
|
||||
self.mongo.set_indexes("storages",
|
||||
[('date', pymongo.ASCENDING), ("cluster", pymongo.ASCENDING), ("node", pymongo.ASCENDING)])
|
||||
self.mongo.set_indexes("instances",
|
||||
[('date', pymongo.ASCENDING), ("cluster", pymongo.ASCENDING),
|
||||
("node", pymongo.ASCENDING), ("vmid", pymongo.ASCENDING)])
|
||||
self.mongo.set_indexes("nodes",
|
||||
[('date', pymongo.ASCENDING), ("cluster", pymongo.ASCENDING), ("node", pymongo.ASCENDING)])
|
||||
self.mongo.set_indexes("clusters",
|
||||
[('date', pymongo.ASCENDING), ("cluster", pymongo.ASCENDING)])
|
||||
self.mongo.set_indexes("cluster_conf", [('name', pymongo.ASCENDING)])
|
||||
|
||||
indexes_result = {
|
||||
"value": "{0}".format("All indexes created"),
|
||||
"result": "OK",
|
||||
"type": "HYPERPROXMOX"
|
||||
}
|
||||
|
||||
return indexes_result
|
||||
|
||||
"""
|
||||
#######################
|
||||
# STORAGES MANAGEMENT #
|
||||
|
|
|
@ -75,6 +75,8 @@ class MongoDB:
|
|||
collection = self.collection_instances
|
||||
elif col == "nodes":
|
||||
collection = self.collection_nodes
|
||||
elif col == "clusters_conf":
|
||||
collection = self.collection_clusters_conf
|
||||
elif col == "disks":
|
||||
collection = self.collection_disks
|
||||
elif col == "storages":
|
||||
|
@ -145,14 +147,14 @@ class MongoDB:
|
|||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(
|
||||
dumps(self.db[self.collection_clusters].find({'date': int(date)})))
|
||||
dumps(self.db[self.collection_clusters].find({'date': int(date)}).hint(self.collection_clusters)))
|
||||
}
|
||||
else:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(
|
||||
dumps(self.db[self.collection_clusters].find(
|
||||
{'$and': [{'date': int(date), 'cluster': cluster}]})))
|
||||
{'$and': [{'date': int(date), 'cluster': cluster}]}).hint(self.collection_clusters)))
|
||||
}
|
||||
|
||||
except BaseException as serr:
|
||||
|
@ -299,7 +301,7 @@ class MongoDB:
|
|||
"result": "OK",
|
||||
"value": json.loads(
|
||||
dumps(self.db[self.collection_nodes].find(
|
||||
{'date': int(date)})))
|
||||
{'date': int(date)}).hint(self.collection_nodes)))
|
||||
}
|
||||
|
||||
elif not node:
|
||||
|
@ -307,14 +309,16 @@ class MongoDB:
|
|||
"result": "OK",
|
||||
"value": json.loads(
|
||||
dumps(self.db[self.collection_nodes].find(
|
||||
{'$and': [{'date': int(date), 'cluster': cluster}]})))
|
||||
{'$and': [{'date': int(date), 'cluster': cluster}]})
|
||||
.hint(self.collection_nodes)))
|
||||
}
|
||||
else:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(
|
||||
dumps(self.db[self.collection_nodes].find(
|
||||
{'$and': [{'date': int(date), 'cluster': cluster, 'node': node}]})))
|
||||
{'$and': [{'date': int(date), 'cluster': cluster, 'node': node}]})
|
||||
.hint(self.collection_nodes)))
|
||||
}
|
||||
|
||||
except BaseException as serr:
|
||||
|
@ -346,28 +350,33 @@ class MongoDB:
|
|||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_instances].find({"date": int(date)})))
|
||||
self.db[self.collection_instances].find({"date": int(date)})
|
||||
.hint(self.collection_instances)))
|
||||
}
|
||||
elif not node:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_instances].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster}]})
|
||||
.hint(self.collection_instances)))
|
||||
}
|
||||
elif not vmid:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_instances].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node}]})
|
||||
.hint(self.collection_instances)))
|
||||
|
||||
}
|
||||
else:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_instances].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node, "vmid": int(vmid)}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node, "vmid": int(vmid)}]})
|
||||
.hint(self.collection_instances)))
|
||||
}
|
||||
|
||||
except BaseException as serr:
|
||||
|
@ -400,21 +409,24 @@ class MongoDB:
|
|||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_storages].find({"date": int(date)})))
|
||||
self.db[self.collection_storages].find({"date": int(date)})
|
||||
.hint(self.collection_storages)))
|
||||
}
|
||||
elif not node:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_storages].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster}]})
|
||||
.hint(self.collection_storages)))
|
||||
}
|
||||
else:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_storages].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node}]})
|
||||
.hint(self.collection_storages)))
|
||||
}
|
||||
except BaseException as serr:
|
||||
result = {
|
||||
|
@ -447,28 +459,32 @@ class MongoDB:
|
|||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_disks].find({"date": int(date)})))
|
||||
self.db[self.collection_disks].find({"date": int(date)})
|
||||
.hint(self.collection_disks)))
|
||||
}
|
||||
elif not node:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_disks].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster}]})
|
||||
.hint(self.collection_disks)))
|
||||
}
|
||||
elif not vmid:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_disks].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node}]})
|
||||
.hint(self.collection_disks)))
|
||||
}
|
||||
else:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(
|
||||
self.db[self.collection_disks].find(
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node, "vmid": str(vmid)}]})))
|
||||
{'$and': [{"date": int(date), "cluster": cluster, "node": node, "vmid": str(vmid)}]})
|
||||
.hint(self.collection_disks)))
|
||||
}
|
||||
|
||||
except BaseException as serr:
|
||||
|
@ -477,4 +493,17 @@ class MongoDB:
|
|||
"type": "MongoDB - Request on get_disks",
|
||||
"value": "MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr)
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
def set_indexes(self, col, list):
|
||||
try:
|
||||
# Add here a test if exist, pass
|
||||
result = self.db[self.__mappingcol(col)].create_index(list, background=True, name=col)
|
||||
except BaseException as serr:
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"type": "MongoDB - Set Indexes",
|
||||
"value": "MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr)
|
||||
}
|
||||
return result
|
Loading…
Reference in a new issue