Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update hierarchy query library #1223

Closed
wants to merge 7 commits into from

Conversation

bprusinowski
Copy link
Collaborator

@bprusinowski bprusinowski commented Oct 17, 2023

This PR updates the cube-hierarchy-query library to v2.2.0.

It also updates the way of how we fetch dimension values metadata for hierarchies by using new syntax to directly specify the properties we need to fetch along the hierarchy. However, this seem to make it significantly less performant that our current approach, which means we should probably only update library, but keep the old way of fetching things.

New auto-generated query used to fetch hierarchies and metadata (NFI dataset) – approx. 15s

PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT { 
  ?resource1 schema:identifier ?resource2 .
  ?resource1 schema:name ?resource3 .
  ?resource1 schema:description ?resource5 .
  ?resource1 schema:position ?resource7 .
  ?resource1 schema:alternateName ?resource8 .
  ?resource1 rdf:type ?resource10 .
  ?resource11 schema:containedInPlace ?resource1 .
  ?resource11 schema:identifier ?resource12 .
  ?resource11 schema:name ?resource13 .
  ?resource11 schema:description ?resource15 .
  ?resource11 schema:position ?resource17 .
  ?resource11 schema:alternateName ?resource18 .
  ?resource11 rdf:type ?resource20 .
}
WHERE {
  VALUES ( ?resource1 ) {
    ( <https://ld.admin.ch/country/CHE> )
  }
  {   
    ?resource1 schema:identifier ?resource2 .
  } UNION {
  {
    VALUES ( ?resource1 ) {
     ( <https://ld.admin.ch/country/CHE> )
    }
    ?resource1 schema:name ?resource3 .
    FILTER (lang(?resource3) = "de" ) }
  } UNION {    
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

      ?resource1 schema:description ?resource5 .
      FILTER (lang(?resource5) = "de" ) }

    } UNION {
      
?resource1 schema:position ?resource7 .

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

      ?resource1 schema:alternateName ?resource8 .
      FILTER (lang(?resource8) = "de" ) }

    } UNION {
      
?resource1 rdf:type ?resource10 .

    } UNION {
      { 
    
?resource11 schema:containedInPlace ?resource1 .
 
  } UNION {
      
            
?resource11 schema:containedInPlace ?resource1 .

            

{ 
    
?resource11 schema:identifier ?resource12 .
 
  } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

?resource11 schema:containedInPlace ?resource1 .



      ?resource11 schema:name ?resource13 .
      FILTER (lang(?resource13) = "de" ) }

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

?resource11 schema:containedInPlace ?resource1 .



      ?resource11 schema:description ?resource15 .
      FILTER (lang(?resource15) = "de" ) }

    } UNION {
      
?resource11 schema:position ?resource17 .

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

?resource11 schema:containedInPlace ?resource1 .



      ?resource11 schema:alternateName ?resource18 .
      FILTER (lang(?resource18) = "de" ) }

    } UNION {
      
?resource11 rdf:type ?resource20 .

    }

          
    }
    }


      ?resource1 ^schema:containedInPlace ?resource11 .
      ?resource11 a <https://environment.ld.admin.ch/foen/nfi/UnitOfReference/827> .
    
  
}

Old auto-generated query to fetch hierarchies ((NFI dataset) ) – approx. 2s

PREFIX schema: <http://schema.org/>

CONSTRUCT { 
    ?resource2 schema:containedInPlace ?resource1 .
?resource2 ?resource3 ?resource4 .
?resource1 ?resource5 ?resource6 .
   }

WHERE {

    

VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}
{ 
    { 
    
?resource2 schema:containedInPlace ?resource1 .
 
  } UNION {
      
            
?resource2 schema:containedInPlace ?resource1 .

            


        ?resource2 ?resource3 ?resource4 .
        
        
      

          
    } 
  } UNION {
      
        ?resource1 ?resource5 ?resource6 .
        
        
      
    }


      ?resource1 ^schema:containedInPlace ?resource2 .
      ?resource2 a <https://environment.ld.admin.ch/foen/nfi/UnitOfReference/827> .
    
  
}

@vercel
Copy link

vercel bot commented Oct 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
visualization-tool ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 15, 2023 8:29am

@Rdataflow
Copy link
Contributor

@bprusinowski unfortunately https://visualization-tool-am4klf8am-ixt1.vercel.app/create/new?cube=https://environment.ld.admin.ch/foen/nfi/nfi_C-90/cube/2023-2&dataSource=Prod fails to load with this changes... there might be something important missing yet

@bprusinowski
Copy link
Collaborator Author

