Dynamic Selection Fields with huge amount of entities

Any issue about Cyclos 4 scripting and Webservices

Moderators: alexandre, hugo, rmvanarkel

Post Reply
erich.manser
Posts: 24
Joined: Tue Nov 08, 2022 10:40 am

Dynamic Selection Fields with huge amount of entities

Post by erich.manser »

Hi everybody,

I need some inspiration. I'm looking for a way to solve a problem with two record types, that should be linked.

System record Zip List: fields: zip, city, community, region. Each required, each single line text fields and none of them can be unique. Since one zip can be guilty for different cities and on the other hand one city can have different zips. The data is a mess with its own charme. 8)

User record Community: fields: community_name required as single line text, cities required as Dynamic multiple selection referencing Zip List. And some more single and multiple selection fields.

The mechanism works fine in both old and new frontend with Cyclos 4.16.11.

My problem is that there are more than 19000 records in Zip List. The load value script combines all of the values of one record in Zip List to one value string for the DynamicFieldValueVO. This leads to a getData result of 199kb and takes almost 20 seconds to load in the old frontend and 4 minutes to load in the new frontend. Then trying to select a city takes another 10 seconds to open the dropdown.

I'm looking for an elegant solution to connect the cities to a Community.
Any ideas how I could solve this problem?
jakob.schumann
Posts: 63
Joined: Thu Apr 23, 2020 5:37 pm

Re: Dynamic Selection Fields with huge amount of entities

Post by jakob.schumann »

Hello Erich,

do you mind sharing your script?
I'm not sure what you mean by "The [...] script combines [...] to one value string for the DynamicFieldValueVO." Don't you create multiple DynamicFieldValueVO, one for each ZIP?

With "getData result of 199kb" you mean the resulting XHR query in the new frontend? Is the time taken in the backend/by the script the problem or the time loading/displaying in the frontend? Best solution for both of these issues would probably be if Cyclos had the option to use autocomplete fields for lists with very many options.

If the problem is actually generating so many DynamicFieldValueVOs, there are multiple different approaches for those load value scripts:

1) very primitive, here listing ad categories:

Code: Select all

def cats = adCategoryService.listRoot(true)
def outs = []
cats.each{cat ->
    def vo = new DynamicFieldValueVO(
        cat.internalName/*value*/,
        cat.name/*label*/
    )
    outs.add(vo)
}

return outs
2) offloading most of the work to internal methods instead of creating the list in groovy, here listing all groups a user can access:

Code: Select all

def loadUserGroups() {
    def variables = binding.variables
    def groupsHandler = variables.groupsHandler as GroupsHandler
    def sessionData = variables.sessionData as SessionData

    return groupsHandler
            .getAccessibleUserGroups(sessionData.getLoggedUser())
            .stream()
            .map { new DynamicFieldValueVO(String.valueOf(it.id), it.name) }
            .collect(Collectors.toList())
}

loadUserGroups()
3) not returning DynamicFieldValueVOs at all but a query result, see the example "Linked user: active brokers" in the documentation. This is probably the most performant option but would only work for "linked entity" fields, not for dynamic selections.

Best regards,
Jakob
erich.manser
Posts: 24
Joined: Tue Nov 08, 2022 10:40 am

Re: Dynamic Selection Fields with huge amount of entities

Post by erich.manser »

Hello Jakob,

this is the script. It's rather simple:

Code: Select all

import org.cyclos.impl.users.RecordServiceLocal
import org.cyclos.model.system.fields.DynamicFieldValueVO
import org.cyclos.model.users.records.RecordVO
import org.cyclos.model.users.records.SystemRecordQuery
import org.cyclos.model.users.recordtypes.RecordTypeVO

//file:noinspection GrPackageDynamicFieldValueVO

def q = new SystemRecordQuery(
  type: new RecordTypeVO(
    internalName: 'orte'
  )
)
q.setUnlimited()
//q.setPageSize(10)
def zips = (recordService as RecordServiceLocal).search(q)

def result = []

zips.each { RecordVO it ->
  String Ort = Helper.getCustomValueByInternalName( it, 'ort')
  String Gemeinde = Helper.getCustomValueByInternalName( it, 'gemeindename')
  String Wert = Helper.getCustomValueByInternalName( it, 'plz') + ' ' +
    Ort
  if ( !Ort.equals(Gemeinde)) {
    Wert += ' - ' + Gemeinde
  }
  result << new DynamicFieldValueVO(
    it.id.toString(),
    Wert
  )
}
return result
Now to your questions:
1. Yes getData is the XHR call from new Frontend.
2. The problem is the time loading/displaying in the frontend.
3. With the three solutions you provide, I'm currently not sure what you mean by that. E. g. should solution 3 be a groovy load value script?

Thanks for your help.
jakob.schumann
Posts: 63
Joined: Thu Apr 23, 2020 5:37 pm

Re: Dynamic Selection Fields with huge amount of entities

Post by jakob.schumann »

Yes, all my solutions are meant to be "load custom values" scripts, in case this is the bottleneck. Performance in the frontend could probably only be solved by not loading all values at once but using autocomplete, but this is something Cyclos must support first.
Post Reply