Skip to content
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

CSV accurate count #991

Merged
merged 4 commits into from
Apr 2, 2024
Merged
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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,18 @@ My Title,Hello World!
```

The files uploaded to your Active Storage service provider will be renamed to
include an ISO 8601 timestamp and the Task name in snake case format. The CSV is
expected to have a trailing newline at the end of the file.
include an ISO 8601 timestamp and the Task name in snake case format.

The implicit `#count` method loads and parses the entire file to determine the
accurate number of rows. With files with millions of rows, it takes several
seconds to process. Consider skipping the count (defining a `count` that returns
`nil`) or use an approximation, eg: count the number of new lines:

```ruby
def count(task)
task.csv_content.count("\n") - 1
end
```

#### Batch CSV Tasks

Expand Down
9 changes: 4 additions & 5 deletions app/models/maintenance_tasks/csv_collection_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ def collection(task)
CSV.new(task.csv_content, headers: true)
end

# The number of rows to be processed. Excludes the header row from the
# count and assumes a trailing newline is at the end of the CSV file.
# Note that this number is an approximation based on the number of
# newlines.
# The number of rows to be processed.
# It uses the CSV library for an accurate row count.
# Note that the entire file is loaded. It will take several seconds with files with millions of rows.
#
# @return [Integer] the approximate number of rows to process.
def count(task)
task.csv_content.count("\n") - 1
CSV.new(task.csv_content, headers: true).count
end

# Return that the Task processes CSV content.
Expand Down
Loading