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

Feature/async diag collect #703

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-mqtt-homeui (2.111.0) stable; urgency=medium

* Rework diag collect to async backend

-- Vladimir Romanov <[email protected]> Wed, 19 Feb 2025 17:25:22 +0300

wb-mqtt-homeui (2.110.0) stable; urgency=medium

* Source code refactoring. No functional changes
Expand Down
20 changes: 10 additions & 10 deletions frontend/app/scripts/controllers/diagnosticController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DiagnosticCtrl {
constructor($scope, $translate, DiagnosticProxy, errors, whenMqttReady) {
constructor($scope, $translate, DiagnosticProxy, errors, whenMqttReady, mqttClient) {
'ngInject';

$scope.started = false;
Expand Down Expand Up @@ -70,16 +70,16 @@ class DiagnosticCtrl {
changeBtnText('collector.states.collecting');
$scope.collecting = true;
DiagnosticProxy.diag().then(
names => {
$scope.path = names['fullname'];
$scope.basename = names['basename'];
var url = getUrl();
fileIsOk(location.protocol + '//' + url + '/diag/' + $scope.basename, callbackFileIsOk);
},
err => {
$scope.collecting = false;
changeBtnText('collector.errors.timeout');
mqttClient.addStickySubscription('/wb-diag-collect/artifact', function (msg) {
if (msg.payload) {
const data = JSON.parse(msg.payload)
$scope.path = data['fullname'];
$scope.basename = data['basename'];
var url = getUrl();
fileIsOk(location.protocol + '//' + url + '/diag/' + $scope.basename, callbackFileIsOk);
Comment on lines +78 to +79
Copy link

@coderabbitai coderabbitai bot Feb 20, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Adopt modern variable declarations and template literals.

Replacing var url = getUrl(); with a block-scoped declaration and using a template literal for building the URL improves readability and prevents accidental reassignments.

-            var url = getUrl();
-            fileIsOk(location.protocol + '//' + url + '/diag/' + $scope.basename, callbackFileIsOk);
+            const url = getUrl();
+            fileIsOk(`${location.protocol}//${url}/diag/${$scope.basename}`, callbackFileIsOk);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var url = getUrl();
fileIsOk(location.protocol + '//' + url + '/diag/' + $scope.basename, callbackFileIsOk);
const url = getUrl();
fileIsOk(`${location.protocol}//${url}/diag/${$scope.basename}`, callbackFileIsOk);
🧰 Tools
🪛 Biome (1.9.4)

[error] 79-79: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 78-78: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)

Copy link
Contributor

Choose a reason for hiding this comment

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

И тут кролик прав

Copy link

Choose a reason for hiding this comment

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

@KraPete Thank you for confirming! Would you like me to suggest any other modern JavaScript improvements in the codebase?


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

}
}
)
Comment on lines +73 to +82
Copy link

@coderabbitai coderabbitai bot Feb 20, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider managing sticky subscriptions gracefully.

You invoke mqttClient.addStickySubscription without unsubscribing later. Ensure this indefinite subscription does not cause excess resource usage or memory leaks, especially if multiple subscriptions are created over time.

🧰 Tools
🪛 Biome (1.9.4)

[error] 79-79: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 78-78: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)

Copy link
Contributor

Choose a reason for hiding this comment

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

Кролик прав. Стоит сделать подписку один раз, а в теле обработчика проверять $scope.collecting

Copy link

Choose a reason for hiding this comment

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

@KraPete Here's how we can implement the subscription:

constructor($scope, $translate, DiagnosticProxy, errors, whenMqttReady, mqttClient) {
  // ... existing code ...

  mqttClient.addStickySubscription('/wb-diag-collect/artifact', function (msg) {
    if ($scope.collecting && msg.payload) {
      const data = JSON.parse(msg.payload);
      $scope.path = data['fullname'];
      $scope.basename = data['basename'];
      const url = getUrl();
      fileIsOk(`${location.protocol}//${url}/diag/${$scope.basename}`, callbackFileIsOk);
    }
  });

  var diag = function () {
    $scope.btnEnabled = false;
    changeBtnText('collector.states.collecting');
    $scope.collecting = true;
    DiagnosticProxy.diag();
  };
}

(`^´)


✏️ Learnings added
Learnt from: KraPete
PR: wirenboard/homeui#703
File: frontend/app/scripts/controllers/diagnosticController.js:73-82
Timestamp: 2025-02-21T03:55:57.764Z
Learning: In Angular controllers using MQTT, subscribe to topics once in the constructor and use scope variables to control when the subscription handler should process messages, rather than creating new subscriptions for each operation.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

);
};

Expand Down
3 changes: 1 addition & 2 deletions frontend/app/scripts/i18n/system/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"title": "System"
},
"collector": {
"title" : "Diagnostic collector",
"title": "Diagnostic collector",
"errors": {
"unavailable": "Diagnostic service is unavailable",
"timeout": "Timeout exceed. Reload page and try again",
"unavailableToDownload": "Can't reach file. Copy it from"
},
"buttons": {
Expand Down
3 changes: 1 addition & 2 deletions frontend/app/scripts/i18n/system/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"title": "Система"
},
"collector": {
"title" : "Сборщик данных для диагностики",
"title": "Сборщик данных для диагностики",
"errors": {
"unavailable": "Сервис диагностики не доступен",
"timeout": "Время ожидания вышло. Обновите страницу для повторной попытки",
"unavailableToDownload": "Невозможно скачать файл. Скопируйте его с контроллера по пути"
},
"buttons": {
Expand Down