Skip to content

Instantly share code, notes, and snippets.

@mtsmfm
Last active September 9, 2021 09:05
Show Gist options
  • Save mtsmfm/bc0b9619369f0b8833a0c470c2cebf99 to your computer and use it in GitHub Desktop.
Save mtsmfm/bc0b9619369f0b8833a0c470c2cebf99 to your computer and use it in GitHub Desktop.
Find not used Rails endpoints via BigQuery
require 'bundler/inline'
gemfile do
eval File.read(File.join(__dir__, 'Gemfile'))
gem 'js_regex'
gem 'pry-byebug'
end
require_relative 'config/environment'
data = Rails.application.routes.routes.group_by(&:verb).transform_values {|xs|
xs.map {|x|
{regexp: JsRegex.new(x.path.to_regexp).source, spec: x.path.spec.to_s}
}
}
specs = data.values.flatten.map {|d| d[:spec] }.uniq
# Replace with your table name
# This script assumes the table has path and method column
log_table_name = 'project.dataset.table'
sql = <<~SQL
CREATE TEMPORARY FUNCTION
EXTRACT_SPEC(method STRING, path STRING)
RETURNS STRING
LANGUAGE js AS r"""
const data = #{data.to_json};
const result = (data[method] || []).find(r => new RegExp(r.regexp).exec(path));
return result && result.spec;
""";
WITH specs AS (
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64, name STRING>> [
#{specs.map.with_index {|o, i| "(#{i}, #{o.dump})" }.join(',')}
])
),
specs_with_count AS (
SELECT EXTRACT_SPEC(method, path) AS spec, COUNT(*) AS count
FROM `#{log_table_name}`
GROUP BY spec
)
SELECT name FROM specs LEFT OUTER JOIN specs_with_count ON specs.name = specs_with_count.spec WHERE specs_with_count.count IS NULL
SQL
File.write('query.sql', sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment