Skip to content

Commit c1fda93

Browse files
committed
feat: fixed slow loading fetching times. implementd laravel jobs
1 parent a028788 commit c1fda93

22 files changed

+324
-248
lines changed

.env.dev.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ GITHUB_CALLBACK_URI=http://propromo.test/auth/github/callback
2323
BROADCAST_DRIVER=log
2424
CACHE_DRIVER=file
2525
FILESYSTEM_DISK=local
26-
QUEUE_CONNECTION=sync
26+
QUEUE_CONNECTION=database
2727
SESSION_DRIVER=file
2828
SESSION_LIFETIME=120
2929

.env.docker.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ GITHUB_CALLBACK_URI=http://propromo.test/auth/github/callback
2323
BROADCAST_DRIVER=log
2424
CACHE_DRIVER=file
2525
FILESYSTEM_DISK=local
26-
QUEUE_CONNECTION=sync
26+
QUEUE_CONNECTION=database
2727
SESSION_DRIVER=file
2828
SESSION_LIFETIME=120
2929

app/Events/MonitorProcessed.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@ class MonitorProcessed implements ShouldBroadcast
2020
/**
2121
* Create a new event instance.
2222
*/
23-
public function __construct($monitorId, $message)
23+
public function __construct($message)
2424
{
25-
$this->monitorId = $monitorId;
2625
$this->message = $message;
2726
}
2827

2928

3029
public function broadcastOn()
3130
{
3231
Log::info("$this->message");
33-
return new Channel('monitors');
32+
return new Channel('monitor-job-progress');
3433
}
3534

36-
public function broadcastAs(){
37-
return 'MonitorProcessed';
35+
public function broadcastWith()
36+
{
37+
return [
38+
'message' => $this->message
39+
];
3840
}
3941

4042
}

app/Jobs/CollectIssues.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Monitor;
6+
use App\Models\Milestone;
7+
use App\Traits\IssueCollector;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
use Illuminate\Queue\InteractsWithQueue;
12+
use Illuminate\Queue\SerializesModels;
13+
use Illuminate\Support\Facades\Cache;
14+
use Exception;
15+
use Log;
16+
17+
class CollectIssues implements ShouldQueue
18+
{
19+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, IssueCollector;
20+
21+
public Monitor $monitor;
22+
23+
/**
24+
* Create a new job instance.
25+
*
26+
* @param Monitor $monitor
27+
*/
28+
public function __construct(Monitor $monitor)
29+
{
30+
$this->monitor = $monitor;
31+
}
32+
33+
/**
34+
* Execute the job.
35+
*/
36+
public function handle(): void
37+
{
38+
$cacheKey = 'collect_issues_job_' . $this->monitor->id;
39+
40+
if (Cache::has($cacheKey)) {
41+
Log::info("The job for monitor {$this->monitor->id} is already running.");
42+
return;
43+
}
44+
45+
Cache::put($cacheKey, 'running', now()->addMinutes(10));
46+
47+
try {
48+
$repositories = $this->monitor->repositories;
49+
50+
foreach ($repositories as $repository) {
51+
foreach ($repository->milestones as $milestone) {
52+
$this->collect_tasks($milestone);
53+
}
54+
}
55+
} catch (Exception $e) {
56+
Log::error('Error while collecting issues: ' . $e->getMessage());
57+
} finally {
58+
Cache::forget($cacheKey);
59+
}
60+
}
61+
}

app/Jobs/CreateMonitor.php

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,28 @@
22

33
namespace App\Jobs;
44

5-
use App\Events\MonitorProcessed;
65
use App\Models\Monitor;
7-
use App\Models\MonitorLogEntries;
8-
use App\Models\MonitorLogs;
9-
use App\Services\ContributionService;
10-
use App\Services\DeploymentService;
11-
use App\Services\IssueService;
12-
use App\Services\MonitorCreatorService;
13-
use App\Services\MonitorJoinerApiService;
14-
use App\Services\RepositoryFetcherService;
15-
use App\Services\RepositoryIssueFetcherService;
16-
use App\Services\TokenCreatorService;
17-
use App\Services\VulnerabilityService;
186
use App\Traits\ContributionCollector;
197
use App\Traits\DeploymentCollector;
208
use App\Traits\IssueCollector;
219
use App\Traits\MonitorCreator;
10+
use App\Traits\ReleaseCollector;
2211
use App\Traits\RepositoryCollector;
23-
use App\Traits\RepositoryIssueCollector;
24-
use Exception;
12+
use App\Traits\VulnerabilityCollector;
2513
use Illuminate\Bus\Queueable;
2614
use Illuminate\Contracts\Queue\ShouldQueue;
2715
use Illuminate\Foundation\Bus\Dispatchable;
2816
use Illuminate\Queue\InteractsWithQueue;
2917
use Illuminate\Queue\SerializesModels;
18+
use Illuminate\Support\Facades\Cache;
3019
use Log;
20+
use Exception;
3121

