[hyena] Reorganize, simplify



commit 1bea579949cd0358e71b6b5882fd28e650b228f5
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Feb 12 15:41:42 2010 -0800

    Reorganize, simplify
    
    Get rid of categories, replaced by a by-convention / separated name
    string.  Add Metric ctor taking a single sample value, so we don't have
    to create delegates for such metrics.

 src/Hyena/Hyena.Metrics/DbSampleStore.cs     |    2 +-
 src/Hyena/Hyena.Metrics/MemorySampleStore.cs |    2 +-
 src/Hyena/Hyena.Metrics/Metric.cs            |   24 ++++-----
 src/Hyena/Hyena.Metrics/MetricsCollection.cs |   68 ++++++++++---------------
 src/Hyena/Hyena.Metrics/Sample.cs            |    2 +-
 5 files changed, 40 insertions(+), 58 deletions(-)
---
diff --git a/src/Hyena/Hyena.Metrics/DbSampleStore.cs b/src/Hyena/Hyena.Metrics/DbSampleStore.cs
index dc37d35..ff6a9f3 100644
--- a/src/Hyena/Hyena.Metrics/DbSampleStore.cs
+++ b/src/Hyena/Hyena.Metrics/DbSampleStore.cs
@@ -46,7 +46,7 @@ namespace Hyena.Metrics
 
         public IEnumerable<Sample> GetFor (Metric metric)
         {
-            return FetchAllMatching ("MetricName = ? ORDER BY Stamp ASC", metric.FullName);
+            return FetchAllMatching ("MetricName = ? ORDER BY Stamp ASC", metric.Name);
         }
 
         public void Clear ()
diff --git a/src/Hyena/Hyena.Metrics/MemorySampleStore.cs b/src/Hyena/Hyena.Metrics/MemorySampleStore.cs
index 274fe1c..00ad1a4 100644
--- a/src/Hyena/Hyena.Metrics/MemorySampleStore.cs
+++ b/src/Hyena/Hyena.Metrics/MemorySampleStore.cs
@@ -46,7 +46,7 @@ namespace Hyena.Metrics
 
         public IEnumerable<Sample> GetFor (Metric metric)
         {
-            return this[metric.FullName];
+            return this[metric.Name];
         }
     }
 }
diff --git a/src/Hyena/Hyena.Metrics/Metric.cs b/src/Hyena/Hyena.Metrics/Metric.cs
index 07997cf..773a5bd 100644
--- a/src/Hyena/Hyena.Metrics/Metric.cs
+++ b/src/Hyena/Hyena.Metrics/Metric.cs
@@ -32,30 +32,26 @@ namespace Hyena.Metrics
 {
     public sealed class Metric : IDisposable
     {
-        public string FullName {
-            get { return String.Format ("{0}.{1}", Category, Name); }
-        }
-
-        public string Category { get; private set; }
         public string Name { get; private set; }
-        public bool IsEventDriven { get; private set; }
+        public bool CanTakeSample { get { return sample_func != null; } }
 
         private ISampleStore store;
         private Func<object> sample_func;
 
-        internal Metric (string category, string name, ISampleStore store, Func<object> sampleFunc, bool isEventDriven)
+        internal Metric (string name, ISampleStore store)
         {
-            Category = category;
             Name = name;
             this.store = store;
+        }
+
+        internal Metric (string name, ISampleStore store, Func<object> sampleFunc) : this (name, store)
+        {
             sample_func = sampleFunc;
-            IsEventDriven = isEventDriven;
+        }
 
-            if (!isEventDriven) {
-                // Take the sample and forget the delegate so it can be GC'd
-                TakeSample ();
-                sample_func = null;
-            }
+        internal Metric (string name, ISampleStore store, object value) : this (name, store)
+        {
+            PushSample (value);
         }
 
         public void Dispose ()
diff --git a/src/Hyena/Hyena.Metrics/MetricsCollection.cs b/src/Hyena/Hyena.Metrics/MetricsCollection.cs
index d558490..834162f 100644
--- a/src/Hyena/Hyena.Metrics/MetricsCollection.cs
+++ b/src/Hyena/Hyena.Metrics/MetricsCollection.cs
@@ -45,15 +45,24 @@ namespace Hyena.Metrics
             Store = store;
         }
 
-        public Metric Add (string category, string metricName, Func<object> sampleFunc)
+        public Metric Add (string name)
         {
-            return Add (category, metricName, sampleFunc, false);
+            return Add (new Metric (name, Store));
         }
 
-        public Metric Add (string category, string metricName, Func<object> sampleFunc, bool isEventDriven)
+        public Metric Add (string name, object value)
         {
-            var metric = new Metric (category, metricName, Store, sampleFunc, isEventDriven);
-            Add (metric);
+            return Add (new Metric (name, Store, value));
+        }
+
+        public Metric Add (string name, Func<object> sampleFunc)
+        {
+            return Add (new Metric (name, Store, sampleFunc));
+        }
+
+        public new Metric Add (Metric metric)
+        {
+            base.Add (metric);
             return metric;
         }
 
@@ -65,56 +74,33 @@ namespace Hyena.Metrics
             Clear ();
         }
 
