Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/dbt/metrics_recorder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Dbt
class MetricsRecorder
def initialize(schema)
@schema = schema
@metrics = []
end

def record(model_name, model_path, start_time, end_time)
@metrics << {
model_name: model_name,
model_path: model_path,
start_time: start_time,
end_time: end_time,
duration: end_time - start_time
}
end

def save
begin
# Create the metrics table if it doesn't exist
ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE IF NOT EXISTS #{@schema}.dbt_internal_metrics (
id SERIAL PRIMARY KEY,
run_id INTEGER,
model_name TEXT,
model_path TEXT,
start_time TIMESTAMP,
end_time TIMESTAMP,
duration FLOAT
)
SQL

# Generate a new run_id
result = ActiveRecord::Base.connection.execute("SELECT MAX(run_id) AS last_run_id FROM #{@schema}.dbt_internal_metrics")
last_run_id = result.first["last_run_id"] || 0
run_id = last_run_id + 1

# Insert all metrics into the database
@metrics.each do |metric|
ActiveRecord::Base.connection.execute <<-SQL
INSERT INTO #{@schema}.dbt_internal_metrics (run_id, model_name, model_path, start_time, end_time, duration)
VALUES (
#{run_id},
'#{metric[:model_name]}',
'#{metric[:model_path]}',
'#{metric[:start_time]}',
'#{metric[:end_time]}',
#{metric[:duration]}
)
SQL
end
rescue => e
# Handle exceptions within the class
puts "Warning: MetricsRecorder encountered an error during save: #{e.message}"
end
end
end
end
11 changes: 10 additions & 1 deletion lib/dbt/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ def run(custom_schema = nil, glob_path = "app/sql/**/*.sql")
graph = Dagwood::DependencyGraph.new dependencies
md = Mermaid.markdown_for dependencies
Mermaid.generate_file md
metrics_recorder = MetricsRecorder.new(schema)
graph.order.each do |model_name|
models.find { |m| m.name == model_name }.build
model = models.find { |m| m.name == model_name }
start_time = Time.now
model.build
end_time = Time.now
# Record the timing data
metrics_recorder.record(model.name, model.filepath, start_time, end_time)
end
# Save all metrics after the run completes
metrics_recorder.save
graph.order
end

def test
Expand Down