-- Query generated by:
-- templates/clients_daily_scalar_aggregates.sql.py --agg-type keyed_scalars
WITH valid_build_ids AS (
  SELECT DISTINCT
    (build.build.id) AS build_id
  FROM
    `moz-fx-data-shared-prod.telemetry.buildhub2`
),
filtered AS (
  SELECT
    *,
    SPLIT(application.version, '.')[OFFSET(0)] AS app_version,
    DATE(submission_timestamp) AS submission_date,
    normalized_os AS os,
    application.build_id AS app_build_id,
    normalized_channel AS channel
  FROM
    `moz-fx-data-shared-prod.telemetry_stable.main_v5`
  INNER JOIN
    valid_build_ids
    ON (application.build_id = build_id)
  WHERE
    DATE(submission_timestamp) = @submission_date
    AND normalized_channel IN ("release", "beta", "nightly")
    AND client_id IS NOT NULL
),
grouped_metrics AS (
  SELECT
    sample_id,
    client_id,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    ARRAY<STRUCT<name STRING, process STRING, value ARRAY<STRUCT<key STRING, value INT64>>>>[
      (
        'browser_engagement_navigation_about_home',
        'parent',
        payload.processes.parent.keyed_scalars.browser_engagement_navigation_about_home
      ),
      (
        'browser_engagement_navigation_about_newtab',
        'parent',
        payload.processes.parent.keyed_scalars.browser_engagement_navigation_about_newtab
      ),
      (
        'browser_engagement_navigation_contextmenu',
        'parent',
        payload.processes.parent.keyed_scalars.browser_engagement_navigation_contextmenu
      ),
      (
        'browser_engagement_navigation_searchbar',
        'parent',
        payload.processes.parent.keyed_scalars.browser_engagement_navigation_searchbar
      ),
      (
        'browser_engagement_navigation_urlbar',
        'parent',
        payload.processes.parent.keyed_scalars.browser_engagement_navigation_urlbar
      ),
      (
        'browser_engagement_navigation_webextension',
        'parent',
        payload.processes.parent.keyed_scalars.browser_engagement_navigation_webextension
      ),
      (
        'browser_errors_collected_count_by_filename',
        'parent',
        payload.processes.parent.keyed_scalars.browser_errors_collected_count_by_filename
      ),
      (
        'browser_search_ad_clicks',
        'parent',
        payload.processes.parent.keyed_scalars.browser_search_ad_clicks
      ),
      (
        'browser_search_data_transferred',
        'parent',
        payload.processes.parent.keyed_scalars.browser_search_data_transferred
      ),
      (
        'browser_search_with_ads',
        'parent',
        payload.processes.parent.keyed_scalars.browser_search_with_ads
      ),
      (
        'devtools_accessibility_accessible_context_menu_item_activated',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_accessibility_accessible_context_menu_item_activated
      ),
      (
        'devtools_accessibility_audit_activated',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_accessibility_audit_activated
      ),
      (
        'devtools_accessibility_select_accessible_for_node',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_accessibility_select_accessible_for_node
      ),
      (
        'devtools_accessibility_simulation_activated',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_accessibility_simulation_activated
      ),
      (
        'devtools_current_theme',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_current_theme
      ),
      (
        'devtools_inspector_three_pane_enabled',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_inspector_three_pane_enabled
      ),
      (
        'devtools_responsive_open_trigger',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_responsive_open_trigger
      ),
      (
        'devtools_toolbox_tabs_reordered',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_toolbox_tabs_reordered
      ),
      (
        'devtools_tooltip_shown',
        'parent',
        payload.processes.parent.keyed_scalars.devtools_tooltip_shown
      ),
      (
        'dom_event_confluence_load_count',
        'content',
        payload.processes.content.keyed_scalars.dom_event_confluence_load_count
      ),
      (
        'dom_event_office_online_load_count',
        'content',
        payload.processes.content.keyed_scalars.dom_event_office_online_load_count
      ),
      (
        'dom_ipc_rejected_window_actor_message',
        'content',
        payload.processes.content.keyed_scalars.dom_ipc_rejected_window_actor_message
      ),
      (
        'dom_ipc_rejected_window_actor_message',
        'parent',
        payload.processes.parent.keyed_scalars.dom_ipc_rejected_window_actor_message
      ),
      (
        'extensions_updates_rdf',
        'parent',
        payload.processes.parent.keyed_scalars.extensions_updates_rdf
      ),
      (
        'gfx_advanced_layers_failure_id',
        'parent',
        payload.processes.parent.keyed_scalars.gfx_advanced_layers_failure_id
      ),
      (
        'gfx_small_paint_phase_weight',
        'content',
        payload.processes.content.keyed_scalars.gfx_small_paint_phase_weight
      ),
      (
        'images_webp_content_frequency',
        'content',
        payload.processes.content.keyed_scalars.images_webp_content_frequency
      ),
      (
        'images_webp_content_frequency',
        'parent',
        payload.processes.parent.keyed_scalars.images_webp_content_frequency
      ),
      (
        'networking_data_transferred_kb',
        'parent',
        payload.processes.parent.keyed_scalars.networking_data_transferred_kb
      ),
      (
        'networking_data_transferred_v3_kb',
        'parent',
        payload.processes.parent.keyed_scalars.networking_data_transferred_v3_kb
      ),
      (
        'normandy_recipe_freshness',
        'parent',
        payload.processes.parent.keyed_scalars.normandy_recipe_freshness
      ),
      (
        'pictureinpicture_closed_method',
        'parent',
        payload.processes.parent.keyed_scalars.pictureinpicture_closed_method
      ),
      (
        'pictureinpicture_opened_method',
        'content',
        payload.processes.content.keyed_scalars.pictureinpicture_opened_method
      ),
      (
        'preferences_browser_home_page_change',
        'parent',
        payload.processes.parent.keyed_scalars.preferences_browser_home_page_change
      ),
      (
        'preferences_browser_home_page_count',
        'parent',
        payload.processes.parent.keyed_scalars.preferences_browser_home_page_count
      ),
      (
        'preferences_search_query',
        'parent',
        payload.processes.parent.keyed_scalars.preferences_search_query
      ),
      (
        'preferences_use_bookmark',
        'parent',
        payload.processes.parent.keyed_scalars.preferences_use_bookmark
      ),
      (
        'preferences_use_current_page',
        'parent',
        payload.processes.parent.keyed_scalars.preferences_use_current_page
      ),
      (
        'qm_origin_directory_unexpected_filename',
        'parent',
        payload.processes.parent.keyed_scalars.qm_origin_directory_unexpected_filename
      ),
      (
        'resistfingerprinting_content_window_size',
        'parent',
        payload.processes.parent.keyed_scalars.resistfingerprinting_content_window_size
      ),
      (
        'security_client_cert',
        'parent',
        payload.processes.parent.keyed_scalars.security_client_cert
      ),
      (
        'security_contentblocker_permissions',
        'parent',
        payload.processes.parent.keyed_scalars.security_contentblocker_permissions
      ),
      (
        'security_webauthn_used',
        'parent',
        payload.processes.parent.keyed_scalars.security_webauthn_used
      ),
      (
        'storage_sync_api_usage_items_stored',
        'parent',
        payload.processes.parent.keyed_scalars.storage_sync_api_usage_items_stored
      ),
      (
        'storage_sync_api_usage_storage_consumed',
        'parent',
        payload.processes.parent.keyed_scalars.storage_sync_api_usage_storage_consumed
      ),
      (
        'telemetry_accumulate_clamped_values',
        'parent',
        payload.processes.parent.keyed_scalars.telemetry_accumulate_clamped_values
      ),
      (
        'telemetry_accumulate_unknown_histogram_keys',
        'content',
        payload.processes.content.keyed_scalars.telemetry_accumulate_unknown_histogram_keys
      ),
      (
        'telemetry_accumulate_unknown_histogram_keys',
        'gpu',
        payload.processes.gpu.keyed_scalars.telemetry_accumulate_unknown_histogram_keys
      ),
      (
        'telemetry_accumulate_unknown_histogram_keys',
        'parent',
        payload.processes.parent.keyed_scalars.telemetry_accumulate_unknown_histogram_keys
      ),
      (
        'telemetry_event_counts',
        'content',
        payload.processes.content.keyed_scalars.telemetry_event_counts
      ),
      ('telemetry_event_counts', 'gpu', payload.processes.gpu.keyed_scalars.telemetry_event_counts),
      (
        'telemetry_event_counts',
        'parent',
        payload.processes.parent.keyed_scalars.telemetry_event_counts
      ),
      (
        'telemetry_keyed_scalars_exceed_limit',
        'parent',
        payload.processes.parent.keyed_scalars.telemetry_keyed_scalars_exceed_limit
      ),
      (
        'telemetry_keyed_scalars_unknown_keys',
        'content',
        payload.processes.content.keyed_scalars.telemetry_keyed_scalars_unknown_keys
      ),
      (
        'telemetry_keyed_scalars_unknown_keys',
        'gpu',
        payload.processes.gpu.keyed_scalars.telemetry_keyed_scalars_unknown_keys
      ),
      (
        'telemetry_keyed_scalars_unknown_keys',
        'parent',
        payload.processes.parent.keyed_scalars.telemetry_keyed_scalars_unknown_keys
      ),
      (
        'update_binarytransparencyresult',
        'parent',
        payload.processes.parent.keyed_scalars.update_binarytransparencyresult
      ),
      ('update_bitshresult', 'parent', payload.processes.parent.keyed_scalars.update_bitshresult),
      ('urlbar_tips', 'parent', payload.processes.parent.keyed_scalars.urlbar_tips),
      (
        'webrtc_sdp_parser_diff',
        'content',
        payload.processes.content.keyed_scalars.webrtc_sdp_parser_diff
      ),
      (
        'webrtc_video_recv_codec_used',
        'content',
        payload.processes.content.keyed_scalars.webrtc_video_recv_codec_used
      ),
      (
        'webrtc_video_send_codec_used',
        'content',
        payload.processes.content.keyed_scalars.webrtc_video_send_codec_used
      )
    ] AS metrics
  FROM
    filtered
),
flattened_metrics AS (
  SELECT
    sample_id,
    client_id,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    metrics.name AS metric,
    metrics.process AS process,
    value.key AS key,
    value.value AS value
  FROM
    grouped_metrics
  CROSS JOIN
    UNNEST(metrics) AS metrics,
    UNNEST(metrics.value) AS value
),
sampled_data AS (
  SELECT
    *
  FROM
    flattened_metrics
  WHERE
    channel IN ("nightly", "beta")
    OR (channel = "release" AND os != "Windows")
    OR (channel = "release" AND os = "Windows" AND MOD(sample_id, @sample_size) = 0)
),
-- Using `min` for when `agg_type` is `count` returns null when all rows are null
aggregated AS (
  SELECT
    submission_date,
    sample_id,
    client_id,
    os,
    app_version,
    app_build_id,
    channel,
    metric,
    process,
    key,
    MAX(value) AS max,
    MIN(value) AS min,
    AVG(value) AS avg,
    SUM(value) AS sum,
    IF(MIN(value) IS NULL, NULL, COUNT(*)) AS count
  FROM
    sampled_data
  GROUP BY
    submission_date,
    sample_id,
    client_id,
    os,
    app_version,
    app_build_id,
    channel,
    metric,
    process,
    key
)
SELECT
  sample_id,
  client_id,
  submission_date,
  os,
  app_version,
  app_build_id,
  channel,
  ARRAY_CONCAT_AGG(
    ARRAY<
      STRUCT<
        metric STRING,
        metric_type STRING,
        key STRING,
        process STRING,
        agg_type STRING,
        value FLOAT64
      >
    >[
      (metric, 'keyed-scalar', key, process, 'max', max),
      (metric, 'keyed-scalar', key, process, 'min', min),
      (metric, 'keyed-scalar', key, process, 'avg', avg),
      (metric, 'keyed-scalar', key, process, 'sum', sum),
      (metric, 'keyed-scalar', key, process, 'count', count)
    ]
  ) AS scalar_aggregates
FROM
  aggregated
GROUP BY
  sample_id,
  client_id,
  submission_date,
  os,
  app_version,
  app_build_id,
  channel
