cleanup commands

This commit is contained in:
2024-10-05 18:48:38 +03:00
parent 52a1ec5c5a
commit bb27811ddf
14 changed files with 119 additions and 123 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
const channel = getContext("channel"); const channel = getContext("channel");
export let folder; export let folder;
export let schema; export let schema;
export let expanded = folder.shoudlExpand; export let expanded = folder.shouldExpand;
function toggleExpand() { function toggleExpand() {
expanded = !expanded; expanded = !expanded;
+8 -3
View File
@@ -7,7 +7,7 @@
const readableSchemas = getContext("readableSchemas"); const readableSchemas = getContext("readableSchemas");
function addToFolder(tree, folderPath, aSchema) { function addToFolder(tree, folderPath, aSchema) {
let shoudlExpand = aSchema.name === schema?.name; let shouldExpand = aSchema.name === schema?.name;
if (folderPath === "") { if (folderPath === "") {
tree.files.push(aSchema) tree.files.push(aSchema)
return tree return tree
@@ -16,12 +16,15 @@
folderNames.forEach(folderName => { folderNames.forEach(folderName => {
let queriedFolder = tree.folders.find(folder => folder.name === folderName) let queriedFolder = tree.folders.find(folder => folder.name === folderName)
if (!queriedFolder) { if (!queriedFolder) {
queriedFolder = {name: folderName, files: [], folders: [], shoudlExpand: shoudlExpand}; queriedFolder = {name: folderName, files: [], folders: [], shouldExpand: shouldExpand};
} }
folderNames.shift() folderNames.shift()
let remainingFolderPath = folderNames.join("."); let remainingFolderPath = folderNames.join(".");
queriedFolder = addToFolder(queriedFolder, remainingFolderPath, aSchema) queriedFolder = addToFolder(queriedFolder, remainingFolderPath, aSchema)
tree.folders = tree.folders.filter(f => f.name !== queriedFolder.name)
tree.folders.push(queriedFolder); tree.folders.push(queriedFolder);
}) })
return tree; return tree;
@@ -30,7 +33,9 @@
const schemaTree = readableSchemas.reduce((carry, schema) => { const schemaTree = readableSchemas.reduce((carry, schema) => {
carry = addToFolder(carry, schema.folder,schema) carry = addToFolder(carry, schema.folder,schema)
return carry; return carry;
}, {name: "", files: [], folders: [], shoudlExpand:true}); }, {name: "", files: [], folders: [], shouldExpand:true});
console.log({schemaTree})
</script> </script>
<div class="sidebar-top"> <div class="sidebar-top">
<a class="logo" href="{channel.lucentUrl}">{channel.name}</a> <a class="logo" href="{channel.lucentUrl}">{channel.name}</a>
@@ -48,7 +48,7 @@
</script> </script>
<div class="preview-file" class:is-trashed={edge._isTrashed}> <div class="preview-file" class:is-trashed={edge?._isTrashed}>
<div style="display: flex;align-items: center;gap: 10px;"> <div style="display: flex;align-items: center;gap: 10px;">
<div class="image"> <div class="image">
<Preview {record} size="small"/> <Preview {record} size="small"/>
@@ -60,8 +60,8 @@
href="{channel.lucentUrl}/records/{record.id}" href="{channel.lucentUrl}/records/{record.id}"
> >
{cardTitle} {cardTitle}
{#if edge._isTrashed} {#if edge?._isTrashed}
<span class="trashed-text">Trashed</span> <span class="trashed-text">will remove on save</span>
{/if} {/if}
</a> </a>
<small class="d-block"> <small class="d-block">
@@ -92,7 +92,7 @@
{/if} {/if}
{#if hasDelete} {#if hasDelete}
<div class="reference-action"> <div class="reference-action">
{#if edge._isTrashed} {#if edge?._isTrashed}
<button <button
title="Restore" title="Restore"
class="button" class="button"
@@ -35,7 +35,7 @@
</script> </script>
<div class="preview-reference" class:is-trashed={edge._isTrashed}> <div class="preview-reference" class:is-trashed={edge?._isTrashed}>
<div style="display: flex;align-items: center;gap: 10px;"> <div style="display: flex;align-items: center;gap: 10px;">
{#if cardImageRecord} {#if cardImageRecord}
@@ -51,8 +51,8 @@
> >
{cardTitle} {cardTitle}
{#if edge._isTrashed} {#if edge?._isTrashed}
<span class="trashed-text">Trashed</span> <span class="trashed-text">will remove on save</span>
{/if} {/if}
</a> </a>
@@ -69,7 +69,7 @@
</div> </div>
{#if hasDelete} {#if hasDelete}
<div class="reference-action"> <div class="reference-action">
{#if edge._isTrashed} {#if edge?._isTrashed}
<button <button
title="Restore" title="Restore"
class="button" class="button"
+3 -1
View File
@@ -31,7 +31,9 @@
&:hover{ &:hover{
background: var(--p30); background: var(--p30);
.reference-action{ .reference-action{
display: block; display: flex;
align-items: center;
gap: 3px;
} }
} }
} }
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Lucent\Commands;
use Illuminate\Console\Command;
use Lucent\Database\Database;
use Lucent\Edge\EdgeService;
use Lucent\Query\Query;
class EmptyTrash extends Command
{
protected $signature = 'lucent:empty-trash {--force}';
protected $description = 'Removes all trashed documents';
public function __construct()
{
parent::__construct();
}
public function handle(): void
{
$trashedIds = Database::make()->table("records")->select("id")->where("status", "trashed")->get()->pluck("id")->toArray();
$count = count($trashedIds);
if (!($this->option('force') || $this->confirm("$count trashed records found. Delete?"))) {
return;
}
$numOfRowsDeleted = Database::make()->table("records")->whereIn("id", $trashedIds)->delete();
Database::make()->table("revisions")->whereIn("recordId", $trashedIds)->delete();
Database::make()->table("edges")->whereIn("source", $trashedIds)->delete();
Database::make()->table("edges")->whereIn("target", $trashedIds)->delete();
$this->info("$numOfRowsDeleted records were deleted");
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ use Lucent\Schema\CollectionSchema;
class GenerateCollectionSchema extends Command class GenerateCollectionSchema extends Command
{ {
protected $signature = 'lucent:generate:collection {name}'; protected $signature = 'lucent:make:collection-schema {name}';
protected $description = 'Generate a lucent collection'; protected $description = 'Generate a lucent collection';
+1 -1
View File
@@ -9,7 +9,7 @@ use Lucent\Schema\FilesSchema;
class GenerateFileSchema extends Command class GenerateFileSchema extends Command
{ {
protected $signature = 'lucent:generate:file {name}'; protected $signature = 'lucent:make:file-schema {name}';
protected $description = 'Generate a lucent file schema'; protected $description = 'Generate a lucent file schema';
+1 -1
View File
@@ -16,7 +16,7 @@ use Lucent\Schema\Type;
class RebuildThumbnails extends Command class RebuildThumbnails extends Command
{ {
protected $signature = 'lucent:rebuild:thumbnails'; protected $signature = 'lucent:rebuild-thumbnails';
protected $description = 'Rebuilds thumbnails for path'; protected $description = 'Rebuilds thumbnails for path';
+11 -5
View File
@@ -9,14 +9,12 @@ use Lucent\Query\Query;
class RemoveOrphanEdges extends Command class RemoveOrphanEdges extends Command
{ {
protected $signature = 'lucent:removeOrphanEdges'; protected $signature = 'lucent:remove-orphan-edges {--force}';
protected $description = 'Searches and remove orphan edges'; protected $description = 'Searches and remove orphan edges';
public function __construct( public function __construct()
)
{ {
parent::__construct(); parent::__construct();
} }
@@ -24,17 +22,25 @@ class RemoveOrphanEdges extends Command
public function handle(EdgeService $edgeService, Query $query): void public function handle(EdgeService $edgeService, Query $query): void
{ {
$edges = $edgeService->findAll();
if (!($this->option('force') || $this->confirm("Delete orphan edges?"))) {
return;
}
$edges = $edgeService->findAll();
$counter = 0;
foreach ($edges as $edge) { foreach ($edges as $edge) {
$source = $query->filter(["id" => $edge->source])->run()->records; $source = $query->filter(["id" => $edge->source])->run()->records;
$target = $query->filter(["id" => $edge->target])->run()->records; $target = $query->filter(["id" => $edge->target])->run()->records;
if ($source->isEmpty() || $target->isEmpty()) { if ($source->isEmpty() || $target->isEmpty()) {
$this->info("Edge is orphan"); $this->info("Edge is orphan");
$edgeService->remove($edge); $edgeService->remove($edge);
$counter++;
} }
} }
$this->info("$counter edges were deleted");
} }
} }
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Lucent\Commands;
use Illuminate\Console\Command;
use Lucent\Channel\ChannelService;
use Lucent\Database\Database;
use Lucent\Edge\EdgeService;
use Lucent\Query\Query;
class RemoveOrphanRecords extends Command
{
protected $signature = 'lucent:remove-orphan-records {--force}';
protected $description = 'Searches and removes records without an active schema';
public function __construct()
{
parent::__construct();
}
public function handle(ChannelService $channelService): void
{
$schemas = $channelService->channel->schemas->pluck("name");
$count = Database::make()->table("revisions")->whereNotIn("schema",$schemas)->count();
if (!($this->option('force') || $this->confirm("$count records found. Delete?"))) {
return;
}
$numOfRowsDeleted = Database::make()->table("records")->whereNotIn("schema",$schemas)->delete();
Database::make()->table("revisions")->whereNotIn("schema",$schemas)->delete();
$this->info("$numOfRowsDeleted records were deleted");
}
}
+1 -7
View File
@@ -5,14 +5,12 @@ namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Lucent\Account\AccountService; use Lucent\Account\AccountService;
use Lucent\Account\AuthService;
use Lucent\Channel\ChannelService; use Lucent\Channel\ChannelService;
use Lucent\LucentException; use Lucent\LucentException;
use Lucent\Query\Operator\OperatorRegistry; use Lucent\Query\Operator\OperatorRegistry;
use Lucent\Query\Query; use Lucent\Query\Query;
use Lucent\Record\InputData\EdgeInputData; use Lucent\Record\InputData\EdgeInputData;
use Lucent\Record\InputData\RecordInputData; use Lucent\Record\InputData\RecordInputData;
use Lucent\Record\Manager;
use Lucent\Record\QueryRecord; use Lucent\Record\QueryRecord;
use Lucent\Record\RecordService; use Lucent\Record\RecordService;
use Lucent\Record\Status; use Lucent\Record\Status;
@@ -31,7 +29,6 @@ class RecordController extends Controller
private readonly ChannelService $channelService, private readonly ChannelService $channelService,
private readonly Svelte $svelte, private readonly Svelte $svelte,
private readonly Query $query, private readonly Query $query,
private readonly Manager $recordManager,
private readonly OperatorRegistry $operatorRegistry private readonly OperatorRegistry $operatorRegistry
) )
{ {
@@ -174,7 +171,6 @@ class RecordController extends Controller
} }
$schema = $this->channelService->channel->schemas->where("name", $request->input("schema"))->first(); $schema = $this->channelService->channel->schemas->where("name", $request->input("schema"))->first();
$recordHistory = $this->recordManager->fromSession($request->session())->getRecords();
$record = $this->recordService->createEmpty($schema); $record = $this->recordService->createEmpty($schema);
$queryRecord = QueryRecord::fromRecord($record); $queryRecord = QueryRecord::fromRecord($record);
return $this->svelte->render( return $this->svelte->render(
@@ -184,7 +180,6 @@ class RecordController extends Controller
data: [ data: [
"schema" => $schema, "schema" => $schema,
"record" => $queryRecord, "record" => $queryRecord,
"recordHistory" => $recordHistory,
"isCreateMode" => true, "isCreateMode" => true,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas()) "isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
] ]
@@ -238,6 +233,7 @@ class RecordController extends Controller
$record = $graph->records->first(); $record = $graph->records->first();
if (!in_array($record->schema, $this->accountService->currentReadableSchemas())) { if (!in_array($record->schema, $this->accountService->currentReadableSchemas())) {
return $this->svelte->render( return $this->svelte->render(
layout: "channel", layout: "channel",
@@ -247,7 +243,6 @@ class RecordController extends Controller
} }
$schema = $this->channelService->getSchema($record->schema)->get(); $schema = $this->channelService->getSchema($record->schema)->get();
$recordHistory = $this->recordManager->fromSession($request->session())->push($rid)->getRecords($rid);
return $this->svelte->render( return $this->svelte->render(
layout: "channel", layout: "channel",
view: "recordEdit", view: "recordEdit",
@@ -257,7 +252,6 @@ class RecordController extends Controller
"graph" => toArray($graph), "graph" => toArray($graph),
"record" => toArray($record), "record" => toArray($record),
"users" => $this->accountService->all(), "users" => $this->accountService->all(),
"recordHistory" => $recordHistory,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas()) "isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
] ]
); );
+4
View File
@@ -9,10 +9,12 @@ use Illuminate\Support\ServiceProvider;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService; use Lucent\Channel\ChannelService;
use Lucent\Commands\CompileSchemas; use Lucent\Commands\CompileSchemas;
use Lucent\Commands\EmptyTrash;
use Lucent\Commands\GenerateCollectionSchema; use Lucent\Commands\GenerateCollectionSchema;
use Lucent\Commands\GenerateFileSchema; use Lucent\Commands\GenerateFileSchema;
use Lucent\Commands\RebuildThumbnails; use Lucent\Commands\RebuildThumbnails;
use Lucent\Commands\RemoveOrphanEdges; use Lucent\Commands\RemoveOrphanEdges;
use Lucent\Commands\RemoveOrphanRecords;
use Lucent\Commands\Setup; use Lucent\Commands\Setup;
use Lucent\Commands\SetupDatabase; use Lucent\Commands\SetupDatabase;
use Lucent\Commands\UpgradeFiles122; use Lucent\Commands\UpgradeFiles122;
@@ -80,10 +82,12 @@ class LucentServiceProvider extends ServiceProvider
CompileSchemas::class, CompileSchemas::class,
RebuildThumbnails::class, RebuildThumbnails::class,
RemoveOrphanEdges::class, RemoveOrphanEdges::class,
RemoveOrphanRecords::class,
SetupDatabase::class, SetupDatabase::class,
GenerateCollectionSchema::class, GenerateCollectionSchema::class,
GenerateFileSchema::class, GenerateFileSchema::class,
UpgradeFiles122::class, UpgradeFiles122::class,
EmptyTrash::class,
]); ]);
} }
-93
View File
@@ -1,93 +0,0 @@
<?php
namespace Lucent\Record;
use Illuminate\Contracts\Session\Session;
use Lucent\Query\Query;
class Manager
{
public array $records = [];
public Session $session;
public function fromSession(Session $session): Manager
{
$this->session = $session;
$this->records = $session->get("manager") ?? [];
return $this;
}
public function __construct(
private readonly Query $query
)
{
}
/**
* @param string[] $records
*/
public function addRecords(array $records): Manager
{
$this->records = $records;
return $this;
}
public function push(string $recordId): Manager
{
$records = $this->getIdsExcept($recordId);
$records[] = $recordId;
$records = array_unique($records);
$records = array_values($records);
$records = array_slice($records, -5);
$records = array_values($records);
$this->records = $records;
$this->save();
return $this;
}
private function save(): void
{
$this->session->put("manager", $this->records);
}
public function getIdsExcept(?string $id): array
{
return collect($this->records)->filter(fn($arec) => $arec !== $id)->values()->toArray();
}
/**
* @param QueryRecord[] $records
* @return QueryRecord[] $records
*/
public function order(array $records): array
{
$recordsById = collect($records)->keyBy("id")->toArray();
return collect($this->records)->reverse()->values()->reduce(function ($carry, $arecId) use ($recordsById) {
if (isset($recordsById[$arecId])) {
$carry[] = $recordsById[$arecId];
}
return $carry;
}, []);
}
public function getRecords(?string $ignoreId = null): array
{
$graph = $this->query
->filter(["id_in" => $this->getIdsExcept($ignoreId)])
->limit(7)
->run();
return $this->order($graph->records->toArray());
}
}