-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Optimize pass rate calculation
- Loading branch information
Showing
9 changed files
with
157 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service; | ||
|
||
use App\Entity\Group; | ||
use App\Entity\Question; | ||
use App\Repository\SolutionEventRepository; | ||
use App\Service\Types\PassRate; | ||
|
||
/** | ||
* Get the pass rate of a question in the optimized matter. | ||
*/ | ||
readonly class PassRateService | ||
{ | ||
public function __construct( | ||
private SolutionEventRepository $solutionEventRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* Get the pass rate in this group of a question. | ||
* | ||
* @param Question $question the question to calculate the pass rate | ||
* @param Group|null $group the group to calculate the pass rate, null for no group | ||
* | ||
* @return PassRate the pass rate, see {@link PassRate} for details | ||
*/ | ||
public function getPassRate(Question $question, ?Group $group): PassRate | ||
{ | ||
$attempts = $this->solutionEventRepository->getTotalAttempts($question, $group); | ||
|
||
return new PassRate($attempts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service\Types; | ||
|
||
use App\Entity\SolutionEvent; | ||
use App\Entity\SolutionEventStatus; | ||
|
||
/** | ||
* The pass rate of a question. | ||
* | ||
* - `total`: the total number of attempts | ||
* - `passed`: the number of successful attempts | ||
* - `passRate`: the pass rate of the question in percentage | ||
* - `level`: the level of the pass rate, can be 'low', 'medium', or 'high' | ||
*/ | ||
readonly class PassRate | ||
{ | ||
/** | ||
* @var int the total number of attempts | ||
*/ | ||
public int $total; | ||
|
||
/** | ||
* @var int the number of successful attempts | ||
*/ | ||
public int $passed; | ||
|
||
/** | ||
* @param SolutionEvent[] $attempts | ||
*/ | ||
public function __construct( | ||
array $attempts, | ||
) { | ||
$this->total = \count($attempts); | ||
$this->passed = \count(array_filter($attempts, fn (SolutionEvent $event) => SolutionEventStatus::Passed == $event->getStatus())); | ||
} | ||
|
||
/** | ||
* Calculate the pass rate of a question. | ||
* | ||
* @return float the pass rate of the question in percentage | ||
*/ | ||
public function getPassRate(): float | ||
{ | ||
if (0 === $this->total) { | ||
return 0; | ||
} | ||
|
||
return round($this->passed / $this->total * 100, 2); | ||
} | ||
|
||
/** | ||
* @return string the level of the pass rate, can be 'low', 'medium', or 'high' | ||
*/ | ||
public function getLevel(): string | ||
{ | ||
$passRate = $this->getPassRate(); | ||
|
||
return match (true) { | ||
$passRate <= 40 => 'low', | ||
$passRate <= 70 => 'medium', | ||
default => 'high', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters