def build_logout_response(user_obj):
    '''
	Builds the logout response for the specified user.

	Args:
		user_obj: The object representation for the specified user

	Returns:
		The Python dictionary that can be converted to JSON and sent as a response.
	'''

    # create the attachment
    contents = {}
    contents['title'] = COMPANY_NAME
    contents['title_link'] = COMPANY_URL
    contents['color'] = "danger"

    # Determine the text of the attachment
    contents[
        'text'] = 'You are now logged out of Epoch, changing your state to OFFLINE.'

    # build the hours
    fields = []
    f1 = {}
    f1['title'] = 'Total Hours (month)'
    f1['value'] = '%.2f' % (user_session.get_total_worked(user_obj.uuid))
    f1['short'] = True
    f2 = {}
    f2['title'] = 'Goal Hours (month)'
    f2['value'] = user.get_goal_total(user_obj.uuid)
    f2['short'] = True
    fields.append(f1)
    fields.append(f2)
    contents['fields'] = fields

    # attach the footer
    _attach_footer(contents)

    # construct the response message with the attachment
    message = slack_api.Message()
    message.add_attachment(contents)
    return message.get_contents()
Beispiel #2
0
def handle_list_command():
    '''
	Handles the parsing of the list command. This allows the user to see what 
	users we are tracking.
	'''

    # USER --- STATE --- LAST LOGIN --- HOURS WORKED --- GOAL HOURS
    all_users = user.get_all_users()
    if all_users is not None and len(all_users) > 0:
        for uuid, name in all_users:
            state = user_session.get_state(uuid)
            last_login = user_session.get_session_timestamp(uuid)
            total_hours = '%.2f' % user_session.get_total_worked(uuid)
            goal_hours = user.get_goal_total(uuid)

            data = (name, state, last_login, total_hours, goal_hours)

            print('[' + str(name) + '][' + str(state) + '] was last seen at ' +
                  str(last_login) + '. They are at ' + str(total_hours) +
                  ' / ' + str(goal_hours) + ' hours.')
Beispiel #3
0
def handle_report_command():
    '''
	Handles the parsing of the report command. This allows the user to print
	session information about a user and get detailed information about them.
	'''
    req_name = raw_input(
        'Name of the user to generate the report for (Ex: stephen)?: ')
    if req_name is None:
        print('You must provide the name of the user!')
        return None

    user_data = user.get_user(req_name)
    if user_data is None:
        print('Unable to find ' + str(req_name) + '. Are you sure they exist?')
        return None

    start_date = raw_input(
        'The starting date to query for the user (Ex: YYYY-MM-DD)?: ')
    if start_date is None:
        print('You must provide a valid start date, such as 2016-11-16')
        return None

    end_date = raw_input(
        'The ending date to query for the user (Ex: YYYY-MM-DD)?: ')
    if end_date is None:
        print('You must provide a valid end date, such as 2016-11-16')
        return None

    # attempt to format their input
    try:
        s_year, s_month, s_day = _format_date(start_date)
        e_year, e_month, e_day = _format_date(end_date)
    except Exception as e:
        print(e)
        print(
            'Unable to convert dates to representation... Are you sure they are entered correctly?'
        )
        return None

    # convert to timestamp that SQL knows
    start_date = datetime.datetime(s_year, s_month, s_day, 0, 0,
                                   0).strftime('%Y-%m-%d %H:%M:%S')
    end_date = datetime.datetime(e_year, e_month, e_day, 0, 0,
                                 0).strftime('%Y-%m-%d %H:%M:%S')

    # get all their session logs
    session_info = user_session.get_session_logs(user_data[0], start_date,
                                                 end_date)

    if session_info is not None and len(session_info) > 0:

        # sort the logs to verified/not
        not_verified = []
        verified = []
        for log_id, user_id, work_time, start, end, approved in session_info:
            if approved == 'None':
                not_verified.append(
                    (log_id, user_id, work_time, start, end, approved))
            else:
                verified.append(
                    (log_id, user_id, work_time, start, end, approved))

        # determine the time that was verified
        verified_time = 0
        for log_id, user_id, work_time, start, end, approved in verified:
            verified_time = verified_time + work_time

        # determine the time that was not verified
        not_verified_time = 0
        for log_id, user_id, work_time, start, end, approved in not_verified:
            not_verified_time = not_verified_time + work_time

        print('\nUser report for ' + str(req_name) + ' between ' +
              str(start_date) + ' and ' + str(end_date) + ': \n')

        verified_hours = '%.2f' % (verified_time / 3600000.0)
        not_verified_hours = '%.2f' % (not_verified_time / 3600000.0)
        print(
            str(len(not_verified)) +
            ' transactions were NOT VERIFIED totalling ' +
            str(not_verified_hours) + ' hours.\n')
        print(
            str(len(verified)) + ' transactions were VERIFIED totalling ' +
            str(verified_hours) + ' hours.\n')

        # determine if they've reached their goal
        goal_hours = user.get_goal_total(user_data[0])
        reached_goal = False
        failed_by_hours = goal_hours - float(verified_hours)
        if failed_by_hours <= 0:
            reached_goal = True

        print('The monthly goal hours for ' + str(req_name) + ' is ' +
              str(goal_hours))

        if reached_goal:
            print(str(req_name) + ' has reached their monthly goal!')
        else:
            print(
                str(req_name) + ' was ' + str(failed_by_hours) +
                ' hours short of their goal!')

        send_report = raw_input(
            'Would you like to me to send this report to the user [Yes/No]?: ')
        if send_report is not None and (send_report.lower() == 'yes'
                                        or send_report.lower() == 'y'):
            send_user_report(user_data, start_date, end_date, verified_hours,
                             not_verified_hours, goal_hours)

    else:
        print('No found session information for ' + str(req_name) +
              ' in the time period of ' + str(start_date) + ' and ' +
              str(end_date))
def build_status_response(user_obj):
    '''
	Builds the status response for the specified user.

	Args:
		user_obj: The object representation for the specified user

	Returns:
		The Python dictionary that can be converted to JSON and sent as a response.
	'''

    # USER --- STATE --- LAST LOGIN --- HOURS WORKED --- GOAL HOURS
    all_users = user.get_all_users()
    if all_users is not None and len(all_users) > 0:

        # construct the response message with the attachment
        message = slack_api.Message()

        for uuid, name in all_users:
            state = user_session.get_state(uuid)
            last_login = user_session.get_session_timestamp(uuid)
            total_hours = '%.2f' % user_session.get_total_worked(uuid)
            goal_hours = user.get_goal_total(uuid)

            data = (name, state, last_login, total_hours, goal_hours)

            # create the attachment
            contents = {}
            contents['title'] = str(name)

            # TODO maybe a link to our workers page?
            contents['title_link'] = COMPANY_URL

            # color the attachment based off of state
            if state == 'ONLINE':
                contents['color'] = "good"
            elif state == 'OFFLINE':
                contents['color'] = "danger"
            elif state == 'PAUSED':
                contents['color'] = "warning"

            # Determine the text of the attachment
            #contents['text'] = 'Last state change at ' + str(last_login) + ' EST.'

            # build the hours
            fields = []
            f1 = {}
            f1['title'] = 'Total Hours (month)'
            f1['value'] = total_hours
            f1['short'] = True
            f2 = {}
            f2['title'] = 'Goal Hours (month)'
            f2['value'] = goal_hours
            f2['short'] = True
            fields.append(f1)
            fields.append(f2)
            contents['fields'] = fields

            if type(contents) is dict:

                # general footer
                contents['footer'] = 'Epoch API'
                contents['footer_icon'] = ICON_URL

                if last_login is not None:

                    # attach the timestamp
                    contents['ts'] = int(last_login.strftime('%s'))

            # add the attachment
            message.add_attachment(contents)

        return message.get_contents()
    else:
        return Response(
            'Unexpected error occurred locally when parsing STATE request.'
        ), 200
def build_info_response(user_obj):
    '''
	Builds the info response for the specified user.

	Args:
		user_obj: The object representation for the specified user

	Returns:
		The Python dictionary that can be converted to JSON and sent as a response.
	'''
    # get the state of the user
    state = user_session.get_state(user_obj.uuid)

    # create the attachment
    contents = {}
    contents['title'] = COMPANY_NAME
    contents['title_link'] = COMPANY_URL

    # color the attachment based off of state
    if state == 'ONLINE':
        contents['color'] = "good"
    elif state == 'OFFLINE':
        contents['color'] = "danger"
    elif state == 'PAUSED':
        contents['color'] = "warning"

    # Determine the text of the attachment
    contents['text'] = 'Your current state is ' + str(state) + '.'

    if state == 'ONLINE' or state == 'PAUSED':

        work_time = user_session.get_work_time(user_obj.uuid)

        # build the hours
        fields = []
        f1 = {}
        f1['title'] = 'Worked Time (session)'
        f1['value'] = '%.2f' % (work_time / 3600000.0)
        f1['short'] = True
        f2 = {}
        f2['title'] = 'Total Hours (month)'
        f2['value'] = user_session.get_total_worked(user_obj.uuid)
        f2['short'] = True
        fields.append(f1)
        fields.append(f2)
        contents['fields'] = fields
    elif state == 'OFFLINE':
        # build the hours
        fields = []
        f1 = {}
        f1['title'] = 'Total Hours (month)'
        f1['value'] = user_session.get_total_worked(user_obj.uuid)
        f1['short'] = True
        f2 = {}
        f2['title'] = 'Goal Hours (month)'
        f2['value'] = user.get_goal_total(user_obj.uuid)
        f2['short'] = True
        fields.append(f1)
        fields.append(f2)
        contents['fields'] = fields

    # attach the footer
    _attach_footer(contents)

    # construct the response message with the attachment
    message = slack_api.Message()
    message.add_attachment(contents)
    return message.get_contents()