def save_answer(answer_id, question_id, user_id, result): answer = Entity() answer.PartitionKey = answer_id answer.RowKey = question_id answer.result = result answer.created_by = user_id table_service.insert_entity('fsanswers', answer)
def put_classification_result(self, image_uuid, results): task = Entity() task.PartitionKey = self.ImagePartitionKey task.RowKey = image_uuid task.results = str(results) ret = self.table_service.insert_or_replace_entity( self.table_name, task) return ret
def computeFaceDetectionFacePlusPlus(self,face_image_url, file_name, gender): table_name = 'FacePlusPlus' partition_name = 'FacePlusPlus' boundary = '----------%s' % hex(int(time.time() * 1000)) data = [] data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key') data.append(self.faceplusplus_key) data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret') data.append(self.faceplusplus_secret) data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes') data.append('gender') data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'image_url') data.append(face_image_url) data.append('--%s--\r\n' % boundary) http_body='\r\n'.join(data) req=urllib.request.Request(self.faceplusplus_http_url) req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary) req.data = str.encode(http_body) try: resp = urllib.request.urlopen(req, timeout=5) qrcont=resp.read().decode("utf-8") faces = json.loads(qrcont) success = False faceDetected = False genderPrediction = 'None' if 'faces' in faces.keys(): faceDetected = True genderPrediction = faces["faces"][0]["attributes"]["gender"]["value"] if gender.lower()==genderPrediction.lower(): success = True else: success = None faceDetected = None time.sleep(2) face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower() except urllib.request.HTTPError as e: return None, None, None, None
def add_order(table_service): order = Entity() order.PartitionKey = 'ordersSTC' order.RowKey = '002' order.customer = 'Bismark' order.po = '200' order.podate = '05/20/2020' order.deldate = '05/24/2020' order.qty = '800' order.presentation = 'Flobin' order.order = '8500687926' table_service.insert_entity('ordertable', order)
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP Submit trigger received a request') logging.debug('Creating blob service') table_service = TableService( account_name=os.getenv('AZURE_STORAGE_ACCOUNT'), account_key=os.getenv('AZURE_STORAGE_ACCESS_KEY')) headers_dict = { "Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "Post" } schema = submit_schema.SubmitMessageSchema() try: job_dict = schema.loads(req.get_body()) except ValidationError: error = f'Failed to validate the submit message' return func.HttpResponse(error, headers=headers_dict, status_code=400) table_name = os.getenv('AZURE_TABLE_NAME') table_service.create_table(table_name) guid = uuid.uuid4() try: job_dict = schema.dump(job_dict) except ValidationError: error = f'Failed to submit job' return func.HttpResponse(error, headers=headers_dict, status_code=400) entity = Entity() entity.PartitionKey = 'await' entity.RowKey = str(guid) entity.Error = "" entity.area_name = job_dict['area_name'] entity.crop = job_dict['crop'] entity.planting_date = job_dict['planting_date'] entity.irrigated = job_dict['irrigated'] entity.fraction = job_dict['fraction'] entity.geometry = json.dumps(job_dict['geometry']) try: table_service.insert_entity(table_name, entity) except TypeError: error = f'Failed to insert to table' return func.HttpResponse(error, headers=headers_dict, status_code=400) response_dict = {} response_dict['guid'] = guid schema = submit_schema.SubmitResponseSchema() response_message = schema.dumps(response_dict) return func.HttpResponse(response_message, headers=headers_dict, mimetype='application/json')
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') CF.BaseUrl.set("https://emotiontrack.cognitiveservices.azure.com/face/v1.0") CF.Key.set("4a1e0d41a8494d71ac0b9028464d8e62") rowkey = req.params.get('rowkey') if not rowkey: logging.error("Missing parameter(s)") return func.HttpResponse("Missing one or more parameter.", status_code=400) face = req.get_json() face_rect = face['faceRectangle'] table = TableService(connection_string=conn_string) if not table: logging.error("Failed to connect to the storage") return func.HttpResponse("Failed to connect to the storage. Please try again later.", status_code=500) test_img = getFaceImage(table, rowkey, face_rect) test_imgIO = io.BytesIO() test_img.save(test_imgIO, format='JPG') entities = table.query_entities(table_name, filter=None) isMatch = False for entity in entities: img = getFaceImage(table, entity.RowKey, entity.rect) imgIO = io.BytesIO() img.save(imgIO, format='JPG') try: res = CF.face.verify(test_imgIO, imgIO) if res['isIdentical']: # update entry entity.RowKey = rowkey entity.rect = face_rect table.update_entity(table_name, entity) isMatch = True break if not isMatch: # new entry entity = Entity() entity.PartitionKey = "1" entity.RowKey = str(uuid.uuid4()) entity.rect = face_rect table.insert_entity(table_name, entity) return func.HttpResponse(entity.RowKey, status_code=200)
def store_feedback_skills(job_id, hm_id, cand_id, skills_scores, recommendation, org): try: task = Entity() task.PartitionKey = str(uuid.uuid4()) task.HmId = hm_id task.RowKey = str(cand_id) now = datetime.now() task.LastUpdated = str(now) task.SkillScores = json.dumps(skills_scores) task.JobId = str(job_id) table_service.insert_or_replace_entity(table_name, task) except: raise
def upload_price(self, price, fuel_type, location): entry = Entity() try: entry.PartitionKey = "trondelag" entry.RowKey = str(uuid.uuid4()) # Generate new random UUID entry.price = price entry.location = location entry.fueltype = fuel_type self.table_service.insert_entity(self.table_name, entry) except AttributeError: print("Error trying to upload: Fuel type '" + fuel_type + "' Price '" + price + "'") return "Something went wrong. Try check your syntax" return "Price inserted successfully"
def add_order(table_service, req_body): row_key = get_rows(table_service) + 1 print(row_key) order = Entity() order.PartitionKey = req_body.get('PartitionKey') order.RowKey = '00' + str(row_key) order.customer = req_body.get('customer') order.po = req_body.get('po') order.podate = req_body.get('poDate') order.deldate = req_body.get('delDate') order.qty = req_body.get('qty') order.presentation = req_body.get('presentation') order.order = req_body.get('order') table_service.insert_entity('ordertable', order)
def data_load(self): data_set = { "required-modules": [ "custom-vnet|azurerm", "custom-sg|azurerm", "custom-blob|azurerm", "custom-vpc|aws", "custom-sg|aws" ], "approved-instances": [ "Standard_A1_v2|azurerm", "Standard_A2_v2|azurerm", "Standard_A4_v2|azurerm", "Standard_A8_v2|azurerm", "t3.micro|aws", "t3.small|aws", "t3.medium|aws", "t3.large|aws" ], "prohibited-resources": [ "azurerm_resource_group|azurerm", "azurerm_virtual_network|azurerm", "azurerm_network_security_group|azurerm", "azurerm_subnet_network_security_group_association|azurerm", "aws_internet_gateway|aws", "aws_route|aws", "aws_route_table|aws", "aws_route_table_association|aws", "aws_subnet|aws", "aws_vpc|aws", "aws_security_group|aws" ], "allowed-resources": [ "azurerm_virtual_machine|azurerm", "azurerm_network_interface|azurerm", "azurerm_public_ip|azurerm", "azurerm_storage_account|azurerm", "aws_instance|aws", "aws_s3_bucket|aws", "aws_s3_bucket_policy|aws" ], "prevent-deletion": ["true"], "default-provider": ["azurerm"], "mandatory-tags": ["Department", "Environment"], "max-cost": ["100"], "ddb-encryption": ["true"], "no-star-access": ["true"] } # delete all entries items = self.table_service.query_entities(self.table_name) for itm in items: self.table_service.delete_entity(self.table_name, itm.PartitionKey, itm.RowKey) # add all entries for category in data_set: for value in data_set[category]: item = Entity() item.PartitionKey = category item.RowKey = value self.table_service.insert_entity(self.table_name, item) return True
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') # logging.info(table) new_name = req.params.get('name') new_school = req.params.get('school') new_person = Entity() new_person.PartitionKey = "1" new_person.RowKey = str(uuid.uuid4()) new_person.name = new_name new_person.school = new_school etag = table.insert_entity(table_name, new_person) return func.HttpResponse(str(new_person.RowKey), status_code=200)
def add_to_list(self, list_name, value, provider=None): pkey = "" if provider is not None: pkey = "|" + provider item = Entity() item.PartitionKey = list_name item.RowKey = value + pkey main_list = self.get_list(list_name, provider) try: self.table_service.insert_entity(self.table_name, item) except ValueError: pass else: main_list.append(value) return main_list
def insert_sub_date_entry(self, entry): sub_date = Entity() sub_date.PartitionKey = entry.subreddit sub_date.RowKey = entry.title sub_date.created_utc = entry.created_utc sub_date.post_id = entry.post_id try: self.table_service.insert_or_replace_entity( 'mostrecentsubdate', sub_date) except TypeError as error: print(error) print( f"The mostrecentsubdate object is formatted incorrectly and was not updated. One of the parameters is not an int, str, bool or datetime, or defined custom EntityProperty. Continuing..." )
def set_container_location(): # if POST is not JSON or does not contain a trackerId abort if not request.json or 'DeviceId' not in request.json: abort(400) e = Entity() e.PartitionKey = 'container' e.RowKey = request.json['DeviceId'] e.device_id = request.json['DeviceId'] e.latitude = request.json['Properties']['1012'] e.longitude = request.json['Properties']['1013'] e.time = request.json['Properties']['5018'] table_service.insert_or_replace_entity('emerson', e) # return success with submitted shipping location return jsonify(e), 201
def add_entity(StoreName, Address, MaxCapacity, Action, Value, ZipCode, IP, DateTime): #instantiate new entity row = Entity() row.PartitionKey = Address #store location serves as the partition key row.RowKey = DateTime #timestamp serves as the row key previous = get_most_recent_entity(StoreName, Address) #if this is the first entity we are adding if previous is None: if MaxCapacity == None or ZipCode == None or IP == None: return False row.MaxCapacity = MaxCapacity row.ZipCode = ZipCode row.IP = IP if Action == 'inc': row.CurrentOccupancy = Value elif Action == 'dec': row.CurrentOccupancy = str(int(Value) * -1) else: return False # otherwise use previous entity to get previous CurrentOccupancy and other parameters if necessary else: if MaxCapacity == None: row.MaxCapacity = previous.MaxCapacity else: row.MaxCapacity = MaxCapacity if ZipCode == None: row.ZipCode = previous.ZipCode else: row.ZipCode = ZipCode if IP == None: row.IP = previous.IP else: row.IP = IP if Action == 'inc': row.CurrentOccupancy = str( int(previous.CurrentOccupancy) + int(Value)) elif Action == 'dec': row.CurrentOccupancy = str( int(previous.CurrentOccupancy) - int(Value)) else: return False #insert entity into table for the given StoreName table_service.insert_entity(StoreName, row) return True
def create_tweet_entity(record): row = Entity() row.PartitionKey = record["user_id"] row.RowKey = record["id"] row.user_screen_name = record["user_screen_name"] row.created_at = record["obj"]["created_at"] row.text = record["obj"]["text"] row.source = record["obj"]["source"] row.retweet_count = record["obj"]["retweet_count"] row.favorite_count = record["obj"]["favorite_count"] row.entities = record["obj"]["entities"] row.coordinates = str(record["obj"]["coordinates"]) row.lang = record["obj"]["lang"] row.is_retweet = record["is_retweet"] row.is_quote = record["is_quote"] row.is_reply = record["is_reply"] row.last_hydrated = record["last_hydrated"] return row
def post(self): """ Inserts user into an Azure table. """ if request.is_json: try: user = Entity() user.PartitionKey = request.json["email"] user.RowKey = '' user.info = EntityProperty(EdmType.BINARY, dumps(request.json)) table_service.insert_or_replace_entity('users', user) except (KeyError): return 'Please provide a json object conforming to \ the following pattern: {\"email\":\"[email protected]\",\ \"password\":\"xxx\", \"full_name\":\"Foo Bar\"}', 400 return None, 201 else: return 'Please supply a json object in order to add a user.', 400
def main(msg: func.QueueMessage) -> None: body = msg.get_body().decode('utf-8') body_json = json.loads(body) table_service = TableService(connection_string=os.environ["TableStorage"]) logging.info('Python queue trigger function processed a queue item: %s', msg.get_body().decode('utf-8')) task = Entity() task.PartitionKey = body_json["party"] task.RowKey = str(uuid.uuid4()) task.count = body_json["count"] task.electoralPlace = body_json["electoralPlace"] task.electoralUnit = body_json["electoralUnit"] table_service.insert_entity('votes', task) # datetime object containing current date and time now = datetime.now() logging.info(now.strftime("%d/%m/%Y %H:%M:%S") + ' - Processing done')
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') # logging.info(table) pid = req.params.get('pid') ts = int(req.params.get('ts')) value = req.params.get('value') data = req.get_json() new_es = Entity() new_es.PartitionKey = "1" new_es.RowKey = str(uuid.uuid4()) new_es.pid = pid new_es.ts = ts new_es.value = value new_es.data = data etag = table.insert_entity(table_name, new_es) return func.HttpResponse(str(new_es.RowKey), status_code=200)
def set_value(self, key, value): item = Entity() item.PartitionKey = key item.RowKey = value retval = False try: entries = self.table_service.query_entities( self.table_name, filter="PartitionKey eq '" + key + "'") old_value = "invalid" for entry in entries: old_value = entry.RowKey self.table_service.delete_entity(self.table_name, key, old_value) self.table_service.insert_entity(self.table_name, item) except ValueError: pass else: retval = True return retval
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') logging.info(str(req.url)) logging.info(str(req.get_body())) now = datetime.now() # logging.info(table) pid = req.params.get('pid') ts = int(now.timestamp() * 100) site = req.get_json()['site'] metadata = Entity() metadata.PartitionKey = "1" metadata.RowKey = str(uuid.uuid4()) metadata.pid = pid metadata.ts = ts metadata.site = site etag = table.insert_entity(table_name, metadata) return func.HttpResponse(str(metadata.RowKey), status_code=200)
def insert_submission_entry(self, entry): submission = Entity() submission.PartitionKey = entry.subreddit submission.RowKey = entry.id submission.author = entry.author submission.created_utc = entry.created_utc submission.flair = entry.flair submission.title = entry.title # Flatten list of keywords into comma separated string submission.title_keywords = ','.join(map(str, entry.title_keywords)) submission.title_sentiment = entry.title_sentiment try: submission.body_keywords = ','.join(map(str, entry.body_keywords)) submission.body_sentiment = entry.body_sentiment except AttributeError: submission.body_keywords = "" submission.body_sentiment = "" self.table_service.insert_entity('submissions', submission)
def insert_comment_entry(self, entries): for entry in entries: comment = Entity() comment.PartitionKey = entry.link_id comment.RowKey = entry.id comment.author = entry.author comment.body = entry.body comment.created_utc = entry.created_utc comment.parent_id = entry.parent_id comment.score = entry.score comment.subreddit = entry.subreddit comment.subreddit_id = entry.subreddit_id comment.total_awards_received = entry.total_awards_received comment.sentiment = entry.sentiment # Flatten list of keywords into comma separated string comment.keywords = ','.join(map(str, entry.keywords)) self.table_service.insert_entity('comments', comment)
def create_db_entity(self, spoke, vm_details): vm = Entity() # PartitionKey is nothing but the spoke name vm.PartitionKey = spoke # RowKey is nothing but the VM name itself. vm.RowKey = vm_details['hostname'] vm.name = vm_details['name'] vm.serial_no = vm_details['serial'] vm.ip_addr = vm_details['ip-address'] vm.connected = vm_details['connected'] vm.deactivated = vm_details['deactivated'] vm.subs_id = self.subscription_id vm.delicensed_on = 'not applicable' vm.is_delicensed = 'No' try: self.table_service.insert_entity(self.vmss_table_name, vm) self.logger.info("VM %s with serial no. %s in db" % (vm_details['hostname'], vm_details['serial'])) except Exception as e: self.logger.info("Insert entry to db for %s failed with error %s" % (vm_details['hostname'], e)) return False return True
def post_feedback(enable_feedbacks): # if current user is active if enable_feedbacks: # populate the feedback entity feedback = Entity() feedback.PartitionKey = 'foodex2feedbacks' time = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') feedback.RowKey = time desc = request.get_json().get("desc") from_ln = request.get_json().get("lang") desc = get_translation(from_ln, desc) if desc else desc feedback.description = desc feedback.code = request.get_json().get("code") # insert the entity in the table table_service.insert_entity(tableName, feedback) return json.dumps({'message': 'Feedback sent correctly'}) else: return json.dumps({ 'message': 'An error occurred while sending the feedback please try the administrator.' })
def set_target_stock(self,df_target): # ['price','volume', 'per','eps'] df_target["date"]=time.strftime('%Y%m%d') stockdf_table = df_target.rename(columns={"date": "PartitionKey", "code": "RowKey"}) for index, row in stockdf_table.iterrows(): #print(row) #print(row['PartitionKey']) #print(">> start row") #print(row) task = Entity() task.PartitionKey = row.to_dict()['PartitionKey'] task.RowKey = str(row.to_dict()['RowKey']) task.price = row.to_dict()['price'] task.volume = row.to_dict()['volume'] task.per = row.to_dict()['per'] task.eps = row.to_dict()['eps'] self.table_service.insert_or_merge_entity('stocktarget', task) print(">> set target stock..." + str(row.to_dict()['RowKey']) )
def table(): account_name = config.STORAGE_ACCOUNT_NAME account_key = config.STORAGE_ACCOUNT_KEY table_service = TableService(account_name=account_name, account_key=account_key) table_name = config.TABLE_NAME #table_service.create_table(table_name) imageId = str(uuid.uuid4()) task = Entity() task.PartitionKey = 'dlws' task.RowKey = imageId task.description = 'test' table_service.insert_or_replace_entity(table_name, task) task = table_service.get_entity(table_name, 'dlws', imageId) print(task.description) tasks = table_service.query_entities('tasktable') for task in tasks: print(task.description) print(task.RowKey)
def prepare_pipeline_data( partition_key: str, token: str, pipeline_params: Dict[str, Union[int, str]], notification_web_params: Dict[str, str], data: Any, ) -> Entity: pipeline_data = Entity() pipeline_data.PartitionKey = partition_key pipeline_data.RowKey = token pipeline_data.factory_name = pipeline_params["factory_name"] pipeline_data.resource_group = pipeline_params["resource_group"] pipeline_data.pipeline_name = pipeline_params["pipeline_name"] pipeline_data.expiration_time = pipeline_params["expiration_time"] pipeline_data.data = json.dumps(data) pipeline_data.acted_upon = ( 0 # to be marked as read (1) once the pipeline has restarted ) pipeline_data.web_path = notification_web_params["web_path"] pipeline_data.share_name = notification_web_params["share_name"] return pipeline_data
def upload_json_prices(self, prices): error = False for val in prices: # Loop through new_prices and add to database entry = Entity() try: entry.PartitionKey = val["county"] entry.RowKey = str(uuid.uuid4()) # Generate new random UUID entry.price = val["price"] entry.location = val["location"] if (val["fueltype"] == "diesel" or val["fueltype"] == "gasoline"): entry.fueltype = val["fueltype"] else: entry.fueltype = "unknown" self.table_service.insert_entity(self.table_name, entry) except AttributeError: error = True print("Error trying to parse JSON object: " + val) if error: return "Something went wrong. Try check your syntax" else: return "Inserted successfully"
def computeFaceDetectionGoogle(self,face_image_url, file_name, gender): table_name = 'Google' partition_name = 'Google' self.clientImgAnnotator = vision.ImageAnnotatorClient() image = vision.types.Image() image.source.image_uri = face_image_url try: response = self.clientImgAnnotator.face_detection(image=image) r = MessageToDict(response, preserving_proto_field_name = True) success = False faceDetected = False genderPrediction = 'None' if len(r) == 0: success = False faceDetected = False faces=[] elif len(r)>0: faceDetected = True success = False faces = r['face_annotations'] face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower() except WatsonApiException as ex: print("Method failed with status code " + str(ex.code) + ": " + ex.message)