3222
class CreateMonitor implements ShouldQueue
3323
{
34-
use Dispatchable,
35-
InteractsWithQueue,
36-
Queueable,
37-
SerializesModels,
38-
MonitorCreator,
39-
RepositoryCollector,
40-
DeploymentCollector,
41-
ContributionCollector,
42-
IssueCollector;
24+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MonitorCreator, RepositoryCollector, DeploymentCollector, ContributionCollector, DeploymentCollector, VulnerabilityCollector, ReleaseCollector, IssueCollector;
4325

44-
public $monitor;
45-
protected $logId;
26+
public Monitor $monitor;
4627

4728
public function __construct($monitor)
4829
{
@@ -51,52 +32,31 @@ public function __construct($monitor)
5132

5233
public function handle(): void
5334
{
54-
try {
55-
$monitorLog = MonitorLogs::where('monitor_id', $this->monitor->id)
56-
->latest()
57-
->first();
35+
$cacheKey = 'monitor_processing_' . $this->monitor->id;
5836

59-
if (!$monitorLog) {
60-
Log::warning("No monitor log found for monitor ID: {$this->monitor->id}");
61-
return;
62-
}
63-
64-
$this->logId = $monitorLog->id;
37+
if (Cache::has($cacheKey)) {
38+
Log::info("Monitor job for monitor ID {$this->monitor->id} is already running.");
39+
return;
40+
}
6541

66-
$this->log("Starting monitor process...", "info");
42+
try {
43+
Cache::put($cacheKey, true, now()->addMinutes(5));
6744

6845
$repositories = $this->collect_repositories($this->monitor);
69-
$this->log("Found " . $repositories->count() . " repositories.", "info");
7046

7147
foreach ($repositories as $repository) {
72-
Log::info("REPOSITORY {$repository->id} created.", "info");
73-
74-
$this->log("Processing repository: {$repository->name}", "info");
75-
7648
foreach ($repository->milestones as $milestone) {
77-
$issues = $this->collect_tasks($milestone);
78-
$this->log("Milestone {$milestone->title}: Found " .$issues->count() . " issues.", "info");
49+
$this->collect_tasks($milestone);
7950
}
8051
}
8152

82-
// Log successful completion
83-
$this->log("Monitor process completed successfully.", "success");
84-
53+
$this->collect_releases($this->monitor);
54+
$this->collect_deployments($this->monitor);
55+
$this->collect_vulnerabilities($this->monitor);
8556
} catch (Exception $e) {
86-
Log::error($e->getMessage());
87-
$this->log("Error: " . $e->getMessage(), "error");
57+
Log::error("Error processing monitor: " . $e->getMessage());
58+
} finally {
59+
Cache::forget($cacheKey);
8860
}
89-
90-
// Broadcast event after processing
91-
// broadcast(new MonitorProcessed($this->monitor->id, 'Monitor has been successfully created.'));
92-
}
93-
94-
protected function log($message, $level = "info")
95-
{
96-
MonitorLogEntries::create([
97-
'monitor_log_id' => $this->logId,
98-
'message' => $message,
99-
'level' => $level,
100-
]);
10161
}
10262
}

app/Livewire/Monitors/DeploymentsView.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class DeploymentsView extends Component
1717
public function mount(Monitor $monitor)
1818
{
1919
$this->monitor = $monitor;
20-
$this->loadDeployments();
20+
$this->deployments = $monitor->deployments()->get();
21+
// $this->loadDeployments();
2122
}
2223

2324
public function loadDeployments()

app/Livewire/Monitors/ReleasesView.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public function mount(Monitor $monitor)
3838
{
3939
$this->monitor = $monitor;
4040
$this->loadRepositories();
41-
$result = $this->collect_releases($this->monitor);
42-
$this->totalReleases = $result['total_count'];
41+
// $result = $this->collect_releases($this->monitor);
42+
$this->totalReleases = $this->monitor->releases()->count();
4343
$this->loadReleases();
4444
}
4545

@@ -70,9 +70,9 @@ public function updatedFilterType()
7070

7171
private function getCachedReleases($query)
7272
{
73-
$cacheKey = 'releases_' . $this->monitor->id . '_' .
74-
$this->selectedRepository . '_' .
75-
$this->filterType . '_' .
73+
$cacheKey = 'releases_' . $this->monitor->id . '_' .
74+
$this->selectedRepository . '_' .
75+
$this->filterType . '_' .
7676
$this->search . '_' .
7777
$this->page;
7878

@@ -86,7 +86,7 @@ public function loadReleases()
8686
try {
8787
$this->isLoading = true;
8888
$query = $this->monitor->releases()->with(['tag.author', 'repository']);
89-
89+
9090
// Initialize statistics with zero values
9191
$this->preReleaseCount = 0;
9292
$this->totalChanges = 0;
@@ -99,7 +99,7 @@ public function loadReleases()
9999
$this->totalChanges = $allReleases->sum(fn($r) => $r->tag->additions + $r->tag->deletions);
100100
$this->totalFilesChanged = $allReleases->sum(fn($r) => $r->tag->changed_files);
101101
}
102-
102+
103103
if ($this->selectedRepository) {
104104
$query->where('repository_id', $this->selectedRepository);
105105
}
@@ -115,15 +115,15 @@ public function loadReleases()
115115
$query->where('is_draft', true);
116116
break;
117117
}
118-
118+
119119
if ($this->search) {
120120
$query->where(function($q) {
121121
$searchTerm = strtolower($this->search);
122122
$q->whereRaw('LOWER(name) LIKE ?', ["%{$searchTerm}%"])
123123
->orWhereRaw('LOWER(description) LIKE ?', ["%{$searchTerm}%"]);
124124
});
125125
}
126-
126+
127127
$this->releases = $query->orderByDesc('created_at')
128128
->paginate($this->perPage, ['*'], 'releasePage');
129129
$this->filteredCount = $this->releases->total();

app/Livewire/Monitors/VulnerabilitiesView.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function mount(Monitor $monitor)
2323
public function loadVulnerabilities()
2424
{
2525
try {
26-
$result = $this->collect_vulnerabilities($this->monitor);
27-
$this->vulnerabilities = $result['vulnerabilities'];
28-
$this->totalVulnerabilities = $result['total_count'];
26+
//$result = $this->collect_vulnerabilities($this->monitor);
27+
$this->vulnerabilities = $this->monitor->vulnerabilities()->get();
28+
$this->totalVulnerabilities = $this->monitor->vulnerabilities()->count();
2929
} catch (\Exception $e) {
3030
$this->addError('vulnerabilities', $e->getMessage());
3131
}

app/Traits/MonitorCreator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function create_monitor($project_url, $pat_token)
6565
}
6666
$type = "USER";
6767
}
68+
$this->dispatch('monitor-log-sent', ["message" => "Creating monitor from $project_url..."]);
6869

6970
$monitor = Monitor::create([
7071
"project_url" => $project_url,
@@ -110,7 +111,10 @@ public function create_monitor($project_url, $pat_token)
110111
$monitor->save();
111112

112113
$monitor->users()->attach(Auth::user()->id);
113-
114+
$this->dispatch('monitor-log-sent', ["message" => "Successfully created monitor!"]);
115+
$this->dispatch('monitor-created', [
116+
'monitorId' => $monitor->id
117+
]);
114118
return $monitor;
115119
} catch (Exception $e) {
116120
Log::error('Monitor Creation Error:', [

app/Traits/RepositoryCollector.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ public function collect_repositories(Monitor $monitor)
4343
$repository->name = $repositoryData["name"];
4444

4545
$get_repository = $monitor->repositories()->save($repository); // Save the repository
46-
MonitorLogEntries::create([
47-
'monitor_log_id' => $monitor->monitor_logs()->first()->id,
48-
'message' => 'Repository created: ' . $repository->name,
49-
'level' => 'info',
50-
'context' => [
51-
],
52-
]);
5346

5447
$milestones = $repositoryData["milestones"]["nodes"];
5548
if (count($milestones) > 0) {
@@ -68,31 +61,8 @@ public function collect_repositories(Monitor $monitor)
6861
'repository_id' => $get_repository->id
6962
]);
7063
$repository->milestones()->save($milestone);
71-
MonitorLogEntries::create([
72-
'monitor_log_id' => $monitor->monitor_logs()->first()->id,
73-
'message' => 'Milestone created: ' . $milestone->title . ' (URL: ' . $milestone->url . ')',
74-
'level' => 'info',
75-
'context' => [
76-
],
77-
]);
78-
}else{
79-
MonitorLogEntries::create([
80-
'monitor_log_id' => $monitor->monitor_logs()->first()->id,
81-
'message' => 'Looks like no milestone defined in your gh-project!' . 'Check out for https://propromo-docs.vercel.app/blog/how-to-use-propromos-github-scrum-template-project.mdx for further assistance.',
82-
'level' => 'error',
83-
'context' => [
84-
],
85-
]);
8664
}
8765
}
88-
}else{
89-
MonitorLogEntries::create([
90-
'monitor_log_id' => $monitor->monitor_logs()->first()->id,
91-
'message' => 'Looks like no milestone defined in your gh-project!' . 'Check out for https://propromo-docs.vercel.app/blog/how-to-use-propromos-github-scrum-template-project.mdx for further assistance.',
92-
'level' => 'error',
93-
'context' => [
94-
],
95-
]);
9666
}
9767
}
9868
return Repository::where("monitor_id", "=", $monitor->id);

0 commit comments

Comments
 (0)