Skip to content

Commit

Permalink
statusicon: add sync progress stats (one repo at a time only for now).
Browse files Browse the repository at this point in the history
…Closes hbons#222
  • Loading branch information
hbons committed Dec 30, 2011
1 parent aaf540a commit eb65f5e
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 21 deletions.
20 changes: 12 additions & 8 deletions SparkleLib/Git/SparkleFetcherGit.cs
Expand Up @@ -93,7 +93,10 @@ public override bool Fetch ()

double percentage = 1.0;
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);


DateTime last_change = DateTime.Now;
TimeSpan change_interval = new TimeSpan (0, 0, 0, 1);

while (!this.git.StandardError.EndOfStream) {
string line = this.git.StandardError.ReadLine ();
Match match = progress_regex.Match (line);
Expand All @@ -107,28 +110,29 @@ public override bool Fetch ()
// the "Receiving objects" stage which we count as the last 80%
if (line.Contains ("|"))
// "Receiving objects" stage
number = (number / 100 * 75 + 20);
number = (number / 100 * 80 + 20);
else
// "Compressing objects" stage
number = (number / 100 * 20);
}

if (number >= percentage) {
percentage = number;

// FIXME: for some reason it doesn't go above 95%
base.OnProgressChanged (percentage);

if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) {
base.OnProgressChanged (percentage);
last_change = DateTime.Now;
}
}

System.Threading.Thread.Sleep (100);
}

this.git.WaitForExit ();

SparkleHelpers.DebugInfo ("Git", "Exit code " + this.git.ExitCode.ToString ());


if (this.git.ExitCode != 0) {
return false;

} else {
InstallConfiguration ();
InstallExcludeRules ();
Expand Down
60 changes: 58 additions & 2 deletions SparkleLib/Git/SparkleRepoGit.cs
Expand Up @@ -172,11 +172,66 @@ public override bool SyncUp ()
Commit (message);
}

SparkleGit git = new SparkleGit (LocalPath, "push origin master");

SparkleGit git = new SparkleGit (LocalPath,
"push --progress " + // Redirects progress stats to standarderror
"origin master");

git.StartInfo.RedirectStandardError = true;
git.Start ();
git.StandardOutput.ReadToEnd ();

double percentage = 1.0;
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);

DateTime last_change = DateTime.Now;
TimeSpan change_interval = new TimeSpan (0, 0, 0, 1);

while (!git.StandardError.EndOfStream) {
string line = git.StandardError.ReadLine ();
Match match = progress_regex.Match (line);
string speed = "";
double number = 0.0;

if (match.Success) {
number = double.Parse (match.Groups [1].Value);

// The cloning progress consists of two stages: the "Compressing
// objects" stage which we count as 20% of the total progress, and
// the "Writing objects" stage which we count as the last 80%
if (line.StartsWith ("Compressing")) {
// "Compressing objects" stage
number = (number / 100 * 20);

} else {
// "Writing objects" stage
number = (number / 100 * 80 + 20);

if (line.Contains ("|")) {
speed = line.Substring (line.IndexOf ("|") + 1).Trim ();
speed = speed.Replace (", done.", "").Trim ();
speed = speed.Replace ("i", "");
speed = speed.Replace ("KB/s", "ᴋʙ/s");
speed = speed.Replace ("MB/s", "ᴍʙ/s");
}
}
}

if (number >= percentage) {
percentage = number;

if (percentage == 100.0)
percentage = 99.0;

if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) {
base.OnSyncProgressChanged (percentage, speed);
last_change = DateTime.Now;
}
}
}

git.WaitForExit ();


if (git.ExitCode == 0)
return true;
else
Expand Down Expand Up @@ -239,6 +294,7 @@ public override bool SyncDown ()
if (value) {
if (!File.Exists (unsynced_file_path))
File.Create (unsynced_file_path).Close ();

} else {
File.Delete (unsynced_file_path);
}
Expand Down
47 changes: 41 additions & 6 deletions SparkleLib/SparkleRepoBase.cs
Expand Up @@ -44,11 +44,13 @@ public abstract class SparkleRepoBase {
private TimeSpan poll_interval;
private System.Timers.Timer local_timer = new System.Timers.Timer () { Interval = 0.25 * 1000 };
private System.Timers.Timer remote_timer = new System.Timers.Timer () { Interval = 10 * 1000 };
private DateTime last_poll = DateTime.Now;
private List<double> sizebuffer = new List<double> ();
private bool has_changed = false;
private Object change_lock = new Object ();
private Object watch_lock = new Object ();
private DateTime last_poll = DateTime.Now;
private List<double> sizebuffer = new List<double> ();
private bool has_changed = false;
private Object change_lock = new Object ();
private Object watch_lock = new Object ();
private double progress_percentage = 0.0;
private string progress_speed = "";

protected SparkleListenerBase listener;
protected SyncStatus status;
Expand All @@ -74,6 +76,9 @@ public abstract class SparkleRepoBase {
public delegate void SyncStatusChangedEventHandler (SyncStatus new_status);
public event SyncStatusChangedEventHandler SyncStatusChanged;

public delegate void SyncProgressChangedEventHandler (double percentage, string speed);
public event SyncProgressChangedEventHandler SyncProgressChanged;

public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set);
public event NewChangeSetEventHandler NewChangeSet;

Expand Down Expand Up @@ -156,6 +161,20 @@ public SparkleRepoBase (string path, SparkleBackend backend)
}


