Skip to content
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
4 changes: 3 additions & 1 deletion app/admin/audio/audio_recitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@
FileUtils.mkdir_p("#{Rails.root}/public/segments_data")
FileUtils.mv(file, file_path)

AudioSegment::SurahBySurah.delay.import(
AsyncResourceActionJob.perform_later(
AudioSegment::SurahBySurah,
:import,
recitation_id: resource.id,
file_path: file_path,
remove_existing: remove_existing
Expand Down
4 changes: 3 additions & 1 deletion app/admin/audio/recitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
FileUtils.mkdir_p("#{Rails.root}/public/segments_data")
FileUtils.mv(file, file_path)

AudioSegment::AyahByAyah.delay.import(
AsyncResourceActionJob.perform_later(
AudioSegment::AyahByAyah,
:import,
recitation_id: resource.id,
file_path: file_path,
remove_existing: remove_existing
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/segments/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ def check_segments_database
return redirect_to segments_setup_db_path, alert: "Segments database is missing"
end

::Segments::Database.current.load_db
begin
::Segments::Database.current.load_db
rescue => e
return redirect_to segments_setup_db_path, alert: "Can't load the current DB. Please update a new zip"
end
end

def init_presenter
Expand Down
4 changes: 3 additions & 1 deletion app/models/segments/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def download_db
end
end
end

rescue Exception => e
Rails.logger.error "Failed to download and extract DB file: #{e.message}"
ensure
File.delete(zip_temp_path) if File.exist?(zip_temp_path)
end

Expand Down
141 changes: 80 additions & 61 deletions lib/tasks/audio_segments.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,45 @@ namespace :audio_segments do
recitations.each do |recitation|
puts "Reciter #{recitation.id} - #{recitation.name}:"

output_path = Rails.root.join("public", "segment_gaps", "reciter", "#{recitation.get_resource_content.id}.csv")

CSV.open(output_path, "w") do |csv|
csv << [
"ayah",
"start",
"end",
"next ayah",
"next start",
"gap"
]

1.upto(114) do |chapter_id|
segments = recitation
.audio_segments
.where(chapter_id: chapter_id)
.order('verse_id ASC')

segments.each_with_index do |seg, i|
nxt = segments[i + 1]
next if nxt.nil?

gap = seg.timestamp_to - nxt.timestamp_from
next if gap == 0
invalid_segments = []
1.upto(114) do |chapter_id|
segments = recitation
.audio_segments
.where(chapter_id: chapter_id)
.order('verse_id ASC')

segments.each_with_index do |seg, i|
nxt = segments[i + 1]
next if nxt.nil?

gap = seg.timestamp_to - nxt.timestamp_from
next if [0, 1, -1].include?(gap)

invalid_segments << [
seg.verse_key,
seg.timestamp_from,
seg.timestamp_to,
nxt.verse_key,
nxt.timestamp_from,
gap
]
end
end

csv << [
seg.verse_key,
seg.timestamp_from,
seg.timestamp_to,
nxt.verse_key,
nxt.timestamp_from,
gap
]
if invalid_segments.present?
output_path = Rails.root.join("public", "segment_gaps", "reciter", "#{recitation.get_resource_content.id}.csv")

CSV.open(output_path, "w") do |csv|
csv << [
"ayah",
"start",
"end",
"next ayah",
"next start",
"gap"
]
invalid_segments.each do |row|
csv < row
end
end
end
Expand Down Expand Up @@ -77,39 +83,52 @@ namespace :audio_segments do
FileUtils.mkdir_p("public/segment_gaps/manifest")
output_path = Rails.root.join("public", "segment_gaps", "manifest", "#{id}.csv")

CSV.open(output_path, "w") do |csv|
csv << [
"surah",
"ayah",
"start",
"end",
"next_start",
"gap"
]

segments.each do |surah, surah_segments|
sorted = surah_segments.sort_by { |s| s["ayah"] }
sorted.each_with_index do |seg, i|
nxt = sorted[i + 1]
next if nxt.nil?

gap = seg["end"] - nxt["start"]
next unless gap > 0

csv << [
surah,
seg["ayah"],
seg["start"],
seg["end"],
nxt["start"],
gap
]
end
invalid_segments = []
segments.each do |surah, surah_segments|
sorted = surah_segments.sort_by { |s| s["ayah"] }
sorted.each_with_index do |seg, i|
nxt = sorted[i + 1]
next if nxt.nil?

gap = seg["end"] - nxt["start"]
next if [0, 1, -1].include?(gap)

invalid_segments << [
surah,
seg["ayah"],
seg["start"],
seg["end"],
nxt["start"],
gap
]
end
end

puts "CSV exported to #{output_path}"
if invalid_segments.present?
CSV.open(output_path, "w") do |csv|
csv << [
"surah",
"ayah",
"start",
"end",
"next_start",
"gap"
]
end

invalid_segments.each do |row|
csv < row
end
else
puts "Segments for recitation #{id} looks good!"
end
end

output_path = "public/segment_gaps/manifest"
archive_path = "#{output_path}.tar.bz2"
system('tar', '-cjf', archive_path, '-C', File.dirname(output_path), File.basename(output_path))

puts "CSV exported to #{archive_path}"
end

desc "Find Audio::Segment records with missing, misplaced, or invalid timing issues"
Expand Down
Loading
Loading