class HeroSKUAvailabilityByHeroTypeKpi(UnifiedCalculationsScript):

    def __init__(self, data_provider, config_params=None, **kwargs):
        super(HeroSKUAvailabilityByHeroTypeKpi, self).__init__(data_provider, config_params=config_params, **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def calculate(self):
        lvl3_ass_res_df = self.dependencies_data
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.HERO_SKU_AVAILABILITY_BY_HERO_TYPE)
        if not lvl3_ass_res_df.empty:
            location_type_fk = self.util.scif[self.util.scif[ScifConsts.LOCATION_TYPE] == 'Primary Shelf']\
                [ScifConsts.LOCATION_TYPE_FK].values[0]
            product_hero_df = self.util.all_products[[ScifConsts.PRODUCT_FK, self.util.HERO_SKU_LABEL]]
            lvl3_ass_res_df = lvl3_ass_res_df.merge(product_hero_df, left_on='numerator_id',
                                                    right_on=ScifConsts.PRODUCT_FK,
                                                    how='left')
            lvl3_ass_res_df = lvl3_ass_res_df.merge(self.util.hero_type_custom_entity_df,
                                                    left_on=self.util.HERO_SKU_LABEL, right_on='name', how='left')
            lvl3_ass_res_df['count'] = 1
            kpi_res_df = lvl3_ass_res_df.groupby([self.util.HERO_SKU_LABEL, 'entity_fk'],
                                                 as_index=False).agg({'numerator_result': np.sum, 'count': np.sum})
            kpi_res_df['result'] = kpi_res_df['numerator_result'] / kpi_res_df['count'] * 100
            kpi_res_df['score'] = kpi_res_df['result'].apply(lambda x: 100 if x >= 100 else 0)
            for i, res in kpi_res_df.iterrows():
                self.write_to_db_result(fk=kpi_fk, numerator_id=res['entity_fk'],
                                        numerator_result=res['numerator_result'], result=res['result'],
                                        denominator_id=res['entity_fk'], denominator_result=res['count'],
                                        score=res['score'], context_id=location_type_fk)
                self.util.add_kpi_result_to_kpi_results_df(
                    [kpi_fk, res['entity_fk'], res['entity_fk'], res['result'], res['score'], location_type_fk])

    def kpi_type(self):
        pass
class HeroAvailabilityKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(HeroAvailabilityKpi, self).__init__(data_provider,
                                                  config_params=config_params,
                                                  **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def calculate(self):
        lvl3_ass_res_df = self.dependencies_data
        distribution_kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(
            self.util.HERO_SKU_AVAILABILITY)
        if not lvl3_ass_res_df.empty:
            location_type_fk = self.util.scif[self.util.scif[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
                [ScifConsts.LOCATION_TYPE_FK].values[0]
            total_skus_in_ass = len(lvl3_ass_res_df)
            in_store_skus = len(
                self.util.get_available_hero_sku_list(self.dependencies_data))
            res = np.divide(float(in_store_skus),
                            float(total_skus_in_ass)) * 100
            score = 100 if res >= 100 else 0
            self.write_to_db_result(fk=distribution_kpi_fk,
                                    numerator_id=self.util.own_manuf_fk,
                                    numerator_result=in_store_skus,
                                    result=res,
                                    denominator_id=self.util.store_id,
                                    denominator_result=total_skus_in_ass,
                                    score=score,
                                    context_id=location_type_fk)
            self.util.add_kpi_result_to_kpi_results_df([
                distribution_kpi_fk, self.util.own_manuf_fk,
                self.util.store_id, res, score, None
            ])

    def kpi_type(self):
        pass
Example #3
0
class PriceKpi(UnifiedCalculationsScript):

    def __init__(self, data_provider, config_params=None, **kwargs):
        super(PriceKpi, self).__init__(data_provider, config_params=config_params, **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.PRICE_SCENE)

        sku_list = self.util.filtered_scif[ScifConsts.PRODUCT_FK]
        price_kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.PRICE_SCENE)
        for sku in sku_list:
            self.calculate_hero_sku_price(sku, price_kpi_fk)
        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()

    def calculate_hero_sku_price(self, sku, kpi_fk):
        price = -1
        prices_df = self.util.filtered_matches[((~(self.util.filtered_matches[MatchesConsts.PRICE].isnull())) |
                                          (~(self.util.filtered_matches[MatchesConsts.PROMOTION_PRICE].isnull()))) &
                                          (self.util.filtered_matches[ScifConsts.PRODUCT_FK] == sku)]
        if not prices_df.empty:
            prices_list = prices_df[MatchesConsts.PRICE].values.tolist()
            prices_list.extend(prices_df[MatchesConsts.PROMOTION_PRICE].values.tolist())
            prices_list = filter(lambda v: v == v, prices_list)
            prices_list = filter(lambda v: v is not None, prices_list)
            if prices_list:
                price = max(prices_list)
        self.write_to_db_result(fk=kpi_fk, numerator_id=sku, denominator_id=sku,result=price)
        self.util.add_kpi_result_to_kpi_results_df([kpi_fk, sku, None, price, None, None])
class SosBrandOfSegmentKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(SosBrandOfSegmentKpi, self).__init__(data_provider,
                                                   config_params=config_params,
                                                   **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.BRAND_SOS_OF_SEGMENT)
        self.calculate_brand_out_of_sub_category_sos()
        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()

    def calculate_brand_out_of_sub_category_sos(self):
        location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
            [ScifConsts.LOCATION_TYPE_FK].values[0]
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(
            self.util.BRAND_SOS_OF_SEGMENT)
        filtered_matches = self.util.filtered_matches[~(
            self.util.filtered_matches[ScifConsts.SUB_CATEGORY_FK].isnull())]
        products_df = self.util.all_products[[
            MatchesConsts.PRODUCT_FK, ScifConsts.BRAND_FK,
            ScifConsts.CATEGORY_FK, ScifConsts.PRODUCT_TYPE
        ]]
        filtered_matches = filtered_matches.merge(products_df,
                                                  on=MatchesConsts.PRODUCT_FK,
                                                  how='left')
        sub_cat_df = filtered_matches.groupby(
            [ScifConsts.SUB_CATEGORY_FK],
            as_index=False).agg({MatchesConsts.WIDTH_MM_ADVANCE: np.sum})
        sub_cat_df.rename(
            columns={MatchesConsts.WIDTH_MM_ADVANCE: 'sub_cat_len'},
            inplace=True)
        brand_sub_cat_df = filtered_matches.groupby(
            [ScifConsts.BRAND_FK, ScifConsts.SUB_CATEGORY_FK],
            as_index=False).agg({MatchesConsts.WIDTH_MM_ADVANCE: np.sum})
        brand_sub_cat_df = brand_sub_cat_df.merge(
            sub_cat_df, on=ScifConsts.SUB_CATEGORY_FK, how='left')
        brand_sub_cat_df['sos'] = brand_sub_cat_df[
            MatchesConsts.WIDTH_MM_ADVANCE] / brand_sub_cat_df['sub_cat_len']
        for i, row in brand_sub_cat_df.iterrows():
            self.write_to_db_result(
                fk=kpi_fk,
                numerator_id=row[ScifConsts.BRAND_FK],
                numerator_result=row[MatchesConsts.WIDTH_MM_ADVANCE],
                denominator_id=row[ScifConsts.SUB_CATEGORY_FK],
                denominator_result=row['sub_cat_len'],
                result=row['sos'] * 100,
                context_id=location_type_fk)
            self.util.add_kpi_result_to_kpi_results_df([
                kpi_fk, row[ScifConsts.BRAND_FK],
                row[ScifConsts.SUB_CATEGORY_FK], row['sos'] * 100, None, None
            ])
class SosVsTargetHeroSkuKpi(UnifiedCalculationsScript):

    def __init__(self, data_provider, config_params=None, **kwargs):
        super(SosVsTargetHeroSkuKpi, self).__init__(data_provider, config_params=config_params, **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.HERO_SKU_SOS)
        self.calculate_hero_sku_sos()
        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()

    def calculate_hero_sku_sos(self):
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.HERO_SKU_SOS)
        filtered_scif = self.util.filtered_scif
        location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
            [ScifConsts.LOCATION_TYPE_FK].values[0]
        if (not filtered_scif.empty) and (not self.dependencies_data.empty):
            category_df = filtered_scif.groupby([ScifConsts.CATEGORY_FK],
                                                as_index=False).agg({'updated_gross_length': np.sum})
            category_df.rename(columns={'updated_gross_length': 'cat_len'}, inplace=True)

            av_hero_list = self.util.get_available_hero_sku_list(self.dependencies_data)
            filtered_scif = filtered_scif[filtered_scif[ScifConsts.PRODUCT_FK].isin(av_hero_list)]

            unav_hero_list = self.util.get_unavailable_hero_sku_list(self.dependencies_data)
            unav_hero_df = self.util.all_products[self.util.all_products[ScifConsts.PRODUCT_FK].isin(unav_hero_list)] \
                [[ScifConsts.PRODUCT_FK, ScifConsts.CATEGORY_FK]]
            unav_hero_df['updated_gross_length'] = 0
            filtered_scif = filtered_scif.append(unav_hero_df)

            hero_cat_df = filtered_scif.groupby([ScifConsts.PRODUCT_FK, ScifConsts.CATEGORY_FK],
                                                as_index=False).agg({'updated_gross_length': np.sum})
            hero_cat_df = hero_cat_df.merge(category_df, on=ScifConsts.CATEGORY_FK, how='left')
            hero_cat_df['cat_len'] = hero_cat_df['cat_len'].fillna(0)
            hero_cat_df['sos'] = hero_cat_df.apply(self.calculate_sos, axis=1)
            for i, row in hero_cat_df.iterrows():
                self.write_to_db_result(fk=kpi_fk, numerator_id=row[ScifConsts.PRODUCT_FK],
                                        numerator_result=row['updated_gross_length'],
                                        denominator_id=row[ScifConsts.CATEGORY_FK],
                                        denominator_result=row['cat_len'], result=row['sos'],
                                        context_id=location_type_fk)
                self.util.add_kpi_result_to_kpi_results_df(
                    [kpi_fk, row[ScifConsts.PRODUCT_FK], row[ScifConsts.CATEGORY_FK], row['sos'], None, None])

    @staticmethod
    def calculate_sos(row):
        sos = 0
        if row['cat_len'] != 0:
            sos = float(row['updated_gross_length']) / row['cat_len'] * 100
        return sos
class HeroAvailabilitySkuKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(HeroAvailabilitySkuKpi,
              self).__init__(data_provider,
                             config_params=config_params,
                             **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.HERO_SKU_AVAILABILITY_SKU)
        self.calculate_kpi_for_main_shelf()
        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()

    def kpi_type(self):
        pass

    def calculate_kpi_for_main_shelf(self):
        location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
            [ScifConsts.LOCATION_TYPE_FK].values[0]
        lvl3_ass_res = self.util.lvl3_ass_result
        if lvl3_ass_res.empty:
            return

        if not self.util.filtered_scif.empty:
            products_in_session = self.util.filtered_scif.loc[
                self.util.filtered_scif['facings'] > 0]['product_fk'].values
            products_df = self.util.all_products[[
                ScifConsts.PRODUCT_FK, ScifConsts.MANUFACTURER_FK
            ]]
            lvl3_ass_res.loc[
                lvl3_ass_res['product_fk'].isin(products_in_session),
                'in_store'] = 1
            lvl3_ass_res = lvl3_ass_res.merge(products_df,
                                              on=ScifConsts.PRODUCT_FK,
                                              how='left')
            for i, result in lvl3_ass_res.iterrows():
                score = result.in_store * 100
                custom_res = self.util.commontools.get_yes_no_result(score)
                self.write_to_db_result(fk=result.kpi_fk_lvl3,
                                        numerator_id=result.product_fk,
                                        numerator_result=result.in_store,
                                        result=custom_res,
                                        denominator_id=result.manufacturer_fk,
                                        denominator_result=1,
                                        score=score,
                                        context_id=location_type_fk)
                self.util.add_kpi_result_to_kpi_results_df([
                    result['kpi_fk_lvl3'], result['product_fk'],
                    self.util.store_id, custom_res, score, None
                ])
class SosVsTargetBrandKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(SosVsTargetBrandKpi, self).__init__(data_provider,
                                                  config_params=config_params,
                                                  **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.BRAND_SOS)
        self.calculate_brand_out_of_category_sos()
        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()

    def calculate_brand_out_of_category_sos(self):
        location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
            [ScifConsts.LOCATION_TYPE_FK].values[0]
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.BRAND_SOS)
        filtered_scif = self.util.filtered_scif
        category_df = filtered_scif.groupby([ScifConsts.CATEGORY_FK],
                                            as_index=False).agg({
                                                'updated_gross_length':
                                                np.sum
                                            })
        category_df.rename(columns={'updated_gross_length': 'cat_len'},
                           inplace=True)
        brand_cat_df = filtered_scif.groupby(
            [ScifConsts.BRAND_FK, ScifConsts.CATEGORY_FK],
            as_index=False).agg({'updated_gross_length': np.sum})
        brand_cat_df = brand_cat_df.merge(category_df,
                                          on=ScifConsts.CATEGORY_FK,
                                          how='left')
        brand_cat_df['sos'] = brand_cat_df[
            'updated_gross_length'] / brand_cat_df['cat_len']
        for i, row in brand_cat_df.iterrows():
            self.write_to_db_result(
                fk=kpi_fk,
                numerator_id=row[ScifConsts.BRAND_FK],
                numerator_result=row['updated_gross_length'],
                denominator_id=row[ScifConsts.CATEGORY_FK],
                denominator_result=row['cat_len'],
                result=row['sos'] * 100,
                context_id=location_type_fk)
            self.util.add_kpi_result_to_kpi_results_df([
                kpi_fk, row[ScifConsts.BRAND_FK], row[ScifConsts.CATEGORY_FK],
                row['sos'] * 100, None, None
            ])
Example #8
0
class BrandFullBayKpi(UnifiedCalculationsScript):

    def __init__(self, data_provider, config_params=None, **kwargs):
        super(BrandFullBayKpi, self).__init__(data_provider, config_params=config_params, **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.BRAND_FULL_BAY)
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.BRAND_FULL_BAY)
        external_kpi_targets = self.util.commontools.all_targets_unpacked[
            self.util.commontools.all_targets_unpacked['kpi_level_2_fk'] == kpi_fk]
        external_kpi_targets = external_kpi_targets.reset_index(drop=True)
        if not external_kpi_targets.empty:
            external_kpi_targets['group_fk'] = external_kpi_targets['Group Name'].apply(lambda x:
                                                                                        self.util.custom_entities[
                                                                                            self.util.custom_entities[
                                                                                                'name'] == x][
                                                                                            'pk'].values[0])
            filtered_matches = self.util.filtered_matches[~(self.util.filtered_matches['bay_number'] == -1)]
            if not filtered_matches.empty:
                scene_bay_product = filtered_matches.groupby(['scene_fk', 'bay_number', 'product_fk'],
                                                             as_index=False).agg({'count': np.sum})
                scene_bay_product = scene_bay_product.merge(self.util.all_products, on='product_fk', how='left')
                scene_bay = scene_bay_product.groupby(['scene_fk', 'bay_number'], as_index=False).agg({'count': np.sum})
                scene_bay.rename(columns={'count': 'total_facings'}, inplace=True)
                for i, row in external_kpi_targets.iterrows():
                    filters = self.util.get_full_bay_and_positional_filters(row)
                    brand_relevant_df = scene_bay_product[
                        self.util.toolbox.get_filter_condition(scene_bay_product, **filters)]
                    result_df = brand_relevant_df.groupby(['scene_fk', 'bay_number'], as_index=False).agg(
                        {'count': np.sum})
                    result_df = result_df.merge(scene_bay, on=['scene_fk', 'bay_number'], how='left')
                    result_df['ratio'] = result_df['count'] / result_df['total_facings']
                    target_ratio = float(self._config_params['ratio'])
                    result = len(result_df[result_df['ratio'] >= target_ratio])
                    self.write_to_db_result(fk=row['kpi_level_2_fk'], numerator_id=row['group_fk'], result=result,
                                            score=result, target=target_ratio*100)
                    self.util.add_kpi_result_to_kpi_results_df(
                        [row['kpi_level_2_fk'], row['group_fk'], None, None, result, None])

        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()
Example #9
0
class SosVsTargetSegmentKpi(UnifiedCalculationsScript):

    def __init__(self, data_provider, config_params=None, **kwargs):
        super(SosVsTargetSegmentKpi, self).__init__(data_provider, config_params=config_params, **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        # sos_targets = self.util.sos_vs_target_targets.copy()
        # sos_targets = sos_targets[sos_targets['type'] == self._config_params['kpi_type']]
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.PEPSICO_SEGMENT_SOS)
        # self.calculate_pepsico_segment_space_sos_vs_target(sos_targets)
        self.calculate_pepsico_segment_space_sos()
        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()

    def calculate_pepsico_segment_space_sos(self):
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.PEPSICO_SEGMENT_SOS)
        filtered_matches = self.util.filtered_matches
        products_df = self.util.all_products[[MatchesConsts.PRODUCT_FK, ScifConsts.BRAND_FK, ScifConsts.CATEGORY_FK]]
        filtered_matches = filtered_matches.merge(products_df, on=MatchesConsts.PRODUCT_FK, how='left')
        cat_df = filtered_matches.groupby([ScifConsts.CATEGORY_FK],
                                           as_index=False).agg({MatchesConsts.WIDTH_MM_ADVANCE: np.sum})
        cat_df.rename(columns={MatchesConsts.WIDTH_MM_ADVANCE: 'cat_len'}, inplace=True)
        # filtered_scif = filtered_scif[filtered_scif[ScifConsts.MANUFACTURER_FK] == self.util.own_manuf_fk]
        location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
            [ScifConsts.LOCATION_TYPE_FK].values[0]
        if not filtered_matches.empty:
            sub_cat_df = filtered_matches.groupby([ScifConsts.SUB_CATEGORY_FK, ScifConsts.CATEGORY_FK],
                                                  as_index=False).agg({MatchesConsts.WIDTH_MM_ADVANCE: np.sum})
            if not sub_cat_df.empty:
                sub_cat_df = sub_cat_df.merge(cat_df, on=ScifConsts.CATEGORY_FK, how='left')
                sub_cat_df['sos'] = sub_cat_df[MatchesConsts.WIDTH_MM_ADVANCE] / sub_cat_df['cat_len']
                for i, row in sub_cat_df.iterrows():
                    self.write_to_db_result(fk=kpi_fk, numerator_id=row[ScifConsts.SUB_CATEGORY_FK],
                                            numerator_result=row[MatchesConsts.WIDTH_MM_ADVANCE],
                                            denominator_id=row[ScifConsts.CATEGORY_FK],
                                            denominator_result=row['cat_len'], result=row['sos'] * 100,
                                            context_id=location_type_fk)
                    self.util.add_kpi_result_to_kpi_results_df(
                        [kpi_fk, row[ScifConsts.SUB_CATEGORY_FK], row[ScifConsts.CATEGORY_FK], row['sos'] * 100,
                         None, None])
