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

upcoming: [DI-22184] - Added Metric Criteria, Metric components #11392

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

santoshp210-akamai
Copy link
Contributor

Description 📝

  • Added the MetricCriteria and Metric components
  • Added new Unit Tests for the added components and added more tests and checks for existing components to reflect the latest changes
  • Changed the types and validations to reflect the latest API spec

Changes 🔄

  • Added the Metric Criteria component for the Criteria Section using FieldArray to add the Metric components dynamically
  • Added Autocomplete and TextField inputs for Data Field, Aggregation Type, Operator and Threshold in Metric
  • Added the relevant test cases for Unit Tests
  • Added and changed some types of CreateAlertDefinitionPayload, Alert interfaces

Target release date 🗓️

Please specify a release date (and environment, if applicable) to guarantee timely review of this PR. If exact date is not known, please approximate and update it as needed.

Preview 📷

Before After
image image

How to test 🧪

Prerequisites

(How to setup test environment)

  • The tabs are controlled by a featureFlag called aclpAlerting, the flag has been currently disabled. For testing enable the definitions part of the aclpAlerting flag to be true.
  • The API endpoints are not live yet, so use Legacy MSW Handlers for the mock responses

Verification steps

  • In Monitor (once the prerequisites are followed) , Alerts tab should be visible next to Dashboards.
  • Under that, Definition tab and a Create button should be visible.
  • On clicking Create, the Form Page should be visible
  • To choose from the Data Field drop down, selecting a Service is required
  • To choose from the Aggregation Field drop down, selecting a Data Field is required.
  • The Metric component can be added and removed dynamically with the Add Metric and Delete Icon buttons
  • The Delete Icon button is only visible when there is more than one Metric component
Author Checklists

As an Author, to speed up the review process, I considered 🤔

👀 Doing a self review
❔ Our contribution guidelines
🤏 Splitting feature into small PRs
➕ Adding a changeset
🧪 Providing/improving test coverage
🔐 Removing all sensitive information from the code and PR description
🚩 Using a feature flag to protect the release
👣 Providing comprehensive reproduction steps
📑 Providing or updating our documentation
🕛 Scheduling a pair reviewing session
📱 Providing mobile support
♿ Providing accessibility support


  • I have read and considered all applicable items listed above.

As an Author, before moving this PR from Draft to Open, I confirmed ✅

  • All unit tests are passing
  • TypeScript compilation succeeded without errors
  • Code passes all linting rules

@santoshp210-akamai santoshp210-akamai requested a review from a team as a code owner December 10, 2024 17:57
@santoshp210-akamai santoshp210-akamai requested review from hana-akamai and hkhalil-akamai and removed request for a team December 10, 2024 17:57
@santoshp210-akamai
Copy link
Contributor Author

There are currently some discussions with respect to limiting the number of Metric Components and the way to limit them in UI.
As discussed with @jaalah-akamai , the Unit being part of the adornment of TextField and the Threshold input being float are also under discussion. These changes are minimal and will be resolved soon. It will be either updated in this PR or will be followed by a minimal PR.

@santoshp210-akamai
Copy link
Contributor Author

The typecheck manager test is failing as there is a state variable that's not used as it will be used in components that will be added later, but we are setting the state in this component

@santoshp210-akamai santoshp210-akamai self-assigned this Dec 10, 2024
Copy link

github-actions bot commented Dec 10, 2024

Coverage Report:
Base Coverage: 86.89%
Current Coverage: 86.89%

@linode-gh-bot
Copy link
Collaborator

Cloud Manager UI test results

🎉 467 passing tests on test run #2 ↗︎

❌ Failing✅ Passing↪️ Skipped🕐 Duration
0 Failing467 Passing2 Skipped106m 36s

Comment on lines +41 to +42
metric: rule.metric!,
operator: rule.operator!,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to avoid this non-null assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The metric value is null in the first place just to handle clear option in the Autocomplete, and we have validation to make sure it's a required field and the submit won't happen successfully unless and until the field has the proper value . So the non-null assertion is almost never going to be checked.

We actually did the same thing for Severity.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's important to note this isn't a null check, it's just a null assertion which will assume the value is non-null even if it isn't. I prefer to avoid it if at all possible unless the team believes it's harmless. For example, we can use a default instead:

Suggested change
metric: rule.metric!,
operator: rule.operator!,
metric: rule.metric ?? '',
operator: rule.operator ?? 'eq',

Copy link
Member

Choose a reason for hiding this comment

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

I guess to fix this we'd have to update the form types to not allow things like metric and operator to be nullable. This might require us to make those fields not clearable to satisfy the types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fields not being clearable should be fine, but for components to be controlled we have initialized with null. I think the solution suggested by @hkhalil-akamai might be more suited for our use-case. I will try that change and if it's not affecting anything I'll make that change and push the changes.


const { control, watch } = useFormContext<CreateAlertDefinitionForm>();

const metricCriteriaWatcher = watch(name);
Copy link
Member

Choose a reason for hiding this comment

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

Please see #11357 (comment). watch should be avoided if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, going ahead with useWatch()

Comment on lines +45 to +59
React.useEffect(() => {
const formMetricValues = new Set(
metricCriteriaWatcher.map((item: MetricCriteriaForm) => item.metric)
);

const intervalList =
metricDefinitions &&
metricDefinitions.data
.filter((item) => formMetricValues.has(item.metric))
.map((item) => item.scrape_interval);
const maxInterval = Math.max(
...convertToSeconds(intervalList ? intervalList : [])
);
setMaxInterval(maxInterval);
}, [setMaxInterval, metricCriteriaWatcher, metricDefinitions]);
Copy link
Member

Choose a reason for hiding this comment

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

Let's try to remove this useEffect. We're really trying to not use useEffect in our codebase anymore 😖. Usually this can be accomplished by moving this logic into an onChange rather than doing it here as a side-effect of something changing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any particular reason why we are avoiding useEffect ? Because while implementing I saw multiple components using it, so implemented

Copy link
Member

Choose a reason for hiding this comment

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

We've had a handful of bugs caused by useEffect. The official react docs also advocate to not use useEffect: https://react.dev/learn/you-might-not-need-an-effect

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.

4 participants