<commit_before>- if @bike.present?
  = render partial: "/admin/bikes/bike_tabs", locals: {bike: @bike, active_tab: "theft_alerts", display_recovery: true}
  %h1.mt-5.mb-4
    Promoted Alerts
    = link_to "New Promoted alert", new_admin_theft_alert_path(bike_id: @bike.id), class: "small btn btn-outline-success btn-sm ml-2"


- else
  .admin-subnav
    .col-md-5
      %h1 Promoted Alerts
    .col-md-7
      %ul
        %li.nav-item.dropdown.dropleft
          %a.nav-link.dropdown-toggle{ href: "#", role: "button", "data-toggle" => "dropdown", "aria-haspopup" => "true", "aria-expanded" => "false", class: (@status != "all" ? "active" : "") }
            #{@status.titleize} #{@status == "all" ? "statuses" : "alerts"}
          .dropdown-menu
            = link_to "All statuses", url_for(sortable_search_params.merge(search_status: nil)), class: "dropdown-item #{@status == 'all' ? 'active' : '' }"
            .dropdown-divider
            - available_statuses.each do |status|
              = link_to "#{status.humanize} alerts", url_for(sortable_search_params.merge(search_status: status)), class: "dropdown-item #{@status == status ? 'active' : '' }"
        %li.nav-item.dropdown
          %a.nav-link.dropdown-toggle{ href: "#", role: "button", "data-toggle" => "dropdown", "aria-haspopup" => "true", "aria-expanded" => "false", class: (@search_paid_admin.present? ? "active" : "") }
            - if @search_paid_admin.present?
              = @search_paid_admin.humanize
            - else
              Any paid/admin
          .dropdown-menu
            = link_to "Any paid/admin", url_for(sortable_search_params.merge(search_paid_admin: nil)), class: "dropdown-item #{@search_paid_admin == 'all' ? 'active' : '' }"
            .dropdown-divider
            - available_paid_admin.each do |paid_admin|
              = link_to paid_admin.humanize, url_for(sortable_search_params.merge(search_paid_admin: paid_admin)), class: "dropdown-item #{@search_paid_admin == paid_admin ? 'active' : '' }"
        %li.nav-item
          = link_to "fbook data", url_for(sortable_search_params.merge(search_facebook_data: !@search_facebook_data)), class: "nav-link #{@search_facebook_data ? 'active' : ''}"
        %li.nav-item
          = link_to "recovered", url_for(sortable_search_params.merge(search_recovered: !@search_recovered)), class: "nav-link #{@search_recovered ? 'active' : ''}"
        %li.nav-item
          = link_to "graph", url_for(sortable_search_params.merge(render_chart: !@render_chart)), class: "nav-link #{@render_chart ? 'active' : ''}"
    = render partial: "/shared/admin_current_header", locals: { viewing: "Promoted Alerts" }

  - theft_alert_plans = TheftAlertPlan.price_ordered_desc.select { |plan| searched_theft_alerts.where(theft_alert_plan: plan).any? }
  - stolen_records = StolenRecord.recovered.with_theft_alerts.where(theft_alerts: {id: searched_theft_alerts.pluck(:id)})

  - if @render_chart
    :ruby
      theft_alerts_graph = theft_alert_plans.map do |theft_alert_plan|
        {
          name: theft_alert_plan.name,
          data: time_range_counts(collection: searched_theft_alerts.where(theft_alert_plan_id: theft_alert_plan.id))
        }
      end
      alerts_recoveries_graph = theft_alert_plans.map do |theft_alert_plan|
        {
          name: "#{theft_alert_plan.name} recoveries",
          data: time_range_counts(collection: stolen_records.where(theft_alerts: {theft_alert_plan_id: theft_alert_plan.id}), column: "theft_alerts.created_at")
        }
      end

      if @search_facebook_data
        facebook_reach_graph = theft_alert_plans.map do |theft_alert_plan|
          {
            name: theft_alert_plan.name,
            data: time_range_amounts(collection: searched_theft_alerts.where(theft_alert_plan_id: theft_alert_plan.id), amount_column: "reach")
          }
        end
      end
    .col-12.mt-2
      = column_chart theft_alerts_graph, stacked: true, thousands: ","
    - if @search_facebook_data
      %h3.mt-5
        %em Sum of Facebook reach for ads started in the period
      = column_chart facebook_reach_graph, stacked: true, thousands: ","
      .mt-5
    - unless @search_recovered
      .col-12.mt-2
        = column_chart alerts_recoveries_graph, stacked: true, thousands: ","

  - if @location_counts
    .col-md-8.offset-md-2.mb-3
      .full-screen-table{style: "max-height: 500px; overflow-y: scroll;"}
        %table.table.table-striped.table-bordered.table-sm
          %thead
            %th Country
            %th City
            %th State
            %th Count
          %tbody
            - searched_theft_alerts.cities_count.each do |count|
              %tr
                %td.small= count[0]
                %td
                  - link_text = count[1].present? ? count[1] : "Blank location"
                  = link_to link_text, admin_theft_alerts_path(sortable_search_params.merge(search_location: "#{count[1]}, #{count[2]} #{count[0]}"))
                %td.small= count[2]
                %td= admin_number_display(count[3])

  = render partial: "/shared/period_select"
  .row.mb-4.mt-4
    .col
      = form_tag admin_theft_alerts_path, method: :get, class: "form-inline" do
        = hidden_field_tag :search_recovery_display_status, @search_recovered
        = hidden_field_tag :search_status, @status
        = hidden_field_tag :search_paid_admin, @search_paid
        = hidden_field_tag :search_facebook_data, @search_facebook_data
        = hidden_field_tag :search_recovered, @search_recovered
        = hidden_field_tag :search_location_counts, @location_counts
        = render partial: "/shared/hidden_search_fields"

        .form-group.ml-auto.mr-1.mb-2
          %em.small.less-strong.mr-1.d-md-inline.d-none within
          = number_field_tag :search_distance, @distance, class: "form-control", style: "width: 5rem;"
          %em.small.less-strong.ml-1.mr-1.d-md-inline.d-none miles of
        .form-group.mr-2.mb-2
          = text_field_tag :search_location, params[:search_location], placeholder: "anywhere", class: "form-control"
        = submit_tag "Search", name: "search", class: "btn btn-primary mb-2"
      .text-right.mt-2.mb-2
        = link_to "Show location counts", admin_theft_alerts_path(sortable_search_params.merge(search_location_counts: !@location_counts)), class: "btn btn-outline-secondary btn-sm #{@location_counts ? 'active' : ''}"

    - if theft_alert_plans.present?
      .col
        .full-screen-table
          %table.table.table-striped.table-bordered.table-sm
            %thead
              %th
              %th Count
              %th Fbook Reach
              %th Fbook Spend
              %th Revenue
              %th.small Recovered
            %tbody
              - alert_count_total = 0
              - fbook_reach_total = 0
              - fbook_cents_total = 0
              - revenue_cents_total = 0
              - recovered_count_total = 0
              - theft_alert_plans.each do |theft_alert_plan|
                %tr
                  %td
                    = theft_alert_plan.name
                    %small= MoneyFormater.money_format(theft_alert_plan.amount_cents_facebook)
                  %td
                    - alert_count = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).count
                    - alert_count_total += alert_count
                    = admin_number_display(alert_count)
                  %td
                    - fbook_reach = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).sum(:reach)
                    - fbook_reach_total += fbook_reach
                    = admin_number_display(fbook_reach)
                  %td
                    - fbook_cents = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).sum(:amount_cents_facebook_spent)
                    - fbook_cents_total += fbook_cents
                    = MoneyFormater.money_format(fbook_cents)
                  %td.small
                    - revenue_cents = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).paid_cents
                    - revenue_cents_total += revenue_cents
                    = MoneyFormater.money_format(revenue_cents)
                  %td.small
                    - recovered_count = stolen_records.where(theft_alerts: {theft_alert_plan_id: theft_alert_plan.id}).count
                    - recovered_count_total += recovered_count
                    = admin_number_display(recovered_count)
              %tr{style: "border-top: 2px solid #000"}
                %td
                  %strong Total
                %td= admin_number_display(alert_count_total)
                %td= admin_number_display(fbook_reach_total)
                %td= MoneyFormater.money_format(fbook_cents_total)
                %td
                  %small= MoneyFormater.money_format(revenue_cents_total)
                %td.small= admin_number_display(recovered_count_total)


