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

#73 - Add organization page #74

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
33 changes: 0 additions & 33 deletions app/Http/Controllers/GithubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace App\Http\Controllers;

use App\Jobs\FetchRepositoriesJob;
use App\Models\User;
use App\Services\AssignUserToOrganizationsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Bus;
use Inertia\Inertia;
use Inertia\Response;
use Laravel\Socialite\Facades\Socialite;
Expand Down Expand Up @@ -46,36 +43,6 @@ public function callback(): RedirectResponse
return redirect("/");
}

public function fetchData(int $organizationId): JsonResponse
{
$jobs = [
new FetchRepositoriesJob($organizationId, Auth::user()->id),
];

$batch = Bus::batch($jobs)->dispatch();

return response()->json(["batch" => $batch->id]);
}

public function status(string $batchId): JsonResponse
{
$batch = Bus::findBatch($batchId);

if ($batch === null) {
return response()->json(["message" => "Batch not found"], 404);
}

if ($batch->cancelled()) {
return response()->json(["message" => "There was an error, please try again latter."], 500);
}

return response()->json([
"all" => $batch->totalJobs,
"done" => $batch->processedJobs(),
"finished" => $batch->finished(),
]);
}

public function login(): Response
{
return Inertia::render("Login");
Expand Down
91 changes: 91 additions & 0 deletions app/Http/Controllers/OrganizationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Resources\OrganizationResource;
use App\Jobs\FetchRepositoriesJob;
use App\Models\Organization;
use App\Services\AssignUserToOrganizationsService;
use Carbon\Carbon;
use Illuminate\Bus\Batch;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;

class OrganizationController extends Controller
{
public function __construct(
protected AssignUserToOrganizationsService $assignUserService,
) {}

public function show(Request $request)
{
$user = $request->user();
$organizations = $user->organizations()->with(["repositories.workflowRuns.workflowJobs", "repositories.workflowRuns.workflowActor", "users"])->get();
$data = OrganizationResource::collection($organizations);
$status = [];

foreach ($data as $organization) {
$status[$organization->id] = $this->getProgress($organization->id);
}

return Inertia::render("Organization", ["data" => $data, "progress" => $status]);
}

public function fetchData(int $organizationId): JsonResponse
{
$organization = Organization::query()->findOrFail($organizationId);
$batch = $this->findBatch($organizationId);

if ($this->isFetching($batch)) {
return response()->json(["message" => "please wait"], Response::HTTP_CONFLICT);
}

$jobs = [new FetchRepositoriesJob($organizationId, Auth::user()->id)];
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved

$organization->fetch_at = Carbon::now();
$organization->save();

$batch = Bus::batch($jobs)->finally(fn(): bool => Cache::delete("fetch/" . $organizationId))->dispatch();
Cache::set("fetch/" . $organizationId, $batch->id);

return response()->json(["message" => "fetching started"], Response::HTTP_OK);
}

protected function isFetching(?Batch $batch): bool
{
return $batch !== null && !$batch->finished();
}

protected function getProgress(int $organizationId): ?array
{
$batch = $this->findBatch($organizationId);

if ($batch !== null) {
return [
"all" => $batch->totalJobs,
"done" => $batch->processedJobs(),
"finished" => $batch->finished(),
];
}

return null;
}

protected function findBatch(int $organizationId): ?Batch
{
$batchId = Cache::get("fetch/" . $organizationId);

if ($batchId === null) {
return null;
}

return Bus::findBatch($batchId);
}
}
32 changes: 32 additions & 0 deletions app/Http/Resources/OrganizationResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class OrganizationResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
"id" => $this->id,
"github_id" => $this->github_id,
"name" => $this->name,
"avatar_url" => $this->avatar_url,
"fetched_at" => $this->fetch_at,
"users" => $this->users->count(),
"repos" => $this->repositories->count(),
"runs" => $this->workflowRuns->count(),
"jobs" => $this->jobCount,
"actors" => $this->actorCount,
"minutes" => $this->totalMinutes,
"price" => $this->totalPrice,
];
}
}
47 changes: 46 additions & 1 deletion app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

