Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM-1435] Fix tree restoration goal response #548

Merged
merged 15 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@

use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class TerrafundDashboardQueryHelper
{
public static function buildQueryFromRequest($request)
public static function buildQueryFromRequest(Request $request)
{
$filters = $request->all();
$query = QueryBuilder::for(Project::class)
->join('organisations', 'v2_projects.organisation_id', '=', 'organisations.id')
->select('v2_projects.*')
->where('v2_projects.status', 'approved')
->whereIn('organisations.type', ['non-profit-organization', 'for-profit-organization'])
->whereIn('v2_projects.framework_key', ['terrafund', 'terrafund-landscapes'])
->allowedFilters([
AllowedFilter::exact('framework_key'),
AllowedFilter::callback('landscapes', function ($query, $value) {
$query->whereIn('landscape', $value);
}),
AllowedFilter::exact('country'),
AllowedFilter::callback('organisations.type', function ($query, $value) {
$query->whereIn('organisations.type', $value);
}),
AllowedFilter::callback('programmes', function ($query, $value) {
$query->whereIn('framework_key', $value);
}),
AllowedFilter::exact('v2_projects.status'),
AllowedFilter::exact('v2_projects.uuid'),
]);
->where('v2_projects.status', 'approved');

if (data_get($filters, 'filter.country')) {
$query->where('v2_projects.country', data_get($filters, 'filter.country'));
}
if (data_get($filters, 'filter.programmes')) {
$query->whereIn('v2_projects.framework_key', data_get($filters, 'filter.programmes'));
} else {
$query->whereIn('v2_projects.framework_key', ['terrafund', 'terrafund-landscapes']);
}

if (data_get($filters, 'filter.landscapes')) {
$query->whereIn('v2_projects.landscape', data_get($filters, 'filter.landscapes'));
}

if (data_get($filters, 'filter.organisationType')) {
$query->whereIn('organisations.type', data_get($filters, 'filter.organisationType'));
} else {
$query->whereIn('organisations.type', ['non-profit-organization', 'for-profit-organization']);
}

if (data_get($filters, 'filter.projectUuid')) {
$query->where('v2_projects.uuid', data_get($filters, 'filter.projectUuid'));
}

if ($request->has('search')) {
$searchTerm = $request->query('search');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('active-countries-table-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:active-countries-table|'.$cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunActiveCountriesTableJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('active-projects-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:active-projects|'.$cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunActiveProjectsJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('jobs-created-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:jobs-created|'.$cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunJobsCreatedJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('top-trees-planted-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:top-trees-planted|'.$cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunTopTreesJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('total-section-header-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:total-section-header|'.$cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunTotalHeaderJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Jobs\Dashboard\RunTreeRestorationGoalJob;
use App\Models\DelayedJob;
use App\Models\Traits\HasCacheParameter;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
Expand All @@ -16,17 +15,18 @@ class ViewTreeRestorationGoalController extends Controller
{
use HasCacheParameter;

public function __invoke(Request $request): JsonResponse
public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('tree-restoration-goal-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:tree-restoration-goal|' . $cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunTreeRestorationGoalJob(
Expand All @@ -35,14 +35,19 @@ public function __invoke(Request $request): JsonResponse
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);

return (new DelayedJobResource($delayedJob))->additional(['message' => 'Data for total-section-header is being processed']);
} else {
return response()->json(json_decode($cacheValue));
return
(new DelayedJobResource($delayedJob))
->additional(
['message' => 'Data for tree restoration goal is being processed']
);
}

return response()->json(json_decode($cacheValue));
} catch (\Exception $e) {
Log::error('Error calculating tree restoration goal: ' . $e->getMessage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function __invoke(Request $request)
{
try {
$cacheParameter = $this->getParametersFromRequest($request);
$cacheValue = Redis::get('volunteers-survival-rate-'.$cacheParameter);
$cacheValue = Redis::get('dashboard:volunteers-survival-rate|'.$cacheParameter);

if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunVolunteersAverageJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public function __invoke(Request $request)
if (! $cacheValue) {
$frameworks = data_get($request, 'filter.programmes', []);
$landscapes = data_get($request, 'filter.landscapes', []);
$organisations = data_get($request, 'filter.organisations.type', []);
$organisations = data_get($request, 'filter.organisationType', []);
$country = data_get($request, 'filter.country', '');
$uuid = data_get($request, 'filter.projectUuid', '');

$delayedJob = DelayedJob::create();
$job = new RunHectaresRestoredJob(
Expand All @@ -34,6 +35,7 @@ public function __invoke(Request $request)
$landscapes,
$organisations,
$country,
$uuid,
$cacheParameter
);
dispatch($job);
Expand Down
11 changes: 8 additions & 3 deletions app/Jobs/Dashboard/RunActiveCountriesTableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class RunActiveCountriesTableJob implements ShouldQueue

protected $country;

protected $uuid;

protected $cacheParameter;

public function __construct(
Expand All @@ -42,13 +44,15 @@ public function __construct(
array $landscapes,
array $organisations,
string $country,
int $cacheParameter
string $uuid,
string $cacheParameter
) {
$this->delayed_job_id = $delayed_job_id;
$this->frameworks = $frameworks;
$this->landscapes = $landscapes;
$this->organisations = $organisations;
$this->country = $country;
$this->uuid = $uuid;
$this->cacheParameter = $cacheParameter;
}

Expand All @@ -62,13 +66,14 @@ public function handle(RunActiveCountriesTableService $runActiveCountriesTableSe
'country' => $this->country,
'programmes' => $this->frameworks,
'landscapes' => $this->landscapes,
'organisations.type' => $this->organisations,
'organisationType' => $this->organisations,
'projectUuid' => $this->uuid,
],
]);

$response = $runActiveCountriesTableService->getAllCountries($request);

Redis::set('active-countries-table-' . $this->cacheParameter, json_encode([
Redis::set('dashboard:active-countries-table|' . $this->cacheParameter, json_encode([
'data' => $response,
]));

Expand Down
8 changes: 5 additions & 3 deletions app/Jobs/Dashboard/RunActiveProjectsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class RunActiveProjectsJob implements ShouldQueue

protected $cacheParameter;

public function __construct(string $delayed_job_id, array $frameworks, array $landscapes, array $organisations, string $country, int $cacheParameter)
public function __construct(string $delayed_job_id, array $frameworks, array $landscapes, array $organisations, string $country, string $uuid, string $cacheParameter)
{
$this->delayed_job_id = $delayed_job_id;
$this->frameworks = $frameworks;
$this->landscapes = $landscapes;
$this->organisations = $organisations;
$this->country = $country;
$this->uuid = $uuid;
$this->cacheParameter = $cacheParameter;
}

Expand All @@ -59,12 +60,13 @@ public function handle(RunActiveProjectsService $runActiveProjectsService)
'country' => $this->country,
'programmes' => $this->frameworks,
'landscapes' => $this->landscapes,
'organisations.type' => $this->organisations,
'organisationType' => $this->organisations,
'projectUuid' => $this->uuid,
],
]
);
$response = $runActiveProjectsService->runActiveProjectsJob($request);
Redis::set('active-projects-' . $this->cacheParameter, json_encode($response));
Redis::set('dashboard:active-projects|' . $this->cacheParameter, json_encode($response));


$delayedJob->update([
Expand Down
6 changes: 4 additions & 2 deletions app/Jobs/Dashboard/RunHectaresRestoredJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class RunHectaresRestoredJob implements ShouldQueue

protected $cacheParameter;

public function __construct(string $delayed_job_id, array $frameworks, array $landscapes, array $organisations, string $country, string $cacheParameter)
public function __construct(string $delayed_job_id, array $frameworks, array $landscapes, array $organisations, string $country, string $uuid, string $cacheParameter)
{
$this->delayed_job_id = $delayed_job_id;
$this->frameworks = $frameworks;
$this->landscapes = $landscapes;
$this->organisations = $organisations;
$this->country = $country;
$this->uuid = $uuid;
$this->cacheParameter = $cacheParameter;
}

Expand All @@ -59,7 +60,8 @@ public function handle(RunHectaresRestoredService $runHectaresRestoredService)
'country' => $this->country,
'programmes' => $this->frameworks,
'landscapes' => $this->landscapes,
'organisations.type' => $this->organisations,
'organisationsType' => $this->organisations,
'projectUuid' => $this->uuid,
],
]
);
Expand Down
Loading
Loading