[banshee] Add some Banshee-specific metrics (version, etc)



commit bc27b852083b49e86b5efaf2b3afc495863fdc35
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Feb 12 05:03:08 2010 -0800

    Add some Banshee-specific metrics (version, etc)

 .../Banshee.Metrics/BansheeMetrics.cs              |   64 ++++++++++++++++----
 .../Banshee.Preferences/PreferenceService.cs       |    5 ++
 2 files changed, 58 insertions(+), 11 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Metrics/BansheeMetrics.cs b/src/Core/Banshee.Services/Banshee.Metrics/BansheeMetrics.cs
index 9f214b3..3a39d99 100644
--- a/src/Core/Banshee.Services/Banshee.Metrics/BansheeMetrics.cs
+++ b/src/Core/Banshee.Services/Banshee.Metrics/BansheeMetrics.cs
@@ -33,12 +33,14 @@ using Hyena.Metrics;
 
 using Banshee.Configuration;
 using Banshee.ServiceStack;
+using System.Reflection;
 
 namespace Banshee.Metrics
 {
     public class BansheeMetrics : IDisposable
     {
         private static BansheeMetrics banshee_metrics;
+        public BansheeMetrics Instance { get { return banshee_metrics; } }
 
         public static void Start ()
         {
@@ -58,15 +60,17 @@ namespace Banshee.Metrics
         }
 
         private MetricsCollection metrics;
+        private string id_key = "AnonymousUsageData.Userid";
+
+        private Metric shutdown, duration;
 
         private BansheeMetrics ()
         {
-            string key = "AnonymousUsageData.Userid";
-            string unique_userid = DatabaseConfigurationClient.Client.Get<string> (key, null);
+            string unique_userid = DatabaseConfigurationClient.Client.Get<string> (id_key, null);
 
             if (unique_userid == null) {
                 unique_userid = System.Guid.NewGuid ().ToString ();
-                DatabaseConfigurationClient.Client.Set<string> (key, unique_userid);
+                DatabaseConfigurationClient.Client.Set<string> (id_key, unique_userid);
             }
 
             metrics = new MetricsCollection (unique_userid, new DbSampleStore (
@@ -74,28 +78,66 @@ namespace Banshee.Metrics
             ));
             metrics.AddDefaults ();
 
-            foreach (var metric in metrics) {
-                metric.TakeSample ();
-            }
+            // TODO add more Banshee-specific metrics
+            Add ("Client",       () => Application.ActiveClient);
+            Add ("BuildHostCpu", () => Application.BuildHostCpu);
+            Add ("BuildHostOS",  () => Application.BuildHostOperatingSystem);
+            Add ("BuildTime",    () => Application.BuildTime);
+            Add ("BuildVendor",  () => Application.BuildVendor);
+            Add ("Version",      () => Application.Version);
+            Add ("StartedAt",    () => ApplicationContext.StartedAt);
 
-            // TODO remove this, just for testing
-            Log.InformationFormat ("Anonymous usage data collected:\n{0}", metrics.ToString ());
+            // TODO add metrics based on assemblies/versions; put in AddDefaults?
+
+            shutdown = Add ("ShutdownAt",  () => DateTime.Now, true);
+            duration = Add ("RunDuration", () => DateTime.Now - ApplicationContext.StartedAt, true);
+            Application.ShutdownRequested += OnShutdownRequested;
 
-            // TODO add Banshee-specific metrics
             // TODO add Mono.Addins extension point for metric providers?
             // TODO schedule sending the data to the server in some timeout?
+
+            // TODO remove this, just for testing
+            Log.InformationFormat ("Anonymous usage data collected:\n{0}", metrics.ToString ());
+        }
+
+        private bool OnShutdownRequested ()
+        {
+            try {
+                shutdown.TakeSample ();
+                duration.TakeSample ();
+            } catch {
+            } finally {
+                return true;
+            }
+        }
+
+        public Metric Add (string name, Func<object> func)
+        {
+            return Add (name, func, null);
+        }
+
+        public Metric Add (string name, Func<object> func, EventInfo triggerEvent)
+        {
+            return metrics.Add ("Banshee", name, func, triggerEvent);
         }
 
         public void Dispose ()
         {
+            // Disconnect from events we're listening to
+            Application.ShutdownRequested -= OnShutdownRequested;
+
             // Delete any collected data
             metrics.Store.Clear ();
+            metrics.Dispose ();
             metrics = null;
+
+            // Forget the user's unique id
+            DatabaseConfigurationClient.Client.Set<string> (id_key, null);
         }
 
         public static SchemaEntry<bool> EnableCollection = new SchemaEntry<bool> (
-            "core", "send_anonymous_usage_data", false,
+            "core", "send_anonymous_usage_data", false, // disabled by default
             Catalog.GetString ("Improve Banshee by sending anonymous usage data"), null
         );
     }
-}
+}
\ No newline at end of file
diff --git a/src/Core/Banshee.Services/Banshee.Preferences/PreferenceService.cs b/src/Core/Banshee.Services/Banshee.Preferences/PreferenceService.cs
index fdba642..bd0e4ee 100644
--- a/src/Core/Banshee.Services/Banshee.Preferences/PreferenceService.cs
+++ b/src/Core/Banshee.Services/Banshee.Preferences/PreferenceService.cs
@@ -73,6 +73,11 @@ namespace Banshee.Preferences
                     Banshee.Metrics.BansheeMetrics.Stop ();
                 }
             };
+
+            // TODO this should probably be elsewhere
+            if (Banshee.Metrics.BansheeMetrics.EnableCollection.Get ()) {
+                Banshee.Metrics.BansheeMetrics.Start ();
+            }
         }
 
         public void RequestWidgetAdapters ()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]