All files / data_handler mappers.ts

45.87% Statements 50/109
38.09% Branches 24/63
53.84% Functions 7/13
48.42% Lines 46/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1574x     4x 3x 2x 2x 2x                                                                                                                       4x 4x 2x 2x 2x 2x 2x 2x 6x 6x 6x 4x 4x 4x 4x   4x                   4x             4x   4x 4x 4x 4x 8x 8x 4x   8x           4x 4x 8x 8x                   4x 4x 4x     4x 4x     2x 2x 2x                         6x 3x    
import DataHandler from './index'
import type {Entity, EntityData, IdMap, MeasureInfo, ResourceField, ResourceFields, Variable} from '../types'
 
export function variables(this: DataHandler) {
  this.metadata.datasets.forEach((k: string) => {
    this.data_queue[k] = {}
    const m = this.info[k]
    Iif (m) {
      m.id_vars = m.ids.map((id: IdMap) => id.variable)
      m.schema.fields.forEach((v: ResourceField) => {
        const vn = v.name
        if (vn in this.variables) {
          const ve = this.variables[vn]
          ve.datasets.push(k)
          ve.info[k] = v
          Iif ('string' === v.type) {
            ve.levels = []
            ve.level_ids = {}
            ;((v.info && v.info.levels) || Object.keys(v.table)).forEach(l => {
              Iif (!(l in ve.level_ids)) {
                ve.level_ids[l] = ve.levels.length
                ve.levels.push(l)
              }
            })
          }
        } else {
          const ve: Variable = (this.variables[vn] = {
            datasets: [k],
            info: {} as ResourceFields,
            time_range: {},
            type: v.type,
            code: vn,
            name: vn,
            meta: v.info,
            levels: [],
            level_ids: {},
            table: {},
            order: [],
            views: {},
          })
          ve.info[k] = v
          Iif ('string' === v.type) {
            ve.levels = []
            ve.level_ids = {}
            Object.keys(v.table).forEach(l => {
              ve.level_ids[l] = ve.levels.length
              ve.levels.push(l)
            })
          }
          Iif (!ve.meta)
            ve.meta = {
              full_name: vn,
              measure: vn.split(':')[1] || vn,
              short_name: this.format_label(vn),
              type: v.type || 'unknown',
            }
          ve.meta.full_name = vn
          Iif (!('measure' in ve.meta)) ve.meta.measure = vn.split(':')[1] || vn
          Iif (!('short_name' in ve.meta)) ve.meta.short_name = this.format_label(vn)
          Iif (!('long_name' in ve.meta)) ve.meta.long_name = ve.meta.short_name
          Iif (!(vn in this.variable_info)) this.variable_info[vn] = ve.meta as MeasureInfo
        }
      })
    }
  })
}
 
export async function entities(this: DataHandler, g: string): Promise<void> {
  if (g in this.sets && !this.inited[g] && g in this.meta.times) {
    const s = this.sets[g],
      time = this.meta.times[g],
      retriever = DataHandler.retrievers[time.is_single ? 'single' : 'multi'],
      datasets = Object.keys(this.sets),
      infos = this.info
    Object.keys(s).forEach((id: string) => {
      const si = s[id] as EntityData,
        l = id.length
      if ('_meta' !== id) {
        const overwrite = this.entity_features[g][id]
        const f = overwrite || {id: id, name: id}
        f.id = id
        Iif (!f.name) f.name = id
        const e = (
          id in this.entities
            ? this.entities[id]
            : {
                group: g,
                data: si,
                variables: this.variables,
                features: f,
                views: {},
              }
        ) as Entity
        Iif (id in this.entities) {
          e.group = g
          e.data = si
          e.variables = this.variables
          Iif (!e.features) e.features = {}
          Iif (!e.views) e.views = {}
        } else {
          this.entities[id] = e
        }
        if (!(id in this.entity_tree)) this.entity_tree[id] = {parents: {}, children: {}}
        const rel = this.entity_tree[id]
        e.relations = rel
        Object.keys(f).forEach((k: string) => {
          if (!(k in this.features)) this.features[k] = this.format_label(k)
          if ('id' === k || overwrite || !(k in e.features)) {
            e.features[k] = f[k]
          }
          Iif (-1 !== this.metadata.datasets.indexOf(k)) {
            Iif (!(f[k] in this.entity_tree)) this.entity_tree[f[k]] = {parents: {}, children: {}}
            this.entity_tree[f[k]].children[id] = rel
            rel.parents[f[k]] = this.entity_tree[f[k]]
          }
        })
        if (infos) {
          datasets.forEach((d: string) => {
            const p = d in infos && infos[d].id_length
            Iif (p && p < l) {
              const sl = id.substring(0, p)
              Iif (sl in this.sets[d]) {
                Iif (!(sl in this.entity_tree)) this.entity_tree[sl] = {parents: {}, children: {}}
                this.entity_tree[sl].children[id] = rel
                rel.parents[sl] = this.entity_tree[sl]
              }
            }
          })
        }
        this.settings.view_names.forEach((v: string) => {
          if (!(v in e.views)) {
            e.views[v] = {summary: {}, rank: {}, subset_rank: {}}
          }
        })
        e.time = time
        e.get_value = retriever.bind(e)
      }
    })
    this.inited[g] = true
    this.data_promise[g]()
    setTimeout(() => {
      Iif (!this.inited.first) {
        this.hooks.init && this.hooks.init()
        this.inited.first = true
      }
      g in this.data_queue &&
        Object.keys(this.data_queue[g]).forEach((id: string) => {
          this.data_queue[g][id]()
          delete this.data_queue[g][id]
        })
      this.hooks.onload && this.hooks.onload(g)
    }, 0)
  }
  for (const k in this.info) if (Object.prototype.hasOwnProperty.call(this.info, k) && !this.inited[k]) return void 0
  this.all_data_ready()
}