Skip to content

perf(export): stream heartbeat export data #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 38 additions & 25 deletions app/controllers/my/heartbeats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module My
class HeartbeatsController < ApplicationController
include ActionController::Live
before_action :ensure_current_user


def export
all_data = params[:all_data] == "true"
if all_data
Expand All @@ -25,19 +25,36 @@ def export
.order(time: :asc)
end

total_heartbeats = heartbeats.count
total_duration_seconds = heartbeats.duration_seconds

filename = "heartbeats_#{current_user.slack_uid}_#{start_date.strftime('%Y%m%d')}_#{end_date.strftime('%Y%m%d')}.json"

response.headers["Content-Type"] = "application/json"
response.headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
response.headers["X-Accel-Buffering"] = "no"

response.stream.write "{"
response.stream.write "\"export_info\": {"
response.stream.write "\"exported_at\": \"" + Time.current.iso8601 + "\","
response.stream.write "\"date_range\": {"
response.stream.write "\"start_date\": \"" + start_date.iso8601 + "\","
response.stream.write "\"end_date\": \"" + end_date.iso8601 + "\""
response.stream.write "},"
response.stream.write "\"total_heartbeats\": " + total_heartbeats.to_s + ","
response.stream.write "\"total_duration_seconds\": " + total_duration_seconds.to_s
response.stream.write "},"
response.stream.write "\"heartbeats\": ["

export_data = {
export_info: {
exported_at: Time.current.iso8601,
date_range: {
start_date: start_date.iso8601,
end_date: end_date.iso8601
},
total_heartbeats: heartbeats.count,
total_duration_seconds: heartbeats.duration_seconds
},
heartbeats: heartbeats.map do |heartbeat|
{
first = true
heartbeats.find_in_batches(batch_size: 1000) do |batch|
batch.each do |heartbeat|
if first
first = false
else
response.stream.write ","
end
hb_json = {
id: heartbeat.id,
time: Time.at(heartbeat.time).iso8601,
entity: heartbeat.entity,
Expand All @@ -60,20 +77,16 @@ def export
source_type: heartbeat.source_type,
created_at: heartbeat.created_at.iso8601,
updated_at: heartbeat.updated_at.iso8601
}
}.to_json
response.stream.write hb_json
end
}

filename = "heartbeats_#{current_user.slack_uid}_#{start_date.strftime('%Y%m%d')}_#{end_date.strftime('%Y%m%d')}.json"

respond_to do |format|
format.json {
send_data export_data.to_json,
filename: filename,
type: "application/json",
disposition: "attachment"
}
ActiveRecord::Base.clear_active_connections!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure you mean to do this?

Copy link
Contributor Author

@deployor deployor Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea? Thats the exact one change i made from before, Its not global no? It clears the connections from the where clause that are idle-

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if im wrong?

end

response.stream.write "]"
response.stream.write "}"
ensure
response.stream.close
end

private
Expand Down