All files / site dataview.ts

16.19% Statements 34/210
5% Branches 7/140
9.67% Functions 3/31
17% Lines 34/200

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 4642x     2x                                                                                                                 2x               2x                     2x 2x                               2x                       2x                   2x                         2x 2x 2x 2x 2x 2x     2x     2x     2x     2x 2x 2x 2x 2x 2x               2x                             2x                                                                                                                                                                                       2x 2x                         2x   2x 2x             2x   2x                                                                                                     2x                                                                                                                                                                                                                                            
import DataHandler from '../data_handler/index'
import type {Entities, Entity, Filter, Generic, SiteCondition, VariableFilterParsed} from '../types'
import Community from './index'
import {component_fun} from './time_funs'
 
type VariableCondition = {
  variable: string
  type?: string
  value?: string | number
  operator?: string
  component?: string
}
 
export type DataViewSpec = {
  dataset?: string
  ids?: string
  palette?: string
  time?: string
  time_agg?: string
  time_filters?: VariableCondition[]
  x?: string
  y?: string
}
 
export type DataViewParsed = {
  dataset: string
  ids: EntitySelection | false
  features: string
  variables: string
  time_filters: string
  time_agg: number
  id_source: string
  palette: string
  variable_values: Map<string, VariableFilterParsed>
  feature_values: {[index: string]: Filter}
}
 
type EntitySelection = {[index: string]: boolean | Entity}
 
export type DataViewSelection = {
  ids: Entities
  children: Entities
  features: Entities
  variables: Entities
  dataset: Entities
  filtered: Entities
  full_filter: Entities
  all: Entities
}
type DataViewSelectionCount = {
  ids: number
  children: number
  features: number
  variables: number
  dataset: number
  filtered: number
  full_filter: number
  all: number
}
 