@Rdataflow, this is related to the fact that serverless functions for apps deployed on Vercel can execute for max. 15s, while the request was supposed to take longer.

Screenshot 2023-10-18 at 15 34 58

This is related to a fact that by providing a list of properties to fetch along with hierarchy made it less performant, I've updated the description of the PR.

@Rdataflow
Copy link
Contributor

@bprusinowski hmm this would be the contrary of the performance improvements to strive for...
maybe adrian has input on this generated query?
wanna retry and omit the lang filters?

nb: @bprusinowski don't merge this PR yet... I'll tell you more

@Rdataflow
Copy link
Contributor

for the records:
concerned GQL query

curl https://test.visualize.admin.ch/api/graphql -X POST -H 'Content-Type: application/json' -H 'x-visualize-cache-control: no-cache' --data-raw '{"operationName":"ComponentsWithHierarchies","variables":{"iri":"https://environment.ld.admin.ch/foen/nfi/nfi_C-90/cube/2023-2","sourceType":"sparql","sourceUrl":"https://lindas.admin.ch/query","locale":"de"},"query":"query ComponentsWithHierarchies($iri: String!, $sourceType: String!, $sourceUrl: String!, $locale: String!, $latest: Boolean, $filters: Filters, $componentIris: [String!]) {\n  dataCubeByIri(\n    iri: $iri\n    sourceType: $sourceType\n    sourceUrl: $sourceUrl\n    locale: $locale\n    latest: $latest\n  ) {\n    dimensions(\n      sourceType: $sourceType\n      sourceUrl: $sourceUrl\n      componentIris: $componentIris\n    ) {\n      ...dimensionMetadataWithHierarchies\n      __typename\n    }\n    measures(\n      sourceType: $sourceType\n      sourceUrl: $sourceUrl\n      componentIris: $componentIris\n    ) {\n      ...dimensionMetadataWithHierarchies\n      __typename\n    }\n    __typename\n  }\n}\n\nfragment dimensionMetadataWithHierarchies on Dimension {\n  iri\n  label\n  description\n  isNumerical\n  isKeyDimension\n  scaleType\n  dataType\n  order\n  values(sourceType: $sourceType, sourceUrl: $sourceUrl, filters: $filters)\n  unit\n  related {\n    iri\n    type\n    __typename\n  }\n  ... on TemporalDimension {\n    timeUnit\n    timeFormat\n    __typename\n  }\n  ... on NumericalMeasure {\n    isCurrency\n    currencyExponent\n    resolution\n    isDecimal\n    __typename\n  }\n  ...hierarchyMetadata\n}\n\nfragment hierarchyMetadata on Dimension {\n  hierarchy(sourceType: $sourceType, sourceUrl: $sourceUrl) {\n    ...hierarchyValueFields\n    children {\n      ...hierarchyValueFields\n      children {\n        ...hierarchyValueFields\n        children {\n          ...hierarchyValueFields\n          children {\n            ...hierarchyValueFields\n            children {\n              ...hierarchyValueFields\n              __typename\n            }\n            __typename\n          }\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n\nfragment hierarchyValueFields on HierarchyValue {\n  value\n  dimensionIri\n  depth\n  label\n  alternateName\n  hasValue\n  position\n  identifier\n}\n"}' -s -o /dev/null -w '%{time_total}'

@bprusinowski
Copy link
Collaborator Author

bprusinowski commented Oct 19, 2023

@Rdataflow query without language filters is much faster than 15s (takes around 0.5s) :)

PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT { 
    ?resource1 schema:identifier ?resource2 .
?resource1 schema:name ?resource3 .
?resource1 schema:description ?resource4 .
?resource1 schema:position ?resource5 .
?resource1 schema:alternateName ?resource6 .
?resource1 rdf:type ?resource7 .
?resource8 schema:containedInPlace ?resource1 .
?resource8 schema:identifier ?resource9 .
?resource8 schema:name ?resource10 .
?resource8 schema:description ?resource11 .
?resource8 schema:position ?resource12 .
?resource8 schema:alternateName ?resource13 .
?resource8 rdf:type ?resource14 .
   }

