~singpolyma/sgx-jmp

f05fc82879f5f48c34dd732664c931f4c0e6715e — Phillip Davis 5 days ago 6c501f1
dont write cdr if tombed
3 files changed, 82 insertions(+), 8 deletions(-)

M lib/trust_level.rb
M test/test_web.rb
M web.rb
M lib/trust_level.rb => lib/trust_level.rb +20 -0
@@ 32,6 32,10 @@ module TrustLevel
			new if manual == "Tomb"
		end

		def write_cdr?
			false
		end

		def support_call?(*)
			false
		end


@@ 58,6 62,10 @@ module TrustLevel
			new if manual == "Basement" || (!manual && settled_amount < 10)
		end

		def write_cdr?
			true
		end

		def support_call?(rate, concurrency)
			rate <= 0.02 && concurrency < 1
		end


@@ 84,6 92,10 @@ module TrustLevel
			new if manual == "Paragon" || (!manual && settled_amount > 60)
		end

		def write_cdr?
			true
		end

		def support_call?(_, concurrency)
			concurrency < 10
		end


@@ 110,6 122,10 @@ module TrustLevel
			new if manual == "Olympias"
		end

		def write_cdr?
			true
		end

		def support_call?(*)
			true
		end


@@ 151,6 167,10 @@ module TrustLevel
			@max_rate = EXPENSIVE_ROUTE.fetch(plan_name, 0.1)
		end

		def write_cdr?
			true
		end

		def support_call?(rate, concurrency)
			rate <= @max_rate && concurrency < 4
		end

M test/test_web.rb => test/test_web.rb +40 -2
@@ 17,6 17,10 @@ ReachableRedis = Minitest::Mock.new
class WebTest < Minitest::Test
	include Rack::Test::Methods

	def setup
		@cdr_repo = CDRRepo.new(db: FakeDB.new)
	end

	def app
		Web.opts[:customer_repo] = CustomerRepo.new(
			redis: FakeRedis.new(


@@ 33,7 37,8 @@ class WebTest < Minitest::Test
				"jmp_customer_jid-customerid_reach" => "customerid_reach@example.com",
				"catapult_jid-+15551234563" => "customer_customerid_reach@component",
				"jmp_customer_jid-customerid2" => "customer2@example.com",
				"catapult_jid-+15551230000" => "customer_customerid2@component"
				"catapult_jid-+15551230000" => "customer_customerid2@component",
				"jmp_customer_jid-customerid_tombed" => "customer_tombed@example.com"
			),
			db: FakeDB.new(
				["customerid"] => [{


@@ 65,6 70,11 @@ class WebTest < Minitest::Test
					"balance" => BigDecimal(10),
					"plan_name" => "test_usd",
					"expires_at" => Time.now + 100
				}],
				["customerid_tombed"] => [{
					"balance" => BigDecimal(10),
					"plan_name" => "test_usd",
					"expires_at" => Time.now + 100
				}]
			),
			sgx_repo: Bwmsgsv2Repo.new(


@@ 129,7 139,13 @@ class WebTest < Minitest::Test
				)
			)
		)
		Web.opts[:cdr_repo] = CDRRepo.new(db: FakeDB.new)
		Web.opts[:cdr_repo] = @cdr_repo
		Web.opts[:trust_level_repo] = TrustLevelRepo.new(
			db: FakeDB.new,
			redis: FakeRedis.new(
				"jmp_customer_trust_level-customerid_tombed" => "Tomb"
			)
		)
		Web.opts[:common_logger] = FakeLog.new
		Web.opts[:reachability_repo] = ReachabilityRepo::Voice.new(
			redis: ReachableRedis,


@@ 424,6 440,28 @@ class WebTest < Minitest::Test
	end
	em :test_outbound_disconnect

	def test_outbound_disconnect_tombed
		@cdr_repo.stub(:put, ->(*) { raise "put called" }) do
			post(
				"/outbound/calls/status",
				{
					eventType: "disconnect",
					from: "ccustomerid_tombed",
					to: "+15557654321",
					callId: "acall",
					startTime: Time.now.to_s,
					endTime: Time.now.to_s,
					cause: "hangup"
				}.to_json,
				{ "CONTENT_TYPE" => "application/json" }
			)
		end

		assert last_response.ok?
		assert_equal("OK", last_response.body)
	end
	em :test_outbound_disconnect_tombed

	def test_inbound
		CustomerFwd::BANDWIDTH_VOICE.expect(
			:create_call,

M web.rb => web.rb +22 -6
@@ 10,6 10,7 @@ require "sentry-ruby"
require "thin"

require_relative "lib/call_attempt_repo"
require_relative "lib/trust_level_repo"
require_relative "lib/cdr"
require_relative "lib/cdr_repo"
require_relative "lib/oob"


@@ 109,6 110,10 @@ class Web < Roda
		opts[:customer_repo] || CustomerRepo.new(**kwargs)
	end

	def trust_level_repo(**kwargs)
		opts[:trust_level_repo] || TrustLevelRepo.new(**kwargs)
	end

	def reachability_repo(**kwargs)
		opts[:reachability_repo] || ReachabilityRepo::Voice.new(**kwargs)
	end


@@ 433,13 438,24 @@ class Web < Roda
				r.post "status" do
					log.info "#{params['eventType']} #{params['callId']}", loggable_params
					if params["eventType"] == "disconnect"
						customer_id = params["from"].sub(/^(?:\+|c)/, "")
						call_attempt_repo.ending_call(customer_id, params["callId"])
						cdr_repo
							.put(CDR.for_outbound(customer_id, params))
							.catch(&method(:log_error))
						from = params["from"].sub(/^(?:\+|c)/, "")

						customer_repo.find_by_format(from).then { |customer|
							trust_level_repo.find(customer).then { |tl| [customer, tl] }
						}.then { |(customer, trust_level)|
							next "OK" unless trust_level.write_cdr?

							customer_id = customer.customer_id
							call_attempt_repo.ending_call(customer_id, params["callId"])
							cdr_repo
								.put(CDR.for_outbound(customer_id, params))
								.catch(&method(:log_error))

							"OK"
						}
					else
						"OK"
					end
					"OK"
				end

				r.post do