-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from k1LoW/coverage
Add command `ndiag coverage`
- Loading branch information
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright © 2021 Ken'ichiro Oyama <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/goccy/go-json" | ||
"github.com/k1LoW/ndiag/coverage" | ||
"github.com/labstack/gommon/color" | ||
"github.com/mattn/go-runewidth" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cformat string | ||
|
||
// coverageCmd represents the coverage command | ||
var coverageCmd = &cobra.Command{ | ||
Use: "coverage", | ||
Short: "measure document coverage", | ||
Long: `measure document coverage.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, err := newConfig() | ||
if err != nil { | ||
return err | ||
} | ||
cover := coverage.Measure(cfg) | ||
switch cformat { | ||
case "json": | ||
encoder := json.NewEncoder(os.Stdout) | ||
encoder.SetIndent("", " ") | ||
err := encoder.Encode(cover) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
max := runewidth.StringWidth("All Elements") | ||
fmtName := fmt.Sprintf("%%-%ds", max) | ||
fmt.Printf("%s %s\n", color.White(fmt.Sprintf(fmtName, "Elements"), color.B), color.White("Coverage", color.B)) | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "All Elements"), cover.Coverage, cover.Covered, cover.Total) | ||
if cover.Views != nil { | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "Views"), cover.Views.Coverage, cover.Views.Covered, cover.Views.Total) | ||
} | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "Nodes"), cover.Nodes.Coverage, cover.Nodes.Covered, cover.Nodes.Total) | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "Components"), cover.Components.Coverage, cover.Components.Covered, cover.Components.Total) | ||
if cover.Relations != nil { | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "Relations"), cover.Relations.Coverage, cover.Relations.Covered, cover.Relations.Total) | ||
} | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "Layers"), cover.Layers.Coverage, cover.Layers.Covered, cover.Layers.Total) | ||
if cover.Labels != nil { | ||
fmt.Printf("%s %g%% (%d/%d)\n", fmt.Sprintf(fmtName, "Labels"), cover.Labels.Coverage, cover.Labels.Covered, cover.Labels.Total) | ||
} | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(coverageCmd) | ||
coverageCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path") | ||
coverageCmd.Flags().StringVarP(&cformat, "format", "t", "", "output format") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package coverage | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/k1LoW/ndiag/config" | ||
) | ||
|
||
type Coverage struct { | ||
Name string | ||
Coverage float64 | ||
Views *CoverageByElement `json:"views,omitempty"` | ||
Nodes *CoverageByElement | ||
Components *CoverageByElement | ||
Relations *CoverageByElement `json:"relations,omitempty"` | ||
Layers *CoverageByElement | ||
Labels *CoverageByElement `json:"labels,omitempty"` | ||
Covered int | ||
Total int | ||
} | ||
|
||
type CoverageByElement struct { | ||
Coverage float64 | ||
Covered int | ||
Total int | ||
} | ||
|
||
// Measure coverage | ||
func Measure(cfg *config.Config) *Coverage { | ||
cover := &Coverage{ | ||
Name: cfg.Name, | ||
Nodes: &CoverageByElement{}, | ||
Components: &CoverageByElement{}, | ||
Layers: &CoverageByElement{}, | ||
} | ||
// index | ||
cover.Total += 1 | ||
if cfg.Desc != "" { | ||
cover.Covered += 1 | ||
} | ||
|
||
// views | ||
if !cfg.HideViews { | ||
cover.Views = &CoverageByElement{} | ||
for _, v := range cfg.Views { | ||
cover.Views.Total += 1 | ||
if v.Desc != "" { | ||
cover.Views.Covered += 1 | ||
} | ||
} | ||
cover.Views.Coverage = round(float64(cover.Views.Covered) / float64(cover.Views.Total) * 100) | ||
cover.Total += cover.Views.Total | ||
cover.Covered += cover.Views.Covered | ||
} | ||
|
||
// nodes | ||
for _, n := range cfg.Nodes { | ||
cover.Nodes.Total += 1 | ||
if n.Desc != "" { | ||
cover.Nodes.Covered += 1 | ||
} | ||
} | ||
cover.Nodes.Coverage = round(float64(cover.Nodes.Covered) / float64(cover.Nodes.Total) * 100) | ||
cover.Total += cover.Nodes.Total | ||
cover.Covered += cover.Nodes.Covered | ||
|
||
// components | ||
for _, c := range cfg.Components() { | ||
cover.Components.Total += 1 | ||
if c.Desc != "" { | ||
cover.Components.Covered += 1 | ||
} | ||
} | ||
cover.Components.Coverage = round(float64(cover.Components.Covered) / float64(cover.Components.Total) * 100) | ||
cover.Total += cover.Components.Total | ||
cover.Covered += cover.Views.Covered | ||
|
||
// relations | ||
if !(cfg.HideViews && cfg.HideLabels) { | ||
cover.Relations = &CoverageByElement{} | ||
for _, r := range cfg.Relations { | ||
cover.Relations.Total += 1 | ||
if r.Desc != "" { | ||
cover.Relations.Covered += 1 | ||
} | ||
} | ||
cover.Components.Coverage = round(float64(cover.Components.Covered) / float64(cover.Components.Total) * 100) | ||
cover.Total += cover.Components.Total | ||
cover.Covered += cover.Components.Covered | ||
} | ||
|
||
// layers | ||
for _, l := range cfg.Layers() { | ||
cover.Layers.Total += 1 | ||
if l.Desc != "" { | ||
cover.Layers.Covered += 1 | ||
} | ||
} | ||
cover.Layers.Coverage = round(float64(cover.Layers.Covered) / float64(cover.Layers.Total) * 100) | ||
cover.Total += cover.Layers.Total | ||
cover.Covered += cover.Layers.Covered | ||
|
||
// labels | ||
if !cfg.HideLabels { | ||
cover.Labels = &CoverageByElement{} | ||
for _, l := range cfg.Labels() { | ||
cover.Labels.Total += 1 | ||
if l.Desc != "" { | ||
cover.Labels.Covered += 1 | ||
} | ||
} | ||
cover.Labels.Coverage = round(float64(cover.Labels.Covered) / float64(cover.Labels.Total) * 100) | ||
cover.Total += cover.Labels.Total | ||
cover.Covered += cover.Labels.Covered | ||
} | ||
|
||
cover.Coverage = round(float64(cover.Covered) / float64(cover.Total) * 100) | ||
return cover | ||
} | ||
|
||
func round(f float64) float64 { | ||
places := 1 | ||
shift := math.Pow(10, float64(places)) | ||
return math.Floor(f*shift+.5) / shift | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters