class Device(Document): private_key = StringField(required=True) public_cert = StringField(required=True) token_counter = IntField(default=1) token_generated = BooleanField(default=False) _token_value = StringField(default=None) creation_time = DateTimeField() last_checkin = DateTimeField() device_data = StringField(default='\{\}') # JSON blob of device data auth_requested = BooleanField(default=False) def save(self, *args, **kwargs): if not self.creation_time: self.creation_date = datetime.datetime.now() return super(Device, self).save(*args, **kwargs) @staticmethod def get_device(device) -> Device: return Device.objects.get(id=device) @property def token(self) -> str: if not self.token_generated: t = hmac.new(byte(self.private_key), digestmod=hashlib.sha3_256) t.update(str(self.token_counter)) self._token_value = t.hexdigest() self.token_generated = True return self._token_value def increment_token_counter(self) -> None: self.token_counter += 1 self.token_generated = False self.save() def set_auth_requested() -> None: self.auth_requested = True self.save() def unset_auth_requested() -> None: self.auth_requested = False self.save() def decrypt(encrypted_blob: string) -> string: # no crypto for now :( return encrypted_blob def is_valid_token(encrypted_token: string) -> bool: token = self.decrypt(encrypted_token) valid = token == self.token if valid: self.increment_token_counter() self.check_in() self.unset_auth_requested() return valid def check_in(self) -> None: self.last_checkin = datetime.datetime.now() self.save()
class Pool(Document): meta = { "collection": "noc.pools", "strict": False, "auto_create_index": False } name = StringField(unique=True, min_length=1, max_length=16, regex="^[0-9a-zA-Z]{1,16}$") description = StringField() discovery_reschedule_limit = IntField(default=50) # Object id in BI bi_id = LongField(unique=True) _id_cache = cachetools.TTLCache(1000, ttl=60) _bi_id_cache = cachetools.TTLCache(1000, ttl=60) _name_cache = cachetools.TTLCache(1000, ttl=60) reschedule_lock = threading.Lock() reschedule_ts = {} # pool id -> timestamp def __unicode__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id): return Pool.objects.filter(id=id).first() @classmethod @cachetools.cachedmethod(operator.attrgetter("_bi_id_cache"), lock=lambda _: id_lock) def get_by_bi_id(cls, id): return Pool.objects.filter(bi_id=id).first() @classmethod @cachetools.cachedmethod(operator.attrgetter("_name_cache"), lock=lambda _: id_lock) def get_by_name(cls, name): return Pool.objects.filter(name=name).first() def get_delta(self): """ Get delta for next discovery, Limit runs to discovery_reschedule_limit tasks """ t = time.time() dt = 1.0 / float(self.discovery_reschedule_limit) with self.reschedule_lock: lt = self.reschedule_ts.get(self.id) if lt and lt > t: delta = lt - t else: delta = 0 self.reschedule_ts[self.id] = t + dt return delta
class Itinerarie(Document): meta = { 'collection': 'flights.itinerarie', 'strict': False, 'auto_create_index': False, 'indexes': [ '_duration', 'default_price', ('source', 'destination', 'departure_time') ] } source = StringField(required=True) destination = StringField(required=True) departure_time = DateTimeField(required=True) arrival_time = DateTimeField(required=True) _duration = IntField() default_price = FloatField() flights = ListField(EmbeddedDocumentField(Flight)) prices = ListField(EmbeddedDocumentField(Price)) @queryset_manager def objects(cls, queryset): return queryset.order_by('default_price', '_duration') def __str__(self): return 'From {} to {} {} by {}'.format(self.source, self.destination, self.duration, self.default_price) def clean(self): self._duration = (self.arrival_time - self.departure_time).total_seconds() price_for_adult = next( filter(lambda x: x.type == PT_ADULT, self.prices)) self.default_price = price_for_adult.amount if price_for_adult else 0 @property def duration(self): return humanize_date(self._duration) @property def json_data(self): return { 'source': self.source, 'destination': self.destination, 'departure_time': self.departure_time, 'arrival_time': self.arrival_time, 'duration': self.duration, 'flights': [_.to_json() for _ in self.flights], 'prices': [_.to_json() for _ in self.prices], 'price': self.default_price } def to_json(self): return self.json_data
class SubscriberProfile(Document): meta = { "collection": "noc.subscriberprofiles", "strict": False, "auto_create_index": False } name = StringField(unique=True) description = StringField() style = ForeignKeyField(Style, required=False) workflow = PlainReferenceField(Workflow) # FontAwesome glyph glyph = StringField() # Glyph order in summary display_order = IntField(default=100) # Show in total summary show_in_summary = BooleanField(default=True) # Labels labels = ListField(StringField()) effective_labels = ListField(StringField()) # Alarm weight weight = IntField(default=0) # Integration with external NRI and TT systems # Reference to remote system object has been imported from remote_system = PlainReferenceField(RemoteSystem) # Object id in remote system remote_id = StringField() # Object id in BI bi_id = LongField(unique=True) _id_cache = cachetools.TTLCache(maxsize=100, ttl=60) def __str__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id): return SubscriberProfile.objects.filter(id=id).first() @classmethod def can_set_label(cls, label): return Label.get_effective_setting(label, setting="enable_subscriberprofile")
class Submission(Document): meta = {"collection": "submissions"} ID = ObjectIdField(primary_key=True) author = StringField() verdict = StringField() creationTime = StringField() relativeTime = StringField() points = IntField() content = StringField()
class VisualizationModel(mongoengine.Document): meta = {'collection': 'visualization'} Id = StringField() name = StringField() parentType = StringField() visualizationType = StringField() query = StringField() parameters = ListField(StringField()) width = IntField()
class AIndexMembers(Document): """ A股指数成分股 """ # 更新时间 update_date = DateTimeField(default=datetime.utcnow()) # 指数代码 index_code = StringField(required=True, null=True) # 成份股代码 security_code = StringField(required=True, null=True) # 纳入日期 in_date = IntField(required=True, null=True) # 剔除日期 out_date = IntField(required=True, null=True) # 最新标志 current_sign = IntField(required=True, null=True) meta = {'indexes': ['index_code', 'security_code', 'in_date']}
class EscalationItem(EmbeddedDocument): # Delay part delay = IntField() # Match part administrative_domain = ForeignKeyField(AdministrativeDomain) selector = ForeignKeyField(ManagedObjectSelector) time_pattern = ForeignKeyField(TimePattern) min_severity = IntField(default=0) # Action part notification_group = ForeignKeyField(NotificationGroup) template = ForeignKeyField(Template) clear_template = ForeignKeyField(Template) create_tt = BooleanField(default=False) promote_group_tt = BooleanField(default=True) close_tt = BooleanField(default=False) wait_tt = BooleanField(default=False) # Stop or continue to next rule stop_processing = BooleanField(default=False)
class ThresholdProfile(Document): meta = { "collection": "thresholdprofiles", "strict": False, "auto_create_index": False } name = StringField(unique=True) description = StringField() # Handler to filter and modify umbrella alarms umbrella_filter_handler = StringField() # Window function settings # Window depth window_type = StringField(max_length=1, choices=[("m", "Measurements"), ("t", "Time")]) # Window size. Depends on window type # * m - amount of measurements # * t - time in seconds window = IntField(default=1) # Window function # Accepts window as a list of [(timestamp, value)] # and window_config # and returns float value window_function = StringField(choices=wf_choices, default="last") # Window function configuration window_config = StringField() # thresholds config thresholds = ListField(EmbeddedDocumentField(ThresholdConfig)) _id_cache = cachetools.TTLCache(maxsize=100, ttl=60) def __str__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id): return ThresholdProfile.objects.filter(id=id).first() def get_window_function(self): """ Returns window funciton or None if invalid name given :returns: Callable or None """ return get_window_function(self.window_function) def find_threshold(self, name): """ Find Threshold Config by name :param name: Threshold name :return: ThresholdConfig or None """ for cfg in self.thresholds: if cfg.name == name: return cfg return None
class AlarmClassConfig(Document): """ Alarm class """ meta = { "collection": "noc.alarmclassconfigs", "strict": False, "auto_create_index": False } alarm_class = PlainReferenceField(AlarmClass, unique=True) notification_delay = IntField(required=False) control_time0 = IntField(required=False) control_time1 = IntField(required=False) control_timeN = IntField(required=False) def __unicode__(self): return self.alarm_class.name
class CreativeRpt(BaseReport): shop_id = IntField(verbose_name="店铺ID") campaign_id = IntField(verbose_name="计划ID") adgroup_id = IntField(verbose_name="推广组ID") creative_id = IntField(verbose_name="创意ID") meta = { 'collection': 'creative_report', 'indexes': ['campaign_id', 'adgroup_id', 'creative_id'], "shard_key": ('shop_id', ) } # REPORT_CFG = (('SUMMARY', 'SUMMARY'), ('SEARCH,NOSEARCH', '1,2,4,5')) REPORT_CFG = (('SUMMARY', 'SUMMARY'), ) IDENTITY = "creative_id" RESERVED_DAYS = 30 INIT_DAYS = 15
class AuthLDAPServer(EmbeddedDocument): name = StringField() is_active = BooleanField() address = StringField() port = IntField() use_tls = BooleanField() def __unicode__(self): return self.name or self.address
class PerfScoreConf(EmbeddedDocument): """积分档位配置""" desc = StringField(verbose_name='积分档位') count = IntField(verbose_name='名额个数') consult_royalty_equation = DictField(verbose_name='公式系数', default={ 'x0': 0.7349, 'x1': -0.1078, 'x2': 0.0058 }) consult_pay_limit = IntField(verbose_name='顾问提成上限') def get_consult_royalty(self, order_pay): x = order_pay / 10000.0 return round( self.consult_royalty_equation['x2'] * x**2 + self.consult_royalty_equation['x1'] * x + self.consult_royalty_equation['x0'], 4)
class LetterGuessed(EmbeddedDocument): letter = StringField(min_length=1, max_length=1, required=True) attempts_left = IntField(min_value=0, max_value=MAX_ATTEMPTS, required=True) message = StringField(max_length=128, required=True) def __str__(self): return (f'{self.letter}->{self.attempts_left}->{self.message}')
class Employee(Document): name = StringField(required=True) admission = DateField(required=True) initial_day = IntField(required=True) resignation = DateField() routine = StringField()
class Machine(ModelBase, Document): name = StringField(default='') roles = ListField(StringField()) netaddr = StringField(default='') ipaddr = ListField(StringField()) active = BooleanField() # STARTED,STOPPED,RUNNING,FROZEN,CONFIGURED,DELETED state = StringField(choices=("STARTED", "STOPPED", "RUNNING", "FROZEN", "CONFIGURED", "DELETED"), default='CONFIGURED', required=True) mem = IntField() # $in MB cpucore = IntField() description = StringField(default='') otherid = StringField(default='') type = StringField(default='') # VM,LXC # epoch of last time the info was checked from reality lastcheck = IntField(default=j.data.time.getTimeEpoch())
class Audit(ModelBase, Document): user = StringField(default='') result = StringField(default='') call = StringField(default='') status_code = IntField(default=0) args = StringField(default='') kwargs = StringField(default='') timestamp = IntField(default=j.data.time.getTimeEpoch()) meta = extend( default_meta, { 'indexes': [{ 'fields': ['epoch'], 'expireAfterSeconds': 3600 * 24 * 5 }], 'allow_inheritance': True, "db_alias": DB })
class Log(ModelBase, Document): aid = IntField(default=0) pid = IntField(default=0) jid = StringField(default='') masterjid = IntField(default=0) appname = StringField(default="") level = IntField(default=1, required=True) message = StringField(default='') type = StringField(choices=("BUG", "PERF", "OPS", "UNKNOWN"), default="UNKNOWN", required=True) state = StringField(choices=("NEW", "ALERT", "CLOSED"), default="NEW", required=True) # StringField() <--- available starting version 0.9 category = StringField(default="") tags = StringField(default="") epoch = IntField(default=j.data.time.getTimeEpoch())
class Crash(Document): job_id = ObjectIdField() crash_signal = IntField() exploitability = StringField() date = DateTimeField() crash_hash = StringField() verified = BooleanField() crash_path = StringField() additional = StringField()
class Unit(Document): ID = ObjectIdField() title = StringField(unique=True, required=True) about = ListField(StringField()) description = StringField() image_url = URLField() date_created = DateTimeField(default=datetime.utcnow) position = IntField(default=0, required=True) slug = StringField(unique=True, required=True)
class UserModel(Document): meta = {"collection": "user"} email = StringField(required=True) first_name = StringField(max_length=50) last_name = StringField(max_length=50) date_of_birth = DateTimeField() consent_orgs = ListField(IntField()) consent_purposes = ListField(StringField()) consent_hpos = ListField(StringField())
class Test(ModelBase, Document): name = StringField(default='') testrun = StringField(default='') path = StringField(default='') state = StringField(choices=("OK", "ERROR", "DISABLED"), default='OK', required=True) priority = IntField() # lower is highest priority organization = StringField(default='') author = StringField(default='') version = IntField() categories = ListField(StringField()) starttime = IntField(default=j.data.time.getTimeEpoch()) endtime = IntField() enable = BooleanField() result = DictField() output = DictField(default={}) eco = DictField(default={}) license = StringField(default='') source = DictField(default={})
class Area(Document): meta = { "strict": False, "auto_create_index": False, "collection": "noc.gis.areas" } name = StringField() is_active = BooleanField(default=True) min_zoom = IntField(default=0) max_zoom = IntField(default=18) # (EPSG:4326) coordinates SW = GeoPointField() NE = GeoPointField() description = StringField(required=False) def __str__(self): return self.name
class CollectorProtocol(EmbeddedDocument): address = StringField() port = IntField() protocol = StringField() is_active = BooleanField(default=True) is_selectable = BooleanField(default=True) def __unicode__(self): return "%s:%s %s" % (self.address, self.port, self.protocol)
class PathwaySumary(EmbeddedDocument): term = StringField(required=True) name = StringField(required=True) count = IntField(required=True) properties = DictField(default={}) def __str__(self): return "term='%s',name='%s',count='%i'" % (self.term, self.name, self.count)
class BaseDocument(Document): meta = {'allow_inheritance': True, 'abstract': True} sort_order = IntField() reg_user = StringField(required=True) reg_dttm = StringField(required=True) upd_user = StringField() upd_dttm = StringField()
class ObjectInformation(Document): meta = {'collection': 'object_information'} id_ = IntField(required=True,unique=True) name = StringField() name_vi = StringField() description = StringField() sub_description = StringField() url_video = StringField() url_icon = StringField()
class MouldModel(mongoengine.Document): meta = {'collection': 'mould'} Id = StringField() name = StringField() company = ReferenceField(CompanyModel, reverse_delete_rule=mongoengine.DENY) part = ReferenceField(PartModel, reverse_delete_rule=mongoengine.DENY) cavity = IntField() runnerWeight = DecimalField()
class User(Document): firstName = StringField(required=True) lastName = StringField(required=True) username = StringField(required=True, unique=True) email = EmailField(unique=True) password = StringField(required=True) score = IntField(default=0) completed_challenges = ListField(ReferenceField('CTFChallenge'), reverse_delete_rule=PULL) role = StringField(default='Member')
class Profile(Document): username = StringField(null=True) user_id = IntField(unique=True) email = StringField() password = StringField() full_name = StringField(required=False, max_length=255) mobile_number = StringField() otp = StringField() otp_sent_at = DateTimeField() mobile_number_verified = BooleanField() mobile_number_verified_at = DateTimeField() email_activation_token = StringField() email_activation_token_sent_at = DateTimeField() email_verified = BooleanField() email_verified_at = DateTimeField() mac_address = StringField() last_login_at = DateTimeField(null=True) last_login_ip = StringField() last_login_user_agent = StringField() last_login_channel = StringField() created_at = DateTimeField() updated_at = DateTimeField() otp_source = StringField() icmyc_user_id = IntField() sign_up_with = StringField(default='mobile_number') settings = DictField( default={ "email_notifications_preferred": True, "sms_notifications_preferred": True, "push_notifications_preferred": True }) sign_up_ip_address = StringField(null=True) sign_up_user_agent = StringField(null=True) has_at_least_one_social_account = BooleanField(default=False) social_accounts = ListField() channels = ListField() avatar = StringField(null=True) city_id = IntField() ward_id = IntField() meta = {'collection': 'profiles'} def __str__(self): return self.full_name