/**
* @property int $id
Expand All @@ -18,10 +20,15 @@
* @property string $avatar_url
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $fetch_at
*
* @property float $totalMinutes
* @property float $totalPrice
*
* @property Collection<User> $users
* @property Collection<Repository> $repositories
*/
* @property Collection<WorkflowRun> $workflowRuns
* */
class Organization extends Model
{
use HasFactory;
Expand All @@ -41,4 +48,42 @@ public function repositories(): HasMany
{
return $this->HasMany(Repository::class);
}

public function workflowRuns(): HasManyThrough
{
return $this->HasManyThrough(WorkflowRun::class, Repository::class);
}

protected function casts(): array
{
return [
"fetch_at" => "datetime",
];
}

protected function totalMinutes(): Attribute
{
return Attribute::get(fn(): float => $this->repositories->sum("totalMinutes"));
}

protected function jobCount(): Attribute
{
return Attribute::get(fn(): int => $this->workflowRuns()
->withCount("workflowJobs")
->get()
->sum("workflow_jobs_count"));
}

protected function actorCount(): Attribute
{
return Attribute::get(fn(): int => $this->workflowRuns()
->pluck("workflow_actor_id")
->unique()
->count());
}

protected function totalPrice(): Attribute
{
return Attribute::get(fn(): float => $this->repositories->sum("totalPrice"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table("organizations", function (Blueprint $table): void {
$table->timestamp("fetch_at")->nullable();
});
}

public function down(): void
{
Schema::table("organizations", function (Blueprint $table): void {
$table->dropColumn("fetch_at");
});
}
};
1 change: 1 addition & 0 deletions resources/js/Components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function getNavigationClass (tab: string) {
<img src="../../assets/images/icon.png" alt="Logo" class="w-12">
</span>
<div class="flex space-x-4 w-full">
<Link href="/organization"><span :class="getNavigationClass('/organization')" class="cursor-pointer px-8 py-4 text-xs rounded-md bg-blue-400/50 hover:bg-blue-400/75">Organizations</span></Link>
<Link href="/table"><span :class="getNavigationClass('/table')" class="cursor-pointer px-8 py-4 text-xs rounded-md bg-blue-400/50 hover:bg-blue-400/75">Table</span></Link>
<Link href="/repositories"><span :class="getNavigationClass('/repositories')" class="cursor-pointer px-8 py-4 text-xs rounded-md bg-blue-400/50 hover:bg-blue-400/75">Repositories</span></Link>
<Link href="/authors"><span :class="getNavigationClass('/authors')" class="cursor-pointer px-8 py-4 text-xs rounded-md bg-blue-400/50 hover:bg-blue-400/75">Authors</span></Link>
Expand Down
21 changes: 21 additions & 0 deletions resources/js/Components/ProgressButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts" setup>
import {type BatchProgress} from '@/Types/BatchProgress'

const props = defineProps<{
text: string
progress?: BatchProgress
class?: string
}>()

const emit = defineEmits(['click'])
</script>

<template>
<div v-if="!props.progress || props.progress.finished" class="cursor-pointer px-5 py-3 font-bold text-xs rounded-md bg-blue-500 hover:bg-blue-500/75 text-white" :class="props.class" @click="emit('click')">
{{ props.text }}
</div>

<div v-else class="text-center px-5 py-3 text-xs" :class="props.class">
{{ props.progress.done }} / {{ props.progress.all }}
</div>
</template>
6 changes: 5 additions & 1 deletion resources/js/Components/SortableTable.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts" generic="T extends { id: number }">
import { defineProps } from 'vue'
import { withSort } from '@/Utils/sort'
import { Link } from '@inertiajs/vue3'

const props = defineProps<{
data: T[]
Expand Down Expand Up @@ -42,6 +43,9 @@ const {sorted, sortBy} = withSort(props.data, 'id')
</tr>
</tbody>
</table>
<h1 v-else>No logs loaded</h1>
<div v-else>
No data loaded. Go to the organization page to fetch them.
<Link class="cursor-pointer px-5 py-3 text-xs font-bold rounded-md bg-blue-500 hover:bg-blue-500/75 text-white ml-2" href="/organization">Navigate</Link>
</div>
</template>

Loading
Loading