class Human(orm.Document): _db = "test" _collection = "humans" human_id = field.AutoIncrement(collection="human") name = field.Char(required=True, min=2, max=25) age = field.Integer(required=True, min=0, max=3000) height = field.Float(min=1, max=100000) weight = field.Float(min=1, max=30000) jobs = orm.List(type=Job) genitalia = field.Char() location = Location() car = field.ModelChoice(type=Car) color = field.Choice(choices=[{ 'value': 'red', 'display': 'Red' }, { 'value': 'blue', 'display': 'Blue' }, { 'value': 'green', 'display': 'Green' }]) state = field.CollectionChoice(db='test', collection='states', sort=[('fullname', 1)]) email = field.Email()
class RTTP(Interface): exchange = messages.Exchanges.energy measurement_key = "energy.utility" run_delta = timedelta(seconds=30) url = field.Char() un = field.Char() pw = field.Char() def data(self, data=None): url = "{}".format(self.url) auth = (self.un, self.pw) if self.un else None res = requests.get(url, auth=auth, timeout=5, verify=False) return res.text def parse_data(self, data): price = float(data.split(">")[1].split("<")[0]) now = datetime.utcnow() points = [dict( measurement="{}.{}".format(self.measurement_key, "price"), time=now, tags=dict( macid=self.url, interface=str(self._id), ), fields=dict( value=price ), )] return points
class Car(orm.Document): _db = "test" _collection = "cars" owner = field.DocumentId(type=Human) make = field.Char() model = field.Char() year = field.Date()
class Bet(orm.Document): _collection = "bets" amount = field.float(min=0.01, max=1000) title = field.Char(required=True, min=4, max=18) description = field.Char() bet_user = orm.modelChoice(type=User) betee_user = orm.modelChoice(type=User)
class Admin(orm.Document): _db = "lablog" _collection = "client_admins" _indexes = [ orm.Index('email', key=('email', 1), unique=True), ] name = field.Char() email = field.Char() password = field.Char() last_login = field.Date() clients = orm.List(type=ClientRef) social_accounts = orm.List(type=SocialAccount) facebook_pages = orm.List(type=FacebookPage) in_office = field.Boolean(default=False) @staticmethod def passwords_match(pwd, cpwd): return pwd == cpwd def save(self): if self.password: if not password.identify(self.password): self.password = password.encrypt_password(self.password) return super(Admin, self).save() def verify_pwd(self, pwd): return password.check_password(pwd, self.password) def social_account(self, account_type=None): for sa in self.social_accounts: if sa.type == account_type: return sa sa = SocialAccount() sa.type = account_type return sa def get_punchcard(self, influx): try: res = influx.query( "SELECT value from \"lablog\".\"realtime\".\"presence\" where user_id='{}' AND time > now() - 2d" .format(self._id)) r = [p for p in res.get_points()] r.reverse() return r except: return [] def is_authenticated(self): if self._id: return True return False def is_active(self): return True def is_anonymous(self): return False def get_id(self): self.logger.info(unicode(self._id)) return unicode(self._id)
class Scion(Car): any_owner = field.DynamicDocument() color = field.Choice(choices=["Red", "Blue", "Green"]) make = field.Char(default="Scion") model = field.Char(default="xA") year = field.Date(default=datetime.datetime(2007, 1, 1)) silly_date = field.TimeStamp()
class Token(orm.Document): _db = 'lablog' _collection = 'tokens' _indexes = [ orm.Index('access_token', key=('access_token', 1), unique=True), orm.Index('refresh_token', key=('refresh_token', 1), unique=True), orm.Index('client', key=('client', 1)), orm.Index('user', key=('user', 1)), orm.Index('user_agent', key=('user_agent', 1)), ] access_token = field.Char() refresh_token = field.Char() client = field.DocumentId(type=Client) scopes = orm.List(type=unicode) expires = field.Date() user = field.DocumentId(type=Admin) user_agent = field.Char() _type = field.Char() @property def token_type(self): return self._type def delete(self): self.remove()
class Car(orm.Document): _db = "test" _collection = "cars" owner = field.DynamicDocument() make = field.Char() model = field.Char() year = field.Date() silly_date = field.TimeStamp()
class MetabolomicsExperiment(Experiment): """ Metabolomics experiments """ software = field.Char(required=True) version = field.Char(required=True) parameters = field.Char() filenames = orm.List(type=str)
class Human(orm.Document): _db = "test" _collection = "humans" name = field.Char(required=True, min=2, max=25) age = field.Integer(min=0, max=3000) height = field.Float(min=1, max=100000) weight = field.Float(min=1, max=30000) jobs = orm.List(type=Job) genitalia = field.Char()
class Car(orm.Document): _db = "test" _collection = "cars" owner = field.DocumentId(type=Human) make = field.Char() model = field.Char() year = field.Date() features = orm.List(type=unicode) properties = orm.List(type=Property)
class BadHuman(Human): unique = field.Integer() phone = field.Phone() email = field.Email(dbkey="em") car = field.ModelChoice(type=Car) active = field.Boolean() state = field.Char(validate=StateValidator) country = field.Char(validate=orm.FieldValidator) location = Loca() avatar = field.File(database=MongoClient().avatars)
class Experiment(orm.Document): """ Base class for various experiments """ _db = "os_mongo" _collection = "experiments" experiment_id = field.AutoIncrement(collection="experiment") organization = field.Char(required=True) title = field.Char(required=True) description = field.Char(required=False) date = field.Date(required=True)
class Client(orm.Document): _db = "flaskaws" _collection = "clients" _indexes = [ orm.Index('name', key=('name', 1), unique=True), ] name = field.Char() description = field.Char() facebook_page = FacebookPage()
class PhilipsHue(Interface): exchange = messages.Exchanges.node measurement_key = 'actuator.light' bridge_id = field.Char() access_token = field.Char() light_id = field.Integer() def data(self, data=None): pass def parse_data(self, data): pass
class Client(orm.Document): _db = "entropealabs" _collection = "clients" _indexes = [ orm.Index('name', key=('name', 1), unique=True), ] name = field.Char() description = field.Char() social = SocialAccounts(type=SocialAccount)
class Grant(orm.Document): _db = 'lablog' _collection = 'grants' client = field.DocumentId(type=Client) code = field.Char() user = field.DocumentId(type=Admin) scopes = orm.List(type=unicode) expires = field.Date() redirect_url = field.Char() def delete(self): self.remove()
class HomeEnergyMonitor(Interface): exchange = messages.Exchanges.energy measurement_key = "energy.smartmeter" run_delta = timedelta(seconds=30) url = field.Char() un = field.Char() pw = field.Char() def data(self, data=None): url = "{}/both_tables.html".format(self.url) auth = (self.un, self.pw) if self.un else None res = requests.get(url, auth=auth, timeout=5) return res.text def parse_data(self, data): ls = [] for i in data.split("\r\n"): ls.append(i.strip()) data = "".join(ls) data = data.split("</table>") logging.debug(data) table1 = ET.XML("{}</table>".format(data[0])) table2 = ET.XML("{}</table>".format(data[1])) table1_rows = list(table1) table2_rows = list(table2) d = {} d['power'] = 0 d['wattage'] = 0 d['received'] = 0 d['delivered'] = 0 for row in table1_rows[2:]: d['wattage'] += float(row[1].text) for row in table2_rows[2:]: d['power'] += float(row[1].text) d['received'] += float(row[3].text) d['delivered'] += float(row[2].text) now = datetime.utcnow() points = [ dict( measurement="{}.{}".format(self.measurement_key, k), time=now, tags=dict( macid=self.url, interface=str(self._id), ), fields=dict(value=v), ) for k, v in d.iteritems() ] return points
class Wunderground(Interface): exchange = messages.Exchanges.weather measurement_key = "weather" run_delta = timedelta(minutes=5) station_id = field.Char() api_key = field.Char() KEYS = [ 'UV', 'dewpoint_c', 'feelslike_c', 'heatindex_c', 'precip_1hr_metric', 'precip_today_metric', 'pressure_in', 'pressure_mb', 'relative_humidity', 'solarradiation', 'temp_c', 'visibility_km', 'wind_degrees', 'wind_gust_kph', 'wind_kph', 'windchill_c' ] def slugify(self, value): return "{}".format(value.replace("_", "-")).lower() def parse_value(self, v): try: value = float(v) except: try: value = float(v[0:-1]) except: value = 0 return value def data(self, data=None): current_conditions = "http://api.wunderground.com/api/{}/conditions/q/pws:{}.json".format( self.api_key, self.station_id) res = requests.get(current_conditions, timeout=5) return res.json() def parse_data(self, data): points = [] t = datetime.utcnow() for k, v in data.get('current_observation').iteritems(): if k in self.KEYS: value = self.parse_value(v) points.append( dict(measurement="{}.{}".format(self.measurement_key, self.slugify(k)), time=t, tags=dict( station_id=self.station_id, interface=str(self._id), ), fields=dict(value=value))) return points
class Trigger(orm.Document): _db = "lablog" _collection = "triggers" _indexes = [ orm.Index('name', key=('name', 1), unique=True), orm.Index('key', key=('key', 1)), ] name = field.Char() key = field.Char() last_run = field.Date() _me = field.DynamicDocument() def save(self, *args, **kwargs): if not self._id: self._me = {} self.last_run = datetime.utcnow() - timedelta(minutes=60) super(Trigger, self).save(*args, **kwargs) self._me = self return super(Trigger, self).save(*args, **kwargs) def _run(self, message): interface = message['tags']['interface'] try: ret = self._me.run(message) except TriggerEnabled as e: ti = TriggerInstance.find_one({ 'interface': interface, 'trigger.id': self._id }) if not ti: ti = TriggerInstance() ti.enabled = True ti.interface = interface ti.level = e.level ti.key = self._me.key ti.trigger = self._me ti.save() self.last_run = datetime.utcnow() self.save() except TriggerDisabled as e: ti = TriggerInstance.find_one({ 'interface': interface, 'trigger.id': self._id }) if not ti: return ti.enabled = False ti.save() return
class Admin(orm.Document): _db = "entropealabs" _collection = "client_admins" _indexes = [ orm.Index('email', key=('email', 1), unique=True), ] name = field.Char() email = field.Char() password = field.Char() last_login = field.Date() client = field.DocumentId(type=Client) social = SocialAccounts(type=SocialAccount) facebook_pages = orm.List(type=FacebookPage) def social_account(self, account_type=None): for sa in self.social_accounts: if sa.type == account_type: return sa sa = SocialAccount() sa.type = account_type return sa @staticmethod def passwords_match(pwd, cpwd): if pwd == cpwd: return True return False def save(self): if not password.identify(self.password): self.password = password.encrypt_password(self.password) return super(Admin, self).save() def verify_pwd(self, pwd): self.logger.info(password.encrypt_password(pwd)) self.logger.info(self.password) return password.check_password(pwd, self.password) def is_authenticated(self): if self._id: return True return False def is_active(self): return True def is_anonymous(self): return False def get_id(self): self.logger.info(unicode(self._id)) return unicode(self._id)
class TriggerInstance(orm.Document): _db = "lablog" _collection = "trigger_instance" _indexes = [ orm.Index('interface', key=('interface', 1), unique=True), orm.Index('trigger', key=('trigger.id', 1)), orm.Index('enabled', key=('enabled', 1)), orm.Index('key', key=('key', 1)), ] enabled = field.Boolean(default=True) interface = field.Char() level = field.Integer() key = field.Char()
class Human(orm.Document): _db = "test" _collection = "humans" _indexes = [ orm.Index("name", key=[("name", orm.Index.DESCENDING)]), orm.Index("human_id", key=[("human_id", orm.Index.ASCENDING)]), orm.Index("geo_location", key=[("jobs.locations.geo", orm.Index.GEO2D)]) ] human_id = field.AutoIncrement(collection="human") name = field.Char(required=True, min=2, max=25) age = field.Integer(min=0, max=3000) height = field.Float(min=1, max=100000) weight = field.Float(min=1) jobs = orm.List(type=Job, length=3) genitalia = field.Char()
class Node(Interface): measurement_key = "node" exchange = messages.Exchanges.node id = field.Char() def data(self, data=None): j = aes.decrypt(data.ljust(160), KEY) j = json.loads(j) logging.info(j) return j def parse_data(self, data): v_map = {'t':'temperature','h':'humidity','l':'light','c':'co2','v':'voc','d':'dust','f':'fuel', 'n':'decibel', 'r':'rssi'} points = [] for k,v in data.iteritems(): k = v_map.get(k, k) if v: points.append(dict( measurement="{}.{}".format(self.measurement_key, k), tags=dict( node=str(self.id), interface=str(self._id), ), time=datetime.utcnow(), fields=dict( value=v, ) )) return points
class Annotation(orm.EmbeddedDocument): """ metabolite name with its 2 scores """ annotation = field.Char() score1 = field.Float() score2 = field.Float()
class LoginInfo(mongodb.EmbeddedDocument): email = field.Char(min=0, max=50) password = field.Char(min=0, max=64) # not the reserved init function def init(self, userDict=None): self.email = userDict.get('email') self.password = self.hashPassword(userDict.get('password')) def login(self, credentials): hashed = self.hashPassword(credentials.get('password')) success = self.password == hashed return success def hashPassword(self, password): m = hashlib.sha256() m.update(password) return m.hexdigest()
class Beacon(orm.Document): _db = 'lablog' _collection = 'floorplan_beacons' id = field.Char() x = field.Integer() y = field.Integer() level = field.Integer() location = field.DocumentId(type=Location)
class NetAtmo(Interface): exchange = messages.Exchanges.node measurement_key = "netatmo" run_delta = timedelta(seconds=30) mac_address = field.Char() KEYS = ['Noise', 'Temperature', 'temp_trend', 'Humidity', 'Pressure','CO2'] def data(self, data=None): authorization = lablog.util.lnetatmo.ClientAuth() devList = lablog.util.lnetatmo.DeviceList(authorization) payload = devList.getStationsData(device_id=self.mac_address) logging.debug(payload) return payload def point(self, value, namespace, field): t = datetime.utcnow() value = self.parse_value(value) return dict( measurement="{}.{}.{}".format(self.measurement_key, namespace, self.slugify(field)), time=t, tags=dict( station_id=self.mac_address, interface=str(self._id), ), fields=dict( value=value ) ) def parse_data(self, data): points = [] t = datetime.utcnow() outdoor = data["body"]["devices"][0]["modules"][0]["dashboard_data"] indoor = data["body"]["devices"][0]["dashboard_data"] for k in self.KEYS: i = indoor.get(k) o = outdoor.get(k) if i: points.append(self.point(i, 'indoor', k)) if o: points.append(self.point(o, 'outdoor', k)) return points def slugify(self, value): return "{}".format(value.replace("_", "-")).lower() def parse_value(self, v): try: value = float(v) except: try: value = float(v[0:-1]) except: value = 0 return value
class User(mongodb.Document): _db = "rcr" _collection = "users" user_id = field.AutoIncrement(collection="users") username = field.Char(required=True, min=2, max=25) login_info = LoginInfo() user_stats = UserStats() def sysout(self): print self._json()
class Feature(orm.Document): """ Feature class """ _db = "os_mongo" _collection = "features" feature_id = field.AutoIncrement(collection="experiment") experiment_id = field.Integer(required=True) mass = field.Float(required=True) rt = field.Float(required=True) abundances = orm.List(type=Abundance) main_attribution = field.Char() annotations = orm.List(type=Annotation)