src: Updated uses of CSV library
[nit.git] / src / metrics / mmodules_metrics.nit
index f6a169a..dc944d8 100644 (file)
 # Collect common metrics about modules
 module mmodules_metrics
 
-import model
 import metrics_base
-import phase
-import frontend
 
 redef class ToolContext
-       var mmodules_metrics_phase = new MModulesMetricsPhase(self, null)
+       var mmodules_metrics_phase: Phase = new MModulesMetricsPhase(self, null)
 end
 
 # Extract metrics about modules from the model.
 private class MModulesMetricsPhase
        super Phase
-       redef fun process_mainmodule(mainmodule)
+       redef fun process_mainmodule(mainmodule, given_mmodules)
        do
                if not toolcontext.opt_mmodules.value and not toolcontext.opt_all.value then return
+               var csv = toolcontext.opt_csv.value
+               var out = "{toolcontext.opt_dir.value or else "metrics"}/mmodules"
+               out.mkdir
 
                print toolcontext.format_h1("\n# MModules metrics")
 
@@ -41,28 +41,19 @@ private class MModulesMetricsPhase
 
                var model = toolcontext.modelbuilder.model
                var mmodules = new HashSet[MModule]
-               for mproject in model.mprojects do
+               for mpackage in model.mpackages do
 
-                       print  toolcontext.format_h2("\n ## project {mproject}")
-
-                       for mgroup in mproject.mgroups do
+                       print  toolcontext.format_h2("\n ## package {mpackage}")
+                       for mgroup in mpackage.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.clear
                                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)
+                               if csv then metrics.to_csv.write_to_file("{out}/{mgroup}.csv")
                        end
                end
                if not mmodules.is_empty then
@@ -70,16 +61,8 @@ 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)
+                       if csv then metrics.to_csv.write_to_file("{out}/summary.csv")
                end
        end
 end