= render partial: "/shared/pagination", locals: {collection: @theft_alerts, count: searched_theft_alerts.count, name: "Promoted Alert", views_prefix: "admin"}

= render partial: "/admin/theft_alerts/table", locals: {render_sortable: true, theft_alerts: @theft_alerts, skip_bike: @bike.present?}

= render partial: "/shared/pagination", locals: {collection: @theft_alerts, count: searched_theft_alerts.count, name: "Promoted Alert", views_prefix: "admin", skip_total: true}
<commit_msg>better location counts link placement<commit_after>- if @bike.present?
  = render partial: "/admin/bikes/bike_tabs", locals: {bike: @bike, active_tab: "theft_alerts", display_recovery: true}
  %h1.mt-5.mb-4
    Promoted Alerts
    = link_to "New Promoted alert", new_admin_theft_alert_path(bike_id: @bike.id), class: "small btn btn-outline-success btn-sm ml-2"


- else
  .admin-subnav
    .col-md-5
      %h1 Promoted Alerts
    .col-md-7
      %ul
        %li.nav-item.dropdown.dropleft
          %a.nav-link.dropdown-toggle{ href: "#", role: "button", "data-toggle" => "dropdown", "aria-haspopup" => "true", "aria-expanded" => "false", class: (@status != "all" ? "active" : "") }
            #{@status.titleize} #{@status == "all" ? "statuses" : "alerts"}
          .dropdown-menu
            = link_to "All statuses", url_for(sortable_search_params.merge(search_status: nil)), class: "dropdown-item #{@status == 'all' ? 'active' : '' }"
            .dropdown-divider
            - available_statuses.each do |status|
              = link_to "#{status.humanize} alerts", url_for(sortable_search_params.merge(search_status: status)), class: "dropdown-item #{@status == status ? 'active' : '' }"
        %li.nav-item.dropdown
          %a.nav-link.dropdown-toggle{ href: "#", role: "button", "data-toggle" => "dropdown", "aria-haspopup" => "true", "aria-expanded" => "false", class: (@search_paid_admin.present? ? "active" : "") }
            - if @search_paid_admin.present?
              = @search_paid_admin.humanize
            - else
              Any paid/admin
          .dropdown-menu
            = link_to "Any paid/admin", url_for(sortable_search_params.merge(search_paid_admin: nil)), class: "dropdown-item #{@search_paid_admin == 'all' ? 'active' : '' }"
            .dropdown-divider
            - available_paid_admin.each do |paid_admin|
              = link_to paid_admin.humanize, url_for(sortable_search_params.merge(search_paid_admin: paid_admin)), class: "dropdown-item #{@search_paid_admin == paid_admin ? 'active' : '' }"
        %li.nav-item
          = link_to "fbook data", url_for(sortable_search_params.merge(search_facebook_data: !@search_facebook_data)), class: "nav-link #{@search_facebook_data ? 'active' : ''}"
        %li.nav-item
          = link_to "recovered", url_for(sortable_search_params.merge(search_recovered: !@search_recovered)), class: "nav-link #{@search_recovered ? 'active' : ''}"
        %li.nav-item
          = link_to "location counts", admin_theft_alerts_path(sortable_search_params.merge(search_location_counts: !@location_counts)), class: "nav-link #{@location_counts ? 'active' : ''}"
        %li.nav-item
          = link_to "graph", url_for(sortable_search_params.merge(render_chart: !@render_chart)), class: "nav-link #{@render_chart ? 'active' : ''}"
    = render partial: "/shared/admin_current_header", locals: { viewing: "Promoted Alerts" }

  - theft_alert_plans = TheftAlertPlan.price_ordered_desc.select { |plan| searched_theft_alerts.where(theft_alert_plan: plan).any? }
  - stolen_records = StolenRecord.recovered.with_theft_alerts.where(theft_alerts: {id: searched_theft_alerts.pluck(:id)})

  - if @render_chart
    :ruby
      theft_alerts_graph = theft_alert_plans.map do |theft_alert_plan|
        {
          name: theft_alert_plan.name,
          data: time_range_counts(collection: searched_theft_alerts.where(theft_alert_plan_id: theft_alert_plan.id))
        }
      end
      alerts_recoveries_graph = theft_alert_plans.map do |theft_alert_plan|
        {
          name: "#{theft_alert_plan.name} recoveries",
          data: time_range_counts(collection: stolen_records.where(theft_alerts: {theft_alert_plan_id: theft_alert_plan.id}), column: "theft_alerts.created_at")
        }
      end

      if @search_facebook_data
        facebook_reach_graph = theft_alert_plans.map do |theft_alert_plan|
          {
            name: theft_alert_plan.name,
            data: time_range_amounts(collection: searched_theft_alerts.where(theft_alert_plan_id: theft_alert_plan.id), amount_column: "reach")
          }
        end
      end
    .col-12.mt-2
      = column_chart theft_alerts_graph, stacked: true, thousands: ","
    - if @search_facebook_data
      %h3.mt-5
        %em Sum of Facebook reach for ads started in the period
      = column_chart facebook_reach_graph, stacked: true, thousands: ","
      .mt-5
    - unless @search_recovered
      .col-12.mt-2
        = column_chart alerts_recoveries_graph, stacked: true, thousands: ","

  - if @location_counts
    .col-md-8.offset-md-2.mb-3
      .full-screen-table{style: "max-height: 500px; overflow-y: scroll;"}
        %table.table.table-striped.table-bordered.table-sm
          %thead
            %th Country
            %th City
            %th State
            %th Count
          %tbody
            - searched_theft_alerts.cities_count.each do |count|
              %tr
                %td.small= count[0]
                %td
                  - link_text = count[1].present? ? count[1] : "Blank location"
                  = link_to link_text, admin_theft_alerts_path(sortable_search_params.merge(search_location: "#{count[1]}, #{count[2]} #{count[0]}"))
                %td.small= count[2]
                %td= admin_number_display(count[3])

  = render partial: "/shared/period_select"
  .row.mb-4.mt-4
    .col
      = form_tag admin_theft_alerts_path, method: :get, class: "form-inline" do
        = hidden_field_tag :search_recovery_display_status, @search_recovered
        = hidden_field_tag :search_status, @status
        = hidden_field_tag :search_paid_admin, @search_paid
        = hidden_field_tag :search_facebook_data, @search_facebook_data
        = hidden_field_tag :search_recovered, @search_recovered
        = hidden_field_tag :search_location_counts, @location_counts
        = render partial: "/shared/hidden_search_fields"

        .form-group.ml-auto.mr-1.mb-2
          %em.small.less-strong.mr-1.d-md-inline.d-none within
          = number_field_tag :search_distance, @distance, class: "form-control", style: "width: 5rem;"
          %em.small.less-strong.ml-1.mr-1.d-md-inline.d-none miles of
        .form-group.mr-2.mb-2
          = text_field_tag :search_location, params[:search_location], placeholder: "anywhere", class: "form-control"
        = submit_tag "Search", name: "search", class: "btn btn-primary mb-2"

    - if theft_alert_plans.present?
      .col
        .full-screen-table
          %table.table.table-striped.table-bordered.table-sm
            %thead
              %th
              %th Count
              %th Fbook Reach
              %th Fbook Spend
              %th Revenue
              %th.small Recovered
            %tbody
              - alert_count_total = 0
              - fbook_reach_total = 0
              - fbook_cents_total = 0
              - revenue_cents_total = 0
              - recovered_count_total = 0
              - theft_alert_plans.each do |theft_alert_plan|
                %tr
                  %td
                    = theft_alert_plan.name
                    %small= MoneyFormater.money_format(theft_alert_plan.amount_cents_facebook)
                  %td
                    - alert_count = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).count
                    - alert_count_total += alert_count
                    = admin_number_display(alert_count)
                  %td
                    - fbook_reach = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).sum(:reach)
                    - fbook_reach_total += fbook_reach
                    = admin_number_display(fbook_reach)
                  %td
                    - fbook_cents = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).sum(:amount_cents_facebook_spent)
                    - fbook_cents_total += fbook_cents
                    = MoneyFormater.money_format(fbook_cents)
                  %td.small
                    - revenue_cents = searched_theft_alerts.where(theft_alert_plan: theft_alert_plan).paid_cents
                    - revenue_cents_total += revenue_cents
                    = MoneyFormater.money_format(revenue_cents)
                  %td.small
                    - recovered_count = stolen_records.where(theft_alerts: {theft_alert_plan_id: theft_alert_plan.id}).count
                    - recovered_count_total += recovered_count
                    = admin_number_display(recovered_count)
              %tr{style: "border-top: 2px solid #000"}
                %td
                  %strong Total
                %td= admin_number_display(alert_count_total)
                %td= admin_number_display(fbook_reach_total)
                %td= MoneyFormater.money_format(fbook_cents_total)
                %td
                  %small= MoneyFormater.money_format(revenue_cents_total)
                %td.small= admin_number_display(recovered_count_total)


= render partial: "/shared/pagination", locals: {collection: @theft_alerts, count: searched_theft_alerts.count, name: "Promoted Alert", views_prefix: "admin"}

= render partial: "/admin/theft_alerts/table", locals: {render_sortable: true, theft_alerts: @theft_alerts, skip_bike: @bike.present?}

= render partial: "/shared/pagination", locals: {collection: @theft_alerts, count: searched_theft_alerts.count, name: "Promoted Alert", views_prefix: "admin", skip_total: true}

{'fraction_non_alphanumeric': 0.11500425350914505, 'fraction_numerical': 0.0054232241599319435, 'mean_word_length': 2.5052180395080135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3096675', 'n_tokens_mistral': 6070, 'n_tokens_neox': 5888, 'n_words': 993}