Skip to content

Commit 9070053

Browse files
authoredDec 18, 2024
Merge pull request #10 from OS2Forms/feature/avoid-loading-submissions-when-fetching-all
Avoided loading submissions when fetching all submissions
2 parents df8a972 + 7a5505c commit 9070053

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed
 

‎.github/workflows/pr.yaml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
runs-on: ubuntu-latest
2424
strategy:
2525
matrix:
26-
php-versions: [ '8.1' ]
26+
php-versions: [ '8.3' ]
2727
dependency-version: [ prefer-lowest, prefer-stable ]
2828
steps:
2929
- uses: actions/checkout@master
@@ -55,7 +55,7 @@ jobs:
5555
runs-on: ubuntu-latest
5656
strategy:
5757
matrix:
58-
php-versions: [ '8.1' ]
58+
php-versions: [ '8.3' ]
5959
dependency-version: [ prefer-lowest, prefer-stable ]
6060
steps:
6161
- uses: actions/checkout@master
@@ -88,7 +88,7 @@ jobs:
8888
runs-on: ubuntu-latest
8989
strategy:
9090
matrix:
91-
php-versions: [ '8.1' ]
91+
php-versions: [ '8.3' ]
9292
dependency-version: [ prefer-lowest, prefer-stable ]
9393
steps:
9494
- uses: actions/checkout@master
@@ -113,18 +113,24 @@ jobs:
113113
run: |
114114
# We need a Drupal project to run drupal-check (cf. https://github.com/mglaman/drupal-check#usage)
115115
# Install Drupal
116-
composer --no-interaction create-project drupal/recommended-project:^9 --stability=dev drupal
116+
composer --no-interaction create-project drupal/recommended-project:^10 --stability=dev drupal
117117
# Copy our module source code into the Drupal module folder.
118118
mkdir -p drupal/web/modules/contrib/os2forms_rest_api
119119
cp -r os2forms_rest_api.* composer.json src drupal/web/modules/contrib/os2forms_rest_api
120+
121+
# Allow all plugins
122+
composer --working-dir=drupal config --no-plugins allow-plugins true
123+
120124
# Add our module as a composer repository.
121125
composer --no-interaction --working-dir=drupal config repositories.os2forms/os2forms_rest_api path web/modules/contrib/os2forms_rest_api
126+
122127
# Restore Drupal composer repository.
123128
composer --no-interaction --working-dir=drupal config repositories.drupal composer https://packages.drupal.org/8
124129
125-
composer --no-interaction --working-dir=drupal config --no-plugins allow-plugins.cweagans/composer-patches true
126-
composer --no-interaction --working-dir=drupal config --no-plugins allow-plugins.zaporylie/composer-drupal-optimizations true
127-
composer --no-interaction --working-dir=drupal config --no-plugins allow-plugins.simplesamlphp/composer-module-installer true
130+
# Make Drupal 10 compatible
131+
composer --working-dir=drupal --no-interaction require psr/http-message:^1.0
132+
composer --working-dir=drupal --no-interaction require 'mglaman/composer-drupal-lenient'
133+
composer --working-dir=drupal config --no-plugins --merge --json extra.drupal-lenient.allowed-list '["drupal/coc_forms_auto_export", "drupal/webform_node_element"]'
128134
129135
# @see https://getcomposer.org/doc/03-cli.md#modifying-extra-values
130136
composer --no-interaction --working-dir=drupal config --no-plugins --json extra.enable-patching true

‎CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ about writing changes to this log.
88

99
## [Unreleased]
1010

11+
## [2.2.0]
12+
13+
- Avoided loading submissions through `webform_submission` storage.
14+
1115
## [2.1.0]
1216

1317
- Drupal 10 compatibility.
@@ -40,7 +44,8 @@ about writing changes to this log.
4044

4145
- Release 1.0.0
4246

43-
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.1.0...HEAD
47+
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.2.0...HEAD
48+
[2.2.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.1.0...2.2.0
4449
[2.1.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.3...2.1.0
4550
[2.0.3]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.2...2.0.3
4651
[2.0.2]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.1...2.0.2

‎composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"require-dev": {
2727
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
2828
"drupal/coder": "^8.3",
29-
"mglaman/drupal-check": "^1.4"
29+
"mglaman/drupal-check": "^1.4",
30+
"mglaman/phpstan-drupal": "~1.2.0"
3031
},
3132
"scripts": {
3233
"code-analysis/drupal-check": [
@@ -54,7 +55,8 @@
5455
"dealerdirect/phpcodesniffer-composer-installer": true,
5556
"cweagans/composer-patches": true,
5657
"zaporylie/composer-drupal-optimizations": true,
57-
"simplesamlphp/composer-module-installer": true
58+
"simplesamlphp/composer-module-installer": true,
59+
"mglaman/composer-drupal-lenient": true
5860
}
5961
},
6062
"extra": {

‎src/Plugin/rest/resource/WebformAllFormSubmissions.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Drupal\os2forms_rest_api\Plugin\rest\resource;
44

5-
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
6-
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
75
use Drupal\Core\Url;
86
use Drupal\os2forms_rest_api\WebformHelper;
97
use Drupal\rest\ModifiedResourceResponse;
@@ -39,18 +37,18 @@ class WebformAllFormSubmissions extends ResourceBase {
3937
private $currentRequest;
4038

4139
/**
42-
* The entity type manager object.
40+
* The webform helper.
4341
*
44-
* @var \Drupal\Core\Entity\EntityTypeManager
42+
* @var \Drupal\os2forms_rest_api\WebformHelper
4543
*/
46-
private $entityTypeManager;
44+
private $webformHelper;
4745

4846
/**
49-
* The webform helper.
47+
* The database connection.
5048
*
51-
* @var \Drupal\os2forms_rest_api\WebformHelper
49+
* @var \Drupal\Core\Database\Connection
5250
*/
53-
private $webformHelper;
51+
private $database;
5452

5553
/**
5654
* {@inheritdoc}
@@ -60,9 +58,9 @@ class WebformAllFormSubmissions extends ResourceBase {
6058
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
6159
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
6260

63-
$instance->entityTypeManager = $container->get('entity_type.manager');
6461
$instance->currentRequest = $container->get('request_stack')->getCurrentRequest();
6562
$instance->webformHelper = $container->get(WebformHelper::class);
63+
$instance->database = $container->get('database');
6664

6765
return $instance;
6866
}
@@ -112,32 +110,20 @@ public function get(string $webform_id): ModifiedResourceResponse {
112110

113111
$result = ['webform_id' => $webform_id];
114112

115-
try {
116-
$submissionEntityStorage = $this->entityTypeManager->getStorage('webform_submission');
117-
}
118-
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
119-
$errors = [
120-
'error' => [
121-
'message' => $this->t('Could not load webform submission storage'),
122-
],
123-
];
124-
125-
return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR);
126-
}
127-
128-
// Query for webform submissions with this webform_id.
129-
$submissionQuery = $submissionEntityStorage->getQuery()
130-
->condition('webform_id', $webform_id);
131-
132113
$requestQuery = $this->currentRequest->query;
133114

115+
// We use raw SQL to fetch submission sids and uuids.
116+
// This is to avoid degraded performance by having to load every
117+
// single submission through the webform submissions storage.
118+
$query = 'SELECT sid, uuid FROM webform_submission WHERE webform_id = :webform_id';
119+
134120
foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
135121
$value = $requestQuery->get($param);
136122

137123
if (!empty($value)) {
138124
try {
139125
$dateTime = new \DateTimeImmutable($value);
140-
$submissionQuery->condition('created', $dateTime->getTimestamp(), $operator);
126+
$query .= sprintf(' AND created %s %s', $operator, $dateTime->getTimestamp());
141127
$result[$param] = $value;
142128
}
143129
catch (\Exception $e) {
@@ -152,9 +138,10 @@ public function get(string $webform_id): ModifiedResourceResponse {
152138
}
153139
}
154140

155-
// Complete query.
156-
$submissionQuery->accessCheck(FALSE);
157-
$sids = $submissionQuery->execute();
141+
$submissions = $this->database->query(
142+
$query,
143+
[':webform_id' => $webform_id]
144+
)->fetchAllKeyed();
158145

159146
// Generate submission URLs.
160147
try {
@@ -163,12 +150,12 @@ public function get(string $webform_id): ModifiedResourceResponse {
163150
'rest.webform_rest_submission.GET',
164151
[
165152
'webform_id' => $webform_id,
166-
'uuid' => $submission->uuid(),
153+
'uuid' => $submission,
167154
]
168155
)
169156
->setAbsolute()
170157
->toString(TRUE)->getGeneratedUrl(),
171-
$submissionEntityStorage->loadMultiple($sids ?: [])
158+
$submissions ?: []
172159
);
173160
}
174161
catch (\Exception $e) {

‎src/WebformHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf
179179
* @return \Drupal\webform\WebformInterface|null
180180
* The webform if found.
181181
*/
182-
public function getWebform(string $webformId, string $submissionUuid = NULL): ?WebformInterface {
182+
public function getWebform(string $webformId, ?string $submissionUuid = NULL): ?WebformInterface {
183183
if (NULL !== $submissionUuid) {
184184
$storage = $this->entityTypeManager->getStorage('webform_submission');
185185
$submissionIds = $storage

0 commit comments

Comments
 (0)