Skip to content
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

new-log-viewer: Formalize config parameters and add management utilities. #51

Merged
merged 19 commits into from
Aug 16, 2024

Conversation

Henry8192
Copy link
Collaborator

@Henry8192 Henry8192 commented Aug 13, 2024

References

This PR follows #48.

Description

The new utils/config.ts provides 3 functions:

  • getConfig - takes in a CONFIG_NAME (defined in typings/config.ts) and returns the corresponding result defined in ConfigMap. If the code is invalid, return null.
  • testConfig - validate a ConfigUpdate object (essentially a {code, value} pair). It is provided to users to test their configs, and is also used by setConfig to endorse inputs.
  • setConfig - takes in a ConfigUpdate object, validate the input using testConfig, and store to localStorage.

Validation performed

@junhaoliao junhaoliao self-requested a review August 14, 2024 05:56
Copy link
Collaborator

@junhaoliao junhaoliao left a comment

Choose a reason for hiding this comment

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

@Henry8192 Please help addressing the posted comments. I feel some design details could have been more clear to us if we added the function demos / tests.

Comment on lines 208 to 213
decoderOptions: {
// TODO: these shall come from config provider
formatString: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%process.thread.name] %log.level" +
" %message%n",
logLevelKey: "log.level",
timestampKey: "@timestamp",
formatString: DECODER_OPTIONS_DEFAULT.formatString,
logLevelKey: DECODER_OPTIONS_DEFAULT.logLevelKey,
timestampKey: DECODER_OPTIONS_DEFAULT.timestampKey,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should now be getConfig(CONFIG_NAME.DECODER_OPTIONS), right?

new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
case CONFIG_NAME.THEME:
return window.localStorage.getItem(LOCAL_STORAGE_KEY.THEME) as ConfigMap[T];
case CONFIG_NAME.PAGE_SIZE:
return window.localStorage.getItem(LOCAL_STORAGE_KEY.PAGE_SIZE) as ConfigMap[T];
Copy link
Collaborator

Choose a reason for hiding this comment

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

To avoid returning value in undesired types, we previously discussed one possible solution to have TypeScript check the return types:

  1. We can create an object of type Partial<> of ConfigMap.
  2. Then in the switch-cases, into the object we can assign values on different keys code. e.g.
    // TypeScript will show an error if we don't cast (parse) the localStorage value into a number
    result[CONFIG_NAME.PAGE_SIZE] = Number(window.localStorage.getItem(LOCAL_STORAGE_KEY.PAGE_SIZE));
    
  3. return result[code] as ConfigMap[T]; since TypeScript assumes the values can be undefined in the Partial<> type, although we should have assigned a value in the switch-cases.

I feel Step 1 & 3 may be optimized if we gain better knowledge of TypeScript. We can discuss more after you make the changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Due to TypeScript's inability to validation the return type within the function body, the proposed approach is essentially a hack and a bit unclean. This is not implemented for now to avoid confusions.

new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/typings/config.ts Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
@junhaoliao junhaoliao changed the title WIP: New Log Viewer: Add Config Utility to Store Settings in localStorage new-log-viewer: Add config utility functions. Aug 15, 2024
Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

Minor suggestions.

new-log-viewer/src/components/Layout.tsx Show resolved Hide resolved
new-log-viewer/src/typings/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
new-log-viewer/src/utils/config.ts Outdated Show resolved Hide resolved
junhaoliao and others added 4 commits August 16, 2024 00:15
Co-authored-by: kirkrodrigues <2454684+kirkrodrigues@users.noreply.github.com>
…de review

Co-authored-by: kirkrodrigues <2454684+kirkrodrigues@users.noreply.github.com>
Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

For the PR title, how about:

new-log-viewer: Formalize config parameters and add management utilities.

@junhaoliao junhaoliao changed the title new-log-viewer: Add config utility functions. new-log-viewer: Formalize config parameters and add management utilities. Aug 16, 2024
@junhaoliao junhaoliao merged commit 8f33b22 into y-scope:main Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants