[editor] Simple callback for better upload.

This commit is contained in:
Alex Zolotarev 2016-01-23 16:04:15 +03:00 committed by Sergey Yershov
parent 313faabfef
commit 5e7de3a35f
3 changed files with 32 additions and 9 deletions

View file

@ -21,6 +21,7 @@
#include "std/algorithm.hpp"
#include "std/chrono.hpp"
#include "std/future.hpp"
#include "std/target_os.hpp"
#include "std/tuple.hpp"
#include "std/unordered_map.hpp"
#include "std/unordered_set.hpp"
@ -596,12 +597,15 @@ bool Editor::IsAddressEditable(FeatureType const & feature) const
return false;
}
void Editor::UploadChanges(string const & key, string const & secret, TChangesetTags const & tags)
void Editor::UploadChanges(string const & key, string const & secret, TChangesetTags tags,
TFinishUploadCallback callBack)
{
tags["created_by"] = "MAPS.ME " OMIM_OS_NAME;
// TODO(AlexZ): features access should be synchronized.
auto const lambda = [this](string key, string secret, TChangesetTags tags)
auto const lambda = [this](string key, string secret, TChangesetTags tags, TFinishUploadCallback callBack)
{
int uploadedFeaturesCount = 0;
int uploadedFeaturesCount = 0, errorsCount = 0;
// TODO(AlexZ): insert usefull changeset comments.
ChangesetWrapper changeset({key, secret}, tags);
for (auto & id : m_features)
@ -644,8 +648,9 @@ void Editor::UploadChanges(string const & key, string const & secret, TChangeset
{
fti.m_uploadStatus = kDeletedFromOSMServer;
fti.m_uploadAttemptTimestamp = time(nullptr);
fti.m_uploadError = "Node was deleted from the server.";
fti.m_uploadError = ex.what();
LOG(LWARNING, (fti.m_uploadError, ex.what()));
++errorsCount;
}
catch (RootException const & ex)
{
@ -653,20 +658,30 @@ void Editor::UploadChanges(string const & key, string const & secret, TChangeset
fti.m_uploadStatus = kNeedsRetry;
fti.m_uploadAttemptTimestamp = time(nullptr);
fti.m_uploadError = ex.what();
++errorsCount;
}
// TODO(AlexZ): Synchronize save after edits.
// Call Save every time we modify each feature's information.
Save(GetEditorFilePath());
}
}
// TODO(AlexZ): Should we call any callback at the end?
if (callBack)
{
UploadResult result = UploadResult::NothingToUpload;
if (uploadedFeaturesCount)
result = UploadResult::Success;
else if (errorsCount)
result = UploadResult::Error;
callBack(result);
}
};
// Do not run more than one upload thread at a time.
static auto future = async(launch::async, lambda, key, secret, tags);
static auto future = async(launch::async, lambda, key, secret, tags, callBack);
auto const status = future.wait_for(milliseconds(0));
if (status == future_status::ready)
future = async(launch::async, lambda, key, secret, tags);
future = async(launch::async, lambda, key, secret, tags, callBack);
}
void Editor::RemoveFeatureFromStorage(MwmSet::MwmId const & mwmId, uint32_t index)

View file

@ -30,6 +30,13 @@ public:
using TInvalidateFn = function<void()>;
using TFeatureLoaderFn = function<unique_ptr<FeatureType> (FeatureID const & /*fid*/)>;
using TFeatureOriginalStreetFn = function<string(FeatureType const & /*ft*/)>;
enum class UploadResult
{
Success,
Error,
NothingToUpload
};
using TFinishUploadCallback = function<void(UploadResult)>;
enum class FeatureStatus
{
@ -88,7 +95,8 @@ public:
using TChangesetTags = map<string, string>;
/// Tries to upload all local changes to OSM server in a separate thread.
/// @param[in] tags should provide additional information about client to use in changeset.
void UploadChanges(string const & key, string const & secret, TChangesetTags const & tags);
void UploadChanges(string const & key, string const & secret, TChangesetTags tags,
TFinishUploadCallback callBack = TFinishUploadCallback());
private:
// TODO(AlexZ): Synchronize Save call/make it on a separate thread.

View file

@ -384,7 +384,7 @@ void MainWindow::OnUploadEditsMenuItem()
if (key.empty() || secret.empty())
OnLoginMenuItem();
else
osm::Editor::Instance().UploadChanges(key, secret, {{"created_by", "MAPS.ME " OMIM_OS_NAME}});
osm::Editor::Instance().UploadChanges(key, secret, {});
}
void MainWindow::OnBeforeEngineCreation()