2023-02-11 15:16:25 +00:00
|
|
|
import os
|
|
|
|
from pymongo import MongoClient
|
2023-02-11 22:05:30 +00:00
|
|
|
import time
|
2023-02-11 15:16:25 +00:00
|
|
|
import urllib.parse
|
|
|
|
import logger as log
|
|
|
|
|
2023-02-11 22:05:30 +00:00
|
|
|
db_host = os.getenv('OWL_DB_HOST', 'localhost')
|
|
|
|
db_port = os.getenv('OWL_DB_PORT', 27017)
|
|
|
|
db_user = urllib.parse.quote_plus(os.getenv('OWL_DB_USER', "owl"))
|
|
|
|
db_pass = urllib.parse.quote_plus(os.getenv('OWL_DB_PASS', "twittwoo"))
|
|
|
|
db_name = os.getenv('OWL_DB_NAME', "owlboard")
|
|
|
|
|
2023-05-31 22:09:09 +01:00
|
|
|
log.out(f"mongo.py: Database URI: {db_host}:{db_port}", "DBUG")
|
2023-02-11 22:05:30 +00:00
|
|
|
client = MongoClient(f"mongodb://{db_user}:{db_pass}@{db_host}:{db_port}")
|
|
|
|
db = client[db_name]
|
|
|
|
|
2023-05-31 22:09:09 +01:00
|
|
|
log.out("mongo.py: Mongo module loaded", "DBUG")
|
|
|
|
|
2023-02-11 22:05:30 +00:00
|
|
|
def metaCheckTime(target):
|
|
|
|
col = db["meta"]
|
2023-02-12 20:03:20 +00:00
|
|
|
res = col.find_one({"target": target, "type": "collection"})
|
2023-02-16 21:34:31 +00:00
|
|
|
incrementCounter("meta")
|
2023-02-13 20:45:05 +00:00
|
|
|
if type(res) is dict:
|
|
|
|
if 'updated' in res:
|
2023-02-12 21:36:41 +00:00
|
|
|
log.out(f'mongo.metaUpdateTime: {target} last updated at {res["updated"]}', "INFO")
|
|
|
|
return res["updated"]
|
2023-05-31 19:04:33 +01:00
|
|
|
log.out(f'mongo.metaUpdatetime: {target} does not exist', "EROR")
|
2023-02-13 20:45:05 +00:00
|
|
|
return 0
|
2023-02-11 22:05:30 +00:00
|
|
|
|
|
|
|
def metaUpdateTime(target):
|
|
|
|
col = db["meta"]
|
2023-05-31 19:04:33 +01:00
|
|
|
log.out(f'mongo.metaUpdateTime: Updating updated time for {target}', "DBUG")
|
2023-02-12 20:03:20 +00:00
|
|
|
res = col.update_one({"target": target, "type":"collection"}, {"$set":{"updated": int(time.time()),"target":target, "type":"collection"}}, upsert=True)
|
2023-02-16 21:34:31 +00:00
|
|
|
incrementCounter("meta")
|
2023-02-11 22:05:30 +00:00
|
|
|
|
|
|
|
def getLength(collection):
|
|
|
|
col = db[collection]
|
2023-02-16 21:34:31 +00:00
|
|
|
incrementCounter(collection)
|
2023-02-11 22:05:30 +00:00
|
|
|
return col.count_documents({})
|
|
|
|
|
2023-02-12 20:53:59 +00:00
|
|
|
def createSingleIndex(collection, field):
|
|
|
|
col = db[collection]
|
|
|
|
col.create_index(field)
|
2023-02-16 21:34:31 +00:00
|
|
|
incrementCounter(collection)
|
2023-02-12 20:53:59 +00:00
|
|
|
log.out(f'mongo.createSingleIndex: Created index of {field} in {collection}', "INFO")
|
|
|
|
return
|
|
|
|
|
2023-05-08 19:55:09 +01:00
|
|
|
def createTtlIndex(collection, field, time):
|
|
|
|
col = db[collection]
|
|
|
|
col.create_index(field, expireAfterSeconds = time)
|
|
|
|
log.out(f'mongo.createTtlIndex: Created TTL Index of {field} in {collection} to expire after {time} seconds', "INFO")
|
|
|
|
|
2023-02-11 22:05:30 +00:00
|
|
|
def putBulkCorpus(data):
|
2023-02-12 20:53:59 +00:00
|
|
|
collection = "corpus"
|
|
|
|
startCount = getLength(collection)
|
|
|
|
col = db[collection]
|
2023-02-16 21:34:31 +00:00
|
|
|
incrementCounter(collection)
|
2023-02-11 22:05:30 +00:00
|
|
|
if startCount > 0:
|
|
|
|
log.out(f'mongo.putBulkCorpus: Dropping {startCount} CORPUS documents', "INFO")
|
|
|
|
col.drop()
|
|
|
|
col.insert_many(data)
|
2023-02-12 20:53:59 +00:00
|
|
|
endCount = getLength(collection)
|
2023-02-11 22:05:30 +00:00
|
|
|
log.out(f'mongo.putBulkCorpus: {endCount} documents inserted', "INFO")
|
|
|
|
log.out(f'mongo.putBulkCorpus: {endCount - startCount} new documents', "INFO")
|
2023-02-12 20:53:59 +00:00
|
|
|
log.out('mongo.putBulkCorpus: Building collection indexes',"INFO")
|
|
|
|
createSingleIndex(collection, "NLC")
|
|
|
|
createSingleIndex(collection, "3ALPHA")
|
2023-02-11 22:05:30 +00:00
|
|
|
log.out('mongo.putBulkCorpus: Updating meta time',"INFO")
|
2023-02-12 20:53:59 +00:00
|
|
|
metaUpdateTime(collection)
|
2023-02-11 22:05:30 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def putBulkStations(data):
|
2023-02-12 20:53:59 +00:00
|
|
|
collection = "stations"
|
|
|
|
startCount = getLength(collection)
|
|
|
|
col = db[collection]
|
2023-02-16 21:34:31 +00:00
|
|
|
incrementCounter("stations")
|
2023-02-11 22:05:30 +00:00
|
|
|
if startCount > 0:
|
|
|
|
log.out(f'mongo.putBulkStations: Dropping {startCount} station documents', "INFO")
|
|
|
|
col.drop()
|
|
|
|
col.insert_many(data)
|
2023-02-12 20:53:59 +00:00
|
|
|
endCount = getLength(collection)
|
2023-02-11 22:05:30 +00:00
|
|
|
log.out(f'mongo.putBulkStations: {endCount} documents inserted', "INFO")
|
|
|
|
log.out(f'mongo.putBulkStations: {endCount - startCount} new documents', "INFO")
|
2023-02-12 20:53:59 +00:00
|
|
|
log.out('mongo.putBulkStations: Building collection indexes',"INFO")
|
|
|
|
createSingleIndex(collection, "3ALPHA")
|
|
|
|
createSingleIndex(collection, "STANOX")
|
|
|
|
createSingleIndex(collection, "TIPLOC")
|
2023-02-11 22:05:30 +00:00
|
|
|
log.out('mongo.putBulkStations: Updating meta time',"INFO")
|
2023-02-12 20:53:59 +00:00
|
|
|
metaUpdateTime(collection)
|
2023-02-12 21:36:41 +00:00
|
|
|
return
|
|
|
|
|
2023-05-08 19:55:09 +01:00
|
|
|
def putBulkPis(data):
|
|
|
|
collection = "pis"
|
|
|
|
startCount = getLength(collection)
|
|
|
|
col = db[collection]
|
|
|
|
incrementCounter(collection)
|
|
|
|
if startCount > 0:
|
|
|
|
log.out(f'mongo.putBulkPid: Dropping {startCount} pis documents', "INFO")
|
|
|
|
col.drop()
|
|
|
|
col.insert_many(data)
|
|
|
|
endCount = getLength(collection)
|
|
|
|
log.out(f'mongo.putBulkPis: {endCount} documents inserted', "INFO")
|
|
|
|
log.out(f'mongo.putBulkPis: {endCount-startCount} new documents', "INFO")
|
|
|
|
log.out('mongo.putBulkPis: Updating meta time', "INFO")
|
|
|
|
metaUpdateTime(collection)
|
|
|
|
return
|
|
|
|
|
2023-05-31 22:09:09 +01:00
|
|
|
def putMany(collection :str, data :list):
|
|
|
|
log.out(f"mongo.putMany: Inserting many documents to: {collection}")
|
|
|
|
col = db[collection]
|
|
|
|
incrementCounter(collection)
|
|
|
|
col.insert_many(data)
|
|
|
|
metaUpdateTime(collection)
|
|
|
|
|
2023-05-08 19:55:09 +01:00
|
|
|
|
2023-02-16 21:34:31 +00:00
|
|
|
def incrementCounter(target):
|
|
|
|
collection = "meta"
|
|
|
|
col = db[collection]
|
2023-02-16 22:17:35 +00:00
|
|
|
log.out(f'mongo.incrementCounter: Incrementing counter for {target}', "INFO")
|
|
|
|
col.update_one({"target": "counters","type": "count"}, {"$inc":{target: 1}})
|
2023-02-16 21:34:31 +00:00
|
|
|
return
|
|
|
|
|
2023-02-12 21:36:41 +00:00
|
|
|
def metaCounters():
|
|
|
|
collection = "meta"
|
|
|
|
col = db[collection]
|
|
|
|
res = col.find_one({"target": "counters","type": "count"})
|
2023-02-13 20:45:05 +00:00
|
|
|
if type(res) is dict:
|
|
|
|
if 'since' in res:
|
2023-02-12 21:36:41 +00:00
|
|
|
log.out('mongo.metaCounters: counters already exists, skipping', "INFO")
|
2023-02-16 22:23:54 +00:00
|
|
|
incrementCounter(collection)
|
2023-02-13 20:45:05 +00:00
|
|
|
return
|
|
|
|
log.out('mongo.metaCounters: counters does not exist, creating', "INFO")
|
|
|
|
col.update_one({"target": "counters","type": "count"}, {"$set":{"target": "counters","type": "count","since": int(time.time())}}, upsert=True)
|
2023-02-16 22:23:54 +00:00
|
|
|
incrementCounter(collection)
|
2023-03-13 19:46:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def putVersion(version):
|
2023-03-13 20:10:20 +00:00
|
|
|
collection = "meta"
|
2023-03-13 19:46:03 +00:00
|
|
|
col = db[collection]
|
2023-03-13 20:10:20 +00:00
|
|
|
res = col.update_one({"target": "versions"}, {"$set":{"target": "versions","dbmanager": version}}, upsert=True)
|
2023-05-30 23:03:16 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
def putTimetable(data):
|
|
|
|
collection = "timetable"
|
|
|
|
col = db[collection]
|
2023-05-31 19:04:33 +01:00
|
|
|
res = col.insert_many(data)
|
|
|
|
|
|
|
|
def dropCollection(collection):
|
2023-05-31 22:09:09 +01:00
|
|
|
log.out(f"mongo.dropCollection: Dropping collection '{collection}'")
|
2023-05-31 19:04:33 +01:00
|
|
|
col = db[collection]
|
|
|
|
res = col.drop()
|
|
|
|
|
|
|
|
def deleteTimetableData(query):
|
|
|
|
collection = "timetable"
|
|
|
|
col = db[collection]
|
2023-05-31 22:09:09 +01:00
|
|
|
res = col.delete_one(query)
|
|
|
|
|
|
|
|
def getMetaHash(target):
|
|
|
|
collection = "meta"
|
|
|
|
col = db[collection]
|
|
|
|
res = col.find_one({"target": target, "type": "collection"})
|
|
|
|
incrementCounter("meta")
|
|
|
|
if type(res) is dict:
|
|
|
|
if 'updated' in res:
|
|
|
|
try:
|
|
|
|
log.out(f'mongo.metaGetHash: {target} hash is {res["hash"]}', 'INFO')
|
|
|
|
return res["hash"]
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
def putMetaHash(target :str, hash :str):
|
|
|
|
collection = "meta"
|
|
|
|
col = db[collection]
|
|
|
|
filter = {
|
|
|
|
"target": target,
|
|
|
|
"type": "collection"
|
|
|
|
}
|
|
|
|
update = {
|
|
|
|
"target": target,
|
|
|
|
"type": "collection",
|
|
|
|
"hash": hash
|
|
|
|
}
|
2023-05-31 22:59:38 +01:00
|
|
|
res = col.update_one(filter, {"$set": update}, upsert=True)
|
|
|
|
|
|
|
|
def query(collection, query):
|
|
|
|
col = db[collection]
|
|
|
|
log.out(f"mongo.query: Running query: {query}")
|
|
|
|
return col.find_one(query)
|