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

Update forkrun.bash #53

Open
wants to merge 2 commits into
base: forkrun_testing
Choose a base branch
from
Open

Update forkrun.bash #53

wants to merge 2 commits into from

Conversation

jkool702
Copy link
Owner

@jkool702 jkool702 commented Dec 2, 2024

Summary by Sourcery

Enhancements:

  • Refactor the handling of numeric values with prefixes by introducing a new function _forkrun_getVal to simplify and standardize the conversion process.

Copy link
Contributor

sourcery-ai bot commented Dec 2, 2024

Reviewer's Guide by Sourcery

The changes focus on improving code readability and functionality in the forkrun.bash script. The main changes include reorganizing variable declarations, enhancing the number parsing functionality through the _forkrun_getVal function, and fixing indentation consistency throughout the code.

Class diagram for updated variable declarations in forkrun.bash

classDiagram
    class Forkrun {
        - tmpDir
        - fPath
        - outStr
        - delimiterVal
        - delimiterReadStr
        - delimiterRemoveStr
        - exitTrapStr
        - exitTrapStr_kill
        - nLines
        - nLines0
        - nLinesMax
        - nQueueMin
        - nOrder
        - nProcs
        - nProcsMax
        - nBytes
        - tTimeout
        - coprocSrcCode
        - outCur
        - tmpDirRoot
        - returnVal
        - tmpVar
        - t0
        - readBytesProg
        - nullDelimiterProg
        - ddQuietStr
        - trailingNullFlag
        - inotifyFlag
        - lseekFlag
        - fallocateFlag
        - nLinesAutoFlag
        - nQueueFlag
        - substituteStringFlag
        - substituteStringIDFlag
        - nOrderFlag
        - readBytesFlag
        - readBytesExactFlag
        - nullDelimiterFlag
        - subshellRunFlag
        - stdinRunFlag
        - pipeReadFlag
        - rmTmpDirFlag
        - exportOrderFlag
        - noFuncFlag
        - unescapeFlag
        - optParseFlag
        - continueFlag
        - doneIndicatorFlag
        - FORCE_allowCarriageReturnsFlag
        - ddAvailableFlag
        - fd_continue
        - fd_inotify
        - fd_inotify0
        - fd_nAuto
        - fd_nAuto0
        - fd_nOrder
        - fd_nOrder0
        - fd_read
        - fd_read0
        - fd_write
        - fd_stdout
        - fd_stdin
        - fd_stdin0
        - fd_stderr
        - pWrite
        - pOrder
        - pAuto
        - pQueue
        - pWrite_PID
        - pNotify_PID
        - pOrder_PID
        - pAuto_PID
        - pQueue_PID
        - fd_read_pos
        - fd_read_pos_old
        - fd_write_pos
        - DEBUG_FORKRUN
        - PID0
        - nLinesCur
        - nLinesNew
        - nRead
        - nWait
        - nOrder0
        - nBytesRead
        - nQueue
        - nQueueLast
        - nQueueLastCount
        - nCPU
        - v9
        - kkMax
        - kkCur
        - kk
        - kkProcs
        - verboseLevel
        - pLOAD_max
        - pAdd
        - A
        - p_PID
        - runCmd
        - outHave
        - outPrint
        - pLOADA
        - pMap
    }
Loading

Class diagram for _forkrun_getVal function

classDiagram
    class Forkrun {
        + _forkrun_getVal()
    }
    note for Forkrun "Enhances number parsing functionality by expanding IEC and SI prefixes to numeric values."
Loading

File-Level Changes

Change Details Files
Reorganized and improved local variable declarations
  • Reordered variable declarations for better organization
  • Added explicit declarations for integer variables nQueueMin, nLines, and nLinesMax
forkrun.bash
Enhanced the _forkrun_getVal function for better number parsing
  • Improved handling of IEC and SI prefixes
  • Simplified prefix parsing logic
  • Added support for more unit prefixes (e, z, y, r, q)
  • Updated documentation and examples
  • Optimized the implementation using a global associative array
forkrun.bash
Fixed code formatting and indentation
  • Replaced tabs with spaces for consistent indentation
  • Fixed alignment in various code blocks
  • Standardized whitespace usage
