Пример #1
0
    def analyze_project_ar(self, year=None):
        connect = DatabaseHelper()
        data = connect.get_activity_ar(year)
        if data['value']:
            data = pd.DataFrame(data['value'])

            project = analyze_helper.set_fullname(connect.get_project_list())
            project = project[project['project_type'] == 1]
            project.drop(columns=['project_type'], inplace=True)
            project_dict = analyze_helper.set_dict(project.index,
                                                   project.project_name)

            # get_branch=connect.get_department(None)
            # branch=[]
            # for i in get_branch['value']:
            #     for index in range(len(i['branch'])):
            #         branch.append(i['branch'][index])
            # branch_data = analyze_helper.set_branch(branch)
            # branch_data.drop(columns=['amount_student'],inplace=True)
            # branch_dict = analyze_helper.set_dict(branch_data.index, branch_data.branch_name)

            get_branch = connect.get_department_ds()
            get_branch = pd.DataFrame(get_branch['value'])

            branch_data = get_branch[['branch_id', 'branch_name']]
            branch_data = branch_data.set_index('branch_id')
            branch_dict = analyze_helper.set_dict(branch_data.index,
                                                  branch_data.branch_name)

            list_project = project.index.tolist()
            project_set = []
            i = 0
            for pindex in list_project:
                df = data[data['project_id'] == pindex]
                name_project = project_dict[pindex]

                if not df.empty:
                    analyze_by_activity = df.groupby(['branch_name']).size()
                    # analyze_by_activity = analyze_helper.set_fullname_index(branch_dict, analyze_by_activity)

                    analyze_by_activity_gpax = df.groupby(['branch_name'
                                                           ])['gpax'].mean()
                    # analyze_by_activity_gpax = analyze_helper.set_fullname_index(branch_dict, analyze_by_activity_gpax)
                    analyze_by_activity_gpax = analyze_by_activity_gpax.round(
                        2)

                    list_pr = {
                        'project_name':
                        name_project,
                        'analyze_by_activity':
                        analyze_by_activity.to_dict(),
                        'analyze_by_activity_gpax':
                        analyze_by_activity_gpax.to_dict()
                    }

                    project_set.append(list_pr)

                else:

                    list_branch = branch_data.branch_name.tolist()
                    df_empty = pd.DataFrame(list_branch,
                                            columns=['branch_name'])
                    df_empty['value'] = 0
                    df_empty.set_index('branch_name', inplace=True)
                    list_pr = {
                        'project_name': name_project,
                        'analyze_by_activity': df_empty.value.to_dict(),
                        'analyze_by_activity_gpax': df_empty.value.to_dict()
                    }
                    project_set.append(list_pr)
                i = i + 1

            value = {
                'project_set': project_set,
            }

            response = True
            message = "Analyze Successfully"
        else:
            value = {}
            response = False
            message = "Don't have Data"

        return inner_res_helper.make_inner_response(response=response,
                                                    message=message,
                                                    value=value)
Пример #2
0
    def analyze_ar(self, year=None):
        connect = DatabaseHelper()
        data = connect.get_activity_ar(year)

        if data['value']:
            data = pd.DataFrame(data['value'])
            activity = analyze_helper.set_fullname(connect.get_activity_list())
            dupli_activity = activity.activity_name.duplicated(keep=False)
            activity.loc[dupli_activity, 'activity_name'] = activity.loc[dupli_activity, 'activity_name'] + ' (' + \
                                                            activity['education_year'].astype(str) + ')'
            activityAr = activity[activity['project_type'] == 1]
            activity_data = activityAr.drop(columns='project_type')
            activityAr = activityAr[['activity_name']]
            activity_dict = analyze_helper.set_dict(activityAr.index,
                                                    activityAr.activity_name)

            # get_branch=connect.get_department(None)

            # branch=[]
            # for i in get_branch['value']:
            #     for index in range(len(i['branch'])):
            #         branch.append(i['branch'][index])
            # branch_data = analyze_helper.set_branch(branch)
            # branch_data.drop(columns=['amount_student'],inplace=True)
            # branch_dict = analyze_helper.set_dict(branch_data.index, branch_data.branch_name)

            get_branch = connect.get_department_ds()
            get_branch = pd.DataFrame(get_branch['value'])

            branch_data = get_branch[['branch_id', 'branch_name']]
            branch_data = branch_data.set_index('branch_id')
            branch_dict = analyze_helper.set_dict(branch_data.index,
                                                  branch_data.branch_name)

            # activity_data keep data activity such as activity_year and activity_budget
            # activity_dict keep data activity such as activity_year and activity_budget in syntex dic

            count_school = data.school_name.value_counts()
            sort_count_school = count_school.sort_values(
                ascending=False).head()

            gpax_school = data.groupby(['school_name'])['gpax'].mean()
            sort_gpax_school = gpax_school.sort_values(ascending=False).head()
            analyze_by_activity = data.groupby(['project_id', 'branch_name'
                                                ]).size().unstack(fill_value=0)
            # analyze_by_activity = analyze_helper.check_list(activityAr.index, analyze_by_activity)
            # analyze_by_activity = analyze_helper.set_fullname_column(branch_dict, analyze_by_activity)
            # analyze_by_activity = analyze_helper.set_fullname_index(activity_dict, analyze_by_activity)

            analyze_by_activity_gpax = data.groupby(
                ['project_id',
                 'branch_name'])['gpax'].mean().unstack(fill_value=0)
            # analyze_by_activity_gpax = analyze_helper.check_list(activityAr.index, analyze_by_activity_gpax)
            # analyze_by_activity_gpax = analyze_helper.set_fullname_index(activity_dict, analyze_by_activity_gpax)
            # analyze_by_activity_gpax = analyze_helper.set_fullname_column(branch_dict, analyze_by_activity_gpax)
            analyze_by_activity_gpax = analyze_by_activity_gpax.round(2)

            value = {
                'count_school':
                sort_count_school.to_dict(),
                'gpax':
                sort_gpax_school.to_dict(),
                'activity_by_branch_count':
                [analyze_by_activity.to_dict('index')],
                'activity_by_branch_gpax':
                [analyze_by_activity_gpax.to_dict('index')]
            }

            response = True
            message = "Analyze Successfully"
        else:
            value = {}
            response = False
            message = "Don't have Data"

        return inner_res_helper.make_inner_response(response=response,
                                                    message=message,
                                                    value=value)