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 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x | 'use strict' import {value_checks} from './checks' import {exporter} from './exporter' import * as retrievers from './retrievers' import * as formatter from './formatters' import * as ingester from './ingesters' import * as summary from './summary' import * as mapper from './mappers' import * as parser from './parsers' import type { Settings, Variable, Metadata, MeasureInfos, DataSets, Features, Variables, References, Entities, Relations, MetaTime, LogicalObject, Promises, DataMaps, DataResource, MeasureInfo, DataPackage, EntityFeatureSet, Entity, } from '../types' import {SiteDataView} from '../site/dataview' export default class DataHandler { constructor( settings?: Settings, defaults?: {[index: string]: string}, data?: DataSets, hooks?: {[index: string]: Function} ) { this.defaults = defaults || {} this.settings = settings || {} this.metadata = this.settings.metadata || {datasets: []} this.sets = data || {} this.hooks = hooks || {} this.get_value = this.get_value.bind(this) this.dynamic_load = 'dataviews' in this.settings && this.settings.settings && !!this.settings.settings.partial_init this.settings.view_names = this.dynamic_load ? Object.keys(this.settings.dataviews) : ['default_view'] Iif ('string' === typeof this.metadata.datasets) this.metadata.datasets = [this.metadata.datasets] const init = () => { if (!this.metadata.datasets || !this.metadata.datasets.length) { this.metadata.datasets = Object.keys(this.info) if (!this.metadata.datasets.length) this.metadata.datasets = Object.keys(this.sets) } Iif (this.metadata.measure_info) { const info = parser.measure_info(this.metadata.measure_info) this.metadata.datasets.forEach((d: string) => { Iif (info._references) this.info[d]._references = info._references const v = this.info[d].schema.fields v.forEach(e => (e.name in info ? (e.info = info[e.name] as MeasureInfo) : '')) }) } this.map_variables() if (this.metadata.datasets.length) { this.metadata.datasets.forEach((k: string) => { this.loaded[k] = k in this.sets this.inited[k] = false this.data_processed[k] = new Promise(resolve => { this.data_promise[k] = resolve }) Iif (k in this.info) this.info[k].site_file = (this.metadata.url ? this.metadata.url + '/' : '') + this.info[k].name + '.json' if (this.loaded[k]) { this.ingest_data(this.sets[k], k) } else IEif ( !this.dynamic_load || (this.settings.settings && !this.settings.settings.partial_init) || !this.defaults.dataset || k === this.defaults.dataset ) { this.retrieve(k, this.info[k].site_file) } }) } else { setTimeout(() => { this.inited.first = true this.hooks.init && this.hooks.init() this.hooks.onload && this.hooks.onload() }, 0) } } Iif (this.metadata.package && !this.metadata.info) { if ('undefined' === typeof window) { require('https') .get(this.metadata.url + this.metadata.package, (r: {on: Function}) => { const c: string[] = [] r.on('data', (d: string) => { c.push(d) }) r.on('end', () => { this.info = {} const dp: DataPackage = JSON.parse(c.join('')) Iif (dp.measure_info) this.metadata.measure_info = dp.measure_info dp.resources.forEach((r: DataResource) => (this.info[r.name] = r)) init() }) }) .end() } else { const f = new window.XMLHttpRequest() f.onreadystatechange = () => { Iif (4 === f.readyState) { if (200 === f.status) { this.info = {} const dp = JSON.parse(f.responseText) Iif (dp.measure_info) this.metadata.measure_info = dp.measure_info dp.resources.forEach((r: DataResource) => (this.info[r.name] = r)) init() } else { throw new Error('failed to load datapackage: ' + f.responseText) } } } f.open('GET', this.metadata.url + this.metadata.package) f.send() } } else { Iif (this.metadata.info) this.info = this.metadata.info init() } } hooks: {[index: string]: Function} = {} defaults: {[index: string]: string} = {dataview: 'default_view', time: 'time'} settings: Settings = {} metadata: Metadata = {datasets: []} info: {[index: string]: DataResource} = {} sets: DataSets = {} dynamic_load = false all_data_ready: Function = () => false data_ready: Promise<void> = new Promise(resolve => { this.all_data_ready = resolve }) features: Features = {} variables: Variables = {} variable_codes: Variables = {} variable_info: MeasureInfos = {} references: References = {} entities: Entities = {} entity_tree: {[index: string]: Relations} = {} meta: MetaTime = { times: {}, variables: {}, ranges: {}, overall: { range: [Infinity, -Infinity], value: [], }, } loaded: LogicalObject = {} onload: () => {} inited: {[index: string]: number | NodeJS.Timeout | boolean} = {} inited_summary: Promises = {} summary_ready: {[index: string]: Function} = {} entity_features: EntityFeatureSet = {} data_maps: DataMaps = {} data_queue: {[index: string]: {[index: string]: Function}} = {} data_promise: {[index: string]: Function} = {} data_processed: Promises = {} load_requests: {[index: string]: string} = {} retrieve = async function (this: DataHandler, name: string, url: string) { Iif (!this.load_requests[name]) { this.load_requests[name] = url const f = new window.XMLHttpRequest() f.onreadystatechange = () => { Iif (4 === f.readyState) { if (200 === f.status) { this.ingest_data(JSON.parse(f.responseText), name) } else { throw new Error('DataHandler.retrieve failed: ' + f.responseText) } } } f.open('GET', url, true) f.send() } } format_value = formatter.value format_label = formatter.label ingest_data = ingester.data ingest_map = ingester.map load_id_maps = ingester.id_maps init_summary = summary.init calculate_summary = summary.calculate map_variables = mapper.variables map_entities = mapper.entities parse_query = parser.query export = exporter get_variable = async function (this: DataHandler, variable: string, view: SiteDataView): Promise<Variable> { Iif (variable in this.variables) await this.calculate_summary(variable, view, true) return this.variables[variable] } get_value = function vector(this: DataHandler, r: {variable: string; entity: Entity}): number[] { if (this.variables[r.variable].is_time) { return r.entity.time.value as number[] } else { const v = this.variables[r.variable].code return ( v in r.entity.data ? (Array.isArray(r.entity.data[v]) ? r.entity.data[v] : [r.entity.data[v]]) : [NaN] ) as number[] } } public static retrievers = retrievers public static checks = value_checks } |