def create_account(identity): accountid = ID.now() uri = "uri:account:" + accountid candidates = [c for c in Item.query("account", Item.creator == identity)] if len(candidates): raise errors.DuplicateError("You already created an airbot account ") account = Item(parent="service", creator=identity, ID=uri, name=accountid, createdat=ID.now(), search="-", objecttype="account") account.save() owner = Item(parent=uri, creator=identity, ID="uri:user:"******":" + identity, name=identity, search=identity, createdat=ID.now(), objecttype="user") owner.save() return account.attribute_values
def createBot(parent,identity,options) : accountid = parent.split(":")[2] name = options.get("name","unnamedbot"+datetime.now().strftime("%Y%m%D%H%M%S")) uri = "uri:bot:%(accountid)s:%(name)s" % vars() try : existing = Item.get("bot", uri) raise errors.DuplicateError(uri) except Exception : pass input= {} input["ID"] = uri input["objecttype"] = "bot" input["parent"] = parent input["name"] = options.get("name","untitled") input["description"] = options.get("description","no description available") input["search"] = input["name"]+"-"+input["description"] input["createdat"] = ID.now() input["creator"] = identity input["doc"] = { "bucket" : "airbot2018", "database" : "airbot"+input["name"], "status" : "NOTBUILT", "prefix" : input["name"] } item = Item(**input) item.save() bot = Item.get("bot", uri) return bot.attribute_values
def createRule(parent,options, identity): print ">", parent, options, identity try : intent = Item.get("intent",parent) except Exception: raise errors.ObjectNotFound(parent) bot = Item.get("bot",intent.parent) accountid = parent.split(":")[2] botname = parent.split(":")[3] intentname = parent.split(":")[4] name = options["name"] uri = "uri:rule:%(accountid)s:%(botname)s:%(intentname)s:%(name)s" % vars() print uri data={} data["ID"] = uri data["name"] = options["name"] data["objecttype"] = "rule" data["parent"] = parent if parent is not None else "service" data["search"] = name + options.get("description", "-") + options.get("tags", "-") data["createdat"] = ID.now() data["creator"] = identity data["doc"] = { "replacements" : options["replacements"] } item = Item(**data) item.save() d = item.json() d.update(d["doc"]) del d["doc"] return d
def createEntity(identity, botid, data): try: bot = Item.get("bot", botid) except Exception as e: raise errors.ObjectNotFound(botid) print ">>", data parts = botid.split(":") accountid = parts[2] botname = parts[3] entityname = data["name"] uri = "uri:entity:%(accountid)s:%(botname)s:%(entityname)s" % vars() entity = Item(parent=botid, ID=uri, createdat=ID.now(), name=data["name"], description=data.get("description", "-"), search=data["name"] + data.get("description", "-"), objecttype="entity", doc={ "aliases": data["aliases"], "status": ENTITYSTATES.NOT_READY, "database": bot.doc.database, "tablename": data["name"] }) print entity entity.save() return entity.attribute_values
def invite(accountid, email, identity): accountid = accountid.split(":")[2] user = Item(parent=accountid, name=email, objecttype="user", creator=identity, createdat=ID.now(), ID="uri:user:%(accountid)s:%(email)s" % vars(), search=email) user.save()
def create_intent(botid, input, identity): name = input["name"] print "create received", botid, input, identity accountid = botid.split(":")[2] botname = botid.split(":")[3] uri = "uri:intent:%(accountid)s:%(botname)s:%(name)s" % vars() objectid = ID.get() input["ID"] = uri input["objecttype"] = "intent" input["createdat"] = ID.now() input["creator"] = identity input["parent"] = botid input["search"] = input.get("name") + input.get( "description", "-") + input.get("tags", "-") input["doc"] = {} print pprint.pformat(input) item = Item(**input) item.save() return item.json()
def syncVariableValues(variableid): variable = Item.get("variable", variableid) valueiterator = Item.query("value", Item.parent == variableid) with Item.batch_write() as batch: for v in valueiterator: batch.delete(v) entity = Item.get("entity", variable.parent) bot = Item.get("bot", entity.parent) database = bot.doc.database tablename = entity.doc.tablename column = variable.doc.field if variable.doc.type.lower() == "dimension": sql = 'select distinct "%(column)s" from "%(database)s"."%(tablename)s" ' % vars( ) response = AthenaQuery.run(**{"sql": sql}) #logger().info("Athena returned %s", pprint.pformat(response)) values = [ r[column].replace('"', '') for r in response["data"]["records"] ] elif variable.doc.type.lower() == "metric": sql = 'select max( "%(column)s") as mx, min("%(column)s") as mn from "%(database)s"."%(tablename)s" ' % vars( ) response = AthenaQuery.run(**{"sql": sql}) r = response["data"]["records"][0] M = r["mx"] m = r["mn"] print m, M values = scale(m, M) print ">>", values with Item.batch_write() as batch: for i, val in enumerate(values): if type(val) == types.IntType: val = unicode(val) if len(val): slug = unicodedata.normalize('NFKD', val).encode('ascii', 'ignore') cache = Item( **{ "name": slug, "parent": variableid, "doc": {}, "createdat": ID.now(), "objecttype": "value", "ID": variableid + ":" + str(i), "search": slug }) batch.save(cache) return
def createVariable(parent, options, identity): print ">", parent, options, identity try: entity = Item.get("entity", parent) except Exception: raise errors.ObjectNotFound(parent) if entity.doc.status != ENTITYSTATES.READY: raise errors.InvalidParameter( "Could not add variable to unparsed entity") bot = Item.get("bot", entity.parent) accountid = parent.split(":")[2] botname = parent.split(":")[3] entityname = parent.split(":")[4] name = options["name"] uri = "uri:variable:%(accountid)s:%(botname)s:%(entityname)s:%(name)s" % vars( ) print uri data = {} data["ID"] = uri data["name"] = options["name"] data["objecttype"] = "variable" data["parent"] = parent if parent is not None else "service" data["search"] = name + options.get("description", "-") + options.get( "tags", "-") data["createdat"] = ID.now() data["creator"] = identity data["doc"] = { "field": options["field"], "type": options.get("type", "dimension"), "aliases": options["aliases"] } item = Item(**data) item.save() syncVariableValues(variableid=uri) d = item.json() d.update(d["doc"]) del d["doc"] return d '''
def createBot(parent,identity,options) : accountid = parent.split(":")[2] name = options.get("name","unnamedbot"+datetime.now().strftime("%Y%m%D%H%M%S")) uri = "uri:bot:%(accountid)s:%(name)s" % vars() try : existing = Item.get("bot", uri) raise errors.DuplicateError(uri) except Exception : pass lexbotname=(random.choice([chr(i) for i in range(65, 65 + 26)]) + random.choice([chr(i) for i in range(65, 65 + 26)])).lower() assigned = True while assigned : try : Item.get("lex",lexbotname) lexbotname = (random.choice([chr(i) for i in range(65, 65 + 26)]) + random.choice( [chr(i) for i in range(65, 65 + 26)])).lower() except Exception : assigned=False Item(objecttype="lex", ID=lexbotname,name=lexbotname,parent=uri, search=lexbotname,doc={},createdat=ID.now()).save() input= {} input["ID"] = uri input["objecttype"] = "bot" input["botname"] = lexbotname input["parent"] = parent input["name"] = options.get("name","untitled") input["description"] = options.get("description","no description available") input["search"] = input["name"]+"-"+input["description"] input["createdat"] = ID.now() input["creator"] = identity input["doc"] = { "bucket" : "airbot2018", "database" : "airbot"+input["name"], "status" : "NOTBUILT", "prefix" : input["name"] } item = Item(**input) item.save() bot = Item.get("bot", uri) return bot.attribute_values
def create_item(parent, input, identity): print "create %(objectype)s received", parent, input, identity try: name = input["name"] except Exception: raise errors.InvalidParameter("Missing `name` field in input" % vars()) if parent is None: parent = "service" else: try: parent_item = Item.get(parentobjecttype, parent) except Exception: raise errors.ObjectNotFound( "No parent object with type `%(parentobjecttype)s` found with Id :`%(parent)s`" % vars()) candidates = get_children_by_name(parent, objecttype=objecttype, name=input["name"]) if len(candidates): if parentobjecttype is None: parent = "accounts" raise errors.DuplicateError( "An object with name `%(name)s`and type `%(objecttype)s` already exists in `%(parent)s`" % vars()) objectid = ID.get() if objecttype == "account": uri = "uri:account:%(objectid)s" % vars() if objecttype == "bot": accountid = parent.split(":")[2] uri = "uri:bot:%(accountid)s:%(name)s" % vars() if objecttype == "entity": accountid = parent.split(":")[2] botname = parent.split(":")[3] uri = "uri:entity:%(accountid)s:%(botname)s:%(name)s" % vars() if objecttype == "intent": accountid = parent.split(":")[2] botname = parent.split(":")[3] uri = "uri:intent:%(accountid)s:%(botname)s:%(name)s" % vars() if objecttype == "reply": accountid = parent.split(":")[2] botname = parent.split(":")[3] intentname = parent.split(":")[4] uri = "uri:reply:%(accountid)s:%(botname)s:%(intenname)s:(name)s" % vars( ) if objecttype == "variable": accountid = parent.split(":")[2] botname = parent.split(":")[3] entityname = parent.split(":")[4] uri = "uri:variable:%(accountid)s:%(botname)s:%(entityname)s:%(name)s" % vars( ) if objecttype == "rule": accountid = parent.split(":")[2] botname = parent.split(":")[3] intentname = parent.split(":")[4] uri = "uri:rule:%(accountid)s:%(botname)s:%(intentname)s:%(name)s" % vars( ) input["ID"] = uri input["objecttype"] = objecttype input["parent"] = parent if parent is not None else "service" input["search"] = input.get("name") + input.get( "description", "-") + input.get("tags", "-") input["createdat"] = ID.now() input["creator"] = identity input["doc"] = input.get("doc") or '{"":""}' print pprint.pformat(input) item = Item(**input) item.save() return item.attribute_values
def createVariable(parent, options, identity): print ">", parent, options, identity try: entity = Item.get("entity", parent) except Exception: raise errors.ObjectNotFound(parent) if entity.doc.status != ENTITYSTATES.READY: raise errors.InvalidParameter( "Could not add variable to unparsed entity") bot = Item.get("bot", entity.parent) accountid = parent.split(":")[2] botname = parent.split(":")[3] entityname = parent.split(":")[4] name = options["name"] uri = "uri:variable:%(accountid)s:%(botname)s:%(entityname)s:%(name)s" % vars( ) print uri data = {} data["ID"] = uri data["name"] = options["name"] data["objecttype"] = "variable" data["parent"] = parent if parent is not None else "service" data["search"] = name + options.get("description", "-") + options.get( "tags", "-") data["createdat"] = ID.now() data["creator"] = identity data["doc"] = {"aliases": options["aliases"], "field": options["field"]} item = Item(**data) item.save() database = bot.doc.database tablename = entity.doc.tablename columnname = options["field"] sql = 'select %(columnname)s from "%(database)s"."%(tablename)s" group by %(columnname)s limit 30' % vars( ) response = AthenaQuery.run(**{"sql": sql}) values = [ r[columnname].replace('"', '') for r in response["data"]["records"] ] for i, val in enumerate(values): if len(val): slug = unicodedata.normalize('NFKD', val).encode('ascii', 'ignore') cache = Item( **{ "name": slug, "parent": uri, "doc": {}, "createdat": ID.now(), "objecttype": "value", "ID": uri + ":" + str(i), "search": slug }) print cache cache.save() d = item.json() d.update(d["doc"]) del d["doc"] return d