-
-
Save mtsmfm/bc0b9619369f0b8833a0c470c2cebf99 to your computer and use it in GitHub Desktop.
Find not used Rails endpoints via BigQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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