def __calculate_user_responsibility(user_id: int) -> int: ''' Расчитывает показатель ответственности человека :param user_id: идентификатор пользователя :type user_id: int :return: показатель ответственности человека :rtype: int ''' # !Settings # max overwork hours per day (setting) MAX_OVERWORK = 2 # get user plan percent user_plan_percent = Database.query_row( 'SELECT "PlanPercent" from "UserPlanPercent" WHERE "UserID" = %s LIMIT 1', (user_id, )) if user_plan_percent: user_plan_percent = user_plan_percent["PlanPercent"] # get user overwork user_overwork = Database.query_row( """ SELECT sum("Overwork") AS "TotalOverwork", count("UserID") as "TotalDays" FROM "UserOverwork" WHERE "UserID" = %s group by "UserID" """, (user_id, )) # if can't find overwork and plan info return unknown value if not user_overwork and not user_plan_percent: return -1 # if find only user plan info return plan percent elif user_plan_percent and not user_overwork: return int(user_plan_percent / 10) # if find user overwork info elif user_overwork: # calculate overwork percents total_max_overwork = timedelta(hours=MAX_OVERWORK * user_overwork["TotalDays"]) overwork_percents = (user_overwork['TotalOverwork'].total_seconds() / total_max_overwork.total_seconds()) * 100 overwork_percents = 100 if overwork_percents > 100 else overwork_percents if user_plan_percent: return int((overwork_percents + user_plan_percent) / 20) return int(overwork_percents / 10)
def __calculate_user_sociability(user_id: int, total_days: int) -> int: """ Расчитывает показатель комуникабельности человека :param user_id: идентификатор пользователя :type user_id: int :param total_days: кол-во дней по которым собрана статистика на пользователя :type total_days: int :return: аоказатель коммуникабельности :rtype: int """ # !Settings # max user communication minutes per day (setting) MAX_COMMUNICATION = 45 user_communication = Database.query_row( """ select sum("WastedTime") as "WastedTime" from "UserActivity" where "UserID" = %s and "Category" in ('Обмен сообщениями (IM, Почта)', 'Звонки СБИС') """, (user_id, )) if user_communication["WastedTime"]: max_user_communication = timedelta(minutes=MAX_COMMUNICATION * total_days) result = int( round(user_communication['WastedTime'].total_seconds() / max_user_communication.total_seconds() * 10)) return 10 if result > 10 else result else: return -1
def __calculate_user_procrastination(user_id: int, total_days: int) -> int: ''' Расчитывает показатель прокрастенации пользователя :param user_id: идентификатор пользователя :type user_id: int :param total_days: кол-во дней по которым собрана статистика на пользователя :type total_days: int :return: [показатель прокрастенации пользователя :rtype: int ''' # !Settings # max user procrastination minutes per day (setting) MAX_PROCRASTINATION = 45 user_procrastination = Database.query_row( """ select sum("WastedTime") as "WastedTime" from "UserActivity" where "UserID" = %s and "Useful" = -1 """, (user_id, )) if user_procrastination['WastedTime']: total_procrastenation = timedelta(minutes=MAX_PROCRASTINATION * total_days) result = int( round(user_procrastination['WastedTime'].total_seconds() / total_procrastenation.total_seconds() * 10)) return 10 if result > 10 else result else: return 0
def __calculate_user_leaving(user_id: int) -> bool: ''' Расчитывает флаг часто ли пользователь уходит из офиса :param user_id: идентификатор пользователя :type user_id: int :return: флаг частого покидания офиса :rtype: bool ''' # !Settings # max user leavings per day (setting) MAX_LEAVING = 3 user_leavings = Database.query_row( """ select avg("LeavingCount") as "AvgLeavings" from ( select "DateTime"::date as "Date" , count("Status") as "LeavingCount" from "UserLocation" where "UserID" = %s and "Status" = 0 group by "Date" ) as "LeavingPerDay" """, (user_id, )) if user_leavings['AvgLeavings']: return True if int( user_leavings["AvgLeavings"]) > MAX_LEAVING else False else: return None
def get_user_info(user_id: int, sid: str = None) -> dict: mined_users = [ row["UserID"] for row in Database.query('SELECT * FROM "MinedUsers"') ] if user_id not in mined_users: mine_user_info(user_id, sid) # get user days count in dataset (except weekends) total_days = Database.query_row( 'select "TotalDays" from "MinedUsers" where "UserID" = %s', (user_id, ))['TotalDays'] result = {} result['user_responsibility'] = __calculate_user_responsibility(user_id) result['user_sociability'] = __calculate_user_sociability( user_id, total_days) result['user_procrastination'] = __calculate_user_procrastination( user_id, total_days) result['user_often_leaving'] = __calculate_user_leaving(user_id) result['user_punctuality'] = __calculate_user_punctuality(user_id) result['user_leaving_state'] = __calculate_user_leaving_state(user_id) return result
def __calculate_user_leaving_state(user_id: int) -> int: try: user_neural_data = Database.query_row( """ select "UserFirstCalls", "UserLastCalls", "UserFirstDuration", "UserLastDuration", "UserFirstOverwork", "UserLastOverwork" from "UsersNeuralData" where "UserID" = %s """, (user_id, )) XpredictInputData = numpy.array([list(user_neural_data)]) print(XpredictInputData) with NeuralNetwork.graph.as_default(): return int( round( NeuralNetwork.model.predict(XpredictInputData)[0][0] * 10)) except BaseException as exc: print(exc) return -1