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

Fix last point of line charts. And small other fixes to line charts. #21235

Merged
merged 4 commits into from
Jul 20, 2024

Conversation

onnlucky
Copy link
Contributor

@onnlucky onnlucky commented Jun 30, 2024

Breaking change

Proposed change

This PR fixes the rendering of the last point in graphs. Otherwise, especially for items that don't often change state, the last state could be ignored for quite a while, using the average of the last series of entries instead.

Also, for detailed graphs, the initial values could be off, because the initial last value would be computed on bad data, resulting in NaN.

This PR also no longer adds a double entry at end if graph has only one point. Should not affect anything.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

graph: line
type: sensor
entity: number.growatt_max_active_output_percent
detail: 2
hours_to_show: 10
name: Active Output
limits:
  max: 100

That is a sensor I have that is often at 100%, only changes rarely, if when it does, often quickly goes back to 100%. Which was especially prone to showing bad values.

You can test this via developer tools -> set state then inject states into a fictional sensor (eg sensor.foobar). Do 100, 90, 100. Then add a sensor card with line graph on some dashboard. You'll see it dips down and never up. Even if you come back to it minutes or hours later. Also, if you toggle show more detail, the graph dips to zero, and only renders the last edge.

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

Note, I have tested the code only by copying the relevant function in a typescript playground and fixing it from there. Setting up a full home assistent development environment is a bit too much work for me at the moment for such a simple code fix.

If user exposed functionality or configuration variables are added/changed:

Summary by CodeRabbit

  • New Features
    • Enhanced graph functionality to better handle detailed data points, providing more accurate Y coordinate calculations.

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

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

Hi @onnlucky

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft June 30, 2024 15:13
Copy link
Contributor

coderabbitai bot commented Jun 30, 2024

Warning

Rate limit exceeded

@silamon has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 20 minutes and 33 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between ce69831 and cae90e7.

Walkthrough

Walkthrough

The calcPoints function in src/panels/lovelace/common/graph/coordinates.ts has been enhanced to support scenarios where the detail parameter exceeds 1. This update includes a conditional adjustment of the first value and the addition of a new getY function, which calculates the Y coordinate based on the input value, improving overall functionality and clarity.

Changes

File Summary
src/panels/lovelace/common/graph/coordinates.ts The calcPoints function now conditionally adjusts the first value for cases where detail > 1, and a new getY function is introduced for calculating the Y coordinate.

Sequence Diagrams

sequenceDiagram
    participant User
    participant LovelacePanel
    participant GraphCoordinates

    User ->> LovelacePanel: Request graph data
    LovelacePanel ->> GraphCoordinates: Call `calcPoints(detail > 1)`
    GraphCoordinates ->> GraphCoordinates: Adjust `first` value
    GraphCoordinates ->> GraphCoordinates: Call `getY(value)`
    GraphCoordinates -->> LovelacePanel: Return calculated points
    LovelacePanel -->> User: Display graph with new points
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range comments (9)
src/panels/lovelace/common/graph/coordinates.ts (9)

Line range hint 4-4: Avoid using any type.

Specify a different type for the items parameter to enable better type checking.

-const average = (items: any[]): number =>
+const average = (items: { state: string }[]): number =>
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 5-5: Use Number.parseFloat instead of the global parseFloat.

ES2015 moved some globals into the Number namespace for consistency.

-  items.reduce((sum, entry) => sum + parseFloat(entry.state), 0) / items.length;
+  items.reduce((sum, entry) => sum + Number.parseFloat(entry.state), 0) / items.length;
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 7-7: Avoid using any type.

Specify a different type for the items parameter to enable better type checking.

-const lastValue = (items: any[]): number =>
+const lastValue = (items: { state: string }[]): number =>
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 8-8: Use Number.parseFloat instead of the global parseFloat.

ES2015 moved some globals into the Number namespace for consistency.

-  parseFloat(items[items.length - 1].state) || 0;
+  Number.parseFloat(items[items.length - 1].state) || 0;
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 11-11: Avoid using any type.

Specify a different type for the history parameter to enable better type checking.

