Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dev/tslint/lint_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function groupFilesByProject(files: File[]) {
}

/**
* Lints a list of files with eslint. eslint reports are written to the log
* Lints a list of files with tslint. tslint reports are written to the log
* and a FailError is thrown when linting errors occur.
*
* @param {ToolingLog} log
Expand Down
4 changes: 3 additions & 1 deletion src/dev/tslint/run_tslint_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ export function runTslintCli() {
);

list.run().catch((error: any) => {
process.exitCode = 1;

if (!error.errors) {
log.error('Unhandled execption!');
log.error(error);
process.exit(1);
process.exit();
}

for (const e of error.errors) {
Expand Down
20 changes: 20 additions & 0 deletions src/ui/public/vis/lib/calculate_object_hash.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export function calculateObjectHash(obj: object): string;
129 changes: 0 additions & 129 deletions src/ui/public/vis/update_status.js

This file was deleted.

8 changes: 2 additions & 6 deletions src/ui/public/vis/update_status.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const changeFunctions = {
[Status.DATA]: ($scope) => $scope.visData = { foo: 'new' },
[Status.PARAMS]: ($scope) => $scope.vis.params = { foo: 'new' },
[Status.RESIZE]: ($scope) => $scope.vis.size = [50, 50],
[Status.TIME]: ($scope) => $scope.vis.API.timeFilter.getBounds = () => [100, 100],
[Status.TIME]: ($scope) => $scope.vis.filters.timeRange = { from: 'now-7d', to: 'now' },
[Status.UI_STATE]: ($scope) => $scope.uiState = { foo: 'new' },
};

Expand All @@ -41,11 +41,7 @@ describe('getUpdateStatus', () => {
size: [100, 100],
params: {
},
API: {
timeFilter: {
getBounds: () => [50, 50]
}
}
filters: {}
},
uiState: {},
visData: {}
Expand Down
116 changes: 116 additions & 0 deletions src/ui/public/vis/update_status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { calculateObjectHash } from './lib/calculate_object_hash';
import { Vis } from './vis';

enum Status {
AGGS = 'aggs',
DATA = 'data',
PARAMS = 'params',
RESIZE = 'resize',
TIME = 'time',
UI_STATE = 'uiState',
}

/**
* Checks whether the hash of a specific key in the given oldStatus has changed
* compared to the new valueHash passed.
*/
function hasHashChanged<T extends string>(
valueHash: string,
oldStatus: { [key in T]?: string },
name: T
): boolean {
const oldHash = oldStatus[name];
return oldHash !== valueHash;
}

interface Size {
width: number;
height: number;
}

function hasSizeChanged(size: Size, oldSize?: Size): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you invert the params here so we are a bit more aligned with the previous: hasHashChanged function?
first the oldStatus and the new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I can't because oldSize is optional so it must be in the end. If you want I can change the parameter order to the hasHashChanged function if you want to hash, oldStatus, key if you think that increases readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo you are right, sorry. Yes please so we have in the switch part a cleaner readibility,
thanks ❤️

if (!oldSize) {
return true;
}
return oldSize.width !== size.width || oldSize.height !== size.height;
}

function getUpdateStatus<T extends Status>(
requiresUpdateStatus: T[] = [],
obj: any,
param: { vis: Vis; visData: any; uiState: any }
): { [reqStats in T]: boolean } {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (maybe a bit weird looking syntax) will actually tell TypeScript, that the return value contains exactly those keys, that has been specified in the array passed to the first parameter (since that's where the T generic type is used).

That way TypeScript will actually fail on: getUpdateStatus([Status.TIME], {}, {}).data, since it knows the return type is of type { time: boolean; }.

const status = {} as { [reqStats in T]: boolean };

// If the vis type doesn't need update status, skip all calculations
if (requiresUpdateStatus.length === 0) {
return status;
}

if (!obj._oldStatus) {
obj._oldStatus = {};
}

for (const requiredStatus of requiresUpdateStatus) {
let hash;
// Calculate all required status updates for this visualization
switch (requiredStatus) {
case Status.AGGS:
hash = calculateObjectHash(param.vis.aggs);
status.aggs = hasHashChanged(hash, obj._oldStatus, 'aggs');
obj._oldStatus.aggs = hash;
break;
case Status.DATA:
hash = calculateObjectHash(param.visData);
status.data = hasHashChanged(hash, obj._oldStatus, 'data');
obj._oldStatus.data = hash;
break;
case Status.PARAMS:
hash = calculateObjectHash(param.vis.params);
status.params = hasHashChanged(hash, obj._oldStatus, 'param');
obj._oldStatus.param = hash;
break;
case Status.RESIZE:
const width: number = param.vis.size ? param.vis.size[0] : 0;
const height: number = param.vis.size ? param.vis.size[1] : 0;
const size = { width, height };
status.resize = hasSizeChanged(size, obj._oldStatus.resize);
obj._oldStatus.resize = size;
break;
case Status.TIME:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will get removed with #20377

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I think we should remove the TIME parameter itself in a separate PR< since we need to change developer docs with that one.

const timeRange = param.vis.filters && param.vis.filters.timeRange;
hash = calculateObjectHash(timeRange);
status.time = hasHashChanged(hash, obj._oldStatus, 'time');
obj._oldStatus.time = hash;
break;
case Status.UI_STATE:
hash = calculateObjectHash(param.uiState);
status.uiState = hasHashChanged(hash, obj._oldStatus, 'uiState');
obj._oldStatus.uiState = hash;
break;
}
}

return status;
}

export { getUpdateStatus, Status };