public double ProgressPercentage {
get {
return this.progress_percentage;
}
}


public string ProgressSpeed {
get {
return this.progress_speed;
}
}


public virtual string [] UnsyncedFilePaths {
get {
return new string [0];
Expand Down Expand Up @@ -297,7 +316,7 @@ public void CreateListener ()
}


private bool IsSyncing {
public bool IsSyncing {
get {
return (Status == SyncStatus.SyncUp ||
Status == SyncStatus.SyncDown ||
Expand Down Expand Up @@ -461,6 +480,9 @@ private void SyncUpBase ()
} finally {
this.remote_timer.Start ();
EnableWatching ();

this.progress_percentage = 0.0;
this.progress_speed = "";
}
}

Expand Down Expand Up @@ -526,6 +548,9 @@ private void SyncDownBase ()

this.remote_timer.Start ();
EnableWatching ();

this.progress_percentage = 0.0;
this.progress_speed = "";
}


Expand Down Expand Up @@ -596,6 +621,16 @@ public void AddNote (string revision, string note)
}


protected void OnSyncProgressChanged (double progress_percentage, string progress_speed)
{
this.progress_percentage = progress_percentage;
this.progress_speed = progress_speed;

if (SyncProgressChanged != null)
SyncProgressChanged (progress_percentage, progress_speed);
}


// Creates a SHA-1 hash of input
private string SHA1 (string s)
{
Expand Down
5 changes: 4 additions & 1 deletion SparkleShare/Mac/SparkleStatusIcon.cs
Expand Up @@ -110,7 +110,10 @@ public SparkleStatusIcon () : base ()

case IconState.Syncing:

StateText = _("Syncing…");
StateText = _("Syncing… " +
Controller.ProgressPercentage + "% " +
Controller.ProgressSpeed);

StateMenuItem.Title = StateText;

if (!Animation.Enabled)
Expand Down
18 changes: 14 additions & 4 deletions SparkleShare/SparkleControllerBase.cs
Expand Up @@ -37,6 +37,9 @@ public abstract class SparkleControllerBase {
public List <SparkleRepoBase> Repositories;
public readonly string SparklePath = SparkleConfig.DefaultConfig.FoldersPath;

public double ProgressPercentage = 0.0;
public string ProgressSpeed = "";

public event OnQuitWhileSyncingHandler OnQuitWhileSyncing;
public delegate void OnQuitWhileSyncingHandler ();

Expand Down Expand Up @@ -586,11 +589,11 @@ private void AddRepository (string folder_path)
};

repo.SyncStatusChanged += delegate (SyncStatus status) {
/* if (status == SyncStatus.SyncUp) {
foreach (string path in repo.UnsyncedFilePaths)
Console.WriteLine (path);
if (status == SyncStatus.Idle) {
ProgressPercentage = 0.0;
ProgressSpeed = "";
}
*/

if (status == SyncStatus.Idle ||
status == SyncStatus.SyncUp ||
status == SyncStatus.SyncDown ||
Expand All @@ -600,6 +603,13 @@ private void AddRepository (string folder_path)
}
};

repo.SyncProgressChanged += delegate (double percentage, string speed) {
ProgressPercentage = percentage;
ProgressSpeed = speed;

UpdateState ();
};

repo.ChangesDetected += delegate {
UpdateState ();
};
Expand Down
16 changes: 16 additions & 0 deletions SparkleShare/SparkleStatusIconController.cs
Expand Up @@ -58,13 +58,27 @@ public class SparkleStatusIconController {
}


public int ProgressPercentage {
get {
return (int) Program.Controller.ProgressPercentage;
}
}

public string ProgressSpeed {
get {
return Program.Controller.ProgressSpeed;
}
}


public SparkleStatusIconController ()
{
Program.Controller.FolderListChanged += delegate {
if (UpdateMenuEvent != null)
UpdateMenuEvent (CurrentState);
};


Program.Controller.OnIdle += delegate {
if (CurrentState != IconState.Error)
CurrentState = IconState.Idle;
Expand All @@ -73,13 +87,15 @@ public SparkleStatusIconController ()
UpdateMenuEvent (CurrentState);
};


Program.Controller.OnSyncing += delegate {
CurrentState = IconState.Syncing;

if (UpdateMenuEvent != null)
UpdateMenuEvent (IconState.Syncing);
};


Program.Controller.OnError += delegate {
CurrentState = IconState.Error;

Expand Down

0 comments on commit eb65f5e

Please sign in to comment.