class CategoryFullBayKpi(UnifiedCalculationsScript):

    def __init__(self, data_provider, config_params=None, **kwargs):
        super(CategoryFullBayKpi, self).__init__(data_provider, config_params=config_params, **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.CATEGORY_FULL_BAY)
        category_fk = self.util.all_products[self.util.all_products[ProductsConsts.CATEGORY] == self.util.CSN]\
            [ProductsConsts.CATEGORY_FK].values[0]
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(self.util.CATEGORY_FULL_BAY)
        filtered_matches = self.util.filtered_matches[~(self.util.filtered_matches[MatchesConsts.BAY_NUMBER] == -1)]
        if not filtered_matches.empty:
            scene_bay_product = filtered_matches.groupby([MatchesConsts.SCENE_FK, MatchesConsts.BAY_NUMBER,
                                                          ScifConsts.PRODUCT_FK], as_index=False).agg({'count': np.sum})
            scene_bay_product = scene_bay_product.merge(self.util.all_products, on=ProductsConsts.PRODUCT_FK, how='left')
            scene_bay = scene_bay_product.groupby([MatchesConsts.SCENE_FK, MatchesConsts.BAY_NUMBER],
                                                  as_index=False).agg({'count': np.sum})
            scene_bay.rename(columns={'count': 'total_facings'}, inplace=True)

            cat_relevant_df = scene_bay_product[scene_bay_product[ProductsConsts.CATEGORY_FK] == category_fk]
            result_df = cat_relevant_df.groupby([MatchesConsts.SCENE_FK, MatchesConsts.BAY_NUMBER],
                                                as_index=False).agg({'count': np.sum})
            result_df = result_df.merge(scene_bay, on=[MatchesConsts.SCENE_FK, MatchesConsts.BAY_NUMBER], how='left')
            result_df['ratio'] = result_df['count'] / result_df['total_facings']
            target_ratio = float(self._config_params['ratio'])
            result = len(result_df[result_df['ratio'] >= target_ratio])
            self.write_to_db_result(fk=kpi_fk, numerator_id=category_fk, denominator_id=self.util.store_id,
                                    score=result, result=result, target=float(self._config_params['ratio'])*100)
            self.util.add_kpi_result_to_kpi_results_df([kpi_fk, category_fk, self.util.store_id, result, result, None])

        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()
