def read_ad_data(account_id, campaign_id):
    path_manager = EnvManager()
    insight_dir = path_manager.get_insight_dir(mconstant.NODE_TYPE_AD)
    account_dir = os.path.join(insight_dir, account_id)

    config_path = path_manager.get_conf_dir()
    output_path = os.path.join(path_manager.get_output_dir(),
                               mconstant.NODE_TYPE_AD)
    fhelper.make_dir(output_path)

    handler_factory = HandlerFactory(config_path)
    insight_handler = handler_factory.get_insight_handler(
        mconstant.NODE_TYPE_AD)

    in_act_files = fhelper.get_file_list(account_dir)

    data_per_day = read_ad_insight_day(in_act_files, insight_handler,
                                       campaign_id)

    ad_name_key = ''
    group_data = group_by_name(data_per_day, ad_name_key)

    if ad_name_key.strip():
        file_name = account_id + '_' + ad_name_key + '_' + str(
            time.time()) + '.xlsx'
        act_file = os.path.join(output_path, file_name)
        ExcelExporter.export_data_excel(group_data, act_file)
    else:
        file_name = account_id + '_' + str(time.time()) + '.xlsx'
        act_file = os.path.join(output_path, file_name)
        ExcelExporter.export_excel_by_key(iconstant.NEW_FIELD_GROUP_AD_NAME,
                                          iconstant.NEW_FIELD_GROUP_AD_NAME,
                                          group_data, act_file)
Пример #2
0
def merger_all_account_data(node_type):
    path_manager = EnvManager()
    insight_dir = path_manager.get_insight_dir(node_type)
    retention_dir = path_manager.get_retention_dir(node_type)
    account_list = fhelper.get_subdir_name_list(retention_dir)

    config_path = path_manager.get_conf_dir()
    merger_handler = MergerDataHandler(config_path)

    output_path = os.path.join(path_manager.get_output_dir(), node_type)
    fhelper.make_dir(output_path)

    for account_id in account_list:
        in_act_dir = os.path.join(insight_dir, account_id)
        re_act_dir = os.path.join(retention_dir, account_id)
        in_act_files = fhelper.get_file_list(in_act_dir)
        re_act_files = fhelper.get_file_list(re_act_dir)
        merger_data = merger_handler.merger_in_re_per_day(
            in_act_files, re_act_files, node_type)

        output_act_path = os.path.join(output_path, account_id)
        fhelper.make_dir(output_act_path)
        ExcelExporter.export_merger_retention(dhelper.get_key_id(node_type),
                                              dhelper.get_key_id(node_type),
                                              merger_data, output_act_path)
Пример #3
0
def read_all_account_data(node_type):
    path_manager = EnvManager()
    insight_dir = path_manager.get_insight_dir(node_type)
    account_list = fhelper.get_subdir_name_list(insight_dir)

    config_path = path_manager.get_conf_dir()
    output_path = os.path.join(path_manager.get_output_dir(), node_type)
    fhelper.make_dir(output_path)

    handler_factory = HandlerFactory(config_path)
    insight_handler = handler_factory.get_insight_handler(node_type)

    for account_id in account_list:
        in_act_dir = os.path.join(insight_dir, account_id)
        in_act_files = fhelper.get_file_list(in_act_dir)

        file_name = account_id + '_' + str(time.time()) + '.xlsx'
        act_file = os.path.join(output_path, file_name)

        read_insight_per_day(in_act_files, insight_handler, act_file, node_type, account_id)
def export_data_weekly():
    path_manager = EnvManager()
    conf_dir = path_manager.get_conf_dir()
    weekly_conf = ospath.join(conf_dir, 'insight_weekly_conf.json')
    conf_info = fhelper.read_json_file(weekly_conf)
    account_map = conf_info[conconstants.WEEKLY_ACCOUNT_ID]
    node_types = conf_info[conconstants.WEEKLY_NODE_TYPE]
    output_path = conf_info[conconstants.WEEKLY_OUTPUT_PATH]
    insight_path = conf_info[conconstants.WEEKLY_INSIGHT_PATH]
    if not output_path:
        output_path = path_manager.get_output_dir()
    read_start_date = conf_info[conconstants.WEEKLY_READ_START_DATE]

    current_day = chelper.get_now_date()
    output_path = ospath.join(output_path, current_day)
    fhelper.make_dir(output_path)

    if not read_start_date:
        read_start_date = chelper.get_delta_date(-9)
    filter_start_date = chelper.get_delta_date(-8)
    filter_end_date = chelper.get_delta_date(-2)

    handler_factory = HandlerFactory(conf_dir)

    try:
        for (act_id, act_desc) in account_map.items():
            reader = InsightReader(act_id,
                                   handler_factory,
                                   read_start_date,
                                   node_types=node_types,
                                   insight_path=insight_path)
            data_list = []
            sheet_name_list = []
            file_name = act_desc + '_' + act_id + '_' + filter_start_date + '_' + filter_end_date + '.xlsx'
            output_file = ospath.join(output_path, file_name)
            EliLogger.instance().info('Export account: ' + act_desc +
                                      '; outputPath: ' + output_file)
            print 'Export account: ' + act_desc + '; outputPath: ' + output_file

            for ntype in node_types:
                daily_data = reader.read_daily_data(ntype, filter_start_date,
                                                    filter_end_date)
                hourly_data = reader.read_hourly_data(ntype, filter_start_date,
                                                      filter_end_date)
                if daily_data is not None and not daily_data.empty:
                    data_list.append(daily_data)
                    daily_name = inconstants.SHEET_NAME_PREFIX_DAILY + '_' + ntype
                    sheet_name_list.append(daily_name)
                if hourly_data is not None and not hourly_data.empty:
                    data_list.append(hourly_data)
                    hourly_name = inconstants.SHEET_NAME_PREFIX_HOURLY + '_' + ntype
                    sheet_name_list.append(hourly_name)
                EliLogger.instance().info('Succeed to export node type: ' +
                                          ntype)
                print 'Succeed to export node type: ' + ntype

            if len(data_list) > 0:
                ExcelExporter.export_multi_data_excel(output_file, data_list,
                                                      sheet_name_list)
    except Exception, e:
        EliLogger.instance().error('Failed to export insight analysis. ' +
                                   e.message)
        print 'Failed to export insight analysis. ' + e.message