Skip to content

Commit

Permalink
wip: improves contributions loading logic (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfroeller committed Jan 9, 2025
1 parent 841c5eb commit 31e3e75
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 118 deletions.
97 changes: 65 additions & 32 deletions app/Livewire/Monitors/ContributionsView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,95 @@

namespace App\Livewire\Monitors;

use App\Models\Contribution;
use App\Models\Monitor;
use App\Traits\ContributionCollector;
use Carbon\Carbon;
use Exception;
use App\Models\Contribution;
use App\Models\Author;
use Livewire\Component;
use Log;
use Exception;
use Livewire\Attributes\On;

class ContributionsView extends Component
{
use ContributionCollector;

public Monitor $monitor;
public $contributions = [];
public $contributionId;
public $nextRootCursor = null;
public $nextCursor = null;
public bool $hasMoreRepositories = false;
public bool $hasMoreCommits = false;
public bool $loading = false;
public ?string $currentRepositoryName = null;
public ?string $error = null;

public $commit;
public function mount(Monitor $monitor, $contributionId = null)
public function mount(Monitor $monitor)
{
$this->monitor = $monitor;
$this->contributionId = $contributionId;
$this->loadContributions();
$this-> commit = $this->calculateContributions();
$this->loadNext();
}

public function loadContributions()
public function loadNext()
{
try {
$this->contributions = $this->collect_contributions($this->monitor);
$this->loading = true;
$this->error = null;

$result = $this->monitor->collect_contributions($this->nextRootCursor, $this->nextCursor);

if (!$result) {
throw new Exception('No response from API server');
}

Log::info('API Response:', [
'hasMoreCommits' => $result['has_more_commits'] ?? false,
'hasMoreRepositories' => $result['has_more_repositories'] ?? false,
'nextRootCursor' => $result['next_root_cursor'] ?? null,
'nextCursor' => $result['next_cursor'] ?? null,
'currentRepo' => $result['current_repository_name'] ?? 'unknown',
'contributionsCount' => count($result['contributions'] ?? [])
]);

if (!empty($result['contributions'])) {
$contributionIds = collect($result['contributions'])->pluck('id');
$loadedContributions = Contribution::with('authors')
->whereIn('id', $contributionIds)
->orderBy('committed_date', 'desc')
->get();

$this->contributions = array_merge($this->contributions, $loadedContributions->all());
}

$this->nextRootCursor = $result['next_root_cursor'];
$this->nextCursor = $result['next_cursor'];
$this->hasMoreRepositories = $result['has_more_repositories'] ?? false;
$this->hasMoreCommits = $result['has_more_commits'] ?? false;
$this->currentRepositoryName = $result['current_repository_name'];

} catch (Exception $e) {
$this->addError('contributions', $e->getMessage());
$this->error = "Error loading contributions. Please try again.";
$this->hasMoreCommits = false;
$this->hasMoreRepositories = false;
} finally {
$this->loading = false;
}
}


public function calculateContributions()
public function loadMore()
{
$commits = Contribution::selectRaw('authors.name, COUNT(contributions.id) as commit_count')
->leftJoin('authors', 'contributions.author_id', '=', 'authors.id')
->where('committed_date', '>=', Carbon::now()->subDays(14))
->where('committed_date', '<', Carbon::now())
->groupBy('authors.name')
->get();

return $commits;
if (!$this->loading) {
if ($this->hasMoreCommits) {
$this->loadNext();
} else if ($this->hasMoreRepositories) {
$this->nextCursor = null;
$this->loadNext();
}
}
}

public function placeholder()
public function retry()
{
return <<<'HTML'
<div class="flex justify-center mt-64">
<div class="loader"></div>
</div>
HTML;
$this->loadNext();
}


public function render()
{
return view('livewire.monitors.contributions-view');
Expand Down
26 changes: 20 additions & 6 deletions app/Models/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Contribution extends Model
{
public $incrementing = false;
protected $keyType = 'string';

protected $fillable = [
'id',
'commit_url',
'message_headline',
'message_body',
'additions',
'deletions',
'changed_files',
'committed_date',
'author_id',
'committed_date'
];

protected $casts = [
'committed_date' => 'datetime',
'committed_date' => 'datetime'
];

public function author(): BelongsTo
public function authors(): BelongsToMany
{
return $this->belongsToMany(Author::class, 'contribution_author');
}

protected static function booted()
{
return $this->belongsTo(Author::class);
static::creating(function ($contribution) {
if (!$contribution->id) {
if (preg_match('/\/commit\/([a-f0-9]{40})$/', $contribution->commit_url, $matches)) {
$contribution->id = $matches[1];
}
}
});
}
}
2 changes: 2 additions & 0 deletions app/Models/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Traits\ContributionCollector;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand Down Expand Up @@ -66,6 +67,7 @@
class Monitor extends Model
{
use HasFactory;
use ContributionCollector;

protected $fillable = [
"project_url",
Expand Down
Loading

0 comments on commit 31e3e75

Please sign in to comment.