Replace int with std::chrono in for the timer->setInterval() argument #524

pull shaavan wants to merge 1 commits into bitcoin-core:master from shaavan:220112-chrono changing 3 files +14 −13
  1. shaavan commented at 1:48 pm on January 12, 2022: contributor

    The PR is a follow-up to #517

    • It addresses the change suggested in this comment.
    • This PR changes the type of msecsPerSample from int to std::chrono::minutes and makes other relevant subsequent changes that were limited to the trafficgraphwidget file.
  2. in src/qt/trafficgraphwidget.h:29 in 6e947e6df7 outdated
    23@@ -22,7 +24,7 @@ class TrafficGraphWidget : public QWidget
    24 public:
    25     explicit TrafficGraphWidget(QWidget *parent = nullptr);
    26     void setClientModel(ClientModel *model);
    27-    int getGraphRangeMins() const;
    28+    std::chrono::minutes getGraphRangeMins() const;
    


    hebasto commented at 1:51 pm on January 12, 2022:
    0    std::chrono::minutes getGraphRange() const;
    

    Also s/setGraphRangeMins/setGraphRange/


    shaavan commented at 2:40 pm on January 13, 2022:
    Addressed in 9bb48bf657c7f2d36309807fbc7b55d0ab581a76
  3. in src/qt/trafficgraphwidget.h:42 in 6e947e6df7 outdated
    38@@ -37,7 +39,7 @@ public Q_SLOTS:
    39 
    40     QTimer *timer;
    41     float fMax;
    42-    int nMins;
    43+    std::chrono::minutes nMins;
    


    hebasto commented at 1:51 pm on January 12, 2022:
    0    std::chrono::minutes m_range;
    

    shaavan commented at 2:40 pm on January 13, 2022:
    Addressed in 9bb48bf657c7f2d36309807fbc7b55d0ab581a76
  4. in src/qt/trafficgraphwidget.cpp:28 in 6e947e6df7 outdated
    24 TrafficGraphWidget::TrafficGraphWidget(QWidget *parent) :
    25     QWidget(parent),
    26     timer(nullptr),
    27     fMax(0.0f),
    28-    nMins(0),
    29+    nMins(0min),
    


    hebasto commented at 1:52 pm on January 12, 2022:
    Move into a member initializer?

    shaavan commented at 2:40 pm on January 13, 2022:
    Addressed in 9bb48bf657c7f2d36309807fbc7b55d0ab581a76
  5. shaavan force-pushed on Jan 13, 2022
  6. shaavan commented at 11:37 am on January 13, 2022: contributor

    Updated from 6e947e6 to 9bb48bf (pr524.01 -> pr524.02) Addressed @hebasto comments

    Changes:

    • Changed argument type of setGraphRangeMins from int to std::minutes.
    • setGraphRangeMins -> setGraphRange; getGraphRangeMins -> getGraphRange
    • Moved nMins to member initialization.
    • Renamed nMins -> m_range.
  7. DrahtBot commented at 2:38 pm on January 13, 2022: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #539 (gui: scale network graph based on time interval by RandyMcMillan)
    • #492 (Show ToolTip on Network Traffic graph by rebroad)
    • #484 (Don’t clear traffic graph when changing interval by rebroad)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  8. in src/qt/rpcconsole.cpp:1143 in 9bb48bf657 outdated
    1139@@ -1140,7 +1140,7 @@ void RPCConsole::on_sldGraphRange_valueChanged(int value)
    1140 
    1141 void RPCConsole::setTrafficGraphRange(int mins)
    1142 {
    1143-    ui->trafficGraph->setGraphRangeMins(mins);
    1144+    ui->trafficGraph->setGraphRange(std::chrono::minutes(mins));
    


    jonatack commented at 6:56 pm on January 13, 2022:
    0    ui->trafficGraph->setGraphRange(std::chrono::minutes{mins});
    

    Should the include header be added? e.g. include what you use.

    0 #include <QTimer>
    1 #include <QVariant>
    2 
    3+#include <chrono>
    4+
    5 const int CONSOLE_HISTORY = 50;
    

    shaavan commented at 7:12 am on January 16, 2022:

    Thanks, @jonatack, for nit suggestions, as well as catching the missing header. I intended to add the header, but for some reason, I missed adding it before updating the PR for some reason.

    Addressed in 5e6ad43

  9. in src/qt/trafficgraphwidget.cpp:161 in 9bb48bf657 outdated
    159+void TrafficGraphWidget::setGraphRange(std::chrono::minutes mins)
    160 {
    161-    nMins = mins;
    162-    int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES;
    163+    m_range = std::chrono::minutes(mins);
    164+    auto msecsPerSample = m_range * 60 * 1000 / DESIRED_SAMPLES;
    


    jonatack commented at 6:57 pm on January 13, 2022:
    0-    m_range = std::chrono::minutes(mins);
    1-    auto msecsPerSample = m_range * 60 * 1000 / DESIRED_SAMPLES;
    2+    m_range = std::chrono::minutes{mins};
    3+    auto msecsPerSample{m_range * 60 * 1000 / DESIRED_SAMPLES};
    

    hebasto commented at 9:21 pm on January 15, 2022:

    #524#pullrequestreview-853590647

    Tested 9bb48bf on Linux Mint 20.3 (Qt 5.12.8) – the “Network Traffic” tab shows a black window.

    Could be fixed with

    0    const auto msecs_per_sample{std::chrono::duration_cast<std::chrono::milliseconds>(m_range) / DESIRED_SAMPLES};
    

    shaavan commented at 7:10 am on January 16, 2022:

    Thanks, @hebasto for catching it and giving suggestions on how to correct this misbehavior

    Addressed in 5e6ad435b0ed634fca5745668bf949799b306bff


    shaavan commented at 7:12 am on January 16, 2022:
    Addressed in 5e6ad43
  10. in src/qt/trafficgraphwidget.cpp:49 in 9bb48bf657 outdated
    43@@ -42,9 +44,9 @@ void TrafficGraphWidget::setClientModel(ClientModel *model)
    44     }
    45 }
    46 
    47-int TrafficGraphWidget::getGraphRangeMins() const
    48+std::chrono::minutes TrafficGraphWidget::getGraphRange() const
    49 {
    50-    return nMins;
    51+    return m_range;
    


    jonatack commented at 6:59 pm on January 13, 2022:

    nit, the getter could be just one line, feel free to ignore

    0std::chrono::minutes TrafficGraphWidget::getGraphRange() const { return m_range; }
    

    shaavan commented at 7:14 am on January 16, 2022:

    I think it’s better to incorporate the correct way of writing the code from the get-go.

    Addressed in 5e6ad43

  11. jonatack commented at 7:03 pm on January 13, 2022: contributor
    Concept ACK
  12. hebasto added the label Refactoring on Jan 15, 2022
  13. hebasto commented at 8:17 pm on January 15, 2022: member
    Tested 9bb48bf657c7f2d36309807fbc7b55d0ab581a76 on Linux Mint 20.3 (Qt 5.12.8) – the “Network Traffic” tab shows a black window.
  14. shaavan force-pushed on Jan 16, 2022
  15. shaavan commented at 7:08 am on January 16, 2022: contributor

    Updated from 9bb48bf to 5e6ad43 (pr524.02 -> pr524.03) Addressed @jonatack and @hebasto’s suggestions.

    • Done proper casting of m_range variable from mins to milliseconds.
    • Used duration_casting instead of static_casting, as suggested by @hebasto. The reason for doing so is explained in this article.
    • Incorporated other nit suggestions.

    Tested that the network traffic graph is working correctly now.

  16. in src/qt/trafficgraphwidget.cpp:23 in 5e6ad435b0 outdated
    18 
    19 #define XMARGIN                 10
    20 #define YMARGIN                 10
    21 
    22+using namespace std::chrono_literals;
    23+
    


    hebasto commented at 9:14 am on January 16, 2022:
    Why this change?

    shaavan commented at 2:49 pm on January 18, 2022:

    I checked through the file and made sure that this statement is not needed.

    Removed in f7a19ef774ef92ce348215593e3590a750c345e1

  17. in src/qt/trafficgraphwidget.cpp:157 in 5e6ad435b0 outdated
    151@@ -153,12 +152,12 @@ void TrafficGraphWidget::updateRates()
    152     update();
    153 }
    154 
    155-void TrafficGraphWidget::setGraphRangeMins(int mins)
    156+void TrafficGraphWidget::setGraphRange(std::chrono::minutes mins)
    157 {
    158-    nMins = mins;
    159-    int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES;
    160+    m_range = std::chrono::minutes{mins};
    


    hebasto commented at 9:15 am on January 16, 2022:

    Maybe just

    0    m_range = mins;
    

    ?


    shaavan commented at 2:50 pm on January 18, 2022:

    The type-conversion here seems redundant.

    Addressed in f7a19ef774ef92ce348215593e3590a750c345e1

  18. hebasto commented at 9:21 am on January 16, 2022: member

    Approach ACK 5e6ad435b0ed634fca5745668bf949799b306bff, tested on Linux Mint 20.3 (Qt 5.12.8).

    About commit message:

  19. qt,refactor: Use std::chrono in TrafficGraphWidget class f7a19ef774
  20. in src/qt/trafficgraphwidget.h:34 in 5e6ad435b0 outdated
    31     void paintEvent(QPaintEvent *) override;
    32 
    33 public Q_SLOTS:
    34     void updateRates();
    35-    void setGraphRangeMins(int mins);
    36+    void setGraphRange(std::chrono::minutes mins);
    


    hebasto commented at 9:29 am on January 16, 2022:

    Maybe

    0    void setGraphRange(std::chrono::minutes new_range);
    

    ?


    shaavan commented at 2:51 pm on January 18, 2022:

    I think new_range is a more suitable name for the argument, and now its unit is cleared by its type (i.e., minutes)

    Addressed in f7a19ef774ef92ce348215593e3590a750c345e1

  21. shaavan force-pushed on Jan 18, 2022
  22. shaavan commented at 2:48 pm on January 18, 2022: contributor

    Updated from 5e6ad43 to f7a19ef (pr524.03 -> pr524.04) Addressed @hebasto suggestions

    Changes:

    • Removed the redundant using namespace std::chrono_literals; statement.
    • Removed unnecessary type conversion of chrono::minutes variable.
    • Renamed mins -> new_range.
    • Made appropriate changes to commit message as suggested here.
  23. hebasto approved
  24. hebasto commented at 3:33 pm on January 18, 2022: member
    ACK f7a19ef774ef92ce348215593e3590a750c345e1
  25. RandyMcMillan commented at 9:51 pm on January 18, 2022: contributor

    tACK f7a19ef774ef92ce348215593e3590a750c345e1

    on MacOS x86_64 and Arm64

  26. Gemk83 approved
  27. in src/qt/trafficgraphwidget.h:27 in f7a19ef774
    23@@ -22,22 +24,22 @@ class TrafficGraphWidget : public QWidget
    24 public:
    25     explicit TrafficGraphWidget(QWidget *parent = nullptr);
    26     void setClientModel(ClientModel *model);
    27-    int getGraphRangeMins() const;
    28+    std::chrono::minutes getGraphRange() const;
    


    promag commented at 3:56 pm on January 20, 2022:
    Just remove? It’s not used.

    RandyMcMillan commented at 6:19 pm on January 20, 2022:

    @promag @shaavan

    I concur - It appears to be part of the original implementation but never used for anything…

    it can and should be removed…

    https://github.com/bitcoin-core/gui/blame/16781e1bc9f8ffc721ebea73434e0066957bc959/src/qt/trafficgraphwidget.cpp#L45

    Screen Shot 2022-01-20 at 1 16 34 PM


    RandyMcMillan commented at 6:19 pm on January 20, 2022:
    @promag - great catch!

    shaavan commented at 12:31 pm on January 21, 2022:
    Thanks @promag for catching this. Addressed in 5efd391eb1ea1a3551b2335827e4b5577be2aca3

    RandyMcMillan commented at 4:59 pm on January 28, 2022:

    Approach ACK Removing the TrafficGraphWidget::getGraphRangeMins() function nACK

    I use int TrafficGraphWidget::getGraphRangeMins() const in PR #539 (which fixes issue #532)

    I have added a variation to of this PR to #539 c25d3f4 to ensure compatibility with the chrono lib.

    https://github.com/bitcoin-core/gui/blob/c25d3f42084926c92ae2051278cd785cb1907d56/src/qt/trafficgraphwidget.cpp#L46

    Note

    https://github.com/bitcoin-core/gui/pull/524/files#diff-da38549cd85e0c422891b8ef1eb3cc33e76f0b80d15ca63aa7ad6f065c8ba694L44

  28. promag commented at 3:56 pm on January 20, 2022: contributor
    Code review ACK f7a19ef774ef92ce348215593e3590a750c345e1.
  29. shaavan force-pushed on Jan 21, 2022
  30. shaavan commented at 12:30 pm on January 21, 2022: contributor

    Updated from f7a19ef to 5efd391 (pr524.04 -> pr524.05) Addressed @promag suggestions

    Changes:

    • Removed the unused function getGraphRange() from the TrafficGraphWidget class.
  31. w0xlt approved
  32. w0xlt commented at 9:06 am on January 23, 2022: contributor
    tACK 5efd391 on Ubuntu 21.10 (Qt 5.15.2).
  33. hebasto approved
  34. hebasto commented at 9:27 am on January 23, 2022: member
    re-ACK 5efd391eb1ea1a3551b2335827e4b5577be2aca3
  35. in src/qt/trafficgraphwidget.cpp:153 in 5efd391eb1 outdated
    147@@ -153,12 +148,12 @@ void TrafficGraphWidget::updateRates()
    148     update();
    149 }
    150 
    151-void TrafficGraphWidget::setGraphRangeMins(int mins)
    152+void TrafficGraphWidget::setGraphRange(std::chrono::minutes new_range)
    


    promag commented at 4:12 pm on January 23, 2022:
    Drop m_range and rename this arg to range.

    promag commented at 4:13 pm on January 23, 2022:
    Unless you early return like if (m_range == new_range) return, but I think this won’t happen in practice.

    hebasto commented at 6:53 pm on January 23, 2022:

    Drop m_range and rename this arg to range.

    In a separate (first?) commit, no?


    promag commented at 9:42 pm on January 23, 2022:
    Why not the same commit? Just drop nMin instead of rename.

    hebasto commented at 8:09 am on January 24, 2022:
    (a) getting rid of TrafficGraphWidget::getGraphRangeMins() and TrafficGraphWidget::nMin, and (b) refactoring of TrafficGraphWidget::setGraphRangeMins() (renaming and parameter type changing) looks really as two focused commits, no?

    promag commented at 8:47 am on January 24, 2022:
    Either way looks fine to me 👍

    shaavan commented at 2:18 pm on January 27, 2022:

    I think this is a valid suggestion. Better to get rid of a redundant member variable.

    Addressed in bdf7cb9f0c17bdeeb8389d4b9140aca65e08ac71 @hebasto

    (a) getting rid of TrafficGraphWidget::getGraphRangeMins() and TrafficGraphWidget::nMin, and (b) refactoring of TrafficGraphWidget::setGraphRangeMins() (renaming and parameter type changing) looks really as two focused commits, no?

    I agree with your reasoning. However, (a) and (b) are not mutually exclusive, and hence I was having a hard time splitting these two changes while keeping the commits meaningful. Therefore, I reckoned it would be best to make these changes in one commit.

  36. shaavan force-pushed on Jan 27, 2022
  37. shaavan commented at 2:15 pm on January 27, 2022: contributor

    Updated from 5efd391 to bdf7cb9 (pr524.05 -> pr524.06) Addressed @promag suggestion

    Changes:

    • Removed m_range as a member variable of the class TrafficGraphWidget and replaced it with a local range variable.
  38. RandyMcMillan commented at 4:57 pm on January 28, 2022: contributor

    Approach ACK Removing the TrafficGraphWidget::getGraphRangeMins() function nACK

    I use int TrafficGraphWidget::getGraphRangeMins() const in PR #539 (which fixes issue #532)

    I have added a variation to of this PR to #539 https://github.com/bitcoin-core/gui/pull/539/commits/c25d3f42084926c92ae2051278cd785cb1907d56 to ensure compatibility with the chrono lib.

    https://github.com/bitcoin-core/gui/blob/c25d3f42084926c92ae2051278cd785cb1907d56/src/qt/trafficgraphwidget.cpp#L46

    Note

    https://github.com/bitcoin-core/gui/pull/524/files#diff-da38549cd85e0c422891b8ef1eb3cc33e76f0b80d15ca63aa7ad6f065c8ba694L44

  39. shaavan commented at 10:49 am on January 31, 2022: contributor

    I use int TrafficGraphWidget::getGraphRangeMins() const in PR #539 (which fixes issue #532)

    I looked through #532, but I couldn’t find any use case of getGraphRangeMins() there. So I don’t think there should be an issue removing this function in this PR.

    I have added a variation of this PR to #539 c25d3f4 to ensure compatibility with the chrono lib.

    I looked through it, and it looked good. However, I reckon the primary purpose of #539 is to fix an issue by adding a scaling feature. And the purpose of this PR is refactoring. So I think it would be best to keep these PRs separate.

  40. RandyMcMillan commented at 0:02 am on February 1, 2022: contributor

    If you insist - but to scale the graph - we will have to know the current value of the slider - otherwise the graph can’t calculate how many samples to display. It will have to be re-added - it is easier to leave it in.

    https://github.com/bitcoin-core/gui/pull/539

  41. shaavan commented at 7:30 am on February 1, 2022: contributor

    It will have to be re-added - it is easier to leave it in.

    I think you are right. Let me keep the GetGraphRange() function and make it consistent with the previous changes.

  42. shaavan force-pushed on Feb 3, 2022
  43. shaavan commented at 10:11 am on February 3, 2022: contributor

    Updated from bdf7cb9 to cad0e0a (pr524.06 -> pr524.07, diff) Addressed @RandyMcMillan comment

    Changes:

    • Restored the getGraphRange() function, as it is used in #539. So removing and re-adding the function doesn’t seem optimal.

    The updated PR correctly compiles on Ubuntu 20.04, with the Network Traffic widget showing the correct graph.

  44. shaavan force-pushed on Feb 3, 2022
  45. shaavan commented at 11:41 am on February 3, 2022: contributor

    Reverted back to f7a19ef774ef92ce348215593e3590a750c345e1 (pr524.07 -> pr524.04, diff)

    Reason:

    P.S.: To avoid any unintended changes between the PR and f7a19ef774ef92ce348215593e3590a750c345e1, I used the following command to update the PR:

    0git reset --hard HEAD^
    1git cherry-pick f7a19ef774ef92ce348215593e3590a750c345e1
    

    The commit id has changed (which I was not able to restore) but the changes are identical f7a19ef774ef92ce348215593e3590a750c345e1

  46. shaavan force-pushed on Feb 4, 2022
  47. shaavan commented at 10:11 am on February 4, 2022: contributor

    Restored PR to previous commit f7a19ef774ef92ce348215593e3590a750c345e1. The reason for doing so is explained here: #524 (comment)

    P.s.: Thanks, @hebasto, for your help in restoring this commit.

  48. hebasto merged this on Feb 4, 2022
  49. hebasto closed this on Feb 4, 2022

  50. sidhujag referenced this in commit 6152180985 on Feb 6, 2022
  51. RandyMcMillan commented at 6:33 pm on February 8, 2022: contributor
    @shaavan - thanks for leaving that function in - @rebroad and I have work that utilizes this.
  52. shaavan deleted the branch on Feb 9, 2022
  53. bitcoin-core locked this on Feb 9, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/gui. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-23 09:20 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me