Skip to content

Commit

Permalink
Accurate count for CSV tasks (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-system authored Apr 2, 2024
1 parent c818b46 commit 613445c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
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

0 comments on commit 613445c

Please sign in to comment.