Implementation

Persistence Engine

data are stored among 2 databases mongoDB and clickhouse:

mongoDB collections:

  • resources: artefacts
  • relations: relations between artefacts
  • datasets: dataset definitions
  • logs: sxs queries log

clickhouse tables:

  • obs_${dataset._id}(_ddl): dataset's observations
  • evt_${dataset._id}(_ddl): dataset's events

resources collection

  • artefacts are stored after been converted to SDMX3 if required
  • a resource doc is an artefact SDMX3 JSON object
  • sxs just add a '_meta' tag
> db.resources.findOne({ _id : { $regex: /^dataflow/ }})
{
  _id: 'dataflow:CH1:DSI_DIFF_EWMFA_1:1.0.0',
  id: 'DSI_DIFF_EWMFA_1',
  links: [
    {
      rel: 'self',
      urn: 'urn:sdmx:org.sdmx.infomodel.datastructure.Dataflow=CH1:DSI_DIFF_EWMFA_1(1.0.0)'
    },
  ],
  version: '1.0.0',
  agencyID: 'CH1',
  isFinal: true,
  names: {
    en: 'Material flow accounts - Direct input flows and their aggregates',
    fr: 'Comptes de flux de matières - Flux entrants directs et leurs agrégats',
  },
  annotations: [
    {
      type: 'NonProductionDataflow',
      text: 'true',
      texts: { en: 'true' }
    },
  ],
  structure: 'urn:sdmx:org.sdmx.infomodel.datastructure.DataStructure=CH1:DSD_DIFF_EWMFA_1(1.0.0)',
  _meta: {
    createdAt: ISODate('2024-09-15T15:32:23.901Z'),
    sdmxVersion: '1.0'
  }
}

relations collection

  • 'sourceId' and 'targetId' are '_ids' linked to resources collections
  • 'version' may have a pattern (see semver)
  • in case 'version' has a pattern targetId is the last match to a target
  • 'itemId' is used to identify by which item the relation come from
> db.relations.find({ sourceId: 'datastructure:CH1:DSD_DIFF_EWMFA_1:1.0.0'})
[
  {
    _id: ObjectId('66e6fe077f650b99b2c05a06'),
    sourceId: 'datastructure:CH1:DSD_DIFF_EWMFA_1:1.0.0',
    targetId: 'conceptscheme:CH1:CS_DIFF_EWMFA_1:1.0.0',
    itemId: 'OBS_VALUE',
    version: '1.0.0'
  },
  {
    _id: ObjectId('66e6fe077f650b99b2c05a07'),
    sourceId: 'datastructure:CH1:DSD_DIFF_EWMFA_1:1.0.0',
    targetId: 'conceptscheme:CH1:CS_DIFF_EWMFA_1:1.0.0',
    itemId: 'TIME_PERIOD',
    version: '1.0.0'
  },
  {
    _id: ObjectId('66e6fe077f650b99b2c05a17'),
    sourceId: 'datastructure:CH1:DSD_DIFF_EWMFA_1:1.0.0',
    targetId: 'codelist:CH1:CL_MATERIAL:1.0.0',
    version: '1.0.0+'
  },
  ...
]

datasets collection

