<commit_before>class GraphsController < ApplicationController
  before_action :set_tags, :set_root
  before_action :set_graph, only: [:show, :edit, :update, :destroy, :view_graph]
  before_action :set_view_options, only: [:show, :view_graph]
  before_action :path_redirect, :set_graphs, only: [:list_graph]
  before_action :autocomplete_search, only: [:autocomplete_graph]

  # GET /autocomplete_graph?term=xxx
  # for ajax autocomplete
  def autocomplete_graph
    render :json => @autocomplete.map {|node|
      description = node.description ? " (#{node.description})" : ""
      {label: "#{node.path}#{description}", value: node.path}
    }
  end

  # GET /list_graph
  def list_graph
    @units = params[:nav].try(:split, '_') || ['d', 'w'] # default: day and week
    @units = @units.to(1)
    if @root
      @display_graphs = @root.children.first.try(:graph?)
    else
      @display_graphs = false
    end
    render action: 'index'
  end

  # GET /view_graph
  def view_graph
    render action: 'show'
  end

  # GET /graphs
  # GET /graphs.json
  def index
    @graphs = Graph.all
  end

  # GET /graphs/1
  # GET /graphs/1.json
  def show
  end

  # GET /graphs/new
  def new
    @graph = Graph.new
  end

  # GET /graphs/1/edit
  def edit
  end

  # POST /graphs
  # POST /graphs.json
  def create
    success = ActiveRecord::Base.transaction do
      @graph = Graph.find_or_create(graph_params)
      $mfclient.post_graph(@graph.path, post_params)
    end

    respond_to do |format|
      if success
        format.html { redirect_to @graph, notice: 'Graph was successfully created.' }
        format.json { render action: 'show', status: :created, location: @graph }
      else
        format.html { render action: 'new' }
        format.json { render json: @graph.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /graphs/1
  # PATCH/PUT /graphs/1.json
  def update
    success = ActiveRecord::Base.transaction do
      @graph.update(graph_params)
      $mfclient.edit_graph(@graph.path, update_params)
    end

    respond_to do |format|
      if success
        format.html { redirect_to @graph, notice: 'Graph was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @graph.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /graphs/1
  # DELETE /graphs/1.json
  def destroy
    success = ActiveRecord::Base.transaction do
      @graph.destroy
      @graph = $mfclient.delete_graph(@graph.path) rescue nil
    end
    respond_to do |format|
      format.html { redirect_to graphs_url }
      format.json { head :no_content }
    end
  end

  private

  def post_params
    # ToDo
    {:number => 0}
  end

  def update_params
    # ToDo
    {}
  end

  def set_view_options
    @from = params[:from].present? ? Time.parse(params[:from]) : 1.day.ago.localtime
    @to   = params[:to].present?   ? Time.parse(params[:to])   : Time.now.localtime
    @width  = Settings.try(:single_graph).try(:width)
    @height = Settings.try(:single_graph).try(:height)
  end

  def path_redirect
    return unless (path = request.query_parameters[:path])
    not_found unless (node = Node.find_by(path: path))
    if node.directory?
      redirect_to list_graph_path(path)
    else
      redirect_to view_graph_path(path)
    end
  end

  def set_tags
    @tags = Graph.tag_counts_on(:tags).order('count DESC')
  end

  def set_root
    if params[:path].present?
      @root = Node.where(path: params[:path]).first
    else
      @root = Node.roots.first
    end
  end

  def autocomplete_search
    case
    when params[:term]
      term = params[:term].gsub(/ /, '%')
      @autocomplete = Node.select(:path, :description).where("path LIKE ?", "%#{term}%")
    else
      @autocomplete = Node.select(:path, :description).all
    end
    @autocomplete = @autocomplete.without_root.order('path ASC').limit(Settings.try(:autocomplete).try(:limit))
  end<commit_msg>fix a bug<commit_after>class GraphsController < ApplicationController
  before_action :set_tags, :set_root
  before_action :set_graph, only: [:show, :edit, :update, :destroy, :view_graph]
  before_action :set_view_options, only: [:show, :view_graph]
  before_action :path_redirect, :set_graphs, only: [:list_graph]
  before_action :autocomplete_search, only: [:autocomplete_graph]

  # GET /autocomplete_graph?term=xxx
  # for ajax autocomplete
  def autocomplete_graph
    render :json => @autocomplete.map {|node|
      description = node.description ? " (#{node.description})" : ""
      {label: "#{node.path}#{description}", value: node.path}
    }
  end

  # GET /list_graph
  def list_graph
    @units = params[:nav].try(:split, '_') || ['d', 'w'] # default: day and week
    @units = @units.to(1)
    if @root
      @display_graphs = @root.children.first.try(:graph?)
    else
      @display_graphs = false
    end
    render action: 'index'
  end

  # GET /view_graph
  def view_graph
    render action: 'show'
  end

  # GET /graphs
  # GET /graphs.json
  def index
    @graphs = Graph.all
  end

  # GET /graphs/1
  # GET /graphs/1.json
  def show
  end

  # GET /graphs/new
  def new
    @graph = Graph.new
  end

  # GET /graphs/1/edit
  def edit
  end

  # POST /graphs
  # POST /graphs.json
  def create
    success = ActiveRecord::Base.transaction do
      @graph = Graph.find_or_create(graph_params)
      $mfclient.post_graph(@graph.path, post_params)
    end

    respond_to do |format|
      if success
        format.html { redirect_to @graph, notice: 'Graph was successfully created.' }
        format.json { render action: 'show', status: :created, location: @graph }
      else
        format.html { render action: 'new' }
        format.json { render json: @graph.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /graphs/1
  # PATCH/PUT /graphs/1.json
  def update
    success = ActiveRecord::Base.transaction do
      @graph.update(graph_params)
      $mfclient.edit_graph(@graph.path, update_params)
    end

    respond_to do |format|
      if success
        format.html { redirect_to @graph, notice: 'Graph was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @graph.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /graphs/1
  # DELETE /graphs/1.json
  def destroy
    success = ActiveRecord::Base.transaction do
      @graph.destroy
      @graph = $mfclient.delete_graph(@graph.path) rescue nil
    end
    respond_to do |format|
      format.html { redirect_to graphs_url }
      format.json { head :no_content }
    end
  end

  private

  def post_params
    # ToDo
    {:number => 0}
  end

  def update_params
    # ToDo
    {}
  end

  def set_view_options
    @from = params[:from].present? ? Time.parse(params[:from]) : 1.day.ago.localtime
    @to   = params[:to].present?   ? Time.parse(params[:to])   : Time.now.localtime
    @width  = Settings.try(:single_graph).try(:width)
    @height = Settings.try(:single_graph).try(:height)
  end

  def path_redirect
    return unless (path = request.query_parameters[:path])
    not_found unless (node = Node.find_by(path: path))
    if node.directory?
      redirect_to list_graph_path(path)
    else
      redirect_to view_graph_path(path)
    end
  end

  def set_tags
    @tags = Graph.tag_counts_on(:tags).order('count DESC')
  end

  def set_root
    if params[:path].present?
      @root = Node.where(path: params[:path]).first
    else
      @root = Node.roots.first
    end
  end

  def autocomplete_search
    case
    when params[:term]
      term = params[:term].gsub(/ /, '%')
      @autocomplete = Node.select(:path, :description).where("path LIKE ?", "%#{term}%")
    else
      @autocomplete = Node.select(:path, :description).all
    end
    @autocomplete = @autocomplete.without_roots.order('path ASC').limit(Settings.try(:autocomplete).try(:limit))
  end
{'fraction_non_alphanumeric': 0.12103889648316142, 'fraction_numerical': 0.0024853982850751833, 'mean_word_length': 2.901114881240911, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '348685', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2598, 'n_words': 625}