Commit d2012193 authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

.get_bi_id() replaced with .bi_id defaults

--HG--
branch : feature/macdb
parent fd3dfd26
......@@ -77,7 +77,7 @@ class Alarms(Model):
domains = UserAccess.get_domains(user)
# Resolve domains against dict
domain_ids = [
x.get_bi_id()
x.bi_id
for x in AdministrativeDomainM.objects.filter(id__in=domains)
]
filter = query.get("filter", {})
......
......@@ -10,8 +10,12 @@
import struct
# Third-party modules
from csiphash import siphash24
import bson
# NOC modules
from noc.models import is_document
SIPHASH_SEED = b"\x00" * 16
BI_ID_FIELD = "bi_id"
def bi_hash(v):
......@@ -24,27 +28,26 @@ def bi_hash(v):
return int(struct.unpack("!Q", bh)[0] & 0x7fffffffffffffff)
def get_bi_id(self):
def new_bi_id():
"""
Returns BI id
:param self:
Generate new bi_id
:return:
"""
if not self.bi_id:
h = getattr(self, "_bi_id", None)
if not h:
h = bi_hash(self.id)
self._bi_id = h
return h
else:
return self.bi_id
return bi_hash(bson.ObjectId())
def bi_sync(cls):
"""
Denote class to add get_bi_id method
Denote class to add bi_id defaults
:param cls:
:return:
"""
setattr(cls, "get_bi_id", get_bi_id)
if is_document(cls):
f = cls._fields.get(BI_ID_FIELD)
assert f, "%s field must be defined" % BI_ID_FIELD
else:
f = [f for f in cls._meta.fields if f.name == BI_ID_FIELD]
assert f, "%s field must be defined" % BI_ID_FIELD
f = f[0]
f.default = new_bi_id
return cls
......@@ -150,7 +150,7 @@ class ReferenceField(BaseField):
if value is None:
return str(self.default_value)
else:
return str(value.get_bi_id())
return str(value.bi_id)
class IPv4Field(BaseField):
......
......@@ -114,7 +114,7 @@ class ObjectMap(Document):
"report_attempts": mo.object_profile.report_ping_attempts,
"status": None,
"name": mo.name,
"bi_id": mo.get_bi_id()
"bi_id": mo.bi_id
}
if mo.time_pattern:
rr["time_expr"] = TimePattern.get_code(mo.time_pattern.id)
......
......@@ -19,8 +19,8 @@ class CHAdministrativeDomainDataSource(BaseDataSource):
def extract(self):
for ad in AdministrativeDomain.objects.all().order_by("id"):
yield (
ad.get_bi_id(),
ad.bi_id,
ad.id,
ad.name,
ad.parent.get_bi_id() if ad.parent else ""
ad.parent.bi_id if ad.parent else ""
)
......@@ -20,7 +20,7 @@ class CHAlarmClassDataSource(BaseDataSource):
def extract(self):
for ac in AlarmClass.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
ac.get_bi_id(),
ac.bi_id,
ac.id,
ac.name
)
......@@ -21,7 +21,7 @@ class CHContainerDataSource(BaseDataSource):
def extract(self):
for obj in Object.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
obj.get_bi_id(),
obj.bi_id,
obj.id,
obj.name,
bi_hash(obj.container) if obj.container else ""
......
......@@ -20,7 +20,7 @@ class CHInterfaceProfileDataSource(BaseDataSource):
def extract(self):
for p in InterfaceProfile.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
p.get_bi_id(),
p.bi_id,
p.id,
p.name,
1 if p.is_uni else 0
......
......@@ -19,7 +19,7 @@ class CHManagedObjectDataSource(BaseDataSource):
def extract(self):
for mo in ManagedObject.objects.all().order_by("id"):
yield (
mo.get_bi_id(),
mo.bi_id,
mo.id,
mo.name,
mo.address
......
......@@ -20,8 +20,8 @@ class CHAdministrativeDomainDataSource(BaseDataSource):
def extract(self):
for ns in NetworkSegment.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
ns.get_bi_id(),
ns.bi_id,
ns.id,
ns.name,
ns.parent.get_bi_id() if ns.parent else ""
ns.parent.bi_id if ns.parent else ""
)
......@@ -20,7 +20,7 @@ class CHPlatformDataSource(BaseDataSource):
def extract(self):
for p in Platform.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
p.get_bi_id(),
p.bi_id,
p.id,
p.name,
p.vendor.name,
......
......@@ -20,7 +20,7 @@ class CHPoolDataSource(BaseDataSource):
def extract(self):
for pool in Pool.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
pool.get_bi_id(),
pool.bi_id,
pool.id,
pool.name
)
......@@ -20,7 +20,7 @@ class CHProfileClassDataSource(BaseDataSource):
def extract(self):
for a in Profile.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
a.get_bi_id(),
a.bi_id,
a.id,
a.name
)
......@@ -20,7 +20,7 @@ class CHVendorDataSource(BaseDataSource):
def extract(self):
for a in Vendor.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
a.get_bi_id(),
a.bi_id,
a.id,
a.name
)
......@@ -20,7 +20,7 @@ class CHVersionDataSource(BaseDataSource):
def extract(self):
for a in Firmware.objects.all(read_preference=ReadPreference.SECONDARY_PREFERRED).order_by("id"):
yield (
a.get_bi_id(),
a.bi_id,
a.id,
a.version,
a.profile.name,
......
......@@ -29,8 +29,8 @@ class MACCheck(DiscoveryCheck):
now = time.localtime()
date = time.strftime("%Y-%m-%d", now)
ts = time.strftime("%Y-%m-%d %H:%M:%S", now)
mo_id = str(self.object.get_bi_id())
seg_id = str(self.object.segment.get_bi_id())
mo_id = str(self.object.bi_id)
seg_id = str(self.object.segment.bi_id)
unknown_interfaces = set()
disabled_by_profile = set()
total_macs = 0
......@@ -60,7 +60,7 @@ class MACCheck(DiscoveryCheck):
mo_id, # managed_object
str(int(MAC(v["mac"]))), # mac
ifname, # interface
str(ifprofile.get_bi_id()), # interface_profile
str(ifprofile.bi_id), # interface_profile
seg_id, # segment
str(v.get("vlan_id", 0)), # vlan
"1" if iface.profile.is_uni else "0" # is_uni
......
......@@ -352,7 +352,7 @@ class MetricsCheck(DiscoveryCheck):
alarms = []
data = defaultdict(dict)
n_metrics = 0
mo_id = self.object.get_bi_id()
mo_id = self.object.bi_id
ts_cache = {} # timestamp -> (date, ts)
#
for m in result:
......@@ -362,11 +362,11 @@ class MetricsCheck(DiscoveryCheck):
# Counter type
if path:
key = "%x|%s" % (
cfg.metric_type.get_bi_id(),
cfg.metric_type.bi_id,
"|".join(str(p) for p in path)
)
else:
key = "%x" % cfg.metric_type.get_bi_id()
key = "%x" % cfg.metric_type.bi_id
# Restore old value and save new
r = counters.get(key)
counters[key] = (m.ts, m.value)
......@@ -529,11 +529,11 @@ class MetricsCheck(DiscoveryCheck):
# Build window state key
if m.path:
key = "%x|%s" % (
cfg.metric_type.get_bi_id(),
cfg.metric_type.bi_id,
"|".join(str(p) for p in m.path)
)
else:
key = "%x" % cfg.metric_type.get_bi_id()
key = "%x" % cfg.metric_type.bi_id
#
states = self.job.context["metric_windows"]
value = m.abs_value
......
......@@ -121,7 +121,7 @@ class MACApplication(ExtApplication):
mo_q = ManagedObject.get_search_Q(query)
if not mo_q:
mo_q = d_Q(name__contains=query)
mos = [mo.get_bi_id() for mo in ManagedObject.objects.filter(mo_q)[:2]]
mos = [mo.bi_id for mo in ManagedObject.objects.filter(mo_q)[:2]]
if mos:
out = self.api_macdb({"managed_object__in": mos}, limit=limit, offset=start)
# out = self.api_get_maclog(request, mac)
......
......@@ -256,7 +256,7 @@ class MapApplication(ExtApplication):
for mo in o:
for i in o[mo]:
where += [(mo, i)]
where = " or ".join(["(managed_object=%s and path[4]=\'%s\')" % (q(mo.get_bi_id()), q(i.name))
where = " or ".join(["(managed_object=%s and path[4]=\'%s\')" % (q(mo.bi_id), q(i.name))
for mo, i in where])
query = ["select managed_object, path[4], anyLast(load_in), anyLast(load_out) ",
"from interface",
......@@ -268,7 +268,7 @@ class MapApplication(ExtApplication):
for row in ch.execute(" ".join(query)):
mo_in[row[0]] += float(row[2])
mo_out[row[0]] += float(row[3])
mos = [str(ManagedObject.get_by_id(mo["id"]).get_bi_id()) for mo in r["objects"]]
mos = [str(ManagedObject.get_by_id(mo["id"]).bi_id) for mo in r["objects"]]
if len(mos) == 2:
mo1, mo2 = mos
r["utilisation"] = [
......@@ -415,7 +415,7 @@ class MapApplication(ExtApplication):
m["tags"]["interface"]
)
] = m["id"]
object_bi = str(ManagedObject.objects.get(name=m["tags"]["object"]).get_bi_id())
object_bi = str(ManagedObject.objects.get(name=m["tags"]["object"]).bi_id)
tag_id[object_bi, m["tags"]["interface"]] = m["id"]
mlst += [(m["metric"], object_bi, m["tags"]["interface"])]
except KeyError:
......
......@@ -285,7 +285,7 @@ class ReportTraffic(SimpleReport):
"q_group": ["path"]})
report_map["ping"].update({
"q_select": ["managed_object", "round(quantile(0.98)(rtt) / 1000, 2)", "avg(attempts)"]})
moss = {str(mo.get_bi_id()): mo for mo in mos}
moss = {str(mo.bi_id): mo for mo in mos}
if reporttype in ["load_interfaces", "errors"]:
iface_dict = {(
......@@ -315,7 +315,7 @@ class ReportTraffic(SimpleReport):
if not l:
continue
mo = moss[l[0]]
d_url["biid"] = mo.get_bi_id()
d_url["biid"] = mo.bi_id
d_url["oname"] = mo.name.replace("#", "%23")
if zero and allow_archive and not sum(data):
# For InfluxDB query
......
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