Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.18% |
41 / 44 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
ThresholdParser | |
93.18% |
41 / 44 |
|
50.00% |
2 / 4 |
21.14 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parseThresholds | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
7 | |||
getFiltersConfig | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
extractBoundValue | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
11.20 |
1 | <?php |
2 | /** |
3 | * This program is free software: you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation, either version 3 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | */ |
16 | |
17 | namespace ORES; |
18 | |
19 | use Psr\Log\LoggerInterface; |
20 | |
21 | class ThresholdParser { |
22 | |
23 | private LoggerInterface $logger; |
24 | |
25 | public function __construct( LoggerInterface $logger ) { |
26 | $this->logger = $logger; |
27 | } |
28 | |
29 | public function parseThresholds( array $statsData, string $model ): array { |
30 | $thresholds = []; |
31 | foreach ( $this->getFiltersConfig( $model ) as $levelName => $config ) { |
32 | if ( $config === false ) { |
33 | // level is disabled |
34 | continue; |
35 | } |
36 | |
37 | $min = $this->extractBoundValue( 'min', $config['min'], $statsData ); |
38 | |
39 | $max = $this->extractBoundValue( 'max', $config['max'], $statsData ); |
40 | |
41 | if ( $max === null || $min === null ) { |
42 | $data = [ |
43 | 'levelName' => $levelName, |
44 | 'levelConfig' => $config, |
45 | 'max' => $max, |
46 | 'min' => $min, |
47 | 'statsData' => $statsData, |
48 | ]; |
49 | $this->logger->error( 'Unable to parse threshold: ' . json_encode( $data ) ); |
50 | continue; |
51 | } |
52 | |
53 | if ( is_numeric( $min ) && is_numeric( $max ) ) { |
54 | $thresholds[$levelName] = [ |
55 | 'min' => $min, |
56 | 'max' => $max, |
57 | ]; |
58 | } |
59 | } |
60 | |
61 | return $thresholds; |
62 | } |
63 | |
64 | /** |
65 | * @param string $model |
66 | * |
67 | * @return (array|bool)[]|false |
68 | */ |
69 | public function getFiltersConfig( $model ) { |
70 | global $wgOresFiltersThresholds; |
71 | if ( !isset( $wgOresFiltersThresholds[$model] ) ) { |
72 | return false; |
73 | } |
74 | $config = $wgOresFiltersThresholds[$model]; |
75 | return $config; |
76 | } |
77 | |
78 | private function extractBoundValue( $bound, $config, array $statsData ) { |
79 | if ( is_numeric( $config ) ) { |
80 | return $config; |
81 | } |
82 | |
83 | $stat = $config; |
84 | if ( !isset( $statsData['false'] ) || !isset( $statsData['true'] ) ) { |
85 | return null; |
86 | } |
87 | |
88 | if ( !isset( $statsData['false'][$stat] ) && !isset( $statsData['true'][$stat] ) ) { |
89 | return null; |
90 | } |
91 | |
92 | if ( $bound === 'max' && $statsData['false'][$stat] === null ) { |
93 | return null; |
94 | } elseif ( $bound === 'max' && isset( $statsData['false'][$stat]['threshold'] ) ) { |
95 | $threshold = $statsData['false'][$stat]['threshold']; |
96 | $threshold = 1 - $threshold; |
97 | |
98 | return $threshold; |
99 | } elseif ( isset( $statsData['true'][$stat]['threshold'] ) ) { |
100 | $threshold = $statsData['true'][$stat]['threshold']; |
101 | |
102 | return $threshold; |
103 | } |
104 | |
105 | return null; |
106 | } |
107 | |
108 | } |