|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Requests; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
|
|
10
|
|
|
class JudgerModel extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
protected $tableName='judger'; |
|
13
|
|
|
public static $status=[ |
|
14
|
|
|
"-2"=>[ |
|
15
|
|
|
"text"=>"Unavailable", |
|
16
|
|
|
"icon"=>"close-circle", |
|
17
|
|
|
"color"=>"wemd-pink-text", |
|
18
|
|
|
], |
|
19
|
|
|
"-1"=>[ |
|
20
|
|
|
"text"=>"Unknown", |
|
21
|
|
|
"icon"=>"help-circle", |
|
22
|
|
|
"color"=>"wemd-grey-text", |
|
23
|
|
|
], |
|
24
|
|
|
"0"=>[ |
|
25
|
|
|
"text"=>"Operational", |
|
26
|
|
|
"icon"=>"check-circle", |
|
27
|
|
|
"color"=>"wemd-teal-text", |
|
28
|
|
|
], |
|
29
|
|
|
"1"=>[ |
|
30
|
|
|
"text"=>"Critical", |
|
31
|
|
|
"icon"=>"alert-circle", |
|
32
|
|
|
"color"=>"wemd-amber-text", |
|
33
|
|
|
], |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public function list($oid=2) |
|
37
|
|
|
{ |
|
38
|
|
|
$judger_list=DB::table($this->tableName)->where(["oid"=>$oid, "available"=>1])->get()->all(); |
|
39
|
|
|
return $judger_list; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function detail($jid) |
|
43
|
|
|
{ |
|
44
|
|
|
$judger_list=DB::table($this->tableName)->where(["jid"=>$jid])->get()->first(); |
|
45
|
|
|
return $judger_list; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function contestJudger($vcid) { |
|
49
|
|
|
return DB::table("contest_judger")->where(["vcid"=>$vcid, "available"=>1])->get()->all(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function contestJudgerDetail($jid) { |
|
53
|
|
|
return DB::table("contest_judger")->where("jid", $jid)->get()->first(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function server($oid=1) |
|
57
|
|
|
{ |
|
58
|
|
|
$serverList=DB::table("judge_server")->where(["oid"=>$oid, "available"=>1])->get()->all(); |
|
59
|
|
|
// return $serverList[0]; |
|
60
|
|
|
$bestServer=[ |
|
61
|
|
|
"load"=> 99999, |
|
62
|
|
|
"server" => null |
|
63
|
|
|
]; |
|
64
|
|
|
foreach ($serverList as $server) { |
|
65
|
|
|
$serverURL="http://".$server["host"].":".$server["port"]; |
|
66
|
|
|
try { |
|
67
|
|
|
$pong=$this->ping($serverURL.'/ping', $server["port"], hash('sha256', $server["token"])); |
|
68
|
|
|
} catch (Exception $exception) { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (empty($pong)) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($pong["status_code"]==200) { |
|
77
|
|
|
$pong=$pong["body"]; |
|
78
|
|
|
$load=4 * $pong->data->cpu+0.6 * $pong->data->memory; |
|
79
|
|
|
if ($load<$bestServer['load']) { |
|
80
|
|
|
$bestServer=[ |
|
81
|
|
|
'server' => $server, |
|
82
|
|
|
'load' => $load |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
return $bestServer["server"]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function fetchServer($oid=1) |
|
91
|
|
|
{ |
|
92
|
|
|
$serverList=DB::table("judge_server")->where(["oid"=>$oid])->get()->all(); |
|
93
|
|
|
foreach ($serverList as &$server) { |
|
94
|
|
|
$server["status_parsed"]=is_null($server["status"])?self::$status["-1"]:self::$status[$server["status"]]; |
|
95
|
|
|
} |
|
96
|
|
|
return $serverList; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|