def get_children_by_name(parent, objecttype, name): print "get_children_by_name", parent, objecttype, name if objecttype == "account": candidates = Item.query(hash_key=objecttype, filter_condition=Item.name.__eq__(name)) else: candidates = Item.query(hash_key=objecttype, filter_condition=Item.name.__eq__(name) & Item.parent.__eq__(parent)) return [c.attribute_values for c in candidates]
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 get_sample_values(uri): values = Item.query("value", Item.parent == uri, limit=100) cache = [] for v in values: if v.name.lower() not in cache: cache.append(v.name.lower()) yield v.name.lower()
def test_fd_update_rule(self): intent = TestAccount.get_intent() intentid = intent["ID"] candidates = [ c.attribute_values for c in Item.query("rule", Item.parent.__eq__(intentid)) ] rule = candidates[0] ruleid = rule["ID"] print ">>", ruleid event = { "identity": { "claims": TestAccount.get_claim() }, "field": "updateRule", "path": "Mutation/updateRule", "arguments": { "ruleid": ruleid, "input": { "description": "updated" } } } r = App.handler(event, {}) print r self.assertEqual(r["description"], "updated")
def test_ed_update_variable(self): entity = TestAccount.get_entity() entityid = entity["ID"] candidates = [ c.attribute_values for c in Item.query("variable", Item.parent.__eq__(entityid)) ] variable = candidates[0] variableid = variable["ID"] print ">>", variableid event = { "identity": { "claims": TestAccount.get_claim() }, "field": "updateVariable", "path": "Mutation/updateVariable", "arguments": { "variableid": variableid, "input": { "description": "updated" } } } r = App.handler(event, {}) print r self.assertEqual(r["description"], "updated")
def get_accountid(cls, user="******"): candidates = [ c.attribute_values for c in Item.query("account", Item.creator == user) ] if len(candidates): return candidates[0]["ID"] else: return None
def list_accounts(identity, options): accounts = [] iterator = apply_options(Item.query("user", Item.name.__eq__(identity)), options) for user in iterator: try: print user.attribute_values account = Item.get("account", user.parent) accounts.append(account.attribute_values) except Exception, e: pass
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 getVariableValues(variableid, identity): try: variable = Item.get("variable", variableid) print variable except Exception as e: raise errors.ObjectNotFound(variableid) if variable.doc.type.lower() == "dimension": values = [] valueiterator = Item.query("value", Item.parent == variableid, limit=100) for v in valueiterator: if v.name not in values: values.append(v.name) return values else: return [10, 100, 1000, 5000, 10000]
def test_fc_get_rule(self): intent = TestAccount.get_intent() intentid = intent["ID"] candidates = [ c.attribute_values for c in Item.query("rule", Item.parent.__eq__(intentid)) ] rule = candidates[0] ruleid = rule["ID"] print ">>", ruleid event = { "identity": { "claims": TestAccount.get_claim() }, "field": "getRule", "path": "Query/getRule", "arguments": { "ruleid": ruleid } } r = App.handler(event, {}) print r self.assertEqual(r["name"], rule["name"])
def test_ec_get_variable(self): entity = TestAccount.get_entity() entityid = entity["ID"] candidates = [ c.attribute_values for c in Item.query("variable", Item.parent.__eq__(entityid)) ] variable = candidates[0] variableid = variable["ID"] print ">>", variableid event = { "identity": { "claims": TestAccount.get_claim() }, "field": "getVariable", "path": "Query/getVariable", "arguments": { "variableid": variableid } } r = App.handler(event, {}) print r self.assertEqual(r["name"], variable["name"])
def createOrUpdateLexBot(identity, botid): bot = Item.get("bot", botid) entities = Item.query("entity", Item.parent.__eq__(botid)) botgraph = {"slots": {}, "intents": {}} for e in entities: botgraph["slots"][e.name] = { "name": e.name, "ref": "{%(name)s}" % e.attribute_values, "values": e.doc.aliases, "type": "entity" } variables = Item.query("variable", Item.parent.__eq__(e.ID)) for v in variables: nameslot = v.name botgraph["slots"][nameslot] = { "name": nameslot, "ref": "{%(name)s}" % v.attribute_values, "values": v.doc.aliases, #json.loads(v.doc)["aliases"] "type": "variable" } opslot = v.name + "op" botgraph["slots"][opslot] = { "name": opslot, "ref": "{%(name)sop}" % v.attribute_values, "type": "operator", "values": [ "in", "outside", "greater", "equals", "different", "smaller", "bigger", "taller" ] } valslot = v.name + "val" botgraph["slots"][valslot] = { "type": "value", "name": valslot, "ref": "{%(name)sval}" % v.attribute_values, "values": list(get_sample_values(v.ID)) #sample_values(v.ID) } intents = Item.query("intent", Item.parent.__eq__(botid)) for i in intents: botgraph["intents"][i.name] = {} #print " ",i rules = Item.query("rule", Item.parent.__eq__(i.ID)) for r in rules: botgraph["intents"][i.name][ r.name] = r.doc.replacements #json.loads(r.doc)["expressions"] #print "~BG~%~~"*20 #print pprint.pformat(botgraph) logger().info("Creating bot %s", botid) intents = [] for intent in botgraph["intents"].keys(): logger().critical("Adding Intent %s", intent) intent_config = get_intent_config(intent, botgraph, "lex") #print "** ** ** "*30 #print pprint.pformat(intent_config) #print "-- -- -- "*30 intents.append(intent_config) for slot in intent_config["slots"]: logger().critical("Adding Slot Type `%s`", slot["name"]) #print pprint.pformat(slot["enumerationValues"]) Bot.add_slot_type(slot["name"], slot["enumerationValues"]) logger().critical("Saving Intent `%s` to Lex", intent) Bot.add_intent(intent_config) logger().info("Putting Bot %s", bot.name) Bot.build(bot.name, intents=[i["name"] for i in intents]) logger().info("Putting Bot Alias %s %s", bot.name, "demo") Bot.put_alias(bot.name) Bot.put_bot_version(bot.name) return botgraph
def get_intents(cls, botid): intents = Item.query("intent", filter_condition=Item.parent.__eq__(botid)) return [i.attribute_values for i in intents]
def get_entities(cls, botid): entities = Item.query("entity", filter_condition=Item.parent.__eq__(botid)) return [e.attribute_values for e in entities]
def get_bots(cls, user="******"): accountid = cls.get_accountid(user) bots = Item.query("bot", filter_condition=Item.parent.__eq__(accountid)) return [b.attribute_values for b in bots]