示例#1
0
def flask_fdelete():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    file_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or not file_path:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    full_file_path = os.path.join(login, file_path)

    exists = full_file_path in db_user2files.lrange(login, 0, -1)
    if not exists:
        data = {MESSAGE_KEY: "The file for deleting does not exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.NOT_FOUND)

    for node_ip in db_file2nodes.lrange(full_file_path, 0, -1):
        res = request_node(node_ip, '/fdelete', {FULL_PATH_KEY: full_file_path})
        res = get_dict_from_response(res)
        if res is None:
            debug_log(f"Node {node_ip} did not response on /fdelete")
        db_node2files.lrem(node_ip, 0, full_file_path)

    db_user2files.lrem(login, 0, full_file_path)
    db_file2nodes.delete(full_file_path)
    db_file2size.delete(full_file_path)

    data = {MESSAGE_KEY: f"Successfully deleted the file"}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#2
0
def new_transaction():
    auth_token = request.headers['authtoken']
    request_data = request.get_json()

    user_id = decode_auth_token(auth_token)

    if user_id.isnumeric() != True:
        return jsonify({'message': user_id})

    transactions_table = get_table('transactions')

    last_transaction = get_last_transaction(transactions_table, user_id)

    amount = request_data['amount']

    if last_transaction == None:
        data = engine.execute(transactions_table.insert(),
                              u_id=user_id,
                              date_time=str(datetime.datetime.utcnow()),
                              balance=amount,
                              amount=amount)
    else:
        balance = last_transaction.balance + amount
        data = engine.execute(transactions_table.insert(),
                              u_id=user_id,
                              date_time=str(datetime.datetime.utcnow()),
                              balance=balance,
                              amount=amount)

    return jsonify({
        'message': 'Transaction successful',
        'transaction_id': data.inserted_primary_key[0]
    })
示例#3
0
def flask_fwrite():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    file_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or not file_path:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    full_file_path = os.path.join(login, file_path)

    if os.path.dirname(full_file_path) not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: "Can't write to the file. Folder doesn't exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    congestions = [(node_ip, db_congestion.get(node_ip)) for node_ip in db_congestion.keys()]
    congestions = sorted(congestions, key=lambda x: x[1])
    if not congestions:
        data = {MESSAGE_KEY: 'All nodes are down lol'}
        return flask.make_response(flask.jsonify(data), HTTPStatus.GONE)
    node_ip = congestions[0][0]

    data = {NODE_IP_KEY: db_pub.get(node_ip), FULL_PATH_KEY: os.path.join(login, file_path)}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#4
0
def flask_rdir():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    dir_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or dir_path is None:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    dir_path = dir_path[:-1] if dir_path and dir_path[-1] == '/' else dir_path
    full_dir_path = os.path.join(login, dir_path) if dir_path else login

    if full_dir_path not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: f"Can't read the folder, doesn't exist. (ERR: {full_dir_path})"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    dir_list = []

    inner_files_path_list = [full_path[len(full_dir_path) + 1:] for full_path in db_user2files.lrange(login, 0, -1) if
                             full_path.startswith(full_dir_path + '/')]
    dir_list.extend([inner_path.split('/')[0] for inner_path in inner_files_path_list
                     if len(inner_path.split('/')) == 1])

    inner_folders_path_list = [full_path[len(full_dir_path) + 1:] for full_path in db_user2folders.lrange(login, 0, -1)
                               if full_path.startswith(full_dir_path + '/')]
    dir_list.extend([inner_path.split('/')[0] + '/' for inner_path in inner_folders_path_list
                     if len(inner_path.split('/')) == 1])

    data = {DIR_LIST_KEY: dir_list}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#5
0
def flask_mdir():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    dir_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or not dir_path:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    dir_path = dir_path[:-1] if dir_path and dir_path[-1] == '/' else dir_path
    full_dir_path = os.path.join(login, dir_path) if dir_path else login

    if full_dir_path in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: f"Can't create the folder, it already exists"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    if os.path.dirname(full_dir_path) not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: f"Can't create the folder, parent folder doesn't exist. (ERR: {os.path.dirname(full_dir_path)})"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    db_user2folders.lpush(login, full_dir_path)

    data = {MESSAGE_KEY: 'Successfully created a directory'}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#6
0
def flask_ddir():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    dir_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)
    force = flask.request.form.get(key=FORCE_KEY, default=None, type=str)

    if not token or dir_path is None or force is None:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`, `{FORCE_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    dir_path = dir_path[:-1] if dir_path and dir_path[-1] == '/' else dir_path

    if not dir_path:
        data = {MESSAGE_KEY: "Can't delete the root directory"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    full_dir_path = os.path.join(login, dir_path)

    if full_dir_path not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: "The folder for deleting does not exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.NOT_FOUND)

    inner_files_path_list = [full_path for full_path in db_user2files.lrange(login, 0, -1) if
                             full_path.startswith(full_dir_path + '/')]
    inner_folders_path_list = [full_path for full_path in db_user2folders.lrange(login, 0, -1)
                               if full_path.startswith(full_dir_path + '/')]

    if force == 'False' and (inner_files_path_list or inner_folders_path_list):
        data = {MESSAGE_KEY: 'The directory contains files. `force=true` to delete'}
        return flask.make_response(flask.jsonify(data), HTTPStatus.NOT_ACCEPTABLE)

    db_user2folders.lrem(login, 0, full_dir_path)
    for inner_dir in inner_folders_path_list:
        db_user2folders.lrem(login, 0, inner_dir)

    nodes_ip = set()
    for inner_file in inner_files_path_list:
        db_file2size.delete(inner_file)
        db_user2files.lrem(login, 0, inner_file)

        for node_ip in db_file2nodes.lrange(inner_file, 0, -1):
            db_file2nodes.lrem(inner_file, 0, node_ip)
            db_node2files.lrem(node_ip, 0, inner_file)
            nodes_ip.add(node_ip)

    for node_ip in list(nodes_ip):
        res = request_node(node_ip, '/ddir', {FULL_PATH_KEY: full_dir_path})
        res = get_dict_from_response(res)
        if res is None:
            debug_log(f"Node {node_ip} did not response on /ddir ({full_dir_path})")

    data = {MESSAGE_KEY: 'Successfully deleted the directory'}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#7
0
def flask_fmove():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    file_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)
    file_destination_path = flask.request.form.get(key=PATH_DESTINATION_KEY, default=None, type=str)

    if not token or not file_path or file_destination_path is None:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`, `{PATH_DESTINATION_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    if not os.path.basename(file_destination_path):
        data = {MESSAGE_KEY: "The destination file name is empty"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    full_file_path = os.path.join(login, file_path)
    full_file_destination_path = os.path.join(login, file_destination_path)

    if full_file_path not in db_user2files.lrange(login, 0, -1):
        data = {MESSAGE_KEY: "The source file doesn't exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    if os.path.dirname(full_file_destination_path) not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: "Can't move the file. Destination folder doesn't exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    for node_ip in db_file2nodes.lrange(full_file_path, 0, -1):
        res = request_node(node_ip, '/fmove', {FULL_PATH_KEY: full_file_path,
                                               FULL_PATH_DESTINATION_KEY: full_file_destination_path})
        res = get_dict_from_response(res)
        if res is None:
            debug_log(f"Node {node_ip} did not response on /fmove")
        else:
            db_node2files.lrem(node_ip, 0, full_file_path)
            db_node2files.lpush(node_ip, full_file_destination_path)

            db_file2nodes.lrem(full_file_path, 0, node_ip)
            db_file2nodes.lpush(full_file_destination_path, node_ip)

    db_user2files.lrem(login, 0, full_file_path)
    db_user2files.lpush(login, full_file_destination_path)

    db_file2size.set(full_file_destination_path, db_file2size.get(full_file_path))
    db_file2size.delete(full_file_path)

    data = {MESSAGE_KEY: f"Successfully moved the file"}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#8
0
def flask_init():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)

    if not token:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    was_initialised_before = init(login)

    data = {MESSAGE_KEY: f"Successfully {'removed all data and re' if was_initialised_before else ''}initialized"}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#9
0
def fetch_balance():
    auth_token = request.headers['authtoken']
    request_data = request.get_json()
    user_id = decode_auth_token(auth_token)

    if user_id.isnumeric() != True:
        return jsonify({'message': user_id})

    transactions_table = get_table('transactions')

    last_transaction = get_last_transaction(transactions_table, user_id)

    if last_transaction != None:
        return jsonify({'balance': last_transaction.balance})

    return jsonify({'balance': 0})
示例#10
0
def transaction_history():
    auth_token = request.headers['authtoken']
    user_id = decode_auth_token(auth_token)

    if user_id.isnumeric() != True:
        return jsonify({'message': user_id})

    request_data = request.get_json()

    transactions_table = get_table('transactions')

    transactions_data = get_all_transactions_in_desc_order(
        transactions_table, user_id)

    if transactions_data == None:
        return jsonify({'message': 'No records found'})

    return jsonify({'history': transactions_data})
示例#11
0
def dashboardReportUser():
    month = request.args.get('month')
    year = request.args.get('year')
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        auth_token = ''
    response = jsonify({'message': 'Provide a valid auth token.'}), 401
    if auth_token:
        secret_key = os.environ.get('SECRET_KEY', '')
        user = decode_auth_token(secret_key, auth_token)
        data = {}
        today = dt.datetime.today().date()
        if user != 401:
            if year is None:
                year = today.year
            if month:
                start = dt.datetime(year=int(year), month=int(month),
                                    day=1).date()
            else:
                start = dt.datetime(year=int(year), month=today.month,
                                    day=1).date()
            end = last_day_of_month(start)

            totalOfficeHourUserYear = countOfficeHourUserYear(
                mongoClient, user['user_id'], today.year)
            totalOfficeHourUserMonthly = countOfficeHourUserMonthly(
                mongoClient, user['user_id'], str(start), str(end))
            totalReportUserYear = countReportUserYear(mongoClient,
                                                      user['user_id'],
                                                      today.year)
            totalReportUserMonthly = countReportUserMonthly(
                mongoClient, user['user_id'], str(start), str(end))
            array_report_user = arrayReportUser(totalReportUserYear,
                                                totalReportUserMonthly)
            array_office_hour_user = arrayOfficeHourUser(
                totalOfficeHourUserYear, totalOfficeHourUserMonthly)
            data.update(array_report_user)
            data.update(array_office_hour_user)

        response = jsonify(message="success", data=data, status=200)

    return response
示例#12
0
def flask_dir_exists():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    dir_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or dir_path is None:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login:
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    dir_path = dir_path[:-1] if dir_path and dir_path[-1] == '/' else dir_path
    full_dir_path = os.path.join(login, dir_path) if dir_path else login

    if full_dir_path not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: "The folder doesn't exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    data = {MESSAGE_KEY: 'It exists!'}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#13
0
def flask_finfo():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    file_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or not file_path:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    full_file_path = os.path.join(login, file_path)

    if full_file_path not in db_user2files.lrange(login, 0, -1):
        data = {MESSAGE_KEY: f"The file doesn't exist. (ERR: {full_file_path})"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    data = {NODE_IP_KEY: db_file2nodes.lrange(full_file_path, 0, -1),
            FILE_SIZE_KEY: db_file2size.get(full_file_path)}

    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#14
0
def flask_fcreate():
    token = flask.request.form.get(key=TOKEN_KEY, default=None, type=str)
    file_path = flask.request.form.get(key=PATH_KEY, default=None, type=str)

    if not token or not file_path:
        data = {MESSAGE_KEY: f"Missing required parameters: `{TOKEN_KEY}`, `{PATH_KEY}`"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.UNPROCESSABLE_ENTITY)

    login = decode_auth_token(token)
    if not login or (type(login) == str and login not in db_auth.keys()):
        data = {MESSAGE_KEY: "The token is invalid or has expired"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    full_file_path = os.path.join(login, file_path)

    if os.path.dirname(full_file_path) not in db_user2folders.lrange(login, 0, -1):
        data = {MESSAGE_KEY: "Can't create the file. Folder doesn't exist"}
        return flask.make_response(flask.jsonify(data), HTTPStatus.FORBIDDEN)

    congestions = [(node_ip, db_congestion.get(node_ip)) for node_ip in db_congestion.keys()]
    congestions = sorted(congestions, key=lambda x: x[1])
    nodes = [congestion[0] for congestion in congestions[:REPLICATION_FACTOR]]
    debug_log(f"/fcreate - nodes with lowest congestion: {nodes}")

    data_for_node = {FULL_PATH_KEY: full_file_path}
    for node_ip in nodes:
        res = request_node(node_ip, '/fcreate', data_for_node)
        res = get_dict_from_response(res)
        debug_log(f"/fcreate - storage node {node_ip} response: {res}")
        db_node2files.lpush(node_ip, full_file_path)

    db_user2files.lpush(login, full_file_path)
    db_file2nodes.lpush(full_file_path, *nodes)
    db_file2size.set(full_file_path, 0)

    data = {MESSAGE_KEY: f"Successfully created the file"}
    return flask.make_response(flask.jsonify(data), HTTPStatus.OK)
示例#15
0
def transaction_statement():
    auth_token = request.headers['authtoken']
    user_id = decode_auth_token(auth_token)

    if user_id.isnumeric() != True:
        return jsonify({'message': user_id})

    request_data = request.get_json()

    transactions_table = get_table('transactions')

    transactions_data = get_all_transactions_in_desc_order(
        transactions_table, user_id)

    if len(transactions_data) == 0:
        return jsonify({'message': 'No records found'})

    users_table = get_table('users')

    user = session.query(users_table).filter_by(id=user_id).first()

    send_statement_to_user(user, transactions_data)

    return jsonify({'message': 'Please check your mail for the statement'})
示例#16
0
def get_purchased_cars(jwt):
    user_id = decode_auth_token(jwt)
    return jsonify(get_purchases(user_id))
示例#17
0
def dashboardAttendanceUser():
    month = request.args.get('month')
    year = request.args.get('year')
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        auth_token = ''
    response = jsonify({'message': 'Provide a valid auth token.'}), 401
    if auth_token:
        secret_key = os.environ.get('SECRET_KEY', '')
        user = decode_auth_token(secret_key, auth_token)
        data = {}
        if user != 401:
            today = dt.datetime.today().date()
            if year is None:
                year = today.year
            if month:
                start = dt.datetime(year=int(year), month=int(month),
                                    day=1).date()
            else:
                month = today.month
                start = dt.datetime(year=int(year), month=int(month),
                                    day=1).date()
            end = last_day_of_month(start)
            dateRange = np.arange(np.datetime64(start),
                                  np.datetime64(end + timedelta(days=1)),
                                  dtype='datetime64[D]')
            listBusday = np.busdaycalendar(holidays=dateRange,
                                           weekmask=busmask_names)
            listWeekend = np.busdaycalendar(holidays=dateRange,
                                            weekmask=weekmask_names)
            listHoliday = getListHoliday(mongoClient, np, start.year,
                                         start.month)

            get_data_redis = redis_client.get(
                keys_redis(user['user_id'], 'attendances'))

            parse_query_date = dt.datetime.strptime(
                str(year) + '-' + str(month), '%Y-%m').date()

            if get_data_redis and parse_query_date < dt.datetime.today(
            ).replace(day=1).date():
                start_date = dt.datetime.strptime(
                    str(start) + ' 00:00:00', '%Y-%m-%d %H:%M:%S')
                end_date = dt.datetime.strptime(
                    str(end) + ' 23:59:59', '%Y-%m-%d %H:%M:%S')
                list_presence = [
                    data for data in json.loads(get_data_redis) if
                    start_date <= parse_datetime(data['startDate']) <= end_date
                ]
            else:
                list_presence = getPresence(mongoClient, user['user_id'],
                                            str(start), str(end))

            # Delete working days if there are holidays
            listBusday = np.array(
                list(
                    filter(lambda x: x not in listHoliday,
                           listBusday.holidays)))

            latePresence = len([
                data for data in list_presence if parse_datetime(
                    data['startDate']).date() not in listWeekend.holidays and
                max_time_presence < parse_datetime(data['startDate']).time()
            ])
            permit = len([
                data for data in list_presence
                if data['message'] in ['CUTI', 'SAKIT', 'IZIN']
            ])
            totalWfh = len(
                [data for data in list_presence if data['location'] == 'WFH'])
            totalWfo = len(
                [data for data in list_presence if data['location'] == 'WFO'])
            totalPerjadin = len([
                data for data in list_presence
                if data['location'] == 'PERJADIN'
            ])
            presence = len([
                data for data in list_presence
                if parse_datetime(data['startDate']).date() in listBusday
            ])
            presenceWeekend = len([
                data for data in list_presence if parse_datetime(
                    data['startDate']).date() in listWeekend.holidays
            ])

            busDays = len(listBusday)
            weekEnd = len(listWeekend.holidays)
            #check no precene from today
            if parse_query_date >= dt.datetime.today().replace(day=1).date():
                dateRangeFromNow = np.arange(np.datetime64(start),
                                             np.datetime64(today +
                                                           timedelta(days=1)),
                                             dtype='datetime64[D]')
                listBusdayFromNow = np.busdaycalendar(
                    holidays=dateRangeFromNow, weekmask=busmask_names)
                listBusdayFromNow = np.array(
                    list(
                        filter(lambda x: x not in listHoliday,
                               listBusdayFromNow.holidays)))
                noPresence = len(listBusdayFromNow) - presence
            else:
                noPresence = busDays - presence

            precentagePresence = round(
                float(presence - permit) / float(busDays) * 100, 2)
            precentageLatePresence = round(
                float(latePresence) / float(busDays) * 100, 2)
            precentagePermit = round(float(permit) / float(busDays) * 100, 2)
            precentageNoPresence = round(
                float(noPresence) / float(busDays) * 100, 2)
            precentagePresenceWeekend = round(
                float(presenceWeekend) / float(weekEnd) * 100, 2)
            precentageWfo = round(float(totalWfo) / float(busDays) * 100, 2)
            precentageWfh = round(float(totalWfh) / float(busDays) * 100, 2)
            precentagePerjadin = round(
                float(totalPerjadin) / float(busDays) * 100, 2)
            array_presence = arrayPresence(noPresence, precentageNoPresence,
                                           presence - permit,
                                           precentagePresence, presenceWeekend,
                                           precentagePresenceWeekend,
                                           latePresence,
                                           precentageLatePresence)
            array_permit = arrayPermit(permit, precentagePermit)
            array_location_user = arrayLocationUser(totalWfo, precentageWfo,
                                                    totalWfh, precentageWfh,
                                                    totalPerjadin,
                                                    precentagePerjadin)

            data.update(array_presence)
            data.update(array_permit)
            data.update(array_location_user)

        response = jsonify(message="success", data=data, status=200)

    return response
示例#18
0
    def run(self):
        self.slotsStartWatch.start()
        self.slotsEndWatch.start()

        while not self.stopped_thread():
            self.check_timeouts()
            new_timeout = self.get_next_timeout()
            readable, writable, exceptional = select.select(
                self.inputs, [], self.inputs, new_timeout)

            for s in readable:
                if s is self.sock_tcp:
                    (connection, address) = self.sock_tcp.accept()
                    connection.setblocking(False)
                    self.logger.info('TCP CON (%s, %d)' %
                                     (address[0], address[1]))
                    self.inputs.append(connection)
                else:
                    buffer = utils.read_data_from_socket(s)
                    if buffer:
                        self.logger.debug(
                            'Req_data = %s\t client = (%s, %d)' %
                            (buffer, s.getpeername()[0], s.getpeername()[1]))
                        action, data = utils.segment_packet(buffer)

                        if action == utils.GATEWAY_NODES_FLASH:
                            image_name = self.get_image_name(s)
                            result = []
                            for node in data[db_utils.NODES]:
                                if node['status'] == utils.FLASHED:
                                    gateway_id = self.dbConnector.find_gateway_by_addr(
                                        s.getpeername())
                                    node_uid = self.dbConnector.get_node_uid_by_gateway_id_and_node_id(
                                        gateway_id, node['node_id'])
                                    self.dbConnector.node_update_flash_info(
                                        node_uid, 'FINISHED', image_name)
                                    _node = {
                                        '_id': node_uid,
                                        'status': node['status']
                                    }
                                else:  # node['status'] = utils.ERROR
                                    gateway_id = self.dbConnector.find_gateway_by_addr(
                                        s.getpeername())
                                    node_uid = self.dbConnector.get_node_uid_by_gateway_id_and_node_id(
                                        gateway_id, node['node_id'])
                                    _node = {
                                        '_id': node_uid,
                                        'status': node['status']
                                    }
                                result.append(_node)

                            self.handle_gateway_request(s, result)
                            self.inputs.remove(s)
                            s.close()

                        elif action == utils.GATEWAY_NODES_ERASE:
                            result = []
                            for node in data[db_utils.NODES]:
                                if node['status'] == utils.ERASED:
                                    gateway_id = self.dbConnector.find_gateway_by_addr(
                                        s.getpeername())
                                    node_uid = self.dbConnector.get_node_uid_by_gateway_id_and_node_id(
                                        gateway_id, node['node_id'])
                                    self.dbConnector.node_update_flash_info(
                                        node_uid, 'NOT_STARTED', None)
                                    _node = {
                                        '_id': node_uid,
                                        'status': node['status']
                                    }
                                else:  # node['status'] = utils.ERROR
                                    gateway_id = self.dbConnector.find_gateway_by_addr(
                                        s.getpeername())
                                    node_uid = self.dbConnector.get_node_uid_by_gateway_id_and_node_id(
                                        gateway_id, node['node_id'])
                                    _node = {
                                        '_id': node_uid,
                                        'status': node['status']
                                    }
                                result.append(_node)

                            self.handle_gateway_request(s, result)
                            self.inputs.remove(s)
                            s.close()

                        elif action == utils.GATEWAY_NODES_RESET:
                            result = []
                            for node in data[db_utils.NODES]:
                                gateway_id = self.dbConnector.find_gateway_by_addr(
                                    s.getpeername())
                                node_uid = self.dbConnector.get_node_uid_by_gateway_id_and_node_id(
                                    gateway_id, node['node_id'])
                                _node = {
                                    '_id': node_uid,
                                    'status': node['status']
                                }
                                result.append(_node)

                            self.handle_gateway_request(s, result)
                            self.inputs.remove(s)
                            s.close()

                        elif action == utils.IMAGES_GET:  # { token }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                nodetypes_images = utils.get_nodetypes_images(
                                    decoded_token[db_utils.USER])
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'data': nodetypes_images,
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { data, status: 200 }
                                s.sendall(pck)

                        elif action == utils.IMAGE_SAVE:  # { token, image_name, image_data, nodetype_id }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                utils.save_image(decoded_token[db_utils.USER],
                                                 data[db_utils.NODETYPE_ID],
                                                 data[db_utils.IMAGE_NAME],
                                                 data[db_utils.IMAGE_DATA])
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { status: 200 }
                                s.sendall(pck)

                        elif action == utils.IMAGE_DELETE:  # { token, image_name }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                nodetype_id = utils.get_nodetype_by_user_and_image_name(
                                    decoded_token[db_utils.USER],
                                    data[db_utils.IMAGE_NAME])
                                res = utils.delete_image(
                                    decoded_token[db_utils.USER],
                                    data[db_utils.IMAGE_NAME], nodetype_id)
                                pck = utils.create_response_packet(
                                    json.dumps(res).encode())

                                # OnSuccess: { status: 204 }
                                # OnError  : { status: 404 }
                                s.sendall(pck)

                        elif action == utils.NODES_GET:  # { token }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                nodes = self.dbConnector.get_nodes()
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'nodes': nodes,
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { nodes, status: 200 }
                                s.sendall(pck)

                        elif action == utils.NODES_FLASH:  # { token, slot_id, image_name, node_uids}
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                gateway_socks = self.send_flash_request(
                                    decoded_token[db_utils.USER],
                                    data[db_utils.IMAGE_NAME],
                                    data[db_utils.NODE_UIDS])
                                self.inputs.extend(gateway_socks)
                                self.gateway_sockets.append(gateway_socks)
                                self.gateway_sockets_info.append({
                                    'web_socket':
                                    s,
                                    'data': [],
                                    db_utils.IMAGE_NAME:
                                    data[db_utils.IMAGE_NAME]
                                })

                        elif action == utils.NODES_ERASE:  # { token, slot_id, node_uids }
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                gateway_socks = self.send_erase_request(
                                    data[db_utils.NODE_UIDS])
                                self.inputs.extend(gateway_socks)
                                self.gateway_sockets.append(gateway_socks)
                                self.gateway_sockets_info.append({
                                    'web_socket':
                                    s,
                                    'data': [],
                                    db_utils.IMAGE_NAME:
                                    ''
                                })

                        elif action == utils.NODES_RESET:  # { token, slot_id, node_uids }
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                gateway_socks = self.send_reset_request(
                                    data[db_utils.NODE_UIDS])
                                self.inputs.extend(gateway_socks)
                                self.gateway_sockets.append(gateway_socks)
                                self.gateway_sockets_info.append({
                                    'web_socket':
                                    s,
                                    'data': [],
                                    db_utils.IMAGE_NAME:
                                    ''
                                })

                        elif action == utils.TIMESLOTS_SAVE:  # { token, slots: [{start, end}] }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                slots = db_utils.convert_isoformat_to_datetime(
                                    data[db_utils.SLOTS])
                                slots_saved = self.dbConnector.save_timeslots(
                                    decoded_token[db_utils.USER], slots)
                                slots = db_utils.convert_datetime_to_isoformat(
                                    slots_saved)
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'slots': slots,
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { slots: [{start, end}], status: 200 }
                                s.sendall(pck)

                        elif action == utils.TIMESLOTS_GET_DAYSLOTS:  # { token, date }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                slots_day = self.dbConnector.get_day_slots(
                                    data[db_utils.DATE])
                                slots = db_utils.convert_datetime_to_isoformat(
                                    slots_day)
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'slots': slots,
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { slots: [{start, end, user_id}], status: 200 }
                                s.sendall(pck)

                        elif action == utils.TIMESLOTS_GET_USERSLOTS:  # { token }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                user_slots = self.dbConnector.get_user_slots(
                                    decoded_token[db_utils.USER])
                                slots = db_utils.convert_datetime_to_isoformat(
                                    user_slots)
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'slots': slots,
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { slots: [{slot_id, start, end}], status: 200 }
                                s.sendall(pck)

                        elif action == utils.NODETYPES_GET:  # { token }
                            token = data[db_utils.TOKEN]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            else:
                                nodetypes = self.dbConnector.get_nodetypes()
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'nodetypes': nodetypes,
                                        'status': 200
                                    }).encode())

                                # OnSuccess: { nodetypes, status: 201 }
                                s.sendall(pck)

                        elif action == utils.USERS_SIGNUP:  # { email, username, password }
                            res = self.dbConnector.create_user(data)
                            pck = utils.create_response_packet(
                                json.dumps(res).encode())

                            # OnSuccess: { status: 201 }
                            # OnError  : { message, status: 403 }
                            s.sendall(pck)

                        elif action == utils.USERS_LOGIN:  # { email, username }
                            res = self.dbConnector.login_user(data)
                            pck = utils.create_response_packet(
                                json.dumps(res).encode())

                            # OnSuccess: { token, status: 200}
                            # OnError  : { message, status: 401 }
                            s.sendall(pck)

                        elif action == utils.DEBUG_START:  # { token, slot_id }
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                if self.sock_debug:
                                    utils.remove_from_list(
                                        self.inputs, self.sock_debug)
                                    self.sock_debug.close()
                                log_data = [
                                    '=== DEBUG CHANNEL START ===\n===========================\n'
                                ]
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'data': log_data
                                    }).encode())
                                self.sock_debug = s
                                self.sock_debug.sendall(pck)

                        elif action == utils.DEBUG_END:  # { token, slot_id }
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                log_data = [
                                    '=== DEBUG CHANNEL END ===\n=========================\n'
                                ]
                                if self.sock_debug:
                                    self.experiment_info = {}
                                    pck = utils.create_response_packet(
                                        json.dumps({
                                            'data': log_data,
                                            'message': 'STOP DEBUG'
                                        }).encode())
                                    self.sock_debug.sendall(pck)

                                    utils.remove_from_list(
                                        self.inputs, self.sock_debug)
                                    self.sock_debug.close()
                                    self.sock_debug = None

                                    # { status: 204 }
                                    pck = utils.create_response_packet(
                                        json.dumps({
                                            'status': 204
                                        }).encode())
                                    s.sendall(pck)

                        elif action == utils.DEBUG_CLEAR_LOG:  # { token, slot_id }
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                self.clear_debug_log(
                                    decoded_token[db_utils.USER], slot_id)
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'status': 204
                                    }).encode())

                                # OnSuccess: { status: 204 }
                                s.sendall(pck)

                        elif action == utils.DEBUG_GET_LOG:  # { token, slot_id }
                            token = data[db_utils.TOKEN]
                            slot_id = data[db_utils.TIMESLOT_ID]
                            decoded_token = utils.decode_auth_token(token)
                            if not decoded_token:
                                utils.send_invalid_token_error(s)
                            elif not self.dbConnector.get_slot_by_id(slot_id):
                                utils.send_invalid_slot_error(s)
                            else:
                                self.send_debug_log(
                                    s, decoded_token[db_utils.USER], slot_id)

                        elif action == utils.DEBUG_GATEWAY:  # [ TIMESTAMP, NODE_ID, DATA ]
                            print(data[0], '|', data[1], '|', data[2])
                            if self.experiment_info:
                                self.write_debug_log(data)
                            if self.sock_debug:
                                pck = utils.create_response_packet(
                                    json.dumps({
                                        'data': data
                                    }).encode())
                                self.sock_debug.sendall(pck)

                    else:
                        self.logger.info(
                            'TCP DISCON (%s, %d)' %
                            (s.getpeername()[0], s.getpeername()[1]))
                        self.inputs.remove(s)
                        s.close()

            for s in exceptional:
                self.inputs.remove(s)
                s.close()

        for sock in self.inputs:
            self.logger.debug('Exiting. . . Closing [%s]' % sock)
            sock.close()
        self.log_timer.cancel()
        self.sock_tcp.close()
        sys.exit('Exiting. . .')