> db.datasets.findOne()
{
  _id: ObjectId('6644c5b97f99b02e6053cf4f'),
  name: 'OECD.ENV.EPI:DSD_ECH@COAS_FLOOD:1.1',
  context: 'dataflow',
  target: {
    resourceType: 'dataflow',
    agencyID: 'OECD.ENV.EPI',
    id: 'DSD_ECH@COAS_FLOOD',
    version: '1.1+.0'
  },
  structure: {
    dimensions: {
      dataSet: [],
      observation: [
        {
          id: 'REF_AREA',
          name: 'Reference area',
          names: { en: 'Reference area' },
          keyPosition: 0,
          roles: [ 'REF_AREA' ],
          values: [
            {
              id: 'EL43',
              order: 1393,
              name: 'Crete',
              names: [Object],
              parent: 'GRC',
              annotations: [Array]
            },
            ... 834 more items
          ]
        },
        {
          id: 'FREQ',
          name: 'Frequency of observation',
          names: { en: 'Frequency of observation' },
          keyPosition: 1,
          roles: [ 'FREQ' ],
          values: [ { id: 'A', order: 0, name: 'Annual', names: [Object] } ]
        },
        ...
      ]
    },
    measures: {
      observation: [
        {
          id: 'OBS_VALUE',
          links: [
            {
              rel: 'self',
              urn: 'urn:sdmx:org.sdmx.infomodel.datastructure.PrimaryMeasure=OECD.ENV.EPI:DSD_ECH(1.1).OBS_VALUE'
            }
          ]
        }
      ]
    },
    attributes: {
      dataSet: [],
      dimensionGroup: [],
      observation: [
        {
          id: 'OBS_STATUS',
          name: 'Observation status',
          names: { en: 'Observation status' },
          roles: [ 'OBS_STATUS' ],
          values: [],
          relationship: { primaryMeasure: 'OBS_VALUE' }
        },
        {
          id: 'UNIT_MULT',
          name: 'Unit multiplier',
          names: { en: 'Unit multiplier' },
          roles: [ 'UNIT_MULT' ],
          values: [ { id: '0', order: 0, name: 'Units', names: [Object] } ],
          relationship: {
            dimensions: [
              'REF_AREA',
              'FREQ',
              'MEASURE',
              'UNIT_MEASURE',
              'DURATION',
              'HEAT_STRESS',
              'RET_PERIOD',
              'HURRICANE_WIND_SCALE'
            ]
          }
        },
        ...
      ]
    },
    annotations: [
      {
        type: 'NonProductionDataflow',
        text: 'true',
        texts: { en: 'true' }
      },
      ... 903 more items
    ],
    timeDimension: {
      id: 'TIME_PERIOD',
      links: [
        {
          rel: 'self',
          urn: 'urn:sdmx:org.sdmx.infomodel.datastructure.TimeDimension=OECD.ENV.EPI:DSD_ECH(1.1).TIME_PERIOD'
        }
      ],
      position: 8,
      type: 'timeDimension',
      conceptIdentity: 'urn:sdmx:org.sdmx.infomodel.conceptscheme.Concept=OECD.ENV.EPI:EXPOSURE_TO_CLIMATE_HAZARDS(1.0).TIME_PERIOD',
      localRepresentation: {
        textFormat: {
          textType: 'ObservationalTimePeriod',
          isSequence: false,
          isMultiLingual: false
        },
        format: {
          textType: 'ObservationalTimePeriod',
          isSequence: false,
          isMultiLingual: false,
          dataType: 'ObservationalTimePeriod'
        }
      }
    }
  },
  _meta: {
    createdAt: ISODate('2024-05-15T14:24:57.765Z'),
    createdBy: null,
    sdmxVersion: '1.0',
    cMapping: [
      {
        id: 'REF_AREA',
        type: 'dimension',
        level: 'observation',
        key: 'd0',
        format: null,
        keyPosition: 0
      },
      {
        id: 'FREQ',
        type: 'dimension',
        level: 'observation',
        key: 'd1',
        format: null,
        keyPosition: 1
      },
      {
        id: 'MEASURE',
        type: 'dimension',
        level: 'observation',
        key: 'd2',
        format: null,
        keyPosition: 2
      },
      {
        id: 'TIME_PERIOD',
        type: 'dimension',
        level: 'observation',
        isTimeDimension: true,
        format: {
          textType: 'ObservationalTimePeriod',
          isSequence: false,
          isMultiLingual: false,
          dataType: 'ObservationalTimePeriod'
        },
        keyPosition: 8
      },
    ],
    metrics: {
      observations: 251184,
      series: 14784,
      annotations: 1003,
      dimensions: 9,
      attributes: 3,
      measures: 1
    }
  },
  data: {
    annotations: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
  },
  publicationTime: ISODate('2024-05-15T14:24:57.244Z')
}

observations table

clickhouse1 :) select * from obs_66e43fd832247b2d92900ec4_ddl limit 1 format Vertical

Row 1:
──────
LOCATION:     SVN
SUBJECT:      PWHP0000
MEASURE:      AL
FREQUENCY:    A
start:        2024-01-01 00:00:00.000
end:          2024-12-31 23:59:59.000
OBS_VALUE:    1000
OBS_STATUS:   A
UNIT_MEASURE:
UNIT_MULT:
BASE_PER:
__key__:      [[16,192,14,0,6],[1000],[3],[]]

Relations

relations collection describe a graph of dependencies between artefacts

collection is used each time references search params if structure queries not equals 'none'

sxs do not hardcode dependencies to return from a resourceType, but scan recursively the dependency graph dynamically

Artefact dependencies are defined in @dbmx/sdmx/types

To add a new artefact, all we have to do is to define a dedicated class with its children dependencies:

import { Artefact } from './sdmx'
import { urn2FullRef } from '../core'

function getChildRefs(input) {
  if (!input.structure) return []
  return [urn2FullRef(input.structure)]
}

export default Artefact({
  name: 'dataflow',
  structureName: 'dataflows',
  packageName: 'datastructure',
  className: 'Dataflow',
  getChildRefs,
})

cMapping

dataset._meta.cMapping is an internal object that defined dataset's components to avoid to read DSD and so on each request and to keep track of internal implementation (dictionary)

CMapping is built at ingestion time (insert, update) from DSD grammar, so if grammar change, cMapping will not, but in a well design semVer grammar, an update that may impact CMapping must/should require a new dataset.