Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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: 33 additions & 0 deletions app/Http/Controllers/Admin/ExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Services\AnalyticsExporter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ExportController extends AdminController
{
public function __construct(
private readonly AnalyticsExporter $exporter
) {
}

public function index(Request $request): View
{
return view('admin.export');
}

public function store(Request $request): BinaryFileResponse
{
$request->validate([
'type' => 'required|string',
]);

$filePath = $this->exporter->export($request->type);

return response()->download($filePath)->deleteFileAfterSend();
}
}
3 changes: 2 additions & 1 deletion app/Models/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use App\Presenters\ChapterPresenter;
use Hemp\Presenter\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;

Expand All @@ -16,6 +16,7 @@
class Chapter extends Model
{
use Presentable;
use HasFactory;

public string $defaultPresenter = ChapterPresenter::class;

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Exercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Presenters\ExercisePresenter;
use Hemp\Presenter\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand All @@ -17,6 +18,7 @@
class Exercise extends Model
{
use Presentable;
use HasFactory;

public string $defaultPresenter = ExercisePresenter::class;

Expand Down
105 changes: 105 additions & 0 deletions app/Services/AnalyticsExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace App\Services;

use App\Models\Chapter;
use App\Models\Comment;
use App\Models\Exercise;
use App\Models\Solution;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Storage;
use League\Csv\Writer;
use Spatie\Activitylog\Models\Activity;

class AnalyticsExporter
{
public function export(string $type): string
{
return match ($type) {
'users' => $this->exportUsers("export/users.csv"),
'chapters' => $this->exportChapters("export/chapters.csv"),
'exercises' => $this->exportExercises("export/exercises.csv"),
'solutions' => $this->exportSolutions("export/solutions.csv"),
'comments' => $this->exportComments("export/comments.csv"),
'activity' => $this->exportActivityLog("export/activity.csv"),
default => throw new \InvalidArgumentException("Unknown export type: {$type}"),
};
}

private function exportUsers(string $path): string
{
$data = User::select(['id', 'name', 'email', 'email_verified_at', 'github_name', 'points', 'created_at', 'is_admin'])
->get();

return $this->writeCsv($data, $path);
}

private function exportChapters(string $path): string
{
$data = Chapter::select(['id', 'path', 'parent_id', 'created_at'])
->get();

return $this->writeCsv($data, $path);
}

private function exportExercises(string $path): string
{
$data = Exercise::select(['id', 'chapter_id', 'path', 'created_at'])
->get();

return $this->writeCsv($data, $path);
}

private function exportSolutions(string $path): string
{
$data = Solution::select(['id', 'exercise_id', 'user_id', 'created_at'])
->get();

return $this->writeCsv($data, $path);
}

private function exportComments(string $path): string
{
$data = Comment::select(['id', 'user_id', 'commentable_type', 'commentable_id', 'parent_id', 'created_at'])
->get();

return $this->writeCsv($data, $path);
}

private function exportActivityLog(string $path): string
{
$data = Activity::select(['id', 'log_name', 'subject_id', 'subject_type', 'causer_id', 'event', 'created_at'])
->get();

return $this->writeCsv($data, $path);
}

private function writeCsv(Collection $collection, string $path): string
{
$disk = Storage::disk();
$directory = dirname($path);

if (!$disk->exists($directory)) {
$disk->makeDirectory($directory);
}

$csv = Writer::createFromPath(
$disk->path($path),
'w+'
);

$csv->setDelimiter(',');
$csv->setNewline("\n");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут все еще самописная работа с csv. Давай возьмем готовое решение, вот к примеру. https://github.com/thephpleague/csv


if ($collection->isNotEmpty()) {
$csv->insertOne(array_keys($collection->first()->getAttributes()));

foreach ($collection as $item) {
$csv->insertOne(array_values($item->getAttributes()));
}
}

return $disk->path($path);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"laravel/socialite": "^5.11",
"laravel/tinker": "^2.8",
"laravel/ui": "^v4.2.3",
"league/csv": "^9.27",
"mcamara/laravel-localization": "^2.0",
"mikehaertl/php-shellcommand": "^1.7",
"rollbar/rollbar-laravel": "^8.1.3",
Expand Down
119 changes: 106 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions database/factories/ChapterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Factories;

use App\Models\Chapter;
use Illuminate\Database\Eloquent\Factories\Factory;

class ChapterFactory extends Factory
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хм, а у нас ведь нет особо необходимости в генерации глав и упражнений. Они ведь есть в коде. Они не подходят?

{
protected $model = Chapter::class;

public function definition(): array
{
return [
'path' => $this->faker->word,
'parent_id' => null,
'created_at' => now(),
'updated_at' => now(),
];
}
}
Loading
Loading