Sync Feedback

- Added a detailed sync summary message, indicating what happened.
- Improved and localized the feedback messages for the sync command.
- Added color to the add/modify feedback, matching the merge command.
- Enabled the sync timer for debugging output.
This commit is contained in:
Paul Beckingham 2012-10-13 12:14:46 -04:00
parent 8fa7d135db
commit 47d94f370e
4 changed files with 71 additions and 36 deletions

View file

@ -276,7 +276,7 @@ int Context::run ()
<< " init:" << timer_init.total () << " init:" << timer_init.total ()
<< " load:" << timer_load.total () << " load:" << timer_load.total ()
<< " synch:" << timer_synch.total () << " synch:" << timer_sync.total ()
<< " gc:" << timer_gc.total () << " gc:" << timer_gc.total ()
<< " filter:" << timer_filter.total () << " filter:" << timer_filter.total ()
<< " commit:" << timer_commit.total () << " commit:" << timer_commit.total ()
@ -284,7 +284,7 @@ int Context::run ()
<< " render:" << timer_render.total () << " render:" << timer_render.total ()
<< " total:" << (timer_init.total () + << " total:" << (timer_init.total () +
timer_load.total () + timer_load.total () +
timer_synch.total () + timer_sync.total () +
timer_gc.total () + timer_gc.total () +
timer_filter.total () + timer_filter.total () +
timer_commit.total () + timer_commit.total () +

View file

@ -117,7 +117,7 @@ public:
Timer timer_init; Timer timer_init;
Timer timer_load; Timer timer_load;
Timer timer_synch; Timer timer_sync;
Timer timer_gc; Timer timer_gc;
Timer timer_filter; Timer timer_filter;
Timer timer_commit; Timer timer_commit;

View file

@ -25,13 +25,13 @@
// //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// TODO Localize new messages, when they stabilize.
#define L10N // Localization complete. #define L10N // Localization complete.
#include <iostream> #include <iostream>
#include <inttypes.h> #include <inttypes.h>
#include <Context.h> #include <Context.h>
#include <Socket.h> #include <Socket.h>
#include <Color.h>
#include <text.h> #include <text.h>
#include <i18n.h> #include <i18n.h>
#include <CmdSync.h> #include <CmdSync.h>
@ -52,6 +52,7 @@ CmdSync::CmdSync ()
int CmdSync::execute (std::string& output) int CmdSync::execute (std::string& output)
{ {
int status = 0; int status = 0;
context.timer_sync.start ();
// If no server is set up, quit. // If no server is set up, quit.
std::string connection = context.config.get ("taskd.server"); std::string connection = context.config.get ("taskd.server");
@ -75,36 +76,49 @@ int CmdSync::execute (std::string& output)
if (backlog.exists ()) if (backlog.exists ())
backlog.read (payload); backlog.read (payload);
// Count the number of tasks being uploaded.
int upload_count = 0;
{
std::vector <std::string> lines;
split (lines, payload, "\n");
std::vector <std::string>::iterator i;
for (i = lines.begin (); i != lines.end (); ++i)
if ((*i)[0] == '[')
++upload_count;
}
// Send 'sync' + payload. // Send 'sync' + payload.
Msg request, response; Msg request;
request.set ("protocol", "v1"); request.set ("protocol", "v1");
request.set ("type", "sync"); request.set ("type", "sync");
request.set ("org", credentials[0]); request.set ("org", credentials[0]);
request.set ("user", credentials[1]); request.set ("user", credentials[1]);
request.set ("key", credentials[2]); request.set ("key", credentials[2]);
// TODO Add the other header fields. // TODO Add the other necessary header fields.
request.setPayload (payload); request.setPayload (payload);
std::cout << "# request:\n"
<< request.serialize ();
context.debug ("sync with " + connection); std::cout << format (STRING_CMD_SYNC_PROGRESS, connection)
<< "\n";
Msg response;
if (send (connection, request, response)) if (send (connection, request, response))
{ {
std::cout << "# response:\n"
<< response.serialize ();
std::string code = response.get ("code"); std::string code = response.get ("code");
if (code == "200") if (code == "200")
{ {
Color colorAdded (context.config.get ("color.sync.added"));
Color colorChanged (context.config.get ("color.sync.changed"));
int download_count = 0;
payload = response.getPayload (); payload = response.getPayload ();
std::vector <std::string> lines; std::vector <std::string> lines;
split (lines, payload, '\n'); split (lines, payload, '\n');
// Load all tasks.
// TODO This is not necessary if only a synch key was received. // TODO This is not necessary if only a synch key was received.
// TODO Furthermore, 'all' is not used. // Load all tasks.
std::vector <Task> all = context.tdb2.all_tasks (); context.tdb2.all_tasks ();
std::string synch_key = ""; std::string synch_key = "";
std::vector <std::string>::iterator line; std::vector <std::string>::iterator line;
@ -112,6 +126,8 @@ int CmdSync::execute (std::string& output)
{ {
if ((*line)[0] == '[') if ((*line)[0] == '[')
{ {
++download_count;
Task from_server (*line); Task from_server (*line);
std::string uuid = from_server.get ("uuid"); std::string uuid = from_server.get ("uuid");
@ -119,19 +135,21 @@ int CmdSync::execute (std::string& output)
Task dummy; Task dummy;
if (context.tdb2.get (uuid, dummy)) if (context.tdb2.get (uuid, dummy))
{ {
std::cout << " modify " std::cout << " "
<< uuid << colorChanged.colorize (
<< " '" format (STRING_CMD_SYNC_MOD,
<< from_server.get ("description") uuid,
<< "'\n"; from_server.get ("description")))
<< "\n";
context.tdb2.modify (from_server, false); context.tdb2.modify (from_server, false);
} }
else else
{ {
std::cout << " add " std::cout << " "
<< uuid << colorAdded.colorize (
<< " '" format (STRING_CMD_SYNC_ADD,
<< from_server.get ("description") uuid,
from_server.get ("description")))
<< "'\n"; << "'\n";
context.tdb2.add (from_server, false); context.tdb2.add (from_server, false);
} }
@ -158,28 +176,31 @@ int CmdSync::execute (std::string& output)
// Commit all changes. // Commit all changes.
context.tdb2.commit (); context.tdb2.commit ();
context.footnote ("Sync successful."); // Present a clear status message.
// TODO Sync successful. 2 tasks uploaded. if (upload_count == 0 && download_count == 0)
// TODO Sync successful. 4 tasks downloaded. context.footnote (STRING_CMD_SYNC_SUCCESS0);
// TODO Sync successful. 2 tasks uploaded, 4 tasks downloaded. else if (upload_count == 0 && download_count > 0)
context.footnote (format (STRING_CMD_SYNC_SUCCESS2, download_count));
else if (upload_count > 0 && download_count == 0)
context.footnote (format (STRING_CMD_SYNC_SUCCESS1, upload_count));
else if (upload_count > 0 && download_count > 0)
context.footnote (format (STRING_CMD_SYNC_SUCCESS3, upload_count, download_count));
} }
} }
else if (code == "201") else if (code == "201")
{ {
context.footnote ("Sync successful. No updates required."); context.footnote (STRING_CMD_SYNC_SUCCESS_NOP);
} }
else if (code == "430") else if (code == "430")
{ {
context.error ("Sync failed. Either your credentials are incorrect, or " context.error (STRING_CMD_SYNC_FAIL_ACCOUNT);
"your Task Server account is not enabled.");
status = 2; status = 2;
} }
else else
{ {
context.error ("Sync failed. The Task Server returned error: " + context.error (format (STRING_CMD_SYNC_FAIL_ERROR,
code + code,
" " + response.get ("status")));
response.get ("status"));
status = 2; status = 2;
} }
@ -200,12 +221,14 @@ int CmdSync::execute (std::string& output)
// - Wrong port // - Wrong port
// - Firewall // - Firewall
// - Network error // - Network error
// - No signal/cable
else else
{ {
context.error ("Sync failed. Could not connect to the Task Server."); context.error (STRING_CMD_SYNC_FAIL_CONNECT);
status = 1; status = 1;
} }
context.timer_sync.stop ();
return status; return status;
} }
@ -217,7 +240,7 @@ bool CmdSync::send (
{ {
std::string::size_type colon = to.find (':'); std::string::size_type colon = to.find (':');
if (colon == std::string::npos) if (colon == std::string::npos)
throw std::string ("ERROR: Malformed configuration setting '") + to + "'"; throw format (STRING_CMD_SYNC_BAD_SERVER, to);
std::string server = to.substr (0, colon); std::string server = to.substr (0, colon);
int port = strtoimax (to.substr (colon + 1).c_str (), NULL, 10); int port = strtoimax (to.substr (colon + 1).c_str (), NULL, 10);

View file

@ -413,6 +413,18 @@
#define STRING_CMD_SYNC_USAGE "Synchronizes data with the Task Server" #define STRING_CMD_SYNC_USAGE "Synchronizes data with the Task Server"
#define STRING_CMD_SYNC_NO_SERVER "Task Server is not configured." #define STRING_CMD_SYNC_NO_SERVER "Task Server is not configured."
#define STRING_CMD_SYNC_BAD_CRED "Task Server credentials malformed." #define STRING_CMD_SYNC_BAD_CRED "Task Server credentials malformed."
#define STRING_CMD_SYNC_ADD " add {1} '{2}'"
#define STRING_CMD_SYNC_MOD "modify {1} '{2}'"
#define STRING_CMD_SYNC_PROGRESS "Syncing with {1}"
#define STRING_CMD_SYNC_SUCCESS0 "Sync successful."
#define STRING_CMD_SYNC_SUCCESS1 "Sync successful. {1} changes uploaded."
#define STRING_CMD_SYNC_SUCCESS2 "Sync successful. {1} changes downloaded."
#define STRING_CMD_SYNC_SUCCESS3 "Sync successful. {1} changes uploaded, {2} changes downloaded."
#define STRING_CMD_SYNC_SUCCESS_NOP "Sync successful. No changes."
#define STRING_CMD_SYNC_FAIL_ACCOUNT "Sync failed. Either your credentials are incorrect, or your Task Server account is not enabled."
#define STRING_CMD_SYNC_FAIL_ERROR "Sync failed. The Task Server returned error: {1} {2}"
#define STRING_CMD_SYNC_FAIL_CONNECT "Sync failed. Could not connect to the Task Server."
#define STRING_CMD_SYNC_BAD_SERVER "Sync failed. Malformed configuration setting '{1}'"
#define STRING_CMD_DIAG_USAGE "Platform, build and environment details" #define STRING_CMD_DIAG_USAGE "Platform, build and environment details"
#define STRING_CMD_DIAG_PLATFORM "Platform" #define STRING_CMD_DIAG_PLATFORM "Platform"
#define STRING_CMD_DIAG_UNKNOWN "<unknown>" #define STRING_CMD_DIAG_UNKNOWN "<unknown>"