2014-02-24 21:19:46 +00:00
|
|
|
require 'ipaddr'
|
|
|
|
|
|
|
|
|
|
module JamRuby
|
|
|
|
|
class Score < ActiveRecord::Base
|
|
|
|
|
|
2014-08-01 01:16:38 +00:00
|
|
|
MAX_YELLOW_LATENCY = 40 # this round-trip internet latency, not full score
|
2014-05-29 07:19:55 +00:00
|
|
|
|
2014-02-24 21:19:46 +00:00
|
|
|
self.table_name = 'scores'
|
2014-02-26 05:01:55 +00:00
|
|
|
|
2014-07-24 23:39:26 +00:00
|
|
|
attr_accessible :alocidispid, :anodeid, :aaddr, :auserid, :alatencytestid, :blocidispid, :bnodeid, :baddr, :buserid, :blatencytestid, :score, :score_dt, :scorer, :scoring_data
|
2014-02-24 21:19:46 +00:00
|
|
|
|
2016-07-17 15:16:27 +00:00
|
|
|
default_scope { order('score_dt desc') }
|
2014-02-24 21:19:46 +00:00
|
|
|
|
2014-07-24 23:39:26 +00:00
|
|
|
def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt = Time.now, score_data = nil, user_info = {})
|
2014-02-24 21:19:46 +00:00
|
|
|
score_dt = Time.new.utc if score_dt.nil?
|
2014-05-21 19:05:45 +00:00
|
|
|
score = score.ceil
|
2014-09-30 15:51:23 +00:00
|
|
|
raise "alocidispid must be positive" if alocidispid <= 0
|
|
|
|
|
raise "blocidispid must be positive" if blocidispid <= 0
|
2014-05-21 19:05:45 +00:00
|
|
|
raise "score must be positive" if score <= 0
|
2014-07-24 23:39:26 +00:00
|
|
|
ascore = Score.create(alocidispid: alocidispid, anodeid: anodeid, aaddr: aaddr, auserid: user_info[:auserid], alatencytestid: user_info[:alatencytestid], blocidispid: blocidispid, bnodeid: bnodeid, baddr: baddr, buserid: user_info[:buserid], blatencytestid: user_info[:blatencytestid], score: score, scorer: 0, score_dt: score_dt, scoring_data: score_data)
|
2015-07-16 18:12:48 +00:00
|
|
|
bscore = Score.create(alocidispid: blocidispid, anodeid: bnodeid, aaddr: baddr, auserid: user_info[:buserid], alatencytestid: user_info[:blatencytestid], blocidispid: alocidispid, bnodeid: anodeid, baddr: aaddr, buserid: user_info[:auserid], blatencytestid: user_info[:alatencytestid], score: score, scorer: 1, score_dt: score_dt) if alocidispid != blocidispid
|
2014-09-30 15:51:23 +00:00
|
|
|
Score.connection.execute("select update_current_network_scores(#{alocidispid}, #{blocidispid})")
|
2014-07-21 21:42:30 +00:00
|
|
|
return [ascore, bscore]
|
2014-02-24 21:19:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.deletex(alocidispid, blocidispid)
|
|
|
|
|
Score.where(alocidispid: alocidispid, blocidispid: blocidispid).delete_all
|
|
|
|
|
Score.where(alocidispid: blocidispid, blocidispid: alocidispid).delete_all if alocidispid != blocidispid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.findx(alocidispid, blocidispid)
|
|
|
|
|
s = Score.where(alocidispid: alocidispid, blocidispid: blocidispid).first
|
|
|
|
|
return -1 if s.nil?
|
|
|
|
|
return s.score
|
|
|
|
|
end
|
2014-05-20 14:46:45 +00:00
|
|
|
|
|
|
|
|
def self.score_conns(c1, c2, score)
|
2014-07-24 23:39:26 +00:00
|
|
|
user_info = {}
|
|
|
|
|
if c1.user_id
|
|
|
|
|
user_info[:auserid] = c1.user_id
|
|
|
|
|
else
|
|
|
|
|
user_info[:alatencytestid] = c1.latency_tester.id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if c2.user_id
|
|
|
|
|
user_info[:buserid] = c2.user_id
|
|
|
|
|
else
|
|
|
|
|
user_info[:blatencytestid] = c2.latency_tester.id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
self.createx(c1.locidispid, c1.client_id, c1.addr, c2.locidispid, c2.client_id, c2.addr, score, nil, user_info)
|
2014-05-20 14:46:45 +00:00
|
|
|
end
|
2014-05-29 07:19:55 +00:00
|
|
|
|
2014-07-21 21:42:30 +00:00
|
|
|
# locid is a geoiplocation or geoipblock locid.
|
|
|
|
|
# coid is a jamisp coid
|
|
|
|
|
def self.compute_locidispid(locid, coid)
|
|
|
|
|
locid * 1000000 + coid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# if you have the right models in hand, this will compute a valid locidispid
|
|
|
|
|
def self.create_locidispid(geoiplocation_or_geoipblock, jamisp_or_jamcompany)
|
|
|
|
|
compute_locidispid(geoiplocation_or_geoipblock.locid, jamisp_or_jamcompany.coid)
|
|
|
|
|
end
|
2014-09-13 03:30:51 +00:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
def self.record(current_user, aclientid, aip_address, bclientid, bip_address, score, score_data, udpReachable)
|
|
|
|
|
|
|
|
|
|
# parameter checks
|
|
|
|
|
return {message: 'aclientid not specified', error: true} if aclientid.nil?
|
|
|
|
|
return {message: 'aAddr not specified', error: true} if aip_address.nil?
|
|
|
|
|
return {message: 'bclientid not specified', error: true} if bclientid.nil?
|
|
|
|
|
return {message: 'bAddr not specified', error: true} if bip_address.nil?
|
|
|
|
|
return {message: 'aclientid is same as bclientid', error: true} if aclientid == bclientid
|
|
|
|
|
|
|
|
|
|
aAddr = JamRuby::JamIsp.ip_to_num(aip_address)
|
|
|
|
|
return {message: 'aAddr not valid ip_address', error: true} if aAddr.nil?
|
|
|
|
|
bAddr = JamRuby::JamIsp.ip_to_num(bip_address)
|
|
|
|
|
return {message: 'bAddr not valid ip_address', error: true} if bAddr.nil?
|
|
|
|
|
|
|
|
|
|
if aAddr == bAddr
|
|
|
|
|
result = Score.connection.execute("SELECT connection_failed_score('#{aclientid}', '#{bclientid}', INTERVAL '#{APP_CONFIG.scoring_timeout_minutes} minutes', #{APP_CONFIG.scoring_timeout_threshold})")
|
|
|
|
|
in_timeout = result[0]["connection_failed_score"]
|
|
|
|
|
return {message: "aAddr and bAddr are the same (to=#{in_timeout})", error: true }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if udpReachable == false # we don't care if it's nil; it has to be FALSE explicitely
|
|
|
|
|
result = Score.connection.execute("SELECT connection_failed_score('#{aclientid}', '#{bclientid}', INTERVAL '#{APP_CONFIG.scoring_timeout_minutes} minutes', #{APP_CONFIG.scoring_timeout_threshold})")
|
|
|
|
|
in_timeout = result[0]["connection_failed_score"]
|
|
|
|
|
return {message: "udpReachable is false (to=#{in_timeout})", error: true }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if score.nil? || !score.is_a?(Numeric)
|
|
|
|
|
return {message: 'score not specified or not numeric' , error: true}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
result = Score.connection.execute("SELECT connection_good_score('#{aclientid}', '#{bclientid}')");
|
|
|
|
|
in_timeout = result[0]["connection_good_score"]
|
|
|
|
|
|
|
|
|
|
# check a connection's state
|
|
|
|
|
aconn = Connection.where(client_id: aclientid, user_id: current_user.id).first
|
|
|
|
|
return {message: "a's session not found", error: true} if aconn.nil?
|
|
|
|
|
return {message: "a's session addr does not match aAddr", error: true} if aAddr != aconn.addr
|
|
|
|
|
|
|
|
|
|
# check b connection's state
|
|
|
|
|
bconn = Connection.where(client_id: bclientid).first
|
|
|
|
|
return {message: "b's session not found", error: true} if bconn.nil?
|
|
|
|
|
return {message: "b's session addr does not match bAddr", error: true} if bAddr != bconn.addr
|
|
|
|
|
|
|
|
|
|
# check sanity of the score
|
|
|
|
|
return {message: 'score < 0 or score > 999', error: true} if score < 0 or score > 999
|
|
|
|
|
|
|
|
|
|
aloc = JamRuby::GeoIpBlocks.lookup(aAddr)
|
|
|
|
|
aisp = JamRuby::JamIsp.lookup(aAddr)
|
|
|
|
|
return {message: "a's location or isp not found", error: true} if aisp.nil? or aloc.nil?
|
|
|
|
|
alocidispid = aloc.locid*1000000+aisp.coid;
|
|
|
|
|
|
|
|
|
|
bloc = JamRuby::GeoIpBlocks.lookup(bAddr)
|
|
|
|
|
bisp = JamRuby::JamIsp.lookup(bAddr)
|
|
|
|
|
return {message: "b's location or isp not found", error: true} if bisp.nil? or bloc.nil?
|
|
|
|
|
|
|
|
|
|
blocidispid = bloc.locid*1000000+bisp.coid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user_info = {}
|
|
|
|
|
if aconn.user_id
|
|
|
|
|
user_info[:auserid] = aconn.user_id
|
|
|
|
|
else
|
|
|
|
|
user_info[:alatencytestid] = aconn.latency_tester.id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if bconn.user_id
|
|
|
|
|
user_info[:buserid] = bconn.user_id
|
|
|
|
|
else
|
|
|
|
|
user_info[:blatencytestid] = bconn.latency_tester.id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
JamRuby::Score.createx(alocidispid, aclientid, aAddr, blocidispid, bclientid, bAddr, score.ceil, nil, score_data, user_info)
|
|
|
|
|
|
|
|
|
|
return {message: "OK (to=#{in_timeout})", error: false}
|
|
|
|
|
end
|
2014-02-24 21:19:46 +00:00
|
|
|
end
|
|
|
|
|
end
|