Example #11
0
class LinearBrandVsBrandIndexKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(LinearBrandVsBrandIndexKpi,
              self).__init__(data_provider,
                             config_params=config_params,
                             **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self._config_params['kpi_type'])

        index_targets = self.util.get_relevant_sos_vs_target_kpi_targets(
            brand_vs_brand=True)
        index_targets['numerator_id'] = index_targets.apply(
            self.util.retrieve_relevant_item_pks,
            axis=1,
            args=('numerator_type', 'numerator_value'))
        index_targets['denominator_id'] = index_targets.apply(
            self.util.retrieve_relevant_item_pks,
            axis=1,
            args=('denominator_type', 'denominator_value'))
        index_targets['identifier_parent'] = index_targets['KPI Parent'].apply(
            lambda x: self.util.common.get_dictionary(kpi_fk=int(float(x))))
        index_targets = index_targets[index_targets['type'] ==
                                      self._config_params['kpi_type']]
        location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
            [ScifConsts.LOCATION_TYPE_FK].values[0]
        for i, row in index_targets.iterrows():
            general_filters = {
                row['additional_filter_type_1']:
                row['additional_filter_value_1']
            }
            numerator_sos_filters = {
                row['numerator_type']: row['numerator_value']
            }
            num_num_linear, num_denom_linear = self.util.calculate_sos(
                numerator_sos_filters, **general_filters)
            numerator_sos = num_num_linear / num_denom_linear if num_denom_linear else 0

            denominator_sos_filters = {
                row['denominator_type']: row['denominator_value']
            }
            denom_num_linear, denom_denom_linear = self.util.calculate_sos(
                denominator_sos_filters, **general_filters)
            denominator_sos = denom_num_linear / denom_denom_linear if denom_denom_linear else 0

            if denominator_sos == 0:
                index = 0 if numerator_sos == 0 else 1
            else:
                index = numerator_sos / denominator_sos

            self.write_to_db_result(fk=row.kpi_level_2_fk,
                                    numerator_id=row.numerator_id,
                                    numerator_result=num_num_linear,
                                    denominator_id=row.denominator_id,
                                    denominator_result=denom_num_linear,
                                    result=index,
                                    score=index,
                                    context_id=location_type_fk)
            self.util.add_kpi_result_to_kpi_results_df([
                row.kpi_level_2_fk, row.numerator_id, row.denominator_id,
                index, index, None
            ])

        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()
class HeroSOSofCategoryByHeroTypeKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(HeroSOSofCategoryByHeroTypeKpi,
              self).__init__(data_provider,
                             config_params=config_params,
                             **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        hero_sos_kpi_results = self.dependencies_data
        kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(
            self.util.HERO_SKU_SOS_OF_CAT_BY_HERO_TYPE)
        if not hero_sos_kpi_results.empty:
            category_len_df = hero_sos_kpi_results.drop_duplicates(
                subset=['denominator_id'])
            category_len_df = category_len_df[[
                'denominator_id', 'denominator_result'
            ]]

            location_type_fk = self.util.scif[self.util.scif[ScifConsts.LOCATION_TYPE] == 'Primary Shelf'] \
                [ScifConsts.LOCATION_TYPE_FK].values[0]
            product_hero_df = self.util.all_products[[
                ScifConsts.PRODUCT_FK, self.util.HERO_SKU_LABEL
            ]]
            hero_sos_kpi_results = hero_sos_kpi_results.merge(
                product_hero_df,
                left_on='numerator_id',
                right_on=ScifConsts.PRODUCT_FK,
                how='left')
            hero_sos_kpi_results = hero_sos_kpi_results.merge(
                self.util.hero_type_custom_entity_df,
                left_on=self.util.HERO_SKU_LABEL,
                right_on='name',
                how='left')

            hero_type_by_cat = hero_sos_kpi_results.groupby([self.util.HERO_SKU_LABEL, 'entity_fk',
                                                             'denominator_id'], as_index=False).\
                agg({'numerator_result': np.sum})
            hero_type_by_cat = hero_type_by_cat.merge(category_len_df,
                                                      on='denominator_id',
                                                      how='left')
            hero_type_by_cat['sos'] = hero_type_by_cat[
                'numerator_result'] / hero_type_by_cat[
                    'denominator_result'] * 100
            hero_type_by_cat['score'] = hero_type_by_cat['sos'].apply(
                lambda x: 100 if x >= 100 else 0)

            for i, res in hero_type_by_cat.iterrows():
                self.write_to_db_result(
                    fk=kpi_fk,
                    score=res['score'],
                    result=res['sos'],
                    numerator_id=res['entity_fk'],
                    denominator_id=res['denominator_id'],
                    numerator_result=res['numerator_result'],
                    denominator_result=res['denominator_result'],
                    context_id=location_type_fk)
                self.util.add_kpi_result_to_kpi_results_df([
                    kpi_fk, res['entity_fk'], res['denominator_id'],
                    res['sos'], res['score'], location_type_fk
                ])
class FacingsPerProductKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(FacingsPerProductKpi, self).__init__(data_provider,
                                                   config_params=config_params,
                                                   **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def kpi_type(self):
        pass

    def calculate(self):
        self.util.filtered_scif, self.util.filtered_matches = \
            self.util.commontools.set_filtered_scif_and_matches_for_specific_kpi(self.util.filtered_scif,
                                                                                 self.util.filtered_matches,
                                                                                 self.util.FACINGS_PER_PRODUCT)
        if not self.util.filtered_matches.empty:
            kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(
                self.util.FACINGS_PER_PRODUCT)
            filtered_matches = self.util.filtered_matches.copy()
            shelves_arr = np.sort(filtered_matches[
                MatchesConsts.SHELF_NUMBER].unique()).tolist()
            filtered_matches[MatchesConsts.SHELF_NUMBER] = filtered_matches[MatchesConsts.SHELF_NUMBER]. \
                apply(lambda x: shelves_arr.index(x)+1)
            filtered_matches = filtered_matches.merge(
                self.util.all_products,
                on=MatchesConsts.PRODUCT_FK,
                how='left')
            filtered_matches.loc[filtered_matches['att1'] == 'display cardboard box', 'count'] = \
                filtered_matches['count'] * 3
            result_df = filtered_matches.groupby([
                MatchesConsts.PRODUCT_FK, MatchesConsts.SHELF_NUMBER,
                MatchesConsts.BAY_NUMBER
            ],
                                                 as_index=False).agg(
                                                     {'count': np.sum})

            shelves_cust_entity = self.util.custom_entities[
                self.util.custom_entities['entity_type'] == 'shelf_number']
            shelves_cust_entity['name'] = shelves_cust_entity['name'].astype(
                int)
            bays_cust_entity = self.util.custom_entities[
                self.util.custom_entities['entity_type'] == 'bay_number']
            bays_cust_entity['name'] = bays_cust_entity['name'].astype(int)

            result_df = result_df.merge(shelves_cust_entity,
                                        left_on=MatchesConsts.SHELF_NUMBER,
                                        right_on='name',
                                        how='left')
            result_df.rename(columns={'pk': 'shelf_fk'}, inplace=True)
            result_df = result_df.merge(bays_cust_entity,
                                        left_on=MatchesConsts.BAY_NUMBER,
                                        right_on='name',
                                        how='left')
            result_df.rename(columns={'pk': 'bay_fk'}, inplace=True)

            for i, row in result_df.iterrows():
                self.write_to_db_result(
                    fk=kpi_fk,
                    numerator_result=row[MatchesConsts.SHELF_NUMBER],
                    result=row['count'],
                    numerator_id=row[MatchesConsts.PRODUCT_FK],
                    denominator_id=row[MatchesConsts.PRODUCT_FK],
                    denominator_result=row[MatchesConsts.BAY_NUMBER],
                    context_id=row['shelf_fk'],
                    by_scene=True)
                self.util.add_kpi_result_to_kpi_results_df([
                    kpi_fk, row[MatchesConsts.PRODUCT_FK], row['bay_fk'],
                    row['count'], None, row['shelf_fk']
                ])

        self.util.reset_filtered_scif_and_matches_to_exclusion_all_state()
class ShareOfAssortmentByHeroTypeKpi(UnifiedCalculationsScript):
    def __init__(self, data_provider, config_params=None, **kwargs):
        super(ShareOfAssortmentByHeroTypeKpi,
              self).__init__(data_provider,
                             config_params=config_params,
                             **kwargs)
        self.util = PepsicoUtil(None, data_provider)

    def calculate(self):
        lvl3_ass_res_df = self.dependencies_data
        if not lvl3_ass_res_df.empty:
            kpi_fk = self.util.common.get_kpi_fk_by_kpi_type(
                self.util.SHARE_OF_ASSORTMENT_BY_HERO_TYPE)
            in_store_skus = len(
                self.util.get_available_hero_sku_list(
                    self.dependencies_data))  # total recognized
            location_type_fk = self.util.all_templates[self.util.all_templates[ScifConsts.LOCATION_TYPE] == 'Primary Shelf']\
                [ScifConsts.LOCATION_TYPE_FK].values[0]

            product_hero_df = self.util.all_products[[
                ScifConsts.PRODUCT_FK, self.util.HERO_SKU_LABEL
            ]]
            lvl3_ass_res_df = lvl3_ass_res_df.merge(
                product_hero_df,
                left_on='numerator_id',
                right_on=ScifConsts.PRODUCT_FK,
                how='left')
            lvl3_ass_res_df = lvl3_ass_res_df.merge(
                self.util.hero_type_custom_entity_df,
                left_on=self.util.HERO_SKU_LABEL,
                right_on='name',
                how='left')

            kpi_res_df = lvl3_ass_res_df.groupby(
                [self.util.HERO_SKU_LABEL, 'entity_fk'],
                as_index=False).agg({'numerator_result': np.sum})
            kpi_res_df['in_store_total'] = in_store_skus
            kpi_res_df['result'] = kpi_res_df.apply(self.get_result, axis=1)
            kpi_res_df['score'] = kpi_res_df['result'].apply(
                lambda x: 100 if x >= 100 else 0)
            for i, res in kpi_res_df.iterrows():
                self.write_to_db_result(
                    fk=kpi_fk,
                    numerator_id=res['entity_fk'],
                    numerator_result=res['numerator_result'],
                    result=res['result'],
                    denominator_id=self.util.store_id,
                    denominator_result=res['in_store_total'],
                    score=res['score'],
                    context_id=location_type_fk)
                self.util.add_kpi_result_to_kpi_results_df([
                    kpi_fk, res['entity_fk'], res['entity_fk'], res['result'],
                    res['score'], location_type_fk
                ])

    def kpi_type(self):
        pass

    @staticmethod
    def get_result(row):
        rv = float(
            row['numerator_result']
        ) / row['in_store_total'] * 100 if row['in_store_total'] else 0
        return rv