~singpolyma/sgx-jmp

940aa189b32204928a656e18598c99aee583a04e — Stephen Paul Weber 1 year, 11 months ago e1e1383
Retry failed transcription jobs as en

In case language_id guessed wrong
4 files changed, 29 insertions(+), 7 deletions(-)

M lib/rev_ai.rb
M lib/statsd.rb
M sgx_jmp.rb
M web.rb
M lib/rev_ai.rb => lib/rev_ai.rb +20 -3
@@ 7,8 7,9 @@ require "json"
class RevAi
	class Failed < StandardError; end

	def initialize(token: CONFIG[:rev_ai_token])
	def initialize(token: CONFIG[:rev_ai_token], logger: log)
		@token = token
		@log = logger
	end

	def stt(language, media_url, callback_url, **kwargs)


@@ 24,9 25,9 @@ class RevAi
		)
	end

	def stt_result(job)
	def stt_result(job, url)
		job = job["job"]
		return failed(job) if job["status"] == "failed"
		return maybe_retry_as_en(job, url) if job["status"] == "failed"

		req(
			:get,


@@ 79,4 80,20 @@ protected
	def failed(job)
		EMPromise.reject(Failed.new("#{job['failure']} #{job['failure_detail']}"))
	end

	def maybe_retry_as_en(job, url)
		return failed(job) if job["language"] == "en"

		metadata = JSON.parse(job["metadata"]).transform_keys(&:to_sym)
		return failed(job) if metadata[:retry]

		retry_as_en(metadata, url)
	end

	def retry_as_en(metadata, url)
		@log.info "Retry transcription as EN"
		stt("en", metadata["media_url"], url, **metadata.merge(retry: true)).then do
			{}
		end
	end
end

M lib/statsd.rb => lib/statsd.rb +5 -0
@@ 30,4 30,9 @@ Registration::Payment::Mail.statsd_count :write, "registration.payment.mail"
Registration::Finish.extend StatsD::Instrument
Registration::Finish.statsd_count :write, "registration.finish"

RevAi.extend StatsD::Instrument
RevAi.statsd_count :stt, "rev_ai.stt"
RevAi.statsd_count :language_id, "rev_ai.language_id"
RevAi.statsd_count :retry_as_en, "rev_ai.retry_as_en"

# rubocop:enable Layout/LineLength

M sgx_jmp.rb => sgx_jmp.rb +1 -1
@@ 100,8 100,8 @@ require_relative "lib/registration"
require_relative "lib/transaction"
require_relative "lib/tel_selections"
require_relative "lib/snikket"
require_relative "lib/statsd"
require_relative "web"
require_relative "lib/statsd"

ELECTRUM = Electrum.new(**CONFIG[:electrum])
EM::Hiredis::Client.load_scripts_from("./redis_lua")

M web.rb => web.rb +3 -3
@@ 122,7 122,7 @@ class Web < Roda
	end

	def rev_ai
		RevAi.new
		RevAi.new(logger: log.child(loggable_params))
	end

	TEL_CANDIDATES = {


@@ 270,8 270,8 @@ class Web < Roda
						end

						r.post "transcription" do
							rev_ai.stt_result(params).then { |result|
								next "OK" if result["text"].empty?
							rev_ai.stt_result(params, request.url).then { |result|
								next "OK" if result["text"].to_s.empty?

								customer_repo.find(
									result.dig("metadata", "customer_id")