-const calcPoints = (
-  history: any,
+const calcPoints = (
+  history: { state: string }[],
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 60-60: Avoid using any type.

Specify a different type for the history parameter to enable better type checking.

-export const coordinates = (
-  history: any,
+export const coordinates = (
+  history: { state: number, last_changed: string }[],
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 69-69: Avoid reassigning function parameters.

The parameter history is reassigned. Use a local variable instead.

-  history = history.filter((item) => !Number.isNaN(item.state));
+  const filteredHistory = history.filter((item) => !Number.isNaN(item.state));
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 97-98: Avoid reassigning function parameters.

The parameter history is reassigned. Use a local variable instead.

-  history = history.reduce((res, item) => reduce(res, item, false), []);
+  const reducedHistory = history.reduce((res, item) => reduce(res, item, false), []);
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 100-101: Avoid reassigning function parameters.

The parameter history is reassigned. Use a local variable instead.

-  history = history.map((entry) =>
-    entry.reduce((res, item) => reduce(res, item, true), [])
+  const mappedHistory = history.map((entry) =>
+    entry.reduce((res, item) => reduce(res, item, true), [])
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)

@onnlucky onnlucky marked this pull request as ready for review June 30, 2024 15:18
@onnlucky onnlucky changed the title Fixes last point of graphs. Fix last point of line charts. And small other fixes to line charts. Jul 1, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range comments (3)
src/panels/lovelace/common/graph/coordinates.ts (3)

Line range hint 34-47: Avoid using any type for the item parameter in getCoords.

Specify a different type for the item parameter to enable better type checking.

-const getCoords = (item: any[], i: number, offset = 0, depth = 1) => {
+const getCoords = (item: { state: string }[], i: number, offset = 0, depth = 1) => {
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 60-60: Avoid using any type for the history parameter in coordinates.

Specify a different type for the history parameter to enable better type checking.

-export const coordinates = (
-  history: any,
+export const coordinates = (
+  history: { state: number; last_changed: number }[],
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)


Line range hint 69-69: Avoid reassigning function parameters.

Reassigning a function parameter is confusing. Use a local variable instead.

-history = history.filter((item) => !Number.isNaN(item.state));
+const filteredHistory = history.filter((item) => !Number.isNaN(item.state));
-history = history.reduce((res, item) => reduce(res, item, false), []);
+const reducedHistory = history.reduce((res, item) => reduce(res, item, false), []);
-history = history.map((entry) =>
+const mappedHistory = history.map((entry) =>

Also applies to: 98-100

Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

const getY = (value: number): number => {
return height + strokeWidth / 2 - (value - min) / yRatio;
};

const getCoords = (item: any[], i: number, offset = 0, depth = 1) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid using any type.

Specify a different type for the item parameter to enable better type checking.

-const getCoords = (item: any[], i: number, offset = 0, depth = 1) => {
+const getCoords = (item: { state: string }[], i: number, offset = 0, depth = 1) => {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const getCoords = (item: any[], i: number, offset = 0, depth = 1) => {
const getCoords = (item: { state: string }[], i: number, offset = 0, depth = 1) => {
Tools
Biome

[error] 35-35: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)

@@ -22,9 +22,16 @@ const calcPoints = (
let xRatio = width / (hours - (detail === 1 ? 1 : 0));
xRatio = isFinite(xRatio) ? xRatio : width;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Number.isFinite instead of the global isFinite.

ES2015 moved some globals into the Number namespace for consistency.

-  xRatio = isFinite(xRatio) ? xRatio : width;
+  xRatio = Number.isFinite(xRatio) ? xRatio : width;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
xRatio = isFinite(xRatio) ? xRatio : width;
xRatio = Number.isFinite(xRatio) ? xRatio : width;
Tools
Biome

[error] 23-23: isFinite is unsafe. It attempts a type coercion. Use Number.isFinite instead.

See the MDN documentation for more details.
Unsafe fix: Use Number.isFinite instead.

(lint/suspicious/noGlobalIsFinite)

Especially for items that don't often change state, the last state could
be essentially be ignored for quite a while, using the average of the
last series of entries instead.

Also for detailed graphs, the initial values could be off, because the
initial last value would be computed on bad data, resulting in NaN.

And no longer adds a double entry at end if graph has only one point.
bramkragten
bramkragten previously approved these changes Jul 19, 2024
@bramkragten bramkragten enabled auto-merge (squash) July 19, 2024 09:51
auto-merge was automatically disabled July 19, 2024 21:17

Head branch was pushed to by a user without write access

Copy link
Contributor

@silamon silamon left a comment

Choose a reason for hiding this comment

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

Previously approved

@silamon silamon enabled auto-merge (squash) July 20, 2024 08:11
@silamon silamon merged commit 1c978b7 into home-assistant:dev Jul 20, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants