Skip to content

Commit

Permalink
fix: minor issues on frontpage
Browse files Browse the repository at this point in the history
  • Loading branch information
OptimusCrime committed Oct 5, 2024
1 parent bc3ca0f commit 92a7ce3
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 162 deletions.
175 changes: 91 additions & 84 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.0-pl
6.0.1-pl
8 changes: 2 additions & 6 deletions youkok2/src/Biz/Services/Download/UpdateDownloadsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function run(Element $element): void
$course = $element->getCourse();
if ($course) {
$this->updateElementDownloads($course, true);
$this->addAndFlushDownloadForCourseInMostPopularSets($course);
$this->flushDownloadForCourseInMostPopularSets();
}

$this->addAndFlushDownloadForElementInMostPopularSets($element);
Expand Down Expand Up @@ -76,13 +76,9 @@ private function addAndFlushDownloadForElementInMostPopularSets(Element $element
/**
* @throws RedisException
*/
private function addAndFlushDownloadForCourseInMostPopularSets(Element $element): void
private function flushDownloadForCourseInMostPopularSets(): void
{
foreach (MostPopularCourse::collection() as $delta) {
$setKey = CacheKeyGenerator::keyForMostPopularCoursesSetForDelta($delta);

$this->cacheService->updateValueInSet($setKey, 1, $element->id);

$this->cacheService->delete(CacheKeyGenerator::keyForMostPopularCoursesForDelta($delta));
}
}
Expand Down
2 changes: 1 addition & 1 deletion youkok2/src/Biz/Services/FrontpageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getLastVisitedCurses(RouteParserInterface $routeParser): array
return json_decode($cache, true);
}

$payload = $this->courseService->getLastVisitedCourses();
$payload = $this->courseService->getLastVisitedCourses(static::SERVICE_LIMIT);

$data = $this->courseMapper->mapLastVisited($routeParser, $payload);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ private function clearMostPopularCaches(): void
}

foreach (MostPopularCourse::collection() as $delta) {
$this->cacheService->delete(CacheKeyGenerator::keyForMostPopularCoursesSetForDelta($delta));
$this->cacheService->delete(CacheKeyGenerator::keyForMostPopularCoursesForDelta($delta));
}
}
Expand Down
3 changes: 2 additions & 1 deletion youkok2/src/Biz/Services/Models/CourseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ public function getAllCourses(): Collection
/**
* @throws RedisException
*/
public function getLastVisitedCourses(): Collection
public function getLastVisitedCourses(int $limit): Collection
{
return Element::select(Element::ALL_FIELDS)
->where('directory', true)
->where('deleted', false)
->whereNull('parent')
->whereNotNull('last_visited')
->orderBy('last_visited', 'DESC')
->limit($limit)
->get();
}

Expand Down
6 changes: 4 additions & 2 deletions youkok2/src/Biz/Services/Models/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getMostPopularElementsFromDelta(MostPopularElement $delta): Coll
/**
* @throws Exception
*/
public function getMostPopularCurseFromDelta(MostPopularCourse $delta): Collection
public function getMostPopularCursesFromDelta(MostPopularCourse $delta, int $limit): Collection
{
$query = Element::select(Element::ALL_FIELDS)
->where('deleted', false)
Expand All @@ -120,7 +120,9 @@ public function getMostPopularCurseFromDelta(MostPopularCourse $delta): Collecti
break;
}

return $query->get();
return $query
->limit($limit)
->get();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Youkok\Biz\Services\PopularListing;

use Exception;
use Illuminate\Support\Collection;
use Monolog\Logger;

use RedisException;
Expand All @@ -11,30 +12,24 @@
use Youkok\Biz\Services\CacheService;
use Youkok\Biz\Services\Mappers\CourseMapper;
use Youkok\Biz\Services\Models\DownloadService;
use Youkok\Biz\Services\Models\ElementService;
use Youkok\Common\Models\Element;
use Youkok\Common\Utilities\CacheKeyGenerator;
use Youkok\Common\Utilities\SelectStatements;
use Youkok\Enums\MostPopularCourse;

class MostPopularCoursesService
{
private CacheService $cacheService;
private DownloadService $downloadService;
private ElementService $elementService;
private CourseMapper $courseMapper;
private Logger $logger;

public function __construct(
CacheService $cacheService,
DownloadService $downloadService,
ElementService $elementService,
CourseMapper $courseMapper,
Logger $logger
) {
$this->cacheService = $cacheService;
$this->downloadService = $downloadService;
$this->elementService = $elementService;
$this->courseMapper = $courseMapper;
$this->logger = $logger;
}
Expand All @@ -51,33 +46,8 @@ public function getMostPopularCourses(RouteParserInterface $routeParser, MostPop
return json_decode($cache, true);
}

$mostPopularSet = $this->cacheService->getMostPopularCoursesSetFromDelta($delta);
if (count($mostPopularSet) === 0) {
$this->buildMostPopularCacheSet($delta);
$mostPopularSet = $this->cacheService->getMostPopularCoursesSetFromDelta($delta);
}

$elements = [];
foreach ($mostPopularSet as $id => $downloads) {
try {
$element = $this->elementService->getElement(
new SelectStatements('id', $id),
[
ElementService::FLAG_ENSURE_VISIBLE
]
);

$elements[] = $element;

if (count($elements) === $limit) {
break;
}
} catch (ElementNotFoundException $ex) {
$this->logger->debug('Could not find element ' . $id);
continue;
}
}

$elements = $this->downloadService->getMostPopularCursesFromDelta($delta, $limit);
$response = $this->mapMostPopularCourse($routeParser, $elements, $delta, $limit);
$this->cacheService->set($key, json_encode($response));

Expand All @@ -88,7 +58,7 @@ public function getMostPopularCourses(RouteParserInterface $routeParser, MostPop
* @throws RedisException
* @throws Exception
*/
private function mapMostPopularCourse(RouteParserInterface $routeParser, array $elements, MostPopularCourse $delta, int $limit): array
private function mapMostPopularCourse(RouteParserInterface $routeParser, Collection $elements, MostPopularCourse $delta, int $limit): array
{
$fields = [
];
Expand Down Expand Up @@ -135,36 +105,4 @@ private function mapMostPopularCourse(RouteParserInterface $routeParser, array $

return $response;
}

/**
* @throws RedisException
* @throws Exception
*/
private function buildMostPopularCacheSet(MostPopularCourse $delta): void
{
$elements = $this->downloadService->getMostPopularCurseFromDelta($delta);
$key = CacheKeyGenerator::keyForMostPopularCoursesSetForDelta($delta);

foreach ($elements as $element) {
$downloads = static::getDownloadsFromElement($element, $delta);
$this->cacheService->insertIntoSet($key, (int)$downloads, (string)$element->id);
}
}

private static function getDownloadsFromElement(Element $element, MostPopularCourse $delta): int
{
switch ($delta->getValue()) {
case MostPopularCourse::DAY()->getValue():
return $element->downloads_today;
case MostPopularCourse::WEEK()->getValue():
return $element->downloads_week;
case MostPopularCourse::MONTH()->getValue():
return $element->downloads_month;
case MostPopularCourse::YEAR()->getValue():
return $element->downloads_year;
case MostPopularCourse::ALL()->getValue():
default:
return $element->downloads_all;
}
}
}
1 change: 0 additions & 1 deletion youkok2/src/Common/Containers/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public static function load(Container $container): void
new MostPopularCoursesService(
$container->get(CacheService::class),
$container->get(DownloadService::class),
$container->get(ElementService::class),
$container->get(CourseMapper::class),
$container->get('logger')
)
Expand Down

0 comments on commit 92a7ce3

Please sign in to comment.