forkrun.bash
Added value validation using _forkrun_getVal
  • Added validation for nLines parameter
  • Added validation for nProcs and related parameters
  • Added validation for nBytes parameter
forkrun.bash

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @jkool702 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The commented out 'local -AI pMap' declaration should probably be kept to ensure pMap remains an associative integer array
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

forkrun.bash Outdated
nn="${nn%i}";

case "${nn:0:1}" in
'+')
echo "$(( ${nn//[^0-9]/} * ( 1000 ** ${pMap[${nn: -1}]:-0} ) ))"
echo "$(( ${nn//[^0-9]/} * ( 1000 ** ${pMap[${nn:'-1'}]:-0} ) ))"
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): The string slicing syntax ${nn:'-1'} is incorrect - the quotes will prevent it from actually slicing the string

This should be ${nn:-1} for string slicing. The current syntax treats '-1' as a default value rather than a slice index.

forkrun.bash Outdated

local +i nn
local -AI pMap
#local -AI pMap
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): The pMap array declaration is commented out but the array is used throughout the function

Either remove this line entirely if pMap is declared elsewhere, or uncomment it if it's needed here. The current state is ambiguous and could lead to bugs.

@@ -1859,31 +1859,33 @@ _forkrun_get_load() (

_forkrun_getVal() {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider simplifying prefix handling by using a lookup table with pre-computed values instead of runtime calculations

The prefix handling can be simplified using a lookup table approach while preserving all functionality. Here's a suggested implementation:

_forkrun_getVal() {
    local -A multipliers=(
        [k]=1024  [kb]=1024  [kib]=1024
        [m]=1048576  [mb]=1048576  [mib]=1048576
        [+k]=1000  [+kb]=1000  [+kib]=1024
        [+m]=1000000  [+mb]=1000000  [+mib]=1048576
        # Add other prefixes similarly...
    )

    local nn
    for nn in "${@,,}"; do
        nn="${nn%[b]}"
        local base="${nn//[0-9]/}"
        local num="${nn//[^0-9]/}"
        echo "$((num * ${multipliers[$base]:-1}))"
    done
}

This approach:

  1. Uses a single lookup table with pre-computed values
  2. Eliminates complex string manipulations and case statements
  3. Preserves all functionality including IEC/SI prefix handling
  4. Maintains backward compatibility
  5. Makes the behavior more explicit and easier to maintain

apply _forkrun_getVal to all user-provided numeric options so that any user-specified number can use a SI/IEC prefix
@jkool702
Copy link
Owner Author

jkool702 commented Dec 2, 2024

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @jkool702 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -1858,32 +1850,29 @@ _forkrun_get_load() (
)

_forkrun_getVal() {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider simplifying the _forkrun_getVal function by using a more structured approach with clear variable naming and separation of concerns.

The _forkrun_getVal function has become unnecessarily complex. Here's a cleaner approach that maintains all functionality:

_forkrun_getVal() {
    # Handles both IEC (1024^N) and SI (1000^N) prefixes
    # Examples: 1K=1000, 1KiB=1024, +1K=1024
    local -l input  # Force lowercase
    local base multiplier prefix

    for input in "${@%%[Bb]*}"; do  # Strip trailing B/b
        input="${input// /}"  # Remove spaces

        # Extract numeric part and prefix
        local num="${input//[^0-9]/}"
        prefix="${input##*[0-9]}"

        # Determine base (1000 vs 1024)
        if [[ "$input" == *i* ]] || [[ "$input" == +* ]]; then
            base=1024
            multiplier=${pMap[${prefix%i}]:-0}  # Strip 'i' from prefix
        else
            base=1000
            multiplier=${pMap[${prefix}]:-0}
        fi

        # Calculate and output
        if ((base == 1024)); then
            printf '%s\n' "$((num << (10 * multiplier)))"
        else
            printf '%s\n' "$((num * (base ** multiplier)))"
        fi
    done
}

This version:

  1. Clearly separates IEC vs SI prefix handling
  2. Uses descriptive variable names
  3. Has a more logical flow with input normalization first
  4. Maintains all edge case handling
  5. Uses simpler pattern matching

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.

1 participant