<commit_before>module ApplicationController::Timelines
  extend ActiveSupport::Concern

  # Process changes to timeline selection
  def tl_chooser
    @record = identify_tl_or_perf_record
    @tl_record = @record.kind_of?(MiqServer) ? @record.vm : @record # Use related server vm record
    @tl_options[:typ] = params[:tl_typ] if params[:tl_typ]
    @tl_options[:days] = params[:tl_days] if params[:tl_days]
    @tl_options[:hourly_date] = params[:miq_date_1] if params[:miq_date_1] && @tl_options[:typ] == "Hourly"
    @tl_options[:daily_date] = params[:miq_date_1] if params[:miq_date_1] && @tl_options[:typ] == "Daily"

    # set variables for type of timeline is selected
    if params[:tl_show]
      @tl_options[:tl_show] = params[:tl_show]
      tl_gen_timeline_data
      return unless @timeline
    end

    if @tl_options[:tl_show] == "timeline"
      @tl_options[:filter1] = params[:tl_fl_grp1] if params[:tl_fl_grp1]
      @tl_options[:filter2] = params[:tl_fl_grp2] if params[:tl_fl_grp2]
      @tl_options[:filter3] = params[:tl_fl_grp3] if params[:tl_fl_grp3]
      #if pull down values have been switched
      @tl_options[:filter1] = "" if (@tl_options[:filter1] == @tl_options[:filter2] || @tl_options[:filter1] == @tl_options[:filter3]) && !params[:tl_fl_grp1]
      @tl_options[:filter2] = "" if (@tl_options[:filter2] == @tl_options[:filter3] || @tl_options[:filter2] == @tl_options[:filter1]) && !params[:tl_fl_grp2]
      @tl_options[:filter3] = "" if (@tl_options[:filter3] == @tl_options[:filter2] || @tl_options[:filter3] == @tl_options[:filter1]) && !params[:tl_fl_grp3]
    else
      @tl_options[:tl_result] = params[:tl_result] if params[:tl_result]
      if params[:tl_fl_grp_all] == "1"
        @tl_options[:tl_filter_all] = true
        @tl_options[:etypes].sort.each do |e|
          @tl_options[:applied_filters].push(e)
        end
      elsif params[:tl_fl_grp_all] == "null"
        @tl_options[:tl_filter_all] = false
        @tl_options[:applied_filters] = Array.new
        @tl_options[:events].sort.each_with_index do |e,i|
          @tl_options["pol_filter#{i+1}".to_sym] = ""
          @tl_options["pol_fltr#{i+1}".to_sym] = ""
        end
      end
      # Look through the event type checkbox keys
      @tl_options[:etypes].sort.each_with_index do |e,i|
        ekey = "tl_fl_grp#{i+1}__#{e.gsub(" ", "_")}".to_sym
        if params[ekey] == "1" || (@tl_options[:tl_filter_all] && params[ekey] != "null")
          @tl_options["pol_filter#{i+1}".to_sym] = e
          @tl_options[:applied_filters].push(e) unless @tl_options[:applied_filters].include?(e) || @tl_options[:tl_filter_all] = false
        elsif params[ekey] == "null"
          @tl_options[:tl_filter_all] = false
          @tl_options["pol_filter#{i+1}".to_sym] = nil
          @tl_options[:applied_filters].delete(e)
        end
      end
    end

    @tl_options[:fl_typ] = params[:tl_fl_typ] if params[:tl_fl_typ]
    if @tl_options[:tl_show] == "timeline" &&
        (@tl_options[:filter1].nil? || @tl_options[:filter1] == "") &&
        (@tl_options[:filter2].nil? || @tl_options[:filter2] == "") &&
        (@tl_options[:filter3].nil? || @tl_options[:filter3] == "")
      add_flash(_("At least one %s must be selected") % "filter", :warning)
    elsif @tl_options[:tl_show] == "policy_timeline"
      flg = true
      @tl_options[:events].sort.each_with_index do |e,i|
        if !@tl_options["pol_filter#{i+1}".to_sym].nil? && @tl_options["pol_filter#{i+1}".to_sym] != ""
          flg = false
          tl_build_timeline(refresh="n")
          break
        end
      end
      add_flash(_("At least one %s must be selected") % "filter", :warning) if flg
    else
      tl_gen_timeline_data(refresh="n")
      return unless @timeline
    end

    if @tl_options[:tl_show] == "timeline"
      if !@tl_options[:filter1].nil? && @tl_options[:filter1] != ""
        @tl_options[:fltr1] = tl_build_filter(@tl_groups_hash[@tl_options[:filter1]])
      else
        @tl_options[:fltr1] = ""
      end
      if !@tl_options[:filter2].nil? && @tl_options[:filter2] != ""
        @tl_options[:fltr2] = tl_build_filter(@tl_groups_hash[@tl_options[:filter2]])
      else
        @tl_options[:fltr2] = ""
      end
      if !@tl_options[:filter3].nil? && @tl_options[:filter3] != ""
        @tl_options[:fltr3] = tl_build_filter(@tl_groups_hash[@tl_options[:filter3]])
      else
        @tl_options[:fltr3] = ""
      end
    else
      @tl_options[:events].sort.each_with_index do |e,i|
        fltr = "pol_filter#{i+1}".to_sym
        pol_fltr = "pol_fltr#{i+1}".to_sym
        if !@tl_options[fltr].nil? && @tl_options[fltr] != ""
          f = tl_build_policy_filter(@tl_options[fltr])
          @tl_options[pol_fltr] = f == "" ? "" : f
        else
          @tl_options[pol_fltr] = ""
        end
      end
    end
    @timeline = true
    add_flash(_("No events available for this timeline"), :warning) if @tl_options[:sdate].nil? && @tl_options[:edate].nil?
    render :update do |page|
      page.replace("flash_msg_div", :partial => "layouts/flash_msg")
      page.replace("tl_options_div", :partial=>"layouts/tl_options")
      page.replace("tl_div", :partial=>"layouts/tl_detail")
      page << "ManageIQ.calendar.calDateFrom = new Date(#{@tl_options[:sdate]});" unless @tl_options[:sdate].nil?
      page << "ManageIQ.calendar.calDateTo = new Date(#{@tl_options[:edate]});" unless @tl_options[:edate].nil?
      page << 'miqBuildCalendar();'
      if @tl_options[:tl_show] == "timeline"
        page << "$('#filter1').val('#{@tl_options[:fltr1]}');"
        page << "$('#filter2').val('#{@tl_options[:fltr2]}');"
        page << "$('#filter3').val('#{@tl_options[:fltr3]}');"
      else
        @tl_options[:events].sort.each_with_index do |e,i|
          fltr = "pol_fltr#{i+1}".to_sym
          page << "$('#filter#{i + 1}').val('#{@tl_options[fltr]}');"
        end
      end
      page << "miqSparkle(false);"
      #page << 'performFiltering(tl, [0,1]);'
    end
  end

  def timeline_data
    return if session[:tl_xml_blob_id].nil?
    blob = BinaryBlob.find_by_id(session[:tl_xml_blob_id])
    return if blob.nil?
    render :xml => blob.binary
    blob.destroy<commit_msg>Refactor timeline_data method<commit_after>module ApplicationController::Timelines
  extend ActiveSupport::Concern

  # Process changes to timeline selection
  def tl_chooser
    @record = identify_tl_or_perf_record
    @tl_record = @record.kind_of?(MiqServer) ? @record.vm : @record # Use related server vm record
    @tl_options[:typ] = params[:tl_typ] if params[:tl_typ]
    @tl_options[:days] = params[:tl_days] if params[:tl_days]
    @tl_options[:hourly_date] = params[:miq_date_1] if params[:miq_date_1] && @tl_options[:typ] == "Hourly"
    @tl_options[:daily_date] = params[:miq_date_1] if params[:miq_date_1] && @tl_options[:typ] == "Daily"

    # set variables for type of timeline is selected
    if params[:tl_show]
      @tl_options[:tl_show] = params[:tl_show]
      tl_gen_timeline_data
      return unless @timeline
    end

    if @tl_options[:tl_show] == "timeline"
      @tl_options[:filter1] = params[:tl_fl_grp1] if params[:tl_fl_grp1]
      @tl_options[:filter2] = params[:tl_fl_grp2] if params[:tl_fl_grp2]
      @tl_options[:filter3] = params[:tl_fl_grp3] if params[:tl_fl_grp3]
      #if pull down values have been switched
      @tl_options[:filter1] = "" if (@tl_options[:filter1] == @tl_options[:filter2] || @tl_options[:filter1] == @tl_options[:filter3]) && !params[:tl_fl_grp1]
      @tl_options[:filter2] = "" if (@tl_options[:filter2] == @tl_options[:filter3] || @tl_options[:filter2] == @tl_options[:filter1]) && !params[:tl_fl_grp2]
      @tl_options[:filter3] = "" if (@tl_options[:filter3] == @tl_options[:filter2] || @tl_options[:filter3] == @tl_options[:filter1]) && !params[:tl_fl_grp3]
    else
      @tl_options[:tl_result] = params[:tl_result] if params[:tl_result]
      if params[:tl_fl_grp_all] == "1"
        @tl_options[:tl_filter_all] = true
        @tl_options[:etypes].sort.each do |e|
          @tl_options[:applied_filters].push(e)
        end
      elsif params[:tl_fl_grp_all] == "null"
        @tl_options[:tl_filter_all] = false
        @tl_options[:applied_filters] = Array.new
        @tl_options[:events].sort.each_with_index do |e,i|
          @tl_options["pol_filter#{i+1}".to_sym] = ""
          @tl_options["pol_fltr#{i+1}".to_sym] = ""
        end
      end
      # Look through the event type checkbox keys
      @tl_options[:etypes].sort.each_with_index do |e,i|
        ekey = "tl_fl_grp#{i+1}__#{e.gsub(" ", "_")}".to_sym
        if params[ekey] == "1" || (@tl_options[:tl_filter_all] && params[ekey] != "null")
          @tl_options["pol_filter#{i+1}".to_sym] = e
          @tl_options[:applied_filters].push(e) unless @tl_options[:applied_filters].include?(e) || @tl_options[:tl_filter_all] = false
        elsif params[ekey] == "null"
          @tl_options[:tl_filter_all] = false
          @tl_options["pol_filter#{i+1}".to_sym] = nil
          @tl_options[:applied_filters].delete(e)
        end
      end
    end

    @tl_options[:fl_typ] = params[:tl_fl_typ] if params[:tl_fl_typ]
    if @tl_options[:tl_show] == "timeline" &&
        (@tl_options[:filter1].nil? || @tl_options[:filter1] == "") &&
        (@tl_options[:filter2].nil? || @tl_options[:filter2] == "") &&
        (@tl_options[:filter3].nil? || @tl_options[:filter3] == "")
      add_flash(_("At least one %s must be selected") % "filter", :warning)
    elsif @tl_options[:tl_show] == "policy_timeline"
      flg = true
      @tl_options[:events].sort.each_with_index do |e,i|
        if !@tl_options["pol_filter#{i+1}".to_sym].nil? && @tl_options["pol_filter#{i+1}".to_sym] != ""
          flg = false
          tl_build_timeline(refresh="n")
          break
        end
      end
      add_flash(_("At least one %s must be selected") % "filter", :warning) if flg
    else
      tl_gen_timeline_data(refresh="n")
      return unless @timeline
    end

    if @tl_options[:tl_show] == "timeline"
      if !@tl_options[:filter1].nil? && @tl_options[:filter1] != ""
        @tl_options[:fltr1] = tl_build_filter(@tl_groups_hash[@tl_options[:filter1]])
      else
        @tl_options[:fltr1] = ""
      end
      if !@tl_options[:filter2].nil? && @tl_options[:filter2] != ""
        @tl_options[:fltr2] = tl_build_filter(@tl_groups_hash[@tl_options[:filter2]])
      else
        @tl_options[:fltr2] = ""
      end
      if !@tl_options[:filter3].nil? && @tl_options[:filter3] != ""
        @tl_options[:fltr3] = tl_build_filter(@tl_groups_hash[@tl_options[:filter3]])
      else
        @tl_options[:fltr3] = ""
      end
    else
      @tl_options[:events].sort.each_with_index do |e,i|
        fltr = "pol_filter#{i+1}".to_sym
        pol_fltr = "pol_fltr#{i+1}".to_sym
        if !@tl_options[fltr].nil? && @tl_options[fltr] != ""
          f = tl_build_policy_filter(@tl_options[fltr])
          @tl_options[pol_fltr] = f == "" ? "" : f
        else
          @tl_options[pol_fltr] = ""
        end
      end
    end
    @timeline = true
    add_flash(_("No events available for this timeline"), :warning) if @tl_options[:sdate].nil? && @tl_options[:edate].nil?
    render :update do |page|
      page.replace("flash_msg_div", :partial => "layouts/flash_msg")
      page.replace("tl_options_div", :partial=>"layouts/tl_options")
      page.replace("tl_div", :partial=>"layouts/tl_detail")
      page << "ManageIQ.calendar.calDateFrom = new Date(#{@tl_options[:sdate]});" unless @tl_options[:sdate].nil?
      page << "ManageIQ.calendar.calDateTo = new Date(#{@tl_options[:edate]});" unless @tl_options[:edate].nil?
      page << 'miqBuildCalendar();'
      if @tl_options[:tl_show] == "timeline"
        page << "$('#filter1').val('#{@tl_options[:fltr1]}');"
        page << "$('#filter2').val('#{@tl_options[:fltr2]}');"
        page << "$('#filter3').val('#{@tl_options[:fltr3]}');"
      else
        @tl_options[:events].sort.each_with_index do |e,i|
          fltr = "pol_fltr#{i+1}".to_sym
          page << "$('#filter#{i + 1}').val('#{@tl_options[fltr]}');"
        end
      end
      page << "miqSparkle(false);"
      #page << 'performFiltering(tl, [0,1]);'
    end
  end

  def timeline_data
    blob = BinaryBlob.find(session[:tl_xml_blob_id])
    render :xml => blob.binary
    blob.destroy
{'fraction_non_alphanumeric': 0.17422647011177708, 'fraction_numerical': 0.011825692531994168, 'mean_word_length': 3.5310091743119267, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1686286', 'n_tokens_mistral': 4471, 'n_tokens_neox': 4475, 'n_words': 695}