def post(sku=None): """ POST method is used for creating/updating the product records. If sku is none, then assumes that a CSV file with records is present in the request body. If sku is given tries to Update a record with the given sku. :param sku: :return: """ if sku is None: # If sku is None, assume file is present in the request file = request.files["file"] # Read the CSV file and get unique records from it stream = io.StringIO(file.stream.read().decode("UTF8")) reader = csv.DictReader(stream) unique_records = [ dict(y) for y in set(tuple(x.items()) for x in reader) ] # Send the list of records to worker for processing upload_product_csv_records.send(unique_records) trigger_webhooks.send("CSV Upload in progress!") else: request_body = request.json connection = get_connection() name = str(request_body["name"]).strip() description = str(request_body["description"]).strip() active = str(request_body["active"]).strip()[0] if active is True: active = 1 elif active is False: active = 0 cursor = connection.cursor() sql_statement = """ UPDATE product SET name=%s, active=%s, description=%s WHERE sku=%s; """ params = (name, active, description, sku) cursor.execute(sql_statement, params) connection.commit() connection.close() trigger_webhooks.send( f"Product with sku {sku} " f"updated, name is {name} and description is {description}.") return jsonify({"message": "Success"})
def start_counting(data): # запуск расчета баз PNN = 'ПНН' if data['CHECKED'] == True else 'без ПНН' connect_bases = [] for base in data['BASES']: host = redis_store.hmget(base[:3], ['host'])[0] try: if platform == 'linux': call(["ping", "-c", "1", host], timeout=0.25, stdout=DEVNULL) else: call(["ping", "-n", "1", host], timeout=0.25, stdout=DEVNULL) except TimeoutExpired: basename = redis_store.hmget(base[:3], ['name'])[0] emit('server_response', { 'info': 'button', 'procedure': 'error', 'data': basename }) continue connect_bases.append(base) conn = get_connection(base) cursor = conn.cursor() if PNN == 'ПНН': cursor.execute( querySQL.sql_update3, (1, 1, 'Запуск обработок: 2. Выполнить расчет с ПНН')) else: cursor.execute( querySQL.sql_update3, (1, 1, 'Запуск обработок: 1. Выполнить расчет без ПНН')) conn.commit() conn.close() # minutes = redis_store.hmget(base, ['interval'])[0] # check_time = (datetime.now()+timedelta(minutes=int(minutes))).strftime('%d.%m.%Y %H:%M:%S') check_time = (datetime.now()).strftime( '%d.%m.%Y %H:%M:%S') # DELETE THIS ROW redis_store.hmset(base, { 'check_time': check_time, 'PNN': PNN, 'status': '1' }) # need to check if len(connect_bases) > 0: emit('server_response', { 'info': 'button', 'procedure': 'start_counting', 'data': connect_bases }, broadcast=True)
def connection_info(request, id): """ GET /api/connection/{connection id} Responds with all data available for the specified connection (except for document's ID and coordinates). Status codes: * 200 - OK * 404 - Given ID did not match any documents """ doc = DB.get_connection(id) if not doc: return json({'error': True}, 404) else: doc['contributions'] = DB.get_contributions(doc['agent_data']['id']) return json(doc)
def delete(id): """ The DELETE method is used to delete a webhook from the database.. :return: return_json: A list of webhooks """ connection = get_connection() cursor = connection.cursor() sql_statement = """ DELETE FROM webhook WHERE id=%s """ params = id try: cursor.execute(sql_statement, params) connection.commit() except Exception as e: raise e connection.close()
def change_period(data): # изменение даты запрета редактирования time = datetime.strptime('01.' + data['TIME'].replace('.2', '.4'), '%d.%m.%Y') if data['CHECKED'] == True: checked = 1 PO = '(v)' else: checked = 0 PO = '( )' connect_bases = [] for base in data['BASES']: host = redis_store.hmget(base[:3], ['host'])[0] try: if platform == 'linux': call(["ping", "-c", "1", host], timeout=0.25, stdout=DEVNULL) else: call(["ping", "-n", "1", host], timeout=0.25, stdout=DEVNULL) except TimeoutExpired: basename = redis_store.hmget(base[:3], ['name'])[0] emit('server_response', { 'info': 'button', 'procedure': 'error', 'data': basename }) continue connect_bases.append(base) conn = get_connection(base) cursor = conn.cursor() cursor.execute(querySQL.sql_update2, (time, checked)) conn.commit() conn.close() redis_store.hmset(base, {'period': data['TIME'], 'PO': PO}) if len(connect_bases) > 0: emit('server_response', { 'info': 'button', 'procedure': 'change_period', 'data': connect_bases }, broadcast=True)
def upload_product_csv_records(csv_records: list) -> None: """ This is the background function that is responsible for uploading the CSV records onto the database. :param csv_records: list of product records """ total_records = len(csv_records) connection = get_connection() cursor = connection.cursor() for index, product_dict in enumerate(csv_records): # Use Upsert logic as application dictates that record must be updated sql_statement = """ INSERT INTO product (sku, name, description, active) VALUES (%s,%s,%s, %s) ON DUPLICATE KEY UPDATE name=%s, description=%s, active=%s; """ params = ( product_dict["sku"].strip(), product_dict["name"].strip(), product_dict["description"].strip(), random.choice([0, 1]), product_dict["name"].strip(), product_dict["description"].strip(), random.choice([0, 1]), ) cursor.execute(sql_statement, params) # Publish SSE event on flask sse endpoint sse.publish( { "message": "Total:" + str(index + 1) + "/" + str(total_records), "total": total_records, "completed": index + 1, } ) # Commit the changes to the database connection.commit() # Close connection after work is completed. connection.close()
def post(): """ The POST method is used to insert/update a webhook in the database.. """ request_body = request.json connection = get_connection() cursor = connection.cursor() sql_statement = """ INSERT INTO webhook (url) VALUES (%s) ON DUPLICATE KEY UPDATE url=%s; """ params = (request_body["url"], request_body["url"]) try: cursor.execute(sql_statement, params) connection.commit() except Exception as e: raise e connection.close()
def put(): """ The PUT method is invoked for inserting an individual record into the database. In case of item with the same SKU already existing, the existing item is updated with the given form parameters. """ request_body = request.json connection = get_connection() name = str(request_body["name"]).strip() active = bool(str(request_body["active"]).strip()) description = str(request_body["description"]).strip() sku = str(request_body["sku"]).strip() if active is True: active = 1 elif active is False: active = 0 cursor = connection.cursor() sql_statement = """ INSERT INTO product (sku, name, description, active) VALUES (%s,%s,%s, %s) ON DUPLICATE KEY UPDATE name=%s, description=%s, active=%s; """ params = (sku, name, description, active, name, description, active) cursor.execute(sql_statement, params) connection.commit() connection.close() trigger_webhooks.send( f"Product with sku {sku} " f"added, name is {name} and description is {description}.") return jsonify({"message": "Success"})
def get() -> object: """ The GET method is used to return a list of all the products present in the database :return: list of products in json """ connection = get_connection() with connection.cursor() as cursor: sql_statement = """ SELECT * FROM product """ cursor.execute(sql_statement) result = cursor.fetchall() connection.close() # Using pymysql fetch as it is faster that loading SQLAlchemy objects # SQLAlchemy method is commented out # products = Product.query.all() # result = [] # for item in products: # result.append({'id':item.id, 'sku':item.sku, 'name':item.name, 'description':item.description}) return result
def save_page(page): db_conn = get_connection() insert_page(page.url, page.text(), conn=db_conn) db_conn.close()
def server_response(): # проверка баз на окончание расчета current_bases = redis_store.smembers('current_bases') complite_bases = [] grz = redis_store.hmget('grz', ['name', 'host', 'interval']) dbk = redis_store.hmget('dbk', ['name', 'host', 'interval']) lvt = redis_store.hmget('lvt', ['name', 'host', 'interval']) usm = redis_store.hmget('usm', ['name', 'host', 'interval']) grz_work = redis_store.hmget( 'grz_work', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) grz_test = redis_store.hmget( 'grz_test', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) dbk_work = redis_store.hmget( 'dbk_work', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) dbk_test = redis_store.hmget( 'dbk_test', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) lvt_work = redis_store.hmget( 'lvt_work', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) lvt_test = redis_store.hmget( 'lvt_test', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) usm_work = redis_store.hmget( 'usm_work', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) usm_test = redis_store.hmget( 'usm_test', ['period', 'PO', 'check_time', 'PNN', 'status', 'cache']) for base in redis_store.smembers('current_bases'): for operating_mode in ['work', 'test']: if redis_store.hmget( base + '_' + operating_mode, 'status' )[0] == '1': # если у базы статус "идет расчет", то проверяем по документам начисления и льгот str_date = redis_store.hmget(base + '_' + operating_mode, ['check_time'])[0] check_time = datetime.strptime(str_date, '%d.%m.%Y %H:%M:%S') if check_time < datetime.now(): str_date = redis_store.hmget(base + '_' + operating_mode, ['period'])[0] date = datetime.strptime( '01.' + str_date.replace('.2', '.4'), '%d.%m.%Y') PNN = redis_store.hmget(base + '_' + operating_mode, ['PNN'])[0] host = redis_store.hmget(base, ['host'])[0] try: if platform == 'linux': call(["ping", "-c", "1", host], timeout=0.25, stdout=DEVNULL) else: call(["ping", "-n", "1", host], timeout=0.25, stdout=DEVNULL) except TimeoutExpired: continue conn = get_connection(base + '_' + operating_mode) cursor = conn.cursor() cursor.execute(querySQL.sql_select10, ( date, 'Абоненты с групповым измерительным оборудованием (создано обработкой)', 'Абоненты без групповых счетчиков (создано обработкой)' )) result = cursor.fetchone() if result[ 'STATUS'] == 1: # если количество проведенных документов начислений равно общему количеству документов за установленный учетный месяц if PNN == 'ПНН': cursor.execute(querySQL.sql_update3, ( 0, 1, 'Запуск обработок: 2. Выполнить расчет с ПНН')) else: cursor.execute(querySQL.sql_update3, ( 0, 1, 'Запуск обработок: 1. Выполнить расчет без ПНН' )) conn.commit() redis_store.hmset(base + '_' + operating_mode, { 'check_time': '', 'status': '2' }) # меняем статус базы на "расчет завершен" conn.close() elif redis_store.hmget( base + '_' + operating_mode, 'status')[0] == '2': # если у базы "расчет завершен" complite_bases.append(base + '_' + operating_mode) emit('server_response', { 'info': 'server', 'data': complite_bases }, broadcast=True) # if __name__ == '__main__': # socketio.run(app)