|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { calculateObjectHash } from './lib/calculate_object_hash'; |
| 21 | +import { Vis } from './vis'; |
| 22 | + |
| 23 | +enum Status { |
| 24 | + AGGS = 'aggs', |
| 25 | + DATA = 'data', |
| 26 | + PARAMS = 'params', |
| 27 | + RESIZE = 'resize', |
| 28 | + TIME = 'time', |
| 29 | + UI_STATE = 'uiState', |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Checks whether the hash of a specific key in the given oldStatus has changed |
| 34 | + * compared to the new valueHash passed. |
| 35 | + */ |
| 36 | +function hasHashChanged<T extends string>( |
| 37 | + valueHash: string, |
| 38 | + oldStatus: { [key in T]?: string }, |
| 39 | + name: T |
| 40 | +): boolean { |
| 41 | + const oldHash = oldStatus[name]; |
| 42 | + return oldHash !== valueHash; |
| 43 | +} |
| 44 | + |
| 45 | +interface Size { |
| 46 | + width: number; |
| 47 | + height: number; |
| 48 | +} |
| 49 | + |
| 50 | +function hasSizeChanged(size: Size, oldSize?: Size): boolean { |
| 51 | + if (!oldSize) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + return oldSize.width !== size.width || oldSize.height !== size.height; |
| 55 | +} |
| 56 | + |
| 57 | +function getUpdateStatus<T extends Status>( |
| 58 | + requiresUpdateStatus: T[] = [], |
| 59 | + obj: any, |
| 60 | + param: { vis: Vis; visData: any; uiState: any } |
| 61 | +): { [reqStats in T]: boolean } { |
| 62 | + const status = {} as { [reqStats in T]: boolean }; |
| 63 | + |
| 64 | + // If the vis type doesn't need update status, skip all calculations |
| 65 | + if (requiresUpdateStatus.length === 0) { |
| 66 | + return status; |
| 67 | + } |
| 68 | + |
| 69 | + if (!obj._oldStatus) { |
| 70 | + obj._oldStatus = {}; |
| 71 | + } |
| 72 | + |
| 73 | + for (const requiredStatus of requiresUpdateStatus) { |
| 74 | + let hash; |
| 75 | + // Calculate all required status updates for this visualization |
| 76 | + switch (requiredStatus) { |
| 77 | + case Status.AGGS: |
| 78 | + hash = calculateObjectHash(param.vis.aggs); |
| 79 | + status.aggs = hasHashChanged(hash, obj._oldStatus, 'aggs'); |
| 80 | + obj._oldStatus.aggs = hash; |
| 81 | + break; |
| 82 | + case Status.DATA: |
| 83 | + hash = calculateObjectHash(param.visData); |
| 84 | + status.data = hasHashChanged(hash, obj._oldStatus, 'data'); |
| 85 | + obj._oldStatus.data = hash; |
| 86 | + break; |
| 87 | + case Status.PARAMS: |
| 88 | + hash = calculateObjectHash(param.vis.params); |
| 89 | + status.params = hasHashChanged(hash, obj._oldStatus, 'param'); |
| 90 | + obj._oldStatus.param = hash; |
| 91 | + break; |
| 92 | + case Status.RESIZE: |
| 93 | + const width: number = param.vis.size ? param.vis.size[0] : 0; |
| 94 | + const height: number = param.vis.size ? param.vis.size[1] : 0; |
| 95 | + const size = { width, height }; |
| 96 | + status.resize = hasSizeChanged(size, obj._oldStatus.resize); |
| 97 | + obj._oldStatus.resize = size; |
| 98 | + break; |
| 99 | + case Status.TIME: |
| 100 | + const timeRange = param.vis.filters && param.vis.filters.timeRange; |
| 101 | + hash = calculateObjectHash(timeRange); |
| 102 | + status.time = hasHashChanged(hash, obj._oldStatus, 'time'); |
| 103 | + obj._oldStatus.time = hash; |
| 104 | + break; |
| 105 | + case Status.UI_STATE: |
| 106 | + hash = calculateObjectHash(param.uiState); |
| 107 | + status.uiState = hasHashChanged(hash, obj._oldStatus, 'uiState'); |
| 108 | + obj._oldStatus.uiState = hash; |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return status; |
| 114 | +} |
| 115 | + |
| 116 | +export { getUpdateStatus, Status }; |
0 commit comments