Skip to content

Commit

Permalink
Merge pull request #700 from openstudiocoalition/fix_696
Browse files Browse the repository at this point in the history
Correctly remove old measures when downloading new ones
  • Loading branch information
macumber authored Mar 30, 2024
2 parents d81459a + 5b7bbc0 commit 897c493
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 47 deletions.
89 changes: 68 additions & 21 deletions src/openstudio_lib/OSDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
#include <QSettings>
#include <QtGlobal> // Workaround for #659

#include <algorithm>
#include <memory>

#if (defined(_WIN32) || defined(_WIN64))
Expand Down Expand Up @@ -1354,19 +1355,6 @@ void OSDocument::addStandardMeasures() {
enable();
}

boost::optional<BCLComponent> OSDocument::getLocalComponent(const std::string& uid) const {
boost::optional<BCLComponent> result;
if (m_haveLocalBCL) {
try {
result = LocalBCL::instance().getComponent(uid);
} catch (const std::exception& e) {
LOG(Error, "Cannot access local BCL: " << e.what());
m_haveLocalBCL = false;
}
}
return result;
}

boost::optional<BCLComponent> OSDocument::getLocalComponent(const std::string& uid, const std::string& versionId) const {
boost::optional<BCLComponent> result;
if (m_haveLocalBCL) {
Expand All @@ -1380,11 +1368,11 @@ boost::optional<BCLComponent> OSDocument::getLocalComponent(const std::string& u
return result;
}

boost::optional<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid) const {
boost::optional<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid, const std::string& versionId) const {
boost::optional<BCLMeasure> result;
if (m_haveLocalBCL) {
try {
result = LocalBCL::instance().getMeasure(uid);
result = LocalBCL::instance().getMeasure(uid, versionId);
} catch (const std::exception& e) {
LOG(Error, "Cannot access local BCL: " << e.what());
m_haveLocalBCL = false;
Expand All @@ -1393,11 +1381,11 @@ boost::optional<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid)
return result;
}

boost::optional<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid, const std::string& versionId) const {
boost::optional<BCLMeasure> result;
std::vector<BCLMeasure> OSDocument::getLocalMeasures() const {
std::vector<BCLMeasure> result;
if (m_haveLocalBCL) {
try {
result = LocalBCL::instance().getMeasure(uid, versionId);
result = LocalBCL::instance().measures();
} catch (const std::exception& e) {
LOG(Error, "Cannot access local BCL: " << e.what());
m_haveLocalBCL = false;
Expand All @@ -1406,11 +1394,11 @@ boost::optional<BCLMeasure> OSDocument::getLocalMeasure(const std::string& uid,
return result;
}

std::vector<BCLMeasure> OSDocument::getLocalMeasures() const {
std::vector<BCLMeasure> result;
std::vector<BCLComponent> OSDocument::getLocalComponents() const {
std::vector<BCLComponent> result;
if (m_haveLocalBCL) {
try {
result = LocalBCL::instance().measures();
result = LocalBCL::instance().components();
} catch (const std::exception& e) {
LOG(Error, "Cannot access local BCL: " << e.what());
m_haveLocalBCL = false;
Expand All @@ -1419,6 +1407,65 @@ std::vector<BCLMeasure> OSDocument::getLocalMeasures() const {
return result;
}

size_t OSDocument::removeOutdatedLocalComponents(const std::string& uid, const std::string& currentVersionId) const {
// TODO: when https://github.com/NREL/OpenStudio/pull/5129 is merged, we can just call it
// size_t result = 0;
// if (m_haveLocalBCL) {
// try {
// result = LocalBCL::instance().removeOutdatedLocalComponents(uid, currentVersionId);
// } catch (const std::exception& e) {
// LOG(Error, "Cannot access local BCL: " << e.what());
// m_haveLocalBCL = false;
// }
// }
// return result;

auto components = getLocalComponents();
if (components.empty()) {
return {};
}

// Not empty, we do have a localbcl
components.erase(std::remove_if(components.begin(), components.end(),
[&uid, &currentVersionId](const auto& component) {
return (component.uid() != uid) || (component.versionId() == currentVersionId);
}),
components.end());
for (auto& component : components) {
LocalBCL::instance().removeComponent(component);
}
return components.size();
}

size_t OSDocument::removeOutdatedLocalMeasures(const std::string& uid, const std::string& currentVersionId) const {
// TODO: when https://github.com/NREL/OpenStudio/pull/5129 is merged, we can just call it
// size_t result = 0;
// if (m_haveLocalBCL) {
// try {
// result = LocalBCL::instance().removeOutdatedLocalMeasures(uid, currentVersionId);
// } catch (const std::exception& e) {
// LOG(Error, "Cannot access local BCL: " << e.what());
// m_haveLocalBCL = false;
// }
// }
// return result;

auto measures = getLocalMeasures();
if (measures.empty()) {
return {};
}

// Not empty, we do have a localbcl
measures.erase(
std::remove_if(measures.begin(), measures.end(),
[&uid, &currentVersionId](const auto& measure) { return (measure.uid() != uid) || (measure.versionId() == currentVersionId); }),
measures.end());
for (auto& measure : measures) {
LocalBCL::instance().removeMeasure(measure);
}
return measures.size();
}

std::vector<BCLComponent> OSDocument::componentAttributeSearch(const std::vector<std::pair<std::string, std::string>>& pairs) const {
std::vector<BCLComponent> result;
if (m_haveLocalBCL) {
Expand Down
13 changes: 8 additions & 5 deletions src/openstudio_lib/OSDocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,17 @@ class OPENSTUDIO_API OSDocument : public OSQObjectController
// returns false if the LocalBCL cannot be accessed
bool haveLocalBCL() const;

boost::optional<BCLComponent> getLocalComponent(const std::string& uid) const;
boost::optional<BCLComponent> getLocalComponent(const std::string& uid, const std::string& versionId) const;

boost::optional<BCLMeasure> getLocalMeasure(const std::string& uid) const;
boost::optional<BCLMeasure> getLocalMeasure(const std::string& uid, const std::string& versionId) const;
boost::optional<BCLComponent> getLocalComponent(const std::string& uid, const std::string& versionId = "") const;
boost::optional<BCLMeasure> getLocalMeasure(const std::string& uid, const std::string& versionId = "") const;

std::vector<BCLComponent> getLocalComponents() const;
std::vector<BCLMeasure> getLocalMeasures() const;

// Removes all components with uid but NOT currentVersionId
size_t removeOutdatedLocalComponents(const std::string& uid, const std::string& currentVersionId) const;
// Removes all measures with uid but NOT currentVersionId
size_t removeOutdatedLocalMeasures(const std::string& uid, const std::string& currentVersionId) const;

std::vector<BCLComponent> componentAttributeSearch(const std::vector<std::pair<std::string, std::string>>& pairs) const;

boost::optional<BCLMeasure> standardReportMeasure();
Expand Down
8 changes: 8 additions & 0 deletions src/openstudio_lib/openstudio.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,8 @@
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/checked_checkbox_locked.png">../shared_gui_components/images/checked_checkbox_locked.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/checked_checkbox_update_available.png">../shared_gui_components/images/checked_checkbox_update_available.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/delete_softer.png">../shared_gui_components/images/delete_softer.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/delete_softer_over.png">../shared_gui_components/images/delete_softer_over.png</file>
Expand Down Expand Up @@ -1231,6 +1233,10 @@
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/partially_checked_checkbox.png">../shared_gui_components/images/partially_checked_checkbox.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/partially_checked_checkbox_locked.png">../shared_gui_components/images/partially_checked_checkbox_locked.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/partially_checked_checkbox_update_available.png">../shared_gui_components/images/partially_checked_checkbox_update_available.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/pause_over.png">../shared_gui_components/images/pause_over.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/pause_press.png">../shared_gui_components/images/pause_press.png</file>
Expand Down Expand Up @@ -1281,6 +1287,8 @@
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/unchecked_checkbox_locked.png">../shared_gui_components/images/unchecked_checkbox_locked.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/unchecked_checkbox_update_available.png">../shared_gui_components/images/unchecked_checkbox_update_available.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>
<file alias="images/check_for_updates.png">../shared_gui_components/images/check_for_updates.png</file>
<file alias="images/[email protected]">../shared_gui_components/images/[email protected]</file>

Expand Down
17 changes: 4 additions & 13 deletions src/shared_gui_components/BuildingComponentDialogCentralWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,8 @@ void BuildingComponentDialogCentralWidget::comboBoxIndexChanged(const QString& t

void BuildingComponentDialogCentralWidget::componentDownloadComplete(const std::string& uid, const boost::optional<BCLComponent>& component) {
if (component) {
// good
// remove old component
boost::optional<BCLComponent> oldComponent = OSAppBase::instance()->currentDocument()->getLocalComponent(component->uid());
if (oldComponent && oldComponent->versionId() != component->versionId()) {
LocalBCL::instance().removeComponent(*oldComponent);
}
// remove outdated components
OSAppBase::instance()->currentDocument()->removeOutdatedLocalComponents(component->uid(), component->versionId());
} else {
// error downloading component
downloadFailed(uid);
Expand All @@ -304,13 +300,8 @@ void BuildingComponentDialogCentralWidget::componentDownloadComplete(const std::

void BuildingComponentDialogCentralWidget::measureDownloadComplete(const std::string& uid, const boost::optional<BCLMeasure>& measure) {
if (measure) {
// good

// remove old measure
boost::optional<BCLMeasure> oldMeasure = OSAppBase::instance()->currentDocument()->getLocalMeasure(measure->uid());
if (oldMeasure && oldMeasure->versionId() != measure->versionId()) {
LocalBCL::instance().removeMeasure(*oldMeasure);
}
// remove outdated measures
OSAppBase::instance()->currentDocument()->removeOutdatedLocalMeasures(measure->uid(), measure->versionId());
} else {
// error downloading measure
downloadFailed(uid);
Expand Down
27 changes: 19 additions & 8 deletions src/shared_gui_components/MeasureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#include <openstudio/utilities/filetypes/WorkflowStep.hpp>
#include <openstudio/utilities/filetypes/WorkflowStep_Impl.hpp>

#include "../openstudio_lib/OSAppBase.hpp"
#include "../openstudio_lib/OSDocument.hpp"

#include <json/json.h>

#include <QAbstractButton>
Expand Down Expand Up @@ -964,13 +967,11 @@ void MeasureManager::checkForRemoteBCLUpdates() {
+ tr("Would you like update them?"));

QString detailedText;
std::vector<BCLMeasure> oldMeasures;
for (const BCLSearchResult& update : updates) {
detailedText += toQString("* name: " + update.name() + "\n");
detailedText += toQString(" - uid: " + update.uid() + "\n");
auto current = m_bclMeasures.find(toUUID(update.uid()));
if (current != m_bclMeasures.end()) {
oldMeasures.push_back(current->second);
detailedText += toQString(" - old versionId: " + current->second.versionId() + "\n");
}
detailedText += toQString(" - new versionId: " + update.versionId() + "\n\n");
Expand All @@ -981,9 +982,15 @@ void MeasureManager::checkForRemoteBCLUpdates() {
int result = msg.exec();
if (result == QMessageBox::Yes) {
remoteBCL.updateMeasures();
for (auto& oldMeasure : oldMeasures) {
LocalBCL::instance().removeMeasure(oldMeasure);

// remoteBCL.updateMeasures should remove outdated measures, but won't work correctly until https://github.com/NREL/OpenStudio/pull/5129
// if we have the new measure, delete outdated ones
for (const BCLSearchResult& update : updates) {
if (OSAppBase::instance()->currentDocument()->getLocalMeasure(update.uid(), update.versionId())) {
OSAppBase::instance()->currentDocument()->removeOutdatedLocalMeasures(update.uid(), update.versionId());
}
}

updateMeasuresLists(false);
}
}
Expand All @@ -998,22 +1005,26 @@ void MeasureManager::downloadBCLMeasures() {
std::vector<BCLSearchResult> updates = remoteBCL.measuresWithUpdates();

QString detailedText;
std::vector<BCLMeasure> oldMeasures;
for (const BCLSearchResult& update : updates) {
detailedText += toQString("* name: " + update.name() + "\n");
detailedText += toQString(" - uid: " + update.uid() + "\n");
auto current = m_bclMeasures.find(toUUID(update.uid()));
if (current != m_bclMeasures.end()) {
oldMeasures.push_back(current->second);
detailedText += toQString(" - old versionId: " + current->second.versionId() + "\n");
}
detailedText += toQString(" - new versionId: " + update.versionId() + "\n\n");
}

remoteBCL.updateMeasures();
for (auto& oldMeasure : oldMeasures) {
LocalBCL::instance().removeMeasure(oldMeasure);

// remoteBCL.updateMeasures should remove outdated measures, but won't work correctly until https://github.com/NREL/OpenStudio/pull/5129
// if we have the new measure, delete outdated ones
for (const BCLSearchResult& update : updates) {
if (OSAppBase::instance()->currentDocument()->getLocalMeasure(update.uid(), update.versionId())) {
OSAppBase::instance()->currentDocument()->removeOutdatedLocalMeasures(update.uid(), update.versionId());
}
}

updateMeasuresLists(false);

QMessageBox msg(m_app->mainWidget());
Expand Down

0 comments on commit 897c493

Please sign in to comment.