Commit 7455a9de authored by Michael R's avatar Michael R
Browse files

Remove ObjectPath model

parent 759a6ec4
# ---------------------------------------------------------------------
# ObjectPath
# ---------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
from threading import Lock
import operator
# Third-party modules
from mongoengine.document import Document
from mongoengine.fields import ListField, ObjectIdField, IntField
import cachetools
id_lock = Lock()
class ObjectPath(Document):
meta = {"collection": "noc.cache.objectpaths", "strict": False, "auto_create_index": False}
# Object id
object = IntField(primary_key=True)
adm_path = ListField(IntField())
segment_path = ListField(ObjectIdField())
container_path = ListField(ObjectIdField())
_object_cache = cachetools.TTLCache(10000, ttl=300)
@classmethod
def refresh(cls, obj):
ObjectPath._get_collection().update(
{"_id": obj.id},
{
"$set": {
"adm_path": obj.administrative_domain.get_path(),
"segment_path": obj.segment.get_path(),
"container_path": obj.container.get_path() if obj.container else [],
}
},
upsert=True,
)
@classmethod
@cachetools.cachedmethod(operator.attrgetter("_object_cache"), lock=lambda _: id_lock)
def _get_path(cls, object_id):
return ObjectPath.objects.filter(object=object_id).first()
@classmethod
def get_path(cls, object):
if hasattr(object, "id"):
object = object.id
return cls._get_path(object)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment