-
Notifications
You must be signed in to change notification settings - Fork 4
Document vm_today and datetime support in calculated fields #1167
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
35ddc67
Document vm_today and datetime support in calculated fields
nrichers 23bf2f9
Fix whitespace
nrichers a292106
Fix heading level and move inventory field types up one section
nrichers 997d711
Add screenshots, edits after testing
nrichers c7ca75e
Save point before moving content
nrichers d6643a2
Move into its own file and relocate
nrichers dfcd86d
Better intro
nrichers 10422ee
Restore better vm_today example
nrichers b9e4e09
Edits
nrichers 5833c3a
Minor text reschuffle
nrichers f54c007
Mention fields by name
nrichers dd27787
Move field types reference content below prequisites and bump to h2
nrichers 06b2bd0
Merge branch 'main' of github.com:validmind/documentation into sc-746…
nrichers e036be2
Fix example casing
nrichers 7f428d3
Consistency edit
nrichers 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
This file contains hidden or 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
126 changes: 126 additions & 0 deletions
126
site/guide/model-inventory/_example-next-review-date-and-days-remaining.qmd
This file contains hidden or 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,126 @@ | ||
| <!-- Copyright © 2023-2026 ValidMind Inc. All rights reserved. | ||
| Refer to the LICENSE file in the root of this repository for details. | ||
| SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial --> | ||
|
|
||
| :::: {.content-visible unless-format="revealjs"} | ||
|
|
||
| ::: {.callout-button .pl4 .nt2} | ||
| ::: {.callout collapse="true" appearance="minimal"} | ||
|
|
||
| ### Example — Next review date & days remaining | ||
|
|
||
| Combining different inventory field types provides a flexible way to automatically track model review schedules. This example creates fields that calculate the next review date based on approval date and validation frequency, adjusted by risk tier, and then computes the days remaining until that review. | ||
|
|
||
| Common date and time field types available in your formulas include: | ||
|
|
||
| - `vm_today` — Today's date (updates each time the formula runs) | ||
| - `date` — Python's `datetime.date` class | ||
| - `datetime` — Python's `datetime.datetime` class | ||
| - `timedelta` — Duration in days, seconds, or microseconds | ||
| - `relativedelta` — Duration in months, years, etc. (from `dateutil`) | ||
|
|
||
| Here, we show you how to use `date`, `relativedelta`, and `vm_today`. | ||
|
|
||
| #### Calculate the next review date | ||
|
|
||
| Determine the next review date based on an approval date and a frequency of validation that adjusts based on the risk tier: | ||
|
|
||
| 1. Create an `Approved Date` date field. | ||
|
|
||
| 2. Create a `Frequency of Validation` string field with the following options: | ||
|
|
||
| - Weekly | ||
| - Monthly | ||
| - Yearly | ||
|
|
||
| 3. Create a `Risk Tier` calculation field that depends on the frequency of validation: | ||
|
|
||
| ```python | ||
| def formula(params): | ||
| if params.frequencyOfValidation == "Weekly": | ||
| return "Tier 1" | ||
| elif params.frequencyOfValidation == "Monthly": | ||
| return "Tier 2" | ||
| elif params.frequencyOfValidation == "Yearly": | ||
| return "Tier 3" | ||
| else: | ||
| return "N/A" | ||
| ``` | ||
|
|
||
| 4. Create a `Next Review Date` calculation field that depends on the `Approved Date` and `Risk Tier` fields: | ||
|
|
||
| ```python | ||
| def formula(params): | ||
| """ | ||
| Calculate the next review date based on the approval date and review frequency. | ||
|
|
||
| Args: | ||
| params.dmApprovedDate (str): The approval date in 'YYYY-MM-DD' format. | ||
| params.dmRiskTier (string): The review tier (Tier 1, Tier 2, or Tier 3). | ||
|
|
||
| Returns: | ||
| datetime: The next review date. | ||
| """ | ||
| # Guard against empty dates | ||
| if params.dmApprovedDate == "": | ||
| return "N/A" | ||
|
|
||
| # Convert the approved_on date string to a datetime object | ||
| approved_date = date.fromtimestamp((int(params.dmApprovedDate))/1000) | ||
|
|
||
| # Define the review frequency mapping | ||
| review_frequency = { | ||
| "Tier 1": relativedelta(months=3), # Quarterly | ||
| "Tier 2": relativedelta(months=6), # Semi-annually | ||
| "Tier 3": relativedelta(years=1), # Annually | ||
| } | ||
|
|
||
| # Get the appropriate time delta based on the tier | ||
| frequency = review_frequency.get(params.dmRiskTier) | ||
| if not frequency: | ||
| # "Invalid tier. Must be Tier 1, Tier 2, or Tier 3." | ||
| return "N/A" | ||
|
|
||
| # Calculate the next review date | ||
| next_review_date = approved_date + frequency | ||
| return next_review_date.isoformat() | ||
| ``` | ||
|
|
||
| {fig-alt="A screenshot showing the screen for adding a calculation type field that automatically calculates the next review date" width=90% .screenshot} | ||
|
|
||
| You can now determine the next review date in workflows by making a workflow depend on `Approved Date`. To test, change the `Approved Date` after the fact and see how `Next Review Date` changes. | ||
|
|
||
| #### Calculate the days remaining until the next review | ||
|
|
||
| 1. Create a `Days Remaining` calculation field that depends on the `Next Review Date` field: | ||
|
|
||
| ```python | ||
| def formula(params): | ||
| """ | ||
| Calculate days remaining until the next review. | ||
|
|
||
| Args: | ||
| params.nextReviewDate (str): The next review date in ISO format. | ||
|
|
||
| Returns: | ||
| str: Days remaining until review (example:"45 days remaining"). | ||
| """ | ||
| # Get next review date (stored as ISO format string) | ||
| next_review_date = getattr(params, "nextReviewDate", "") | ||
| if not next_review_date: | ||
| return "Not applicable" | ||
| next_review = date.fromisoformat(next_review_date) | ||
|
|
||
| # Calculate days until review using built-in vm_today | ||
| difference = next_review - vm_today | ||
| return f"{difference.days} days remaining" | ||
| ``` | ||
|
|
||
|
|
||
| {fig-alt="A screenshot showing the screen for adding a calculation type field that automatically calculates the days remaining until the next review" width=90% .screenshot} | ||
|
|
||
| You can now check the number of days remaining until the next review. | ||
| ::: | ||
| ::: | ||
|
|
||
| :::: | ||
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.