-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Observability] Split up observability-utils package #199801
Merged
dgieselaar
merged 13 commits into
elastic:main
from
dgieselaar:observability-utils-pkg-split
Nov 13, 2024
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e6bb640
[Observability] Split up observability-utils package
dgieselaar 421ea5b
Merge branch 'main' of github.com:elastic/kibana into observability-u…
dgieselaar 7992539
Fix references
dgieselaar 95ae9f8
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine 7a88cc5
Fix issues in APM plugin
dgieselaar 74f0678
Merge branch 'observability-utils-pkg-split' of github.com:dgieselaar…
dgieselaar dab77c1
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine e9189e0
Merge branch 'main' of github.com:elastic/kibana into observability-u…
dgieselaar 08aef83
Merge branch 'observability-utils-pkg-split' of github.com:dgieselaar…
dgieselaar 2bef512
Fix references
dgieselaar 5c10198
Option to unset value on error
dgieselaar 9ca2da2
Fix lingering import
dgieselaar 8e0eba8
Handle sync callbacks in withSpan
dgieselaar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: commented code, was it a mistake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you! it is, at least the fact that the comment is still there.