-        public override string ToString ()
-        {
-            var sb = new StringBuilder ();
-
-            // TODO handle dates in a culture-invariant manner
-            sb.AppendFormat ("ID: {0}\n", AnonymousUserId);
-            foreach (var category in this.GroupBy<Metric, string> (m => m.Category)) {
-                sb.AppendFormat ("{0}:\n", category.Key);
-                foreach (var metric in category) {
-                    sb.AppendFormat ("  {0}\n", metric.Name);
-                    foreach (var sample in Store.GetFor (metric)) {
-                        sb.AppendFormat ("    {0}\n", sample.Value);
-                    }
-                }
-            }
-
-            return sb.ToString ();
-        }
-
         public string ToJsonString ()
         {
             var report = new Dictionary<string, object> ();
 
             report["ID"] = AnonymousUserId;
-            report["Metrics"] = this.GroupBy<Metric, string> (m => m.Category)
-                .Select (c => {
-                    var d = new Dictionary<string, object> ();
-                    foreach (var metric in c) {
-                        d[metric.Name] = Store.GetFor (metric).Select (s =>
-                            new object [] { s.Stamp, s.Value }
-                        );
-                    }
-                    return d;
-                });
+
+            var metrics = new Dictionary<string, object> ();
+            foreach (var metric in this.OrderBy (m => m.Name)) {
+                metrics[metric.Name] = Store.GetFor (metric).Select (s => new object [] { s.Stamp, s.Value ?? "" });
+            }
+            report["Metrics"] = metrics;
 
             return report.ToJsonString ();
         }
 
         public void AddDefaults ()
         {
-            Add ("Env", "OS Platform",          () => PlatformDetection.SystemName);
-            Add ("Env", "OS Version",           () => System.Environment.OSVersion);
-            Add ("Env", "Processor Count",      () => System.Environment.ProcessorCount);
-            Add ("Env", ".NET Runtime Version", () => System.Environment.Version);
-            Add ("Env", "Debugging",            () => ApplicationContext.Debugging);
-            Add ("Env", "CultureInfo",          () => System.Globalization.CultureInfo.CurrentCulture.Name);
+            Add ("Env/OS Platform",          PlatformDetection.SystemName);
+            Add ("Env/OS Version",           System.Environment.OSVersion);
+            Add ("Env/Processor Count",      System.Environment.ProcessorCount);
+            Add ("Env/.NET Runtime Version", System.Environment.Version);
+            Add ("Env/Debugging",            ApplicationContext.Debugging);
+            Add ("Env/CultureInfo",          System.Globalization.CultureInfo.CurrentCulture.Name);
 
             foreach (var asm in AppDomain.CurrentDomain.GetAssemblies ()) {
                 var name = asm.GetName ();
-                Add ("Assemblies", name.Name, () => name.Version);
+                Add (String.Format ("Assemblies/{0}", name.Name), name.Version);
             }
         }
     }
diff --git a/src/Hyena/Hyena.Metrics/Sample.cs b/src/Hyena/Hyena.Metrics/Sample.cs
index 000989b..d9491c1 100644
--- a/src/Hyena/Hyena.Metrics/Sample.cs
+++ b/src/Hyena/Hyena.Metrics/Sample.cs
@@ -48,7 +48,7 @@ namespace Hyena.Metrics
 
         public Sample (Metric metric, object value)
         {
-            MetricName = metric.FullName;
+            MetricName = metric.Name;
             Stamp = DateTime.Now;
             Value = value == null ? "" : value.ToString ();
         }



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