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(data-contracts-web) handle other schedule types #10919

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,29 @@ type Props = {
};

export const FreshnessScheduleSummary = ({ definition, evaluationSchedule }: Props) => {
const scheduleText =
definition.type === FreshnessAssertionScheduleType.Cron
? `${capitalizeFirstLetter(cronstrue.toString(definition.cron?.cron as string))}.`
: `In the past ${
definition.fixedInterval?.multiple
} ${definition.fixedInterval?.unit.toLocaleLowerCase()}s${
(evaluationSchedule &&
`, as of ${cronstrue.toString(evaluationSchedule.cron as string).toLowerCase()}`) ||
''
}`;
let scheduleText = '';
const cronStr = definition.cron?.cron ?? evaluationSchedule?.cron;
switch (definition.type) {
case FreshnessAssertionScheduleType.Cron:
scheduleText = cronStr
? `${capitalizeFirstLetter(cronstrue.toString(cronStr))}.`
: `Unknown freshness schedule.`;
break;
case FreshnessAssertionScheduleType.SinceTheLastCheck:
scheduleText = cronStr
? `Since the previous check, as of ${cronstrue.toString(cronStr).toLowerCase()}`
: 'Since the previous check';
break;
case FreshnessAssertionScheduleType.FixedInterval:
scheduleText = `In the past ${
definition.fixedInterval?.multiple
} ${definition.fixedInterval?.unit.toLocaleLowerCase()}s${
cronStr ? `, as of ${cronstrue.toString(cronStr).toLowerCase()}` : ''
}`;
break;
default:
break;
}
Comment on lines +16 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

Review of switch statement handling different schedule types

  1. Correctness and Logic:

    • The switch statement correctly handles different types of schedules based on definition.type.
    • The use of cronStr to generate human-readable strings with cronstrue is appropriate and enhances user understanding.
  2. Maintainability:

    • The switch statement is clear and easy to extend with additional schedule types if necessary.
    • However, the repeated use of similar logic for generating scheduleText could be refactored to reduce redundancy.
  3. Performance:

    • The performance impact is minimal, but repeated calls to cronstrue.toString(cronStr) could be optimized by storing the result in a variable if used multiple times.
  4. Best Practices:

    • Consider extracting the text generation into a separate function or using a mapping object to clean up the switch statement and improve code reusability.

Suggested Refactor:

  • Extract repeated logic into a function to avoid redundancy and improve readability.
+ function generateScheduleText(cronStr, prefix, suffix = '') {
+     return cronStr ? `${prefix}${cronstrue.toString(cronStr).toLowerCase()}${suffix}` : prefix;
+ }

  switch (definition.type) {
      case FreshnessAssertionScheduleType.Cron:
-         scheduleText = cronStr ? `${capitalizeFirstLetter(cronstrue.toString(cronStr))}.` : `Unknown freshness schedule.`;
+         scheduleText = generateScheduleText(cronStr, `${capitalizeFirstLetter(cronstrue.toString(cronStr))}.`, 'Unknown freshness schedule.');
          break;
      case FreshnessAssertionScheduleType.SinceTheLastCheck:
-         scheduleText = cronStr ? `Since the previous check, as of ${cronstrue.toString(cronStr).toLowerCase()}` : 'Since the previous check';
+         scheduleText = generateScheduleText(cronStr, 'Since the previous check, as of ', 'Since the previous check');
          break;
      case FreshnessAssertionScheduleType.FixedInterval:
-         scheduleText = `In the past ${definition.fixedInterval?.multiple} ${definition.fixedInterval?.unit.toLocaleLowerCase()}s${cronStr ? `, as of ${cronstrue.toString(cronStr).toLowerCase()}` : ''}`;
+         scheduleText = generateScheduleText(cronStr, `In the past ${definition.fixedInterval?.multiple} ${definition.fixedInterval?.unit.toLocaleLowerCase()}s, as of `, '');
          break;
  }

Committable suggestion was skipped due to low confidence.


return <>{scheduleText}</>;
};
Loading