def get_cost_center_list():
    cost_center_list = []

    if request.method == 'GET':
        unique = request.args.get('unique', False)

        try:
            if unique:
                cost_center_list = get_center_list(unique)
            else:
                cost_center_list = get_center_list(False)

            status = 200
            response = {
                'cost_center_list': cost_center_list
            }

        except Exception as e:
            log.error('Error in getting  group List -- {0}'.format(e))
            status = 500
            response = {
                'message': str(e)
            }

    else:
        try:
            projects = request.json['projects']

            for project in projects:
                project_id = project['project_id']
                project_name = project['project_name'].lower()
                cost_center = project['cost_center'].lower()
                director = project['director'].lower()
                director_email = project['director_email'].lower()
                contact_name = project['contact_name'].lower()
                contact_email = project['contact_email'].lower()
                alert_amount = 0  # project['alert_amount'].lower()

                if not director.strip():
                    director = None
                if not contact_name.strip():
                    contact_name = None
                if not contact_email.strip():
                    contact_email = None
                if not director_email.strip():
                    director_email = None
                '''
                    if not alert_amount.strip():
                       alert_amount = 'None'
                '''

                '''
                    Check if project already in the table
                '''
                query_data = get_project_by_id(project_id)

                if len(query_data) > 0:
                    log.info('DATA IN TABLE - SO UPDATE')
                    query_data = update_project_data(cost_center, project_id, project_name, director, director_email,
                                                     contact_name, contact_email, alert_amount)

                else:
                    log.info('DATA NOT IN TABLE - SO INSERT')
                    query_data = create_project_data(cost_center, project_id, project_name, director, director_email,
                                                     contact_name, contact_email, alert_amount)

            status = 200
            response = {
                'message': ' cost_center_list Created/Updated'
            }
        except Exception as e:
            log.error('Error in creating  group List -- {0}'.format(e))
            status = 500
            response = {
                'message': str(e[0]),
                'cost_center_list': cost_center_list
            }

    resp = Response(response=json.dumps(response),
                    status=status,
                    mimetype="application/json")

    return resp
示例#2
0
def get_cost_center_list():
    cost_center_list = []

    if request.method == 'GET':
        unique = request.args.get('unique', False)

        try:
            if unique:
                cost_center_list = get_center_list(unique)
            else:
                cost_center_list = get_center_list(False)

            status = 200
            response = {'cost_center_list': cost_center_list}

        except Exception as e:
            log.error('Error in getting  group List -- {0}'.format(e))
            status = 500
            response = {'message': str(e)}

    else:
        try:
            projects = request.json['projects']

            for project in projects:
                project_id = project['project_id']
                project_name = project['project_name'].lower()
                cost_center = project['cost_center'].lower()
                director = project['director'].lower()
                director_email = project['director_email'].lower()
                contact_name = project['contact_name'].lower()
                contact_email = project['contact_email'].lower()
                alert_amount = 0  # project['alert_amount'].lower()

                if not director.strip():
                    director = None
                if not contact_name.strip():
                    contact_name = None
                if not contact_email.strip():
                    contact_email = None
                if not director_email.strip():
                    director_email = None
                '''
                    if not alert_amount.strip():
                       alert_amount = 'None'
                '''
                '''
                    Check if project already in the table
                '''
                query_data = get_project_by_id(project_id)

                if len(query_data) > 0:
                    log.info('DATA IN TABLE - SO UPDATE')
                    query_data = update_project_data(cost_center, project_id,
                                                     project_name, director,
                                                     director_email,
                                                     contact_name,
                                                     contact_email,
                                                     alert_amount)

                else:
                    log.info('DATA NOT IN TABLE - SO INSERT')
                    query_data = create_project_data(cost_center, project_id,
                                                     project_name, director,
                                                     director_email,
                                                     contact_name,
                                                     contact_email,
                                                     alert_amount)

            status = 200
            response = {'message': ' cost_center_list Created/Updated'}
        except Exception as e:
            log.error('Error in creating  group List -- {0}'.format(e))
            status = 500
            response = {
                'message': str(e[0]),
                'cost_center_list': cost_center_list
            }

    resp = Response(response=json.dumps(response),
                    status=status,
                    mimetype="application/json")

    return resp
def get_costs(year):
    log.info('-----------In get_costs-----------------------')
    log.info(year)
    project_list_local = dict()
    data = dict(usage_data=[], status=200)

    span = request.args.get('span')
    span_value = request.args.get('span_value')
    view_by = request.args.get('view_by')
    cost_center = request.args.get('cost_center')
    project = request.args.get('project')
    resource = request.args.get('resource')

    log.info('year,span,span_value,view_by,cost_center,project,resource')
    log.info('{0} -{1} -{2} -{3} -{4}-{5}-{6}'.format(year, span, span_value, view_by, cost_center, project, resource))

    '''
        get the project id from the center list that has the name if name was passed in as db needs id
    '''
    cost_center_list = get_center_list(False)
    for project_info in cost_center_list:
        project_list_local[project_info['project_id']] = project_info['project_name']

    for project_id in project_list_local:
        if project_list_local[project_id] == project:
            project = project_id

    if span == 'month' or span == 'quarter':
        span_value = span_value.split('-')[1]
    elif span == 'week':
        month = month_converter(span_value.split('-')[1])
        date = span_value.split('-')[0]
        '''
           sql starts with 0-52
           python/js -- 1-53
        '''
        span_value = datetime.date(int(year), int(month), int(date)).isocalendar()[1] - 1

    try:

        if span == 'year':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info('cost_center == all and project == all and resource == all')
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info('cost_center != all and project == all and resource == all')
                data = get_costs_per_center_year(year, cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info('cost_center != all and project != all and resource == all')
                data = get_costs_per_project_year(year, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info('cost_center != all and project != all and resource != all')
                data = get_costs_per_resource_per_project(year, cost_center, project,
                                                          resource, view_by)
            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info('cost_center != all and project == all and resource != all')
                data = get_costs_per_resource(year, cost_center, resource, view_by)

        elif span == 'month':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info('cost_center == all and project == all and resource == all')
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info('cost_center != all and project == all and resource == all')
                data = get_costs_per_center_month(year, span_value, cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info('cost_center != all and project != all and resource == all')
                data = get_costs_per_resource_month_center(year, span_value, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info('cost_center != all and project != all and resource != all')
                data = get_costs_per_resource_per_project_per_day_month(year, span_value, cost_center, project,
                                                                        resource, view_by)

            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info('cost_center != all and project == all and resource != all')
                data = get_costs_per_resource_all_project_per_day(year, span_value, cost_center, resource, view_by)

        elif span == 'week':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info('cost_center == all and project == all and resource == all')
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info('cost_center != all and project == all and resource == all')
                data = get_costs_per_center_week(year, span_value, cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info('cost_center != all and project != all and resource == all')
                data = get_costs_per_resource_week_center(year, span_value, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info('cost_center != all and project != all and resource != all')
                data = get_costs_per_resource_per_project_per_day_week(year, span_value, cost_center, project,
                                                                       resource, view_by)

            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info('cost_center != all and project == all and resource != all')
                data = get_costs_per_resource_all_project_per_day_week(year, span_value, cost_center, resource, view_by)

        elif span == 'quarter':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info('cost_center == all and project == all and resource == all')
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info('cost_center != all and project == all and resource == all')
                data = get_costs_per_center_year_quarter(year, span_value, cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info('cost_center != all and project != all and resource == all')
                data = get_costs_per_resource_quarter_center(year, span_value, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info('cost_center != all and project != all and resource != all')
                data = get_costs_per_resource_per_project_per_day_quarter(year, span_value, cost_center, project,
                                                                          resource, view_by)

            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info('cost_center != all and project == all and resource != all')
                data = get_costs_per_resource_all_project_per_day_quarter(year, span_value, cost_center, resource,
                                                                          view_by)

        else:
            log.info('Outside all conditions')

        resp = Response(response=json.dumps(data['data']),
                        status=data['status'],
                        mimetype="application/json")

    except Exception as e:
        log.error('Error in get_costs() -- {0}'.format(e[0]))
        data['message'] = e[0]
        resp = Response(response=json.dumps(data),
                        status=500,
                        mimetype="application/json")

    return resp
示例#4
0
def get_costs(year):
    log.info('-----------In get_costs-----------------------')
    log.info(year)
    project_list_local = dict()
    data = dict(usage_data=[], status=200)

    span = request.args.get('span')
    span_value = request.args.get('span_value')
    view_by = request.args.get('view_by')
    cost_center = request.args.get('cost_center')
    project = request.args.get('project')
    resource = request.args.get('resource')

    log.info('year,span,span_value,view_by,cost_center,project,resource')
    log.info('{0} -{1} -{2} -{3} -{4}-{5}-{6}'.format(year, span, span_value,
                                                      view_by, cost_center,
                                                      project, resource))
    '''
        get the project id from the center list that has the name if name was passed in as db needs id
    '''
    cost_center_list = get_center_list(False)
    for project_info in cost_center_list:
        project_list_local[
            project_info['project_id']] = project_info['project_name']

    for project_id in project_list_local:
        if project_list_local[project_id] == project:
            project = project_id

    if span == 'month' or span == 'quarter':
        span_value = span_value.split('-')[1]
    elif span == 'week':
        month = month_converter(span_value.split('-')[1])
        date = span_value.split('-')[0]
        '''
           sql starts with 0-52
           python/js -- 1-53
        '''
        span_value = datetime.date(int(year), int(month),
                                   int(date)).isocalendar()[1] - 1

    try:

        if span == 'year':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center == all and project == all and resource == all'
                )
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project == all and resource == all'
                )
                data = get_costs_per_center_year(year, cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project != all and resource == all'
                )
                data = get_costs_per_project_year(year, cost_center, project,
                                                  view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project != all and resource != all'
                )
                data = get_costs_per_resource_per_project(
                    year, cost_center, project, resource, view_by)
            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project == all and resource != all'
                )
                data = get_costs_per_resource(year, cost_center, resource,
                                              view_by)

        elif span == 'month':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center == all and project == all and resource == all'
                )
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project == all and resource == all'
                )
                data = get_costs_per_center_month(year, span_value,
                                                  cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project != all and resource == all'
                )
                data = get_costs_per_resource_month_center(
                    year, span_value, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project != all and resource != all'
                )
                data = get_costs_per_resource_per_project_per_day_month(
                    year, span_value, cost_center, project, resource, view_by)

            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project == all and resource != all'
                )
                data = get_costs_per_resource_all_project_per_day(
                    year, span_value, cost_center, resource, view_by)

        elif span == 'week':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center == all and project == all and resource == all'
                )
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project == all and resource == all'
                )
                data = get_costs_per_center_week(year, span_value, cost_center,
                                                 view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project != all and resource == all'
                )
                data = get_costs_per_resource_week_center(
                    year, span_value, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project != all and resource != all'
                )
                data = get_costs_per_resource_per_project_per_day_week(
                    year, span_value, cost_center, project, resource, view_by)

            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project == all and resource != all'
                )
                data = get_costs_per_resource_all_project_per_day_week(
                    year, span_value, cost_center, resource, view_by)

        elif span == 'quarter':
            if cost_center == 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center == all and project == all and resource == all'
                )
                data = get_costs_per_cost_month(year, span_value, view_by)

            elif cost_center != 'all' and project == 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project == all and resource == all'
                )
                data = get_costs_per_center_year_quarter(
                    year, span_value, cost_center, view_by)

            elif cost_center != 'all' and project != 'all' and resource == 'all':
                log.info(
                    'cost_center != all and project != all and resource == all'
                )
                data = get_costs_per_resource_quarter_center(
                    year, span_value, cost_center, project, view_by)

            elif cost_center != 'all' and project != 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project != all and resource != all'
                )
                data = get_costs_per_resource_per_project_per_day_quarter(
                    year, span_value, cost_center, project, resource, view_by)

            elif cost_center != 'all' and project == 'all' and resource != 'all':
                log.info(
                    'cost_center != all and project == all and resource != all'
                )
                data = get_costs_per_resource_all_project_per_day_quarter(
                    year, span_value, cost_center, resource, view_by)

        else:
            log.info('Outside all conditions')

        resp = Response(response=json.dumps(data['data']),
                        status=data['status'],
                        mimetype="application/json")

    except Exception as e:
        log.error('Error in get_costs() -- {0}'.format(e[0]))
        data['message'] = e[0]
        resp = Response(response=json.dumps(data),
                        status=500,
                        mimetype="application/json")

    return resp