* @brief Returns the last N samples of every plot-enabled dataset. */
| 455 | * @brief Returns the last N samples of every plot-enabled dataset. |
| 456 | */ |
| 457 | API::CommandResponse API::Handlers::DashboardHandler::tailFrames(const QString& id, |
| 458 | const QJsonObject& params) |
| 459 | { |
| 460 | int count = 32; |
| 461 | if (params.contains(QStringLiteral("count"))) |
| 462 | count = qBound(1, params.value(QStringLiteral("count")).toInt(32), 256); |
| 463 | |
| 464 | QSet<int> filterUids; |
| 465 | bool filterActive = false; |
| 466 | if (params.contains(QStringLiteral("uniqueIds"))) { |
| 467 | const auto arr = params.value(QStringLiteral("uniqueIds")).toArray(); |
| 468 | for (const auto& v : arr) |
| 469 | filterUids.insert(v.toInt()); |
| 470 | |
| 471 | filterActive = !filterUids.isEmpty(); |
| 472 | } |
| 473 | |
| 474 | auto& dashboard = UI::Dashboard::instance(); |
| 475 | const int plotCount = dashboard.widgetCount(SerialStudio::DashboardPlot); |
| 476 | QJsonArray seriesArr; |
| 477 | |
| 478 | for (int i = 0; i < plotCount; ++i) { |
| 479 | const auto& ds = dashboard.getDatasetWidget(SerialStudio::DashboardPlot, i); |
| 480 | if (filterActive && !filterUids.contains(ds.uniqueId)) |
| 481 | continue; |
| 482 | |
| 483 | const auto& series = dashboard.plotData(i); |
| 484 | if (!series.x || !series.y) |
| 485 | continue; |
| 486 | |
| 487 | const auto& xq = *series.x; |
| 488 | const auto& yq = *series.y; |
| 489 | const auto n = std::min<std::size_t>(xq.size(), yq.size()); |
| 490 | if (n == 0) |
| 491 | continue; |
| 492 | |
| 493 | const std::size_t take = std::min<std::size_t>(static_cast<std::size_t>(count), n); |
| 494 | const std::size_t start = n - take; |
| 495 | |
| 496 | QJsonArray xs; |
| 497 | QJsonArray ys; |
| 498 | for (std::size_t k = start; k < n; ++k) { |
| 499 | xs.append(xq[k]); |
| 500 | ys.append(yq[k]); |
| 501 | } |
| 502 | |
| 503 | QJsonObject row; |
| 504 | row[Keys::UniqueId] = ds.uniqueId; |
| 505 | row[QStringLiteral("title")] = ds.title; |
| 506 | row[QStringLiteral("units")] = ds.units; |
| 507 | row[QStringLiteral("groupId")] = ds.groupId; |
| 508 | row[Keys::DatasetId] = ds.datasetId; |
| 509 | row[QStringLiteral("count")] = static_cast<int>(take); |
| 510 | row[QStringLiteral("x")] = xs; |
| 511 | row[QStringLiteral("y")] = ys; |
| 512 | seriesArr.append(row); |
| 513 | } |
| 514 |