[hyena] Tweak metrics



commit d95b99f4cfbee23ab9cfb2553bea00365b753641
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Feb 12 05:02:31 2010 -0800

    Tweak metrics

 src/Hyena/Hyena.Metrics/Metric.cs             |   14 +++++++---
 src/Hyena/Hyena.Metrics/MetricsCollection.cs  |   34 ++++++++++++++++++++----
 src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs |    2 +-
 src/Hyena/Hyena/ApplicationContext.cs         |    2 +
 4 files changed, 41 insertions(+), 11 deletions(-)
---
diff --git a/src/Hyena/Hyena.Metrics/Metric.cs b/src/Hyena/Hyena.Metrics/Metric.cs
index 1a7502d..3d26ab8 100644
--- a/src/Hyena/Hyena.Metrics/Metric.cs
+++ b/src/Hyena/Hyena.Metrics/Metric.cs
@@ -26,24 +26,30 @@
 
 using System;
 using System.Collections.Generic;
+using System.Reflection;
 
 namespace Hyena.Metrics
 {
-    public sealed class Metric
+    public sealed class Metric : IDisposable
     {
         public string Category { get; private set; }
         public string Name { get; private set; }
-        public bool IsEventDriven { get; set; }
+        public bool IsEventDriven { get; private set; }
 
-        private Func<object> sample_func;
         private ISampleStore store;
+        private Func<object> sample_func;
 
-        internal Metric (string category, string name, ISampleStore store, Func<object> sampleFunc)
+        internal Metric (string category, string name, ISampleStore store, Func<object> sampleFunc, bool isEventDriven)
         {
             Category = category;
             Name = name;
             this.store = store;
             sample_func = sampleFunc;
+            IsEventDriven = isEventDriven;
+        }
+
+        public void Dispose ()
+        {
         }
 
         public void TakeSample ()
diff --git a/src/Hyena/Hyena.Metrics/MetricsCollection.cs b/src/Hyena/Hyena.Metrics/MetricsCollection.cs
index dee59be..33ca22c 100644
--- a/src/Hyena/Hyena.Metrics/MetricsCollection.cs
+++ b/src/Hyena/Hyena.Metrics/MetricsCollection.cs
@@ -28,30 +28,52 @@ using System;
 using System.Linq;
 using System.Collections.Generic;
 using System.Text;
+using System.Reflection;
 
 namespace Hyena.Metrics
 {
-    public sealed class MetricsCollection : List<Metric>
+    public sealed class MetricsCollection : List<Metric>, IDisposable
     {
-        public string UniqueUserId { get; private set; }
+        public string AnonymousUserId { get; private set; }
         public ISampleStore Store { get; private set; }
 
         public MetricsCollection (string uniqueUserId, ISampleStore store)
         {
-            UniqueUserId = uniqueUserId;
+            AnonymousUserId = uniqueUserId;
             Store = store;
         }
 
-        public void Add (string category, string metricName, Func<object> sampleFunc)
+        public Metric Add (string category, string metricName, Func<object> sampleFunc)
         {
-            Add (new Metric (category, metricName, Store, sampleFunc));
+            return Add (category, metricName, sampleFunc, false);
+        }
+
+        public Metric Add (string category, string metricName, Func<object> sampleFunc, bool isEventDriven)
+        {
+            var metric = new Metric (category, metricName, Store, sampleFunc, isEventDriven);
+            Add (metric);
+
+            if (!metric.IsEventDriven) {
+                metric.TakeSample ();
+            }
+
+            return metric;
+        }
+
+        public void Dispose ()
+        {
+            foreach (var m in this) {
+                m.Dispose ();
+            }
+            Clear ();
         }
 
         public override string ToString ()
         {
             var sb = new StringBuilder ();
 
-            sb.AppendFormat ("ID: {0}\n", UniqueUserId);
+            // 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) {
diff --git a/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs b/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs
index 88ce8c4..87594c9 100644
--- a/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs
+++ b/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs
@@ -44,7 +44,7 @@ namespace Hyena.Tests
         public void MetricsCollection ()
         {
             var metrics = new MetricsCollection ("myuniqueid", new MemorySampleStore ());
-            Assert.AreEqual ("myuniqueid", metrics.UniqueUserId);
+            Assert.AreEqual ("myuniqueid", metrics.AnonymousUserId);
 
             metrics.AddDefaults ();
             Assert.IsTrue (metrics.Count > 0);
diff --git a/src/Hyena/Hyena/ApplicationContext.cs b/src/Hyena/Hyena/ApplicationContext.cs
index dcae020..c215028 100644
--- a/src/Hyena/Hyena/ApplicationContext.cs
+++ b/src/Hyena/Hyena/ApplicationContext.cs
@@ -38,6 +38,8 @@ namespace Hyena
 
     public static class ApplicationContext
     {
+        public static readonly DateTime StartedAt = DateTime.Now;
+
         static ApplicationContext ()
         {
             Log.Debugging = Debugging;



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