<commit_before>class TransitAssetsController < OrganizationAwareController

  def index
     @fta_asset_class = FtaAssetClass.find_by(code: params[:fta_asset_class_code])
     respond_to do |format|
      format.html
    end
  end

  #-----------------------------------------------------------------------------
  # TODO: MOST of this will be moved to a shareable module
  #-----------------------------------------------------------------------------
  def table
    code = params[:fta_asset_class_code]

    case code
    when 'bus', 'rail_car', 'ferry', 'other_passenger_vehicle'
      response = revenue_vehicles_table
    when 'track'
      response = track_table
    when 'guideway'
      response = guideway_table
    when 'power_signal'
      response = power_signal_table
    when 'capital_equipment'
      response = equipment_table
    when 'service_vehicle'
      response = service_vehicle_table
    when 'passenger_facility', 'admin_facility', 'maintenance_facility', 'parking_facility'
      response = facility_table
    else
      response = {count: 0, rows: []}
    end

    render status: 200, json: response
  end

  def facility_table
    fta_asset_class = FtaAssetClass.find_by(code: params[:fta_asset_class_code])
    fta_asset_class_id = fta_asset_class.id 
    assets = Facility.where(fta_asset_class_id: fta_asset_class_id) 
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || assets.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      search_year = (is_number? search) ? search.to_i : nil  
      query = transit_asset_query_builder(search_string, search_year)
            .or(org_query search_string)
            .or(fta_equipment_type_query search_string)
            .or(asset_subtype_query search_string)

      assets = Facility.joins('left join organizations on organization_id = organizations.id')
               .joins('left join fta_equipment_types on fta_type_id = fta_equipment_types.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .where(fta_asset_class_id: fta_asset_class_id)
               .where(query).where(transam_assetible_type: 'TransitAsset')
    else
      assets = Facility.where(fta_asset_class_id: fta_asset_class_id)
    end

    asset_table = assets.offset(offset).limit(page_size).map{ |a| a.rowify }
    return {count: assets.count, rows: asset_table}
  end

  def service_vehicle_table
    assets = ServiceVehicle.non_revenue
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || assets.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      search_year = (is_number? search) ? search.to_i : nil  
      query = query_builder(search_string, search_year)
            .or(org_query search_string)
            .or(manufacturer_query search_string)
            .or(manufacturer_model_query search_string)
            .or(fta_equipment_type_query search_string)
            .or(asset_subtype_query search_string)

      assets = ServiceVehicle.non_revenue.joins('left join organizations on organization_id = organizations.id')
               .joins('left join manufacturers on manufacturer_id = manufacturers.id')
               .joins('left join manufacturer_models on manufacturer_model_id = manufacturer_models.id')
               .joins('left join fta_equipment_types on fta_type_id = fta_equipment_types.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .where(query).where(transam_assetible_type: 'TransitAsset')
    else
      assets = ServiceVehicle.non_revenue
    end

    asset_table = assets.offset(offset).limit(page_size).map{ |a| a.rowify }
    return {count: assets.count, rows: asset_table}
  end

  def equipment_table
    assets = CapitalEquipment.all 
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || assets.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      search_year = (is_number? search) ? search.to_i : nil  
      query = transit_asset_query_builder(search_string, search_year)
            .or(org_query search_string)
            .or(manufacturer_query search_string)
            .or(manufacturer_model_query search_string)
            .or(fta_equipment_type_query search_string)
            .or(asset_subtype_query search_string)

      assets = CapitalEquipment.joins('left join organizations on organization_id = organizations.id')
               .joins('left join manufacturers on manufacturer_id = manufacturers.id')
               .joins('left join manufacturer_models on manufacturer_model_id = manufacturer_models.id')
               .joins('left join fta_equipment_types on fta_type_id = fta_equipment_types.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .where(query).where(transam_assetible_type: 'TransitAsset')
    else
      assets = CapitalEquipment.all 
    end

    asset_table = assets.offset(offset).limit(page_size).map{ |a| a.rowify }
    return {count: assets.count, rows: asset_table}
  end

  def track_table
    tracks = Track.all
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || tracks.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      query = infrastructure_query_builder(search_string)
              .or(org_query search_string)
              .or(asset_subtype_query search_string)
              .or(infrastructure_division_query search_string)
              .or(infrastructure_subdivision_query search_string)
              .or(infrastructure_track_query search_string)
              .or(infrastructure_segment_type_query search_string)

      tracks = Track.joins('left join organizations on organization_id = organizations.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .joins('left join infrastructure_divisions on infrastructure_division_id = infrastructure_divisions.id')
               .joins('left join infrastructure_subdivisions on infrastructure_subdivision_id = infrastructure_subdivisions.id')
               .joins('left join infrastructure_tracks on infrastructure_track_id = infrastructure_tracks.id')
               .joins('left join infrastructure_segment_types on infrastructure_segment_type_id = infrastructure_segment_types.id')
               .where(query)
    else
      tracks = Track.all 
    end
    
    asset_table = tracks.offset(offset).limit(page_size).map{ |a| a.rowify }
    
    return {count: tracks.count, rows: asset_table}
  end

  def guideway_table
    guideways = Guideway.all
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || guideways.count).to_i<commit_msg>Add sorting to tracks<commit_after>class TransitAssetsController < OrganizationAwareController

  def index
     @fta_asset_class = FtaAssetClass.find_by(code: params[:fta_asset_class_code])
     respond_to do |format|
      format.html
    end
  end

  #-----------------------------------------------------------------------------
  # TODO: MOST of this will be moved to a shareable module
  #-----------------------------------------------------------------------------
  def table
    code = params[:fta_asset_class_code]

    case code
    when 'bus', 'rail_car', 'ferry', 'other_passenger_vehicle'
      response = revenue_vehicles_table
    when 'track'
      response = track_table
    when 'guideway'
      response = guideway_table
    when 'power_signal'
      response = power_signal_table
    when 'capital_equipment'
      response = equipment_table
    when 'service_vehicle'
      response = service_vehicle_table
    when 'passenger_facility', 'admin_facility', 'maintenance_facility', 'parking_facility'
      response = facility_table
    else
      response = {count: 0, rows: []}
    end

    render status: 200, json: response
  end

  def facility_table
    fta_asset_class = FtaAssetClass.find_by(code: params[:fta_asset_class_code])
    fta_asset_class_id = fta_asset_class.id 
    assets = Facility.where(fta_asset_class_id: fta_asset_class_id) 
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || assets.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      search_year = (is_number? search) ? search.to_i : nil  
      query = transit_asset_query_builder(search_string, search_year)
            .or(org_query search_string)
            .or(fta_equipment_type_query search_string)
            .or(asset_subtype_query search_string)

      assets = Facility.joins('left join organizations on organization_id = organizations.id')
               .joins('left join fta_equipment_types on fta_type_id = fta_equipment_types.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .where(fta_asset_class_id: fta_asset_class_id)
               .where(query).where(transam_assetible_type: 'TransitAsset')
    else
      assets = Facility.where(fta_asset_class_id: fta_asset_class_id)
    end

    asset_table = assets.offset(offset).limit(page_size).map{ |a| a.rowify }
    return {count: assets.count, rows: asset_table}
  end

  def service_vehicle_table
    assets = ServiceVehicle.non_revenue
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || assets.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      search_year = (is_number? search) ? search.to_i : nil  
      query = query_builder(search_string, search_year)
            .or(org_query search_string)
            .or(manufacturer_query search_string)
            .or(manufacturer_model_query search_string)
            .or(fta_equipment_type_query search_string)
            .or(asset_subtype_query search_string)

      assets = ServiceVehicle.non_revenue.joins('left join organizations on organization_id = organizations.id')
               .joins('left join manufacturers on manufacturer_id = manufacturers.id')
               .joins('left join manufacturer_models on manufacturer_model_id = manufacturer_models.id')
               .joins('left join fta_equipment_types on fta_type_id = fta_equipment_types.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .where(query).where(transam_assetible_type: 'TransitAsset')
    else
      assets = ServiceVehicle.non_revenue
    end

    asset_table = assets.offset(offset).limit(page_size).map{ |a| a.rowify }
    return {count: assets.count, rows: asset_table}
  end

  def equipment_table
    assets = CapitalEquipment.all 
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || assets.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    query = nil 
    if search
      search_string = "%#{search}%"
      search_year = (is_number? search) ? search.to_i : nil  
      query = transit_asset_query_builder(search_string, search_year)
            .or(org_query search_string)
            .or(manufacturer_query search_string)
            .or(manufacturer_model_query search_string)
            .or(fta_equipment_type_query search_string)
            .or(asset_subtype_query search_string)

      assets = CapitalEquipment.joins('left join organizations on organization_id = organizations.id')
               .joins('left join manufacturers on manufacturer_id = manufacturers.id')
               .joins('left join manufacturer_models on manufacturer_model_id = manufacturer_models.id')
               .joins('left join fta_equipment_types on fta_type_id = fta_equipment_types.id')
               .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
               .where(query).where(transam_assetible_type: 'TransitAsset')
    else
      assets = CapitalEquipment.all 
    end

    asset_table = assets.offset(offset).limit(page_size).map{ |a| a.rowify }
    return {count: assets.count, rows: asset_table}
  end

  def track_table
    tracks = Track.all
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || tracks.count).to_i
    search = (table_params[:search]) 
    offset = page*page_size

    # Joining
    tracks = Track.joins('left join organizations on organization_id = organizations.id')
             .joins('left join asset_subtypes on asset_subtype_id = asset_subtypes.id')
             .joins('left join infrastructure_divisions on infrastructure_division_id = infrastructure_divisions.id')
             .joins('left join infrastructure_subdivisions on infrastructure_subdivision_id = infrastructure_subdivisions.id')
             .joins('left join infrastructure_tracks on infrastructure_track_id = infrastructure_tracks.id')
             .joins('left join infrastructure_segment_types on infrastructure_segment_type_id = infrastructure_segment_types.id')

    # Filter the Results if a search was entered
    if search
      search_string = "%#{search}%"
      # Searching
      query = infrastructure_query_builder(search_string)
              .or(org_query search_string)
              .or(asset_subtype_query search_string)
              .or(infrastructure_division_query search_string)
              .or(infrastructure_subdivision_query search_string)
              .or(infrastructure_track_query search_string)
              .or(infrastructure_segment_type_query search_string)
      tracks = tracks.where(query)
    end

    # Sort the Results
    tracks = tracks.order(current_user.table_sort_string :track)
    
    # Rowify the Results
    asset_table = tracks.offset(offset).limit(page_size).map{ |a| a.rowify }
    
    return {count: tracks.count, rows: asset_table}
  end

  def guideway_table
    guideways = Guideway.all
    page = (table_params[:page] || 0).to_i
    page_size = (table_params[:page_size] || guideways.count).to_i
{'fraction_non_alphanumeric': 0.10675441634915137, 'fraction_numerical': 0.0012469691721510219, 'mean_word_length': 2.8984607075344315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3822828', 'n_tokens_mistral': 4376, 'n_tokens_neox': 4229, 'n_words': 859}