metrics: add to_console method to pretty print metrics result in console
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 7 Mar 2014 06:17:08 +0000 (01:17 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 7 Mar 2014 06:17:08 +0000 (01:17 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/metrics/inheritance_metrics.nit
src/metrics/mclasses_metrics.nit
src/metrics/metrics_base.nit
src/metrics/mmodules_metrics.nit

index 8c799ac..4ba1dc7 100644 (file)
@@ -74,27 +74,9 @@ private class InheritanceMetricsPhase
                                mmodules.add_all(mgroup.mmodules)
                                mclasses.add_all(mod_mclasses)
                                cmetrics.collect(new HashSet[MClass].from(mod_mclasses))
-                               for metric in cmetrics.metrics do
-                                       if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
+                               cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
                                hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules))
-                               for metric in hmetrics.metrics do
-                                       if metric isa FloatMetric then
-                                               print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
+                               hmetrics.to_console(1, not toolcontext.opt_nocolors.value)
                        end
                end
                if not mclasses.is_empty then
@@ -102,28 +84,10 @@ private class InheritanceMetricsPhase
                        print toolcontext.format_h2("\n ## global metrics")
                        cmetrics.clear
                        cmetrics.collect(mclasses)
-                       for metric in cmetrics.metrics do
-                               if metric isa IntMetric then
-                                       print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                       print toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
+                       cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
                        hmetrics.clear
                        hmetrics.collect(mmodules)
-                       for metric in hmetrics.metrics do
-                               if metric isa FloatMetric then
-                                       print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                       print toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
+                       hmetrics.to_console(1, not toolcontext.opt_nocolors.value)
                end
        end
 end
index 58b0200..3376169 100644 (file)
@@ -61,38 +61,19 @@ private class MClassesMetricsPhase
 
                                # Scalar metrics
                                print toolcontext.format_h3("  `- group {mgroup.full_name}")
-
                                var mod_mclasses = new HashSet[MClass]
                                for mmodule in mgroup.mmodules do mod_mclasses.add_all(mmodule.intro_mclasses)
                                if mod_mclasses.is_empty then continue
                                mclasses.add_all(mod_mclasses)
                                metrics.collect(new HashSet[MClass].from(mod_mclasses))
-                               for metric in metrics.metrics do
-                                       if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
+                               metrics.to_console(1, not toolcontext.opt_nocolors.value)
                        end
                end
                if not mclasses.is_empty then
                        # Global metrics
                        print toolcontext.format_h2("\n ## global metrics")
                        metrics.collect(mclasses)
-                       for metric in metrics.metrics do
-                               if metric isa IntMetric then
-                                       print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                       print toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
+                       metrics.to_console(1, not toolcontext.opt_nocolors.value)
                end
        end
 end
index c5c03aa..ef85ca5 100644 (file)
@@ -165,6 +165,23 @@ interface Metric
 
        # The values average
        fun avg: Float is abstract
+
+       # Pretty print the metric results in console
+       fun to_console(indent: Int, colors: Bool) do
+               var max = self.max
+               var min = self.min
+               if colors then
+                       print "{"\t" * indent}{name}: {desc}".green
+                       print "{"\t" * indent}  avg: {avg}".light_gray
+                       print "{"\t" * indent}  max: {max} ({self[max]})".light_gray
+                       print "{"\t" * indent}  min: {min} ({self[min]})".light_gray
+               else
+                       print "{"\t" * indent}{name}: {desc}"
+                       print "{"\t" * indent}  avg: {avg}"
+                       print "{"\t" * indent}  max: {max} ({self[max]})"
+                       print "{"\t" * indent}  min: {min} ({self[min]})"
+               end
+       end
 end
 
 # A Metric that collects integer data
@@ -272,4 +289,9 @@ class MetricSet
                for metric in metrics do metric.collect(elements)
        end
 
+       # Pretty print the resuls in console
+       fun to_console(indent: Int, colors: Bool) do
+               for metric in metrics do metric.to_console(indent, colors)
+       end
+
 end
index f6a169a..72bf41d 100644 (file)
@@ -44,25 +44,14 @@ private class MModulesMetricsPhase
                for mproject in model.mprojects do
 
                        print  toolcontext.format_h2("\n ## project {mproject}")
-
                        for mgroup in mproject.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
                                print  toolcontext.format_h3("  `- group {mgroup.full_name}")
-
                                mmodules.add_all(mgroup.mmodules)
                                metrics.collect(new HashSet[MModule].from(mgroup.mmodules))
-                               for metric in metrics.metrics do
-                                       if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{metric.name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print  toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print  toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
+                               metrics.to_console(1, not toolcontext.opt_nocolors.value)
                        end
                end
                if not mmodules.is_empty then
@@ -70,16 +59,7 @@ private class MModulesMetricsPhase
                        print  toolcontext.format_h2("\n ## global metrics")
                        metrics.clear
                        metrics.collect(mmodules)
-                       for metric in metrics.metrics do
-                               if metric isa IntMetric then
-                                       print toolcontext.format_h4( "\t{metric.name}: {metric.desc}")
-                                       print  toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                               print  toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print  toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
+                       metrics.to_console(1, not toolcontext.opt_nocolors.value)
                end
        end
 end