forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Observability] Split up observability-utils package (elastic#199801)
Split up observability-utils package in browser, common, server. Also made a small change to `withSpan` to automatically log operation times when the debug level for the logger is enabled. --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
5386864
commit ab67ac3
Showing
88 changed files
with
1,224 additions
and
98 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ges/observability/observability_utils/observability_utils_browser/hooks/use_date_range.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { TimeRange } from '@kbn/data-plugin/common'; | ||
import { DataPublicPluginStart } from '@kbn/data-plugin/public'; | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
|
||
export function useDateRange({ data }: { data: DataPublicPluginStart }): { | ||
timeRange: TimeRange; | ||
absoluteTimeRange: { | ||
start: number; | ||
end: number; | ||
}; | ||
setTimeRange: React.Dispatch<React.SetStateAction<TimeRange>>; | ||
} { | ||
const timefilter = data.query.timefilter.timefilter; | ||
|
||
const [timeRange, setTimeRange] = useState(() => timefilter.getTime()); | ||
|
||
const [absoluteTimeRange, setAbsoluteTimeRange] = useState(() => timefilter.getAbsoluteTime()); | ||
|
||
useEffect(() => { | ||
const timeUpdateSubscription = timefilter.getTimeUpdate$().subscribe({ | ||
next: () => { | ||
setTimeRange(() => timefilter.getTime()); | ||
setAbsoluteTimeRange(() => timefilter.getAbsoluteTime()); | ||
}, | ||
}); | ||
|
||
return () => { | ||
timeUpdateSubscription.unsubscribe(); | ||
}; | ||
}, [timefilter]); | ||
|
||
const setTimeRangeMemoized: React.Dispatch<React.SetStateAction<TimeRange>> = useCallback( | ||
(nextOrCallback) => { | ||
const val = | ||
typeof nextOrCallback === 'function' | ||
? nextOrCallback(timefilter.getTime()) | ||
: nextOrCallback; | ||
|
||
timefilter.setTime(val); | ||
}, | ||
[timefilter] | ||
); | ||
|
||
const asEpoch = useMemo(() => { | ||
return { | ||
start: new Date(absoluteTimeRange.from).getTime(), | ||
end: new Date(absoluteTimeRange.to).getTime(), | ||
}; | ||
}, [absoluteTimeRange]); | ||
|
||
return { | ||
timeRange, | ||
absoluteTimeRange: asEpoch, | ||
setTimeRange: setTimeRangeMemoized, | ||
}; | ||
} |
60 changes: 60 additions & 0 deletions
60
.../observability/observability_utils/observability_utils_browser/hooks/use_local_storage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useState, useEffect, useMemo, useCallback } from 'react'; | ||
|
||
export function useLocalStorage<T>(key: string, defaultValue: T) { | ||
// This is necessary to fix a race condition issue. | ||
// It guarantees that the latest value will be always returned after the value is updated | ||
const [storageUpdate, setStorageUpdate] = useState(0); | ||
|
||
const item = useMemo(() => { | ||
return getFromStorage(key, defaultValue); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [key, storageUpdate, defaultValue]); | ||
|
||
const saveToStorage = useCallback( | ||
(value: T) => { | ||
if (value === undefined) { | ||
window.localStorage.removeItem(key); | ||
} else { | ||
window.localStorage.setItem(key, JSON.stringify(value)); | ||
setStorageUpdate(storageUpdate + 1); | ||
} | ||
}, | ||
[key, storageUpdate] | ||
); | ||
|
||
useEffect(() => { | ||
function onUpdate(event: StorageEvent) { | ||
if (event.key === key) { | ||
setStorageUpdate(storageUpdate + 1); | ||
} | ||
} | ||
window.addEventListener('storage', onUpdate); | ||
return () => { | ||
window.removeEventListener('storage', onUpdate); | ||
}; | ||
}, [key, setStorageUpdate, storageUpdate]); | ||
|
||
return useMemo(() => [item, saveToStorage] as const, [item, saveToStorage]); | ||
} | ||
|
||
function getFromStorage<T>(keyName: string, defaultValue: T) { | ||
const storedItem = window.localStorage.getItem(keyName); | ||
|
||
if (storedItem !== null) { | ||
try { | ||
return JSON.parse(storedItem) as T; | ||
} catch (err) { | ||
window.localStorage.removeItem(keyName); | ||
// eslint-disable-next-line no-console | ||
console.log(`Unable to decode: ${keyName}`); | ||
} | ||
} | ||
return defaultValue; | ||
} |
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
x-pack/packages/observability/observability_utils/observability_utils_browser/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../../../../..', | ||
roots: [ | ||
'<rootDir>/x-pack/packages/observability/observability_utils/observability_utils_browser', | ||
], | ||
}; |
5 changes: 5 additions & 0 deletions
5
x-pack/packages/observability/observability_utils/observability_utils_browser/kibana.jsonc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"type": "shared-browser", | ||
"id": "@kbn/observability-utils-browser", | ||
"owner": "@elastic/observability-ui" | ||
} |
6 changes: 6 additions & 0 deletions
6
x-pack/packages/observability/observability_utils/observability_utils_browser/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@kbn/observability-utils-browser", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0" | ||
} |
23 changes: 23 additions & 0 deletions
23
x-pack/packages/observability/observability_utils/observability_utils_browser/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"extends": "../../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
"react" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/data-plugin", | ||
"@kbn/core-ui-settings-browser", | ||
"@kbn/std", | ||
] | ||
} |
Oops, something went wrong.