dta_result_summary.php 6.45 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// This file is part of Moodle - http://moodle.org/.
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file contains the DTA submission plugin result summary entity class.
 *
 * @package   assignsubmission_dta
 * @copyright 2023 Your Name
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

25
26
27
namespace assignsubmission_dta\models;

/**
28
 * Entity class for DTA submission plugin result summary.
29
30
31
32
 *
 * This class holds:
 * - A timestamp for when the summary was generated.
 * - An optional global stack trace (in case the entire process failed).
33
34
35
36
37
 * - A competency profile of how many tests passed for each competency.
 * - A competency profile of the total coverage for each competency.
 * - An array of dta_result objects that detail individual test results.
 *
 * @package assignsubmission_dta
38
39
40
 */
class dta_result_summary {

41
    /** @var int Unix timestamp for the summary. */
42
    public $timestamp;
43
44

    /** @var string A global stacktrace if the entire run had a fatal error (optional). */
45
    public $globalstacktrace;
46
47

    /** @var string Semi-colon-separated numbers for competencies actually passed. */
48
    public $successfultestcompetencies;
49
50

    /** @var string Semi-colon-separated numbers for total tested competencies. */
51
    public $overalltestcompetencies;
52
53

    /** @var dta_result[] Array of individual test results. */
54
55
    public $results;

56
57
58
59
60
61
    /**
     * Decodes a JSON string into a dta_result_summary object.
     *
     * @param string $jsonstring JSON that includes timestamp, globalstacktrace, competency profiles, and results.
     * @return dta_result_summary
     */
62
    public static function assignsubmission_dta_decode_json(string $jsonstring): dta_result_summary {
63
64
65
        $response = json_decode($jsonstring);

        $summary = new dta_result_summary();
66
67
        $summary->timestamp = $response->timestamp ?? 0;
        $summary->globalstacktrace = $response->globalstacktrace ?? '';
68

69
        // If your JSON keys are 'successfulTestCompetencyProfile' and 'overallTestCompetencyProfile'.
70
        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile ?? '';
71
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile ?? '';
72

73
        // Decode the "results" array into an array of dta_result objects.
74
75
76
77
78
        if (!empty($response->results) && is_array($response->results)) {
            $summary->results = self::assignsubmission_dta_decode_json_result_array($response->results);
        } else {
            $summary->results = [];
        }
79
80
81
82

        return $summary;
    }

83
84
85
86
87
88
    /**
     * Helper that transforms a list of JSON objects into an array of dta_result objects.
     *
     * @param array $jsonarray Array of JSON-decoded result objects.
     * @return dta_result[]
     */
89
    private static function assignsubmission_dta_decode_json_result_array(array $jsonarray): array {
90
91
92
        $ret = [];
        foreach ($jsonarray as $entry) {
            $value = new dta_result();
93

94
95
96
97
98
99
100
101
102
103
            $value->packagename   = $entry->packageName ?? '';
            $value->classname     = $entry->className ?? '';
            $value->name          = $entry->name ?? '';
            $value->state         = $entry->state ?? 0;
            $value->failuretype   = $entry->failureType ?? '';
            $value->failurereason = $entry->failureReason ?? '';
            $value->stacktrace    = $entry->stacktrace ?? '';
            $value->columnnumber  = $entry->columnNumber ?? 0;
            $value->linenumber    = $entry->lineNumber ?? 0;
            $value->position      = $entry->position ?? 0;
104
105
106
107
108
109

            $ret[] = $value;
        }
        return $ret;
    }

110
111
112
113
114
    /**
     * Get the total number of results (tests) recorded in this summary.
     *
     * @return int
     */
115
    public function assignsubmission_dta_result_count(): int {
116
117
118
        return count($this->results);
    }

119
120
121
122
123
124
125
126
127
128
129
130
    /**
     * Generic helper to count how many results have the given $state.
     *
     * States can be:
     *  0 => unknown
     *  1 => success
     *  2 => fail
     *  3 => compilation error
     *
     * @param int $state The numeric state code to match.
     * @return int Number of results with that state.
     */
131
    public function assignsubmission_dta_state_occurence_count(int $state): int {
132
133
        $num = 0;
        foreach ($this->results as $r) {
134
            if ((int)$r->state === $state) {
135
136
137
138
139
140
                $num++;
            }
        }
        return $num;
    }

141
142
143
144
145
    /**
     * Count how many results had compilation errors (state=3).
     *
     * @return int
     */
146
    public function assignsubmission_dta_compilation_error_count(): int {
147
        return $this->assignsubmission_dta_state_occurence_count(3);
148
149
    }

150
151
152
153
154
    /**
     * Count how many results failed (state=2).
     *
     * @return int
     */
155
    public function assignsubmission_dta_failed_count(): int {
156
        return $this->assignsubmission_dta_state_occurence_count(2);
157
158
    }

159
160
161
162
163
    /**
     * Count how many results were successful (state=1).
     *
     * @return int
     */
164
    public function assignsubmission_dta_successful_count(): int {
165
        return $this->assignsubmission_dta_state_occurence_count(1);
166
167
    }

168
169
170
171
172
    /**
     * Count how many results are unknown (state=0).
     *
     * @return int
     */
173
    public function assignsubmission_dta_unknown_count(): int {
174
        return $this->assignsubmission_dta_state_occurence_count(0);
175
176
    }

177
178
    /**
     * Computes the success rate as a percentage of all results (0..100).
179
     * Note: This includes tests that might have compile errors or unknown states.
180
181
182
     *
     * @return float A floating percentage between 0.0 and 100.0.
     */
183
184
185
    public function assignsubmission_dta_success_rate(): float {
        $count = $this->assignsubmission_dta_result_count();
        if ($count === 0) {
186
            return 0.0;
187
188
189
        }
        $successful = $this->assignsubmission_dta_successful_count();
        return ($successful / $count) * 100.0;
190
191
    }
}