Beautiful Open-Source Form Builder
Fix slow dashboard form listing and historical view counts (#1298)
Julien Nahum1e1d018b6665
diff --git a/README.md b/README.mdindex cc0840f73..def25c70e 100644--- a/README.md+++ b/README.md@@ -49,6 +49,12 @@ For self-hosted installations, please refer to our [Deployment Guides](https://dThe client `npm run build` also checks the public form bundle size and eager dependencies. This check reads `.nuxt/dist/client/_nuxt`, independently of Nitro's deployment output directory (including AWS Amplify's `.amplify-hosting`). Run its regression tests from `client/` with `node --test scripts/check-public-form-bundle.test.mjs`.+## Form list performance++The dashboard fetches lightweight summaries in batches of 50. The API keeps its default page size of 10 and its maximum of 100 for other callers. It paginates forms before loading view and completed-submission counts for the returned IDs. Historical views use an index on `form_statistics.form_id`, and the aggregate is explicitly aliased as `total_views_count` so the model uses the preloaded value.++The index migration runs outside a transaction and uses `CREATE INDEX CONCURRENTLY` on PostgreSQL to keep writes available. It also recovers an invalid index left by an interrupted build. Run this migration through Laravel normally; do not wrap it in an external transaction. MySQL and SQLite use the standard schema index migration.+## Codex worktreesCodex creates an isolated local environment for each worktree. It uses a dedicated PostgreSQL Docker volume and local Laravel/Nuxt processes, so sibling worktrees do not share ports, data, or API configuration.diff --git a/api/app/Http/Controllers/Forms/FormController.php b/api/app/Http/Controllers/Forms/FormController.phpindex b7bcba79c..d374b39c1 100644--- a/api/app/Http/Controllers/Forms/FormController.php+++ b/api/app/Http/Controllers/Forms/FormController.php@@ -9,7 +9,6 @@ use App\Http\Requests\UploadAssetRequest;use App\Http\Resources\FormListResource;use App\Http\Resources\FormResource;use App\Models\Forms\Form;-use App\Models\Forms\FormSubmission;use App\Models\Version;use App\Models\Workspace;use App\Notifications\Forms\MobileEditorEmail;@@ -17,6 +16,7 @@ use App\Service\Billing\Feature;use App\Service\Forms\FormCleaner;use App\Service\Forms\FormCreationService;use App\Service\Forms\FormDataNormalizer;+use App\Service\Forms\FormListStatsLoader;use App\Service\Forms\FormStructureValidator;use App\Service\Forms\FormUpdateService;use App\Service\Storage\FileUploadPathService;@@ -44,7 +44,7 @@ class FormController extends Controller$this->formCleaner = new FormCleaner();}- public function index(Request $request, Workspace $workspace)+ public function index(Request $request, Workspace $workspace, FormListStatsLoader $statsLoader){$this->authorize('ownsWorkspace', $workspace);$this->authorize('viewAny', Form::class);@@ -67,12 +67,13 @@ class FormController extends Controller'updated_at',])->with(['workspace'])- ->withCount(['submissions as submissions_count' => fn ($q) => $q->where('status', FormSubmission::STATUS_COMPLETED)])- ->withTotalViews()->orderByDesc('updated_at')+ ->orderByDesc('id')->paginate($perPage)->withQueryString();+ $statsLoader->load($forms->getCollection());+return FormListResource::collection($forms);}diff --git a/api/app/Models/Forms/Form.php b/api/app/Models/Forms/Form.phpindex e4f347fbe..b6fbbe6ce 100644--- a/api/app/Models/Forms/Form.php+++ b/api/app/Models/Forms/Form.php@@ -311,12 +311,10 @@ class Form extends Model implements CachableAttributes, VersionableNestedDiff? 'SUM(CAST(JSON_EXTRACT(data, "$.views") AS SIGNED))': 'SUM(CAST(data->>\'views\' AS INTEGER))';- return $query->addSelect([- 'total_views_count' => DB::raw(- 'COALESCE((SELECT COUNT(*) FROM form_views WHERE form_views.form_id = forms.id), 0) + ' .- 'COALESCE((SELECT ' . $statisticsExpression . ' FROM form_statistics WHERE form_statistics.form_id = forms.id), 0)'- )- ]);+ return $query->selectRaw(+ 'COALESCE((SELECT COUNT(*) FROM form_views WHERE form_views.form_id = forms.id), 0) + ' .+ 'COALESCE((SELECT ' . $statisticsExpression . ' FROM form_statistics WHERE form_statistics.form_id = forms.id), 0) AS total_views_count'+ );}diff --git a/api/app/Service/Forms/FormListStatsLoader.php b/api/app/Service/Forms/FormListStatsLoader.phpnew file mode 100644index 000000000..a13fcad50--- /dev/null+++ b/api/app/Service/Forms/FormListStatsLoader.php@@ -0,0 +1,33 @@+<?php++namespace App\Service\Forms;++use App\Models\Forms\Form;+use App\Models\Forms\FormSubmission;+use Illuminate\Database\Eloquent\Collection;++class FormListStatsLoader+{+ /** @param Collection<int, Form> $forms */+ public function load(Collection $forms): void+ {+ if ($forms->isEmpty()) {+ return;+ }++ // Count only the returned page. Counting before OFFSET also evaluates+ // all preceding forms, making later pages increasingly expensive.+ $counts = Form::query()+ ->select('id')+ ->whereKey($forms->modelKeys())+ ->withCount(['submissions as submissions_count' => fn ($query) => $query->where('status', FormSubmission::STATUS_COMPLETED)])+ ->withTotalViews()+ ->get()+ ->keyBy('id');++ foreach ($forms as $form) {+ $form->setAttribute('submissions_count', (int) ($counts->get($form->id)?->getAttribute('submissions_count') ?? 0));+ $form->setAttribute('total_views_count', (int) ($counts->get($form->id)?->getAttribute('total_views_count') ?? 0));+ }+ }+}diff --git a/api/database/migrations/2026_09_18_000000_add_form_id_index_to_form_statistics.php b/api/database/migrations/2026_09_18_000000_add_form_id_index_to_form_statistics.phpnew file mode 100644index 000000000..b481bceaa--- /dev/null+++ b/api/database/migrations/2026_09_18_000000_add_form_id_index_to_form_statistics.php@@ -0,0 +1,42 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\DB;+use Illuminate\Support\Facades\Schema;++return new class () extends Migration {+ public $withinTransaction = false;++ public function up(): void+ {+ if (DB::getDriverName() === 'pgsql') {+ // A failed concurrent build can leave an invalid index behind.+ // Remove it before retrying instead of silently skipping it.+ $index = DB::selectOne("SELECT indisvalid FROM pg_index WHERE indexrelid = to_regclass('form_statistics_form_id_index')");+ if ($index && !$index->indisvalid) {+ DB::statement('DROP INDEX CONCURRENTLY form_statistics_form_id_index');+ }+ DB::statement('CREATE INDEX CONCURRENTLY IF NOT EXISTS form_statistics_form_id_index ON form_statistics (form_id)');++ return;+ }++ Schema::table('form_statistics', function (Blueprint $table) {+ $table->index('form_id');+ });+ }++ public function down(): void+ {+ if (DB::getDriverName() === 'pgsql') {+ DB::statement('DROP INDEX CONCURRENTLY IF EXISTS form_statistics_form_id_index');++ return;+ }++ Schema::table('form_statistics', function (Blueprint $table) {+ $table->dropIndex(['form_id']);+ });+ }+};diff --git a/api/tests/Feature/Forms/FormListPerformanceTest.php b/api/tests/Feature/Forms/FormListPerformanceTest.phpnew file mode 100644index 000000000..5414c4bfd--- /dev/null+++ b/api/tests/Feature/Forms/FormListPerformanceTest.php@@ -0,0 +1,88 @@+<?php++use Illuminate\Support\Facades\DB;++it('loads accurate counters only for the returned page', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $forms = collect(range(1, 3))->map(fn ($i) => $this->createForm($user, $workspace, [+ 'updated_at' => now()->subMinutes($i),+ 'max_submissions_count' => 2,+ ]));+ $target = $forms[2];+ foreach ($forms as $form) {+ DB::table('form_statistics')->insert([+ ['form_id' => $form->id, 'date' => '2026-01-01', 'data' => json_encode(['views' => 7])],+ ['form_id' => $form->id, 'date' => '2026-01-02', 'data' => json_encode(['views' => 5])],+ ]);+ DB::table('form_views')->insert(['form_id' => $form->id]);+ foreach (['completed', 'completed', 'partial'] as $status) {+ DB::table('form_submissions')->insert(['form_id' => $form->id, 'status' => $status, 'data' => '{}']);+ }+ }+ $deleted = $this->createForm($user, $workspace);+ $deleted->delete();+ $otherWorkspace = $this->createUserWorkspace($user);+ $this->createForm($user, $otherWorkspace);++ DB::enableQueryLog();+ DB::flushQueryLog();+ $response = $this->getJson(route('open.workspaces.forms.index', [+ 'workspace' => $workspace->id, 'page' => 3, 'per_page' => 1,+ ]));+ $queries = collect(DB::getQueryLog());+ DB::disableQueryLog();++ $response->assertSuccessful()->assertJsonCount(1, 'data')+ ->assertJsonPath('data.0.id', $target->id)+ ->assertJsonPath('data.0.views_count', 13)+ ->assertJsonPath('data.0.submissions_count', 2)+ ->assertJsonPath('data.0.max_number_of_submissions_reached', true)+ ->assertJsonPath('meta.total', 3);++ $countQueries = $queries->filter(fn ($query) => str_contains($query['query'], 'form_statistics'));+ expect($countQueries)->toHaveCount(1);+ $sql = $countQueries->first()['query'];+ expect(strtolower($sql))->not->toContain('offset');+ expect($sql)->toContain('in ('.$target->id.')');+ // The paginated selection itself must not compute aggregate subqueries.+ $pageQuery = $queries->first(fn ($query) => str_contains(strtolower($query['query']), 'offset'));+ expect($pageQuery)->not->toBeNull();+ expect($pageQuery['query'])->not->toContain('form_statistics', 'form_submissions', 'form_views');+});++it('returns zero counters without statistics and skips count queries for empty pages', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $this->createForm($user, $workspace);+ $this->getJson(route('open.workspaces.forms.index', $workspace->id))+ ->assertSuccessful()->assertJsonPath('data.0.views_count', 0)+ ->assertJsonPath('data.0.submissions_count', 0);++ DB::enableQueryLog();+ DB::flushQueryLog();+ $response = $this->getJson(route('open.workspaces.forms.index', [+ 'workspace' => $workspace->id, 'page' => 2,+ ]));+ $queries = collect(DB::getQueryLog());+ DB::disableQueryLog();+ $response->assertSuccessful()->assertJsonCount(0, 'data')->assertJsonPath('meta.total', 1);+ expect($queries->filter(fn ($query) => str_contains($query['query'], 'form_statistics')))->toHaveCount(0);+});++it('orders forms deterministically when edit timestamps are equal', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $timestamp = now()->startOfSecond();+ $first = $this->createForm($user, $workspace, ['updated_at' => $timestamp]);+ $second = $this->createForm($user, $workspace, ['updated_at' => $timestamp]);+ foreach ([1 => $second, 2 => $first] as $page => $form) {+ $this->getJson(route('open.workspaces.forms.index', [+ 'workspace' => $workspace->id, 'page' => $page, 'per_page' => 1,+ ]))->assertSuccessful()->assertJsonPath('data.0.id', $form->id);+ }+});++it('indexes historical statistics by form', function () {+ expect(\Illuminate\Support\Facades\Schema::hasIndex('form_statistics', ['form_id']))->toBeTrue();+});diff --git a/client/composables/query/forms/useFormsList.js b/client/composables/query/forms/useFormsList.jsindex 51023a71a..0e71bc904 100644--- a/client/composables/query/forms/useFormsList.js+++ b/client/composables/query/forms/useFormsList.js@@ -10,7 +10,7 @@ export function useFormsList(workspaceId, options = {}) {const query = useInfiniteQuery({queryKey: computed(() => ['forms', 'list', workspaceId.value]),queryFn: ({ pageParam = 1 }) => {- const apiFilters = { page: pageParam }+ const apiFilters = { page: pageParam, per_page: 50 }// Note: List endpoint returns lightweight form data (no properties).// Individual form detail queries will fetch the full form when needed.return formsApi.list(workspaceId.value, { params: apiFilters })diff --git a/client/test/unit/useFormsList-pagination.test.ts b/client/test/unit/useFormsList-pagination.test.tsnew file mode 100644index 000000000..58e83e955--- /dev/null+++ b/client/test/unit/useFormsList-pagination.test.ts@@ -0,0 +1,22 @@+import { describe, expect, it, vi } from 'vitest'+import { ref } from 'vue'++const mocks = vi.hoisted(() => ({ list: vi.fn(), useInfiniteQuery: vi.fn() }))+vi.mock('~/api/forms', () => ({ formsApi: { list: mocks.list } }))+vi.mock('@tanstack/vue-query', () => ({+ useInfiniteQuery: mocks.useInfiniteQuery,+ useQueryClient: vi.fn(),+}))+import { useFormsList } from '../../composables/query/forms/useFormsList.js'++describe('form list pagination', () => {+ it('requests bounded larger pages and stops at the API last page', async () => {+ mocks.useInfiniteQuery.mockReturnValue({ data: ref(), hasNextPage: ref(false) })+ useFormsList(ref(42))+ const options = mocks.useInfiniteQuery.mock.calls.at(-1)[0]+ await options.queryFn({ pageParam: 2 })+ expect(mocks.list).toHaveBeenCalledWith(42, { params: { page: 2, per_page: 50 } })+ expect(options.getNextPageParam({ meta: { current_page: 1, last_page: 2 } })).toBe(2)+ expect(options.getNextPageParam({ meta: { current_page: 2, last_page: 2 } })).toBeUndefined()+ })+})