Skip to content

Commit a8d08c4

Browse files
committed
Add the option for preventing the generation of new IDs if the number of non-indexed objects exceeds a defined value
1 parent 2054df6 commit a8d08c4

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ All plugin configuration takes place in base configuration.
2525
* *ID field name*: The name of the ID field to be filled out by the plugin. This has to be a numeric field. The field will only be updated if it is empty, if the parent field has been newly created and if all base fields have been filled out by the user.
2626
* *Base field names*: The names of the base fields to consider when setting the ID. These can be text fields or fields of the [custom data type DANTE](https://github.com/programmfabrik/fylr-plugin-custom-data-type-dante).
2727
* *IDs of pools to consider*: If not empty, IDs are only updated for resources in the specified pools (and their child pools). Also, resources in other pools are not considered during ID generation.
28+
* *Maximum number of non-indexed objects*: If not empty, the plugin will check if the indexer is still working by looking at the number of non-indexed objects. If the number is higher than the value specified here and an ID would have to be created, the object will not be saved and an error message will appear instead. This setting can be used to prevent the generation of incorrect IDs in the case of an overloaded indexer.
29+
* *Text of indexer error message*: This text is shown as the error message that appears if the maximum number of non-indexed objects is exceeded and the object is not saved.
2830

2931
## Example
3032

l10n/numeric-id-auto-incrementer.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fiel
77
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.id_field_name.label,Name des ID-Feldes,ID field name
88
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.base_fields.label,Namen der Basisfelder,Base field names
99
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.pool_ids.label,IDs der berücksichtigten Pools,IDs of pools to consider
10+
server.config.parameter.system.numericIdAutoIncrementer.max_not_indexed.label,Maximale Anzahl nicht indizierter Objekte,Maximum number of non-indexed objects
11+
server.config.parameter.system.numericIdAutoIncrementer.indexer_error_message.label,Text der Indexer-Fehlermeldung,Text of indexer error message
12+
base.error.realm.objectNotSaved,Speichern nicht möglich,Saving not possible

manifest.master.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ base_config:
4444
position: 3
4545
position: 1
4646
position: 0
47+
max_not_indexed:
48+
type: int
49+
position: 1
50+
indexer_error_message:
51+
type: text
52+
position: 2
4753

4854
callbacks:
4955
db_pre_save:

src/server/setIds.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ function getPluginConfiguration(data) {
3636

3737
async function processObject(object, configuration, addedIds) {
3838
const nestedFieldsConfiguration = getNestedFieldsConfiguration(configuration, object._objecttype);
39+
const indexerSettings = {
40+
maxNotIndexed: configuration.max_not_indexed,
41+
errorMessage: configuration.indexer_error_message
42+
};
3943
let changed = false;
4044

4145
for (let nestedFieldConfiguration of nestedFieldsConfiguration) {
4246
if (!isInConfiguredPool(object, nestedFieldConfiguration)) continue;
43-
if (await processNestedFields(object, nestedFieldConfiguration, addedIds)) changed = true;
47+
if (await processNestedFields(object, nestedFieldConfiguration, indexerSettings, addedIds)) changed = true;
4448
}
4549

4650
return changed;
@@ -62,7 +66,7 @@ function isInConfiguredPool(object, nestedFieldConfiguration) {
6266
return false;
6367
}
6468

65-
async function processNestedFields(object, nestedFieldConfiguration, addedIds) {
69+
async function processNestedFields(object, nestedFieldConfiguration, indexerSettings, addedIds) {
6670
const nestedFields = getFieldValues(
6771
object[object._objecttype],
6872
nestedFieldConfiguration.field_path
@@ -78,6 +82,7 @@ async function processNestedFields(object, nestedFieldConfiguration, addedIds) {
7882
nestedFieldConfiguration.id_field_name,
7983
nestedFieldConfiguration.base_fields?.map(field => field.field_name),
8084
nestedFieldConfiguration.pool_ids?.map(pool => pool.pool_id),
85+
indexerSettings,
8186
addedIds
8287
)) changed = true;
8388
}
@@ -103,15 +108,15 @@ function getFieldValues(object, fieldPath) {
103108
}
104109
}
105110

106-
async function addId(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, addedIds) {
111+
async function addId(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, indexerSettings, addedIds) {
107112
if (!idFieldName?.length
108113
|| !baseFieldNames
109114
|| baseFieldNames.find(baseFieldName => !nestedField[baseFieldName])
110115
|| nestedField[idFieldName]
111116
|| nestedField._uuid) return false;
112117

113118
const newId = await getIdValue(
114-
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, addedIds
119+
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, indexerSettings, addedIds
115120
);
116121

117122
nestedField[idFieldName] = newId;
@@ -121,7 +126,8 @@ async function addId(objectType, nestedFields, nestedField, nestedFieldPath, idF
121126
return true;
122127
}
123128

124-
async function getIdValue(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, addedIds) {
129+
async function getIdValue(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, indexerSettings, addedIds) {
130+
await assertIndexerIsFree(indexerSettings);
125131
const existingIdValues = await findExistingIdValues(
126132
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, addedIds
127133
);
@@ -211,7 +217,7 @@ async function findOtherObjects(objectType, nestedField, nestedFieldPath, idFiel
211217
)
212218
) : result.objects;
213219
} catch (err) {
214-
throwErrorToFrontend('Search request failed', JSON.stringify(err));
220+
throwErrorToFrontend('Suchanfrage fehlgeschlagen', JSON.stringify(err));
215221
}
216222
}
217223

@@ -236,12 +242,37 @@ function isDanteConcept(fieldValue) {
236242
&& fieldValue.conceptURI !== undefined;
237243
}
238244

239-
function throwErrorToFrontend(error, description) {
245+
async function assertIndexerIsFree(indexerSettings) {
246+
if (!indexerSettings.maxNotIndexed) return;
247+
248+
const systemStatusData = await getSystemStatusData();
249+
const totalNotIndexed = systemStatusData.Stats.total_not_indexed;
250+
251+
if (totalNotIndexed > indexerSettings.maxNotIndexed) {
252+
throwErrorToFrontend(indexerSettings.errorMessage, undefined, 'objectNotSaved');
253+
}
254+
}
255+
256+
async function getSystemStatusData() {
257+
try {
258+
const response = await fetch('http://fylr.localhost:8082/inspect/system/status/', {
259+
method: 'GET',
260+
headers: {
261+
'Accept': 'application/json'
262+
}
263+
});
264+
return await response.json();
265+
} catch (err) {
266+
throwErrorToFrontend('Systemstatus konnte nicht abgerufen werden', JSON.stringify(err));
267+
}
268+
}
269+
270+
function throwErrorToFrontend(error, description, realm) {
240271
console.log(JSON.stringify({
241272
error: {
242273
code: 'error.numericIdAutoIncrementer',
243274
statuscode: 400,
244-
realm: 'api',
275+
realm: realm ?? 'api',
245276
error,
246277
parameters: {},
247278
description

0 commit comments

Comments
 (0)