Skip to content

Commit b407be6

Browse files
authored
Improve and typescriptify update status (#20546)
* [tslint] use exitCode 1 when linter errors * fix typo * Improve getUpdateStatus * Fix accessing timeRange * Extract comparison functions * Switch parameter order * Revert "[tslint] use exitCode 1 when linter errors" This reverts commit 0ca23a4. * Revert "fix typo" This reverts commit e4e8091.
1 parent 875008a commit b407be6

File tree

4 files changed

+138
-135
lines changed

4 files changed

+138
-135
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
export function calculateObjectHash(obj: object): string;

src/ui/public/vis/update_status.js

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/ui/public/vis/update_status.test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const changeFunctions = {
2828
[Status.DATA]: ($scope) => $scope.visData = { foo: 'new' },
2929
[Status.PARAMS]: ($scope) => $scope.vis.params = { foo: 'new' },
3030
[Status.RESIZE]: ($scope) => $scope.vis.size = [50, 50],
31-
[Status.TIME]: ($scope) => $scope.vis.API.timeFilter.getBounds = () => [100, 100],
31+
[Status.TIME]: ($scope) => $scope.vis.filters.timeRange = { from: 'now-7d', to: 'now' },
3232
[Status.UI_STATE]: ($scope) => $scope.uiState = { foo: 'new' },
3333
};
3434

@@ -41,11 +41,7 @@ describe('getUpdateStatus', () => {
4141
size: [100, 100],
4242
params: {
4343
},
44-
API: {
45-
timeFilter: {
46-
getBounds: () => [50, 50]
47-
}
48-
}
44+
filters: {}
4945
},
5046
uiState: {},
5147
visData: {}

src/ui/public/vis/update_status.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)