Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.18% covered (success)
93.18%
41 / 44
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ThresholdParser
93.18% covered (success)
93.18%
41 / 44
50.00% covered (danger)
50.00%
2 / 4
21.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseThresholds
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
7
 getFiltersConfig
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 extractBoundValue
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
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
17namespace ORES;
18
19use Psr\Log\LoggerInterface;
20
21class ThresholdParser {
22
23    /**
24     * @var LoggerInterface
25     */
26    private $logger;
27
28    public function __construct( LoggerInterface $logger ) {
29        $this->logger = $logger;
30    }
31
32    public function parseThresholds( array $statsData, $model ) {
33        $thresholds = [];
34        foreach ( $this->getFiltersConfig( $model ) as $levelName => $config ) {
35            if ( $config === false ) {
36                // level is disabled
37                continue;
38            }
39
40            $min = $this->extractBoundValue( 'min', $config['min'], $statsData );
41
42            $max = $this->extractBoundValue( 'max', $config['max'], $statsData );
43
44            if ( $max === null || $min === null ) {
45                $data = [
46                    'levelName' => $levelName,
47                    'levelConfig' => $config,
48                    'max' => $max,
49                    'min' => $min,
50                    'statsData' => $statsData,
51                ];
52                $this->logger->error( 'Unable to parse threshold: ' . json_encode( $data ) );
53                continue;
54            }
55
56            if ( is_numeric( $min ) && is_numeric( $max ) ) {
57                $thresholds[$levelName] = [
58                    'min' => $min,
59                    'max' => $max,
60                ];
61            }
62        }
63
64        return $thresholds;
65    }
66
67    /**
68     * @param string $model
69     *
70     * @return (array|bool)[]|false
71     */
72    public function getFiltersConfig( $model ) {
73        global $wgOresFiltersThresholds;
74        if ( !isset( $wgOresFiltersThresholds[$model] ) ) {
75            return false;
76        }
77        $config = $wgOresFiltersThresholds[$model];
78        return $config;
79    }
80
81    private function extractBoundValue( $bound, $config, array $statsData ) {
82        if ( is_numeric( $config ) ) {
83            return $config;
84        }
85
86        $stat = $config;
87        if ( !isset( $statsData['false'] ) || !isset( $statsData['true'] ) ) {
88            return null;
89        }
90
91        if ( !isset( $statsData['false'][$stat] ) && !isset( $statsData['true'][$stat] ) ) {
92            return null;
93        }
94
95        if ( $bound === 'max' && $statsData['false'][$stat] === null ) {
96            return null;
97        } elseif ( $bound === 'max' && isset( $statsData['false'][$stat]['threshold'] ) ) {
98            $threshold = $statsData['false'][$stat]['threshold'];
99            $threshold = 1 - $threshold;
100
101            return $threshold;
102        } elseif ( isset( $statsData['true'][$stat]['threshold'] ) ) {
103            $threshold = $statsData['true'][$stat]['threshold'];
104
105            return $threshold;
106        }
107
108        return null;
109    }
110
111}