def generate()

in sql_generators/mobile_kpi_support_metrics/__init__.py [0:0]


def generate(target_project, output_dir, use_cloud_function):
    """Generate per-app queries, views and metadata for active users and search counts aggregates.

    The parent folders will be created if not existing and existing files will be overwritten.
    """
    env = Environment(loader=FileSystemLoader(str(GENERATOR_ROOT / "templates")))
    output_dir = Path(output_dir) / target_project

    default_template_args = {
        "header": HEADER,
        "version": VERSION,
        "project_id": target_project,
        "bigeye_collection": BIGEYE_COLLECTION,
        "bigeye_notification_slack_channel": BIGEYE_NOTIFICATION_SLACK_CHANNEL,
    }

    query_support_configs = (
        "metadata.yaml",
        "schema.yaml",
        "bigconfig.yml",
    )

    all_possible_attribution_fields = dict(
        ChainMap(
            *[
                product.value.get_product_attribution_fields()
                for product in MobileProducts
            ]
        )
    )

    for template_grain, template in TEMPLATES:
        for product in MobileProducts:

            target_name, target_filename, target_extension = template.split(".")

            target_dataset = (
                product.name + "_derived"
                if target_filename == "query"
                else product.name
            )

            table_id = f"{target_project}.{target_dataset}.{target_name}"

            full_table_id = (
                table_id + f"_{VERSION}" if target_filename == "query" else table_id
            )

            product_args = {
                "dataset": product.name,
                "target_name": target_name,
                "app_name": product.name,
                "name": target_name,
                "product_attribution_groups": product.value.attribution_groups,
                "product_attribution_group_names": product.value.get_attribution_group_names(),
                "product_attribution_group_pings": product.value.get_attribution_pings(),
                "product_attribution_fields": product.value.get_product_attribution_fields(),
            }

            sql_template = env.get_template(template)
            rendered_sql = reformat(
                sql_template.render(
                    **default_template_args,
                    **asdict(product.value),
                    **product_args,
                )
            )

            write_sql(
                output_dir=output_dir,
                full_table_id=full_table_id,
                basename=f"{target_filename}.{target_extension}",
                sql=rendered_sql,
                skip_existing=False,
            )

            # we only want to copy files defined in query_support_configs for query files.
            if target_filename != "query":
                continue

            for query_support_config in query_support_configs:
                if (
                    query_support_config == "bigconfig.yml"
                    and not product.value.enable_monitoring
                ):
                    continue

                support_config_template = env.get_template(
                    f"{target_name}.{query_support_config}"
                )
                support_config_rendered = support_config_template.render(
                    **default_template_args,
                    **asdict(product.value),
                    **product_args,
                    format=False,
                )

                write_sql(
                    output_dir=output_dir,
                    full_table_id=full_table_id,
                    basename=query_support_config,
                    sql=(
                        reformat(support_config_rendered)
                        if query_support_config.endswith(".sql")
                        else support_config_rendered
                    ),
                    skip_existing=False,
                )

        # we only want to generate a union view inside telemetry for views
        if target_filename != "view":
            continue

        # For now skipping attribution_clients union. Will add in the future.
        if target_name == "attribution_clients":
            continue

        target_dataset = "telemetry"

        union_target_name = f"mobile_{target_name}"

        union_sql_template = env.get_template("union.view.sql")
        union_sql_rendered = union_sql_template.render(
            **default_template_args,
            dataset=target_dataset,
            name=target_name,
            target_name=union_target_name,
            target_filename=target_filename,
            template_grain=template_grain,
            format=False,
            products=[
                {
                    "name": product.name,
                    "all_possible_attribution_fields": (
                        [
                            {
                                "exists": field_name
                                in product.value.get_product_attribution_fields().keys(),
                                "name": field_name,
                                "type": field_properties["type"],
                                "client_only": field_properties.get(
                                    "client_only", False
                                ),
                            }
                            for field_name, field_properties in all_possible_attribution_fields.items()
                        ]
                        if not template.startswith("active_users")
                        else []
                    ),
                }
                for product in MobileProducts
            ],
        )

        write_sql(
            output_dir=output_dir,
            full_table_id=f"{target_project}.{target_dataset}.{union_target_name}",
            basename=f"{target_filename}.{target_extension}",
            sql=(reformat(union_sql_rendered)),
            skip_existing=False,
        )