export class SiteDataView {
  id: string
  palette: string
  y: string
  x: string
  time: string
  time_agg: string | number
  time_filters: VariableCondition[]
  time_range = {
    dataset: '',
    variable: '',
    filtered: [-Infinity, Infinity],
    filtered_index: [-Infinity, Infinity],
    index: [-Infinity, Infinity],
    time: [-Infinity, Infinity],
  }
  dataset: string
  ids: string
  features: Generic
  state = 'initial'
  valid: {[index: string]: boolean} = {}
  get: {
    dataset: () => string
    single_id: () => string
    ids: () => EntitySelection | false
    features: () => string
    variables: () => string
    time_filters: () => string
  }
  checks: {
    dataset: (e: Entity) => boolean
    ids: (e: Entity) => boolean
    features: (e: Entity) => boolean
    variables: (e: Entity) => boolean
  }
  site: Community
  parsed: DataViewParsed = {
    dataset: '',
    ids: false,
    features: '',
    variables: '',
    time_filters: '',
    time_agg: 0,
    id_source: '',
    palette: '',
    variable_values: new Map(),
    feature_values: {},
  }
  selection: DataViewSelection = {
    ids: {},
    children: {},
    features: {},
    variables: {},
    dataset: {},
    filtered: {},
    full_filter: {},
    all: {},
  }
  n_selected: DataViewSelectionCount = {
    ids: 0,
    children: 0,
    features: 0,
    variables: 0,
    dataset: 0,
    filtered: 0,
    full_filter: 0,
    all: 0,
  }
  variables: VariableCondition[]
  times: boolean[]
  constructor(site: Community, id: string, spec?: DataViewSpec) {
    this.site = site
    this.id = id
    this.update = this.update.bind(this)
    site.dataviews[id] = this
    if (spec)
      Object.keys(spec).forEach((k: keyof DataViewSpec) => {
        ;(this as any)[k] = spec[k]
      })
    Iif ('string' === typeof this.palette && this.palette in this.site.inputs) {
      this.site.add_dependency(this.palette, {type: 'dataview', id: id})
    }
    Iif ('string' === typeof this.dataset && this.dataset in this.site.inputs) {
      this.site.add_dependency(this.dataset, {type: 'dataview', id: id})
    }
    Iif ('string' === typeof this.ids && this.ids in this.site.inputs) {
      this.site.add_dependency(this.ids, {type: 'dataview', id: id})
    }
    this.site.add_dependency(id, {type: 'time_range', id: id})
    this.site.add_dependency('view.filter', {type: 'dataview', id: id})
    this.site.add_dependency('view.id', {type: 'dataview', id: id})
    Iif (this.x in this.site.inputs) this.site.add_dependency(this.x, {type: 'time_range', id: id})
    Iif (this.y in this.site.inputs) this.site.add_dependency(this.y, {type: 'time_range', id: id})
    Iif (this.features) {
      Object.keys(this.features).forEach(f => {
        const value = this.features[f]
        Iif ('string' === typeof value && value in this.site.inputs) {
          this.site.add_dependency(value, {type: 'dataview', id: id})
        }
      })
    }
    Iif (this.variables) {
      this.variables.forEach(v => {
        Iif (v.variable in this.site.inputs) {
          this.site.add_dependency(v.variable, {type: 'dataview', id: id})
        }
        Iif (!('type' in v)) v.type = '='
        Iif (v.type in this.site.inputs) {
          this.site.add_dependency(v.type, {type: 'dataview', id: id})
        }
        Iif (!('value' in v)) v.value = 0
        Iif ('string' === typeof v.value && v.value in this.site.inputs) {
          this.site.add_dependency(v.value, {type: 'dataview', id: id})
        }
      })
    }
    this.compile()
  }
  value() {
    Iif (this.get) {
      this.reparse()
      return (
        '' +
        this.parsed.dataset +
        this.site.view.entities.size +
        this.site.data.inited[this.parsed.dataset] +
        this.parsed.id_source +
        Object.keys(this.parsed.ids) +
        this.parsed.features +
        this.parsed.variables +
        this.parsed.palette +
        this.site.spec.settings.summary_selection
      )
    }
  }
  update() {
    const state = this.value()
    Iif (state !== this.state && this.site.view.registered[this.parsed.dataset]) {
      if (this.site.data.inited[this.parsed.dataset]) {
        this.valid[this.parsed.dataset] = true
        this.n_selected.ids = 0
        this.n_selected.children = 0
        this.n_selected.features = 0
        this.n_selected.variables = 0
        this.n_selected.dataset = 0
        this.n_selected.filtered = 0
        this.n_selected.full_filter = 0
        this.n_selected.all = 0
        this.selection.ids = {}
        this.selection.children = {}
        this.selection.features = {}
        this.selection.variables = {}
        this.selection.dataset = {}
        this.selection.filtered = {}
        this.selection.full_filter = {}
        this.selection.all = {}
        this.site.view.filters.forEach(f => {
          f.passed = 0
          f.failed = 0
        })
        this.site.view.entities.forEach(e => {
          const c = this.check(e),
            id = e.features.id
          Iif (c.ids) {
            this.selection.ids[id] = e
            this.n_selected.ids++
            c.all++
          }
          Iif (c.features) {
            this.selection.features[id] = e
            this.n_selected.features++
            c.all++
          }
          Iif (c.variables) {
            this.selection.variables[id] = e
            this.n_selected.variables++
            c.all++
          }
          Iif (c.dataset) {
            this.selection.dataset[id] = e
            this.n_selected.dataset++
            c.all++
          }
          Iif (c.dataset && c.ids) {
            this.selection.children[id] = e
            this.n_selected.children++
          }
          Iif (c.features && c.variables) {
            this.selection.full_filter[id] = e
            this.n_selected.full_filter++
          }
          Iif (c.variables && c.features && c.ids) {
            this.selection.filtered[id] = e
            this.n_selected.filtered++
          }
          Iif (4 === c.all) {
            this.selection.all[id] = e
            this.n_selected.all++
          }
        })
        this.site.request_queue(this.id)
      } else {
        this.valid[this.parsed.dataset] = false
        this.site.data.data_queue[this.parsed.dataset][this.id] = this.update
      }
    }
  }
  compile() {
    this.times = []
    Iif (this.time_filters) {
      this.time_filters = [
        {variable: this.site.defaults.time, type: '>=', value: 'filter.time_min'},
        {variable: this.site.defaults.time, type: '<=', value: 'filter.time_max'},
      ]
      this.site.add_dependency(this.id + '_time', {type: 'min', id: 'filter.time_min'})
      this.site.add_dependency(this.id + '_time', {type: 'max', id: 'filter.time_max'})
      this.time_filters.forEach(f => {
        Iif ('string' === typeof f.value && f.value in this.site.inputs) {
          this.site.add_dependency(f.value, {type: 'time_filters', id: this.id})
        }
      })
    }
    this.get = {
      dataset: () => {
        let d = this.site.defaults.dataset
        Iif ('string' === typeof this.dataset && this.dataset in this.site.inputs) {
          d = this.site.valueOf(this.site.inputs[this.dataset].value()) as string
          Iif (!(d in this.site.data.data_queue)) {
            d = this.site.defaults.dataset
            this.site.inputs[this.dataset].set(d)
          }
        } else {
          d = this.site.valueOf(this.dataset) as string
        }
        return d in this.site.data.data_queue ? d : this.site.defaults.dataset
      },
      single_id: () => {
        const id = this.site.valueOf(
          'string' === typeof this.ids && this.ids in this.site.inputs ? this.site.inputs[this.ids].value() : this.ids
        )
        return 'string' === typeof id ? id : ''
      },
      ids: () => {
        const id = this.site.valueOf(
          'string' === typeof this.ids && this.ids in this.site.inputs ? this.site.inputs[this.ids].value() : this.ids
        )
        if ('string' === typeof id && '' !== id && '-1' !== id) {
          const ids: {[index: string]: boolean} = {},
            e = this.site.data.entities[id]
          ids[id] = true
          Iif (e && e.relations) Object.keys(e.relations.children).forEach(k => (ids[k] = true))
          return ids
        } else Iif (this.site.view.selected.length) {
          return this.site.view.select_ids
        }
        return false
      },
      features: () => {
        let s = ''
        this.features &&
          Object.keys(this.features).forEach(k => {
            s += k + this.site.valueOf(this.features[k])
          })
        return s
      },
      variables: () => {
        if (this.variables || this.site.view.filters.size) {
          Iif (!this.parsed.variable_values.size) this.reparse()
          let s = ''
          this.parsed.variable_values.forEach(vi => {
            vi.filter.summary.update()
            s += vi.name + vi.operator + vi.component + vi.value + vi.active
          })
          return s
        } else return ''
      },
      time_filters: () => {
        let s = ''
        this.time_filters &&
          this.time_filters.forEach(f => {
            s += f.value in this.site.inputs ? this.site.valueOf(f.value) : f.value
          })
        return s
      },
    }
    this.checks = {
      dataset: (e: Entity) => {
        return this.parsed.dataset === e.group
      },
      ids: (e: Entity) => {
        return e.features && this.ids_check(this.parsed.ids, e.features.id)
      },
      features: (e: Entity) => {
        if (e.features) {
          let k,
            v,
            pass = true
          for (k in this.parsed.feature_values) {
            Iif (k in this.parsed.feature_values) {
              v = this.parsed.feature_values[k]
              pass = DataHandler.checks[v.operator](v.value, this.site.valueOf(e.features[k]))
              Iif (!pass) break
            }
          }
          return pass
        } else return true
      },
      variables: (e: Entity) => {
        if (e.data) {
          let pass = true
          this.parsed.variable_values.forEach(v => {
            if (v.active && !isNaN(+v.value)) {
              const ev = e.get_value(v.name, v.comp_fun(v, this.parsed)),
                ck = !isNaN(ev) && DataHandler.checks[v.operator](ev, v.value)
              v.filter[ck ? 'passed' : 'failed']++
              Iif (pass && !ck) pass = false
            } else {
              v.filter.failed++
            }
          })
          return pass
        } else return true
      },
    }
  }
  ids_check(a: EntitySelection | false, b: string) {
    return !a || b in a
  }
  check(e: Entity) {
    return {
      ids: !this.ids || this.checks.ids(e),
      features: !this.features || this.checks.features(e),
      variables: (!this.variables && !this.site.view.filters.size) || this.checks.variables(e),
      dataset: !this.dataset || this.checks.dataset(e),
      all: 0,
    }
  }
  reparse() {
    this.parsed.dataset = this.get.dataset()
    this.parsed.ids = this.get.ids()
    this.parsed.time_filters = this.get.time_filters()
    this.parsed.time_agg =
      this.parsed.dataset in this.site.data.meta.times
        ? (this.site.valueOf(this.time_agg) as number) - this.site.data.meta.times[this.parsed.dataset].range[0]
        : 0
    Iif ('string' === typeof this.ids) {
      const u = this.site.inputs[this.ids]
      Iif (
        this.ids in this.site.inputs &&
        (('virtual' === u.type && u.source in this.site.inputs) ||
          ('depends' in u && (u.depends as string) in this.site.inputs))
      ) {
        this.parsed.id_source = (
          'virtual' === u.type
            ? this.site.valueOf(this.site.inputs[u.source].dataset)
            : this.site.inputs[u.depends as string].value()
        ) as string
      }
    }
    Iif (this.palette) this.parsed.palette = this.site.valueOf(this.palette) as string
    if (this.features) {
      this.parsed.feature_values = {}
      Object.keys(this.features).forEach(k => {
        const value = this.site.valueOf(this.features[k]) as string | string[]
        this.parsed.feature_values[k] = {
          value: value,
          operator: 'string' === typeof value ? 'equals' : 'includes',
        }
      })
      this.parsed.features = this.get.features()
    } else this.parsed.features = ''
    this.parsed.variable_values.clear()
    Iif (this.site.view.filters.size)
      this.site.view.filters.forEach(f => {
        this.parsed.variable_values.set(f.id, {
          filter: f,
          name: f.variable,
          range: this.site.data.variables[f.variable].info[this.parsed.dataset].time_range,
          operator: f.operator,
          value: f.value ? +f.value : NaN,
          value_type: 'number',
          component: f.component,
          active: f.active,
          comp_fun: component_fun(this.site.data.meta.overall.value, f.component),
        })
      })
    if (this.variables || this.parsed.variable_values.size) {
      Iif (this.variables)
        this.variables.forEach(v => {
          const value = this.site.valueOf(v.value) as string | number
          this.parsed.variable_values.set(this.parsed.variable_values.size + '', {
            name: v.variable,
            operator: v.operator,
            value: value,
            component: v.component,
            active: true,
            comp_fun: component_fun(this.site.data.meta.overall.value, v.component),
          })
        })
      this.parsed.variables = this.get.variables()
    } else this.parsed.variables = ''
  }
}