WHERE {

    

VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}
{ 
    
?resource1 schema:identifier ?resource2 .
 
  } UNION {
      
?resource1 schema:name ?resource3 .

    } UNION {
      
?resource1 schema:description ?resource4 .

    } UNION {
      
?resource1 schema:position ?resource5 .

    } UNION {
      
?resource1 schema:alternateName ?resource6 .

    } UNION {
      
?resource1 rdf:type ?resource7 .

    } UNION {
      { 
    
?resource8 schema:containedInPlace ?resource1 .
 
  } UNION {
      
            
?resource8 schema:containedInPlace ?resource1 .

            

{ 
    
?resource8 schema:identifier ?resource9 .
 
  } UNION {
      
?resource8 schema:name ?resource10 .

    } UNION {
      
?resource8 schema:description ?resource11 .

    } UNION {
      
?resource8 schema:position ?resource12 .

    } UNION {
      
?resource8 schema:alternateName ?resource13 .

    } UNION {
      
?resource8 rdf:type ?resource14 .

    }

          
    }
    }


      ?resource1 ^schema:containedInPlace ?resource8 .
      ?resource8 a <https://environment.ld.admin.ch/foen/nfi/UnitOfReference/49> .
    
  
}

@Rdataflow
Copy link
Contributor

@bprusinowski good to know, can you share your slow experience and the probable reason here to keep awareness:
https://gitlab.ldbar.ch/bafu/visualize/-/issues/548
THX

@Rdataflow
Copy link
Contributor

@bprusinowski can you rebase this PR to main?

@bprusinowski
Copy link
Collaborator Author

Hey @Rdataflow, the base is actually main. Did you mean to merge it to main?

@Rdataflow
Copy link
Contributor

Hey @Rdataflow, the base is actually main. Did you mean to merge it to main?

I just would like to see the latest commits from main branch included for the preview, thus only rebase on main trunk...

@bprusinowski
Copy link
Collaborator Author

Ah I see, done 👍

@Rdataflow
Copy link
Contributor

@bprusinowski v2.1.1 is out now 🚀 let's give it a try

@bprusinowski
Copy link
Collaborator Author

@Rdataflow it looks that the performance is still not ideal when using language filters; when removed, it gets much better. Let me know if we should simply switch to querying all languages and extracting them on our own 👀

Generated query (not visible in deployment preview due to >15s execution time)

PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT { 
    ?resource1 schema:identifier ?resource2 .
?resource1 schema:name ?resource3 .
?resource1 schema:description ?resource5 .
?resource1 schema:position ?resource7 .
?resource1 schema:alternateName ?resource8 .
?resource1 rdf:type ?resource10 .
?resource11 schema:containedInPlace ?resource1 .
?resource11 schema:identifier ?resource12 .
?resource11 schema:name ?resource13 .
?resource11 schema:description ?resource15 .
?resource11 schema:position ?resource17 .
?resource11 schema:alternateName ?resource18 .
?resource11 rdf:type ?resource20 .
   }

WHERE {

    

VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}
{ 
    
?resource1 schema:identifier ?resource2 .
 
  } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

      ?resource1 schema:name ?resource3 .
      FILTER (lang(?resource3) = "en" ) }

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

      ?resource1 schema:description ?resource5 .
      FILTER (lang(?resource5) = "en" ) }

    } UNION {
      
?resource1 schema:position ?resource7 .

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

      ?resource1 schema:alternateName ?resource8 .
      FILTER (lang(?resource8) = "en" ) }

    } UNION {
      
?resource1 rdf:type ?resource10 .

    } UNION {
      { 
    
?resource11 schema:containedInPlace ?resource1 .
 
  } UNION {
      
            
?resource11 schema:containedInPlace ?resource1 .

            

{ 
    
?resource11 schema:identifier ?resource12 .
 
  } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

?resource11 schema:containedInPlace ?resource1 .



      ?resource11 schema:name ?resource13 .
      FILTER (lang(?resource13) = "en" ) }

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

?resource11 schema:containedInPlace ?resource1 .



      ?resource11 schema:description ?resource15 .
      FILTER (lang(?resource15) = "en" ) }

    } UNION {
      
?resource11 schema:position ?resource17 .

    } UNION {
      
{ 
VALUES ( ?resource1 )
{
( <https://ld.admin.ch/country/CHE> )
}

?resource11 schema:containedInPlace ?resource1 .



      ?resource11 schema:alternateName ?resource18 .
      FILTER (lang(?resource18) = "en" ) }

    } UNION {
      
?resource11 rdf:type ?resource20 .

    }

          
    }
    }


      ?resource1 ^schema:containedInPlace ?resource11 .
      ?resource11 a <https://environment.ld.admin.ch/foen/nfi/UnitOfReference/49> .
    
  
}

@bprusinowski
Copy link
Collaborator Author

Closed in favor of #1364.

@bprusinowski bprusinowski deleted the chore/update-hierarchy-query-library branch March 25, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants