def get_traffic_monitor_field(link_id, field_name, traffic_monitor_table_name="traffic_monitor", link_table_name="link"): from utilities.utils import tuple_list_key_list_to_dict_list traffic_monitors = [] engine, cnn, metadata = connect_to_postgres(logging, postgres_url) query = ''' select public.{0}.{2}, EXTRACT(EPOCH FROM public.{0}.datetime)::bigint, public.{1}.server_ip, public.{1}.client_ip, public.{1}.link_name from public.{0}, public."{1}" where "{1}".link_id = "{0}".link_id and "{0}".link_id = %(link_id)s and "{0}".status = 'done' order by {0}."datetime" Desc '''.format(traffic_monitor_table_name, link_table_name, field_name) res = cnn.execute(query, {"link_id": link_id}) results = res.fetchall() print(results) res = tuple_list_key_list_to_dict_list(results, [ field_name, "datetime", "link_id", "server_ip", "client_ip", "link_name" ]) return res
def get_traffic_monitors(traffic_monitor_table_name="traffic_monitor", link_table_name="link"): from utilities.utils import tuple_list_key_list_to_dict_list traffic_monitors = [] engine, cnn, metadata = connect_to_postgres(logging, postgres_url) query = ''' select public.{0}.*, public.{1}.server_ip, public.{1}.client_ip, public.{1}.link_name from public.{0}, public."{1}" where "{1}".link_id = "{0}".link_id order by {0}."datetime" Desc '''.format(traffic_monitor_table_name, link_table_name) res = cnn.execute(query) results = res.fetchall() print(results) res = tuple_list_key_list_to_dict_list(results, [ "id", "send_tx_max", "send_tx_avg", "send_tx_min", "receive_rx_max", "receive_rx_avg", "receive_rx_min", "both_tx_max", "both_tx_avg", "both_tx_min", "both_rx_max", "both_rx_avg", "both_rx_min", "datetime", "link_id", "status", "error_details", "server_ip", "client_ip", "link_name" ]) return res
def test_endpoint(): from utilities.db import connect_to_postgres from rest.api import get_filtered_accounts try : engine, cnn, metadata = connect_to_postgres(logging, postgres_DB_users) data = dict(request.form) response = {'payload' : data, 'result' : True} return jsonify(response) except Exception as e: return jsonify({"result" : False, "error" : str(e)})
def insert_user(): import pandas as pd from utilities.db import connect_to_postgres try : engine, cnn, metadata = connect_to_postgres(logging, postgres_DB_users) data = dict(request.get_json(force=True)) data_df = pd.DataFrame([data]) data_df.to_sql("users", cnn, if_exists='append') response = {'payload' : data, 'result' : True} return jsonify(response) except Exception as e: return jsonify({"result" : False, "error" : str(e)})
def userlogin(): from utilities.db import connect_to_postgres from rest.api import getemails try : engine, cnn, metadata = connect_to_postgres(logging, postgres_DB_users) data = dict(request.form) #hedhi el data fha el hajt ela jet fil request # w hna idha el login shih yrjalo fil data bidha mta request chnmlo biha ehan hjtna bsh yrjalk el token w hdhi 5dma o5ra response = {'payload' : data, 'result' : True, "login" : getemails (data["userName"], data["password"])} return jsonify(response) except Exception as e: return jsonify({"result" : False, "error" : str(e)})
def func_wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: import traceback DB = os.getenv('postgres_DB', None) engine, cnn, metadata = db.connect_to_postgres(logging, DB) db.create_errors_table(cnn) db.insert_error(cnn, str(e), traceback.format_exc(), errors_table_name=errors_table_name) logs_queue.put(('error', str(e))) return 0
def getemails(userName, password): engine, cnn, metadata = connect_to_postgres(logging, postgres_DB_users) query = ''' SELECT * FROM users WHERE "userName" = %(userName)s and "password" = %(password)s ''' res = cnn.execute(query, {"userName": userName, "password": password}) users = res.fetchall() print(users) if (len(users) > 0): return True else: return False
def get_filtered_accounts(status='%', start_ip="0.0.0.0", end_ip="255.255.255.255", ip_pattern="x.x.x.x", task_id=None): import ipaddress from utilities.utils import tuple_list_key_list_to_dict_list filtered_accounts = [] engine, cnn, metadata = connect_to_postgres(logging, postgres_url) ip_pattern = ip_pattern.replace('.', '\.') ip_pattern = re.sub('x+', 'x', ip_pattern) ip_pattern = ip_pattern.replace('x', '[^\.]+?') query = ''' SELECT * FROM accounts WHERE "status" LIKE %(status)s and "IP" ~ %(ip_pattern)s ''' if (len(task_id) > 0): query = ''' SELECT * FROM accounts WHERE "status" LIKE %(status)s and "IP" ~ %(ip_pattern)s and "task_id" = %(task_id)s ''' res = cnn.execute(query, { "status": status, "ip_pattern": ip_pattern, "task_id": task_id }) accounts = res.fetchall() for account in accounts: in_range = int(ipaddress.IPv4Address(start_ip)) <= int( ipaddress.IPv4Address(account[2])) and int( ipaddress.IPv4Address(account[2])) < int( ipaddress.IPv4Address(end_ip)) print(in_range) if (in_range): filtered_accounts.append(list(account)) res = tuple_list_key_list_to_dict_list(filtered_accounts, [ "username", "password", "IP", "status", "date", "version", "task_id", "mac" ]) return res