# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from mozanalysis.metrics import Metric, DataSource, agg_sum, agg_any


clients_daily = DataSource(
    name='clients_daily',
    from_expr="`moz-fx-data-shared-prod.telemetry.clients_daily`",
)

search_clients_daily = DataSource(
    name='search_clients_daily',
    from_expr='`moz-fx-data-shared-prod.search.search_clients_daily`',
    experiments_column_type=None,
)

main_summary = DataSource(
    name='main_summary',
    from_expr="`moz-fx-data-shared-prod.telemetry.main_summary`"
)

events = DataSource(
    name='events',
    from_expr="`moz-fx-data-shared-prod.telemetry.events`"
)

active_hours = Metric(
    name='active_hours',
    data_source=clients_daily,
    select_expr=agg_sum('active_hours_sum')
)

uri_count = Metric(
    name='uri_count',
    data_source=clients_daily,
    select_expr=agg_sum('scalar_parent_browser_engagement_total_uri_count_sum')
)

search_count = Metric(
    name='search_count',
    data_source=search_clients_daily,
    select_expr=agg_sum('sap')
)

ad_clicks = Metric(
    name='ad_clicks',
    data_source=search_clients_daily,
    select_expr=agg_sum('ad_click')
)

organic_search_count = Metric(
    name='organic_search_count',
    data_source=search_clients_daily,
    select_expr=agg_sum('organic')
)

unenroll = Metric(
    name='unenroll',
    data_source=events,
    select_expr=agg_any("""
            event_category = 'normandy'
            AND event_method = 'unenroll'
            AND event_string_value = '{experiment_slug}'
        """)
)

view_about_logins = Metric(
    name='view_about_logins',
    data_source=events,
    select_expr=agg_any("""
            event_method = 'open_management'
            AND event_category = 'pwmgr'
        """)
)

view_about_protections = Metric(
    name='view_about_protections',
    data_source=events,
    select_expr=agg_any("""
            event_method = 'show'
            AND event_object = 'protection_report'
        """)
)
