Refactor the stats code for generators and transformers

This commit is contained in:
Haiyan Meng
2020-01-16 09:20:24 -08:00
parent 5477bde7e5
commit 9f80da28ae

View File

@@ -46,84 +46,50 @@ func SortMapKeyByValue(m map[string]int) []string {
return keys return keys
} }
func GeneratorAndTransformerStats(ctx context.Context, func GeneratorOrTransformerStats(ctx context.Context,
generatorDocs []*doc.Document, transformerDocs []*doc.Document, docs []*doc.Document, isGenerator bool, idx *index.KustomizeIndex) {
idx *index.KustomizeIndex) {
// allGenerators includes all the documents referred in the generators field
allGenerators := doc.NewUniqueDocuments()
// allTransformers includes all the documents referred in the transformers field fieldName := "generators"
allTransformers := doc.NewUniqueDocuments() if !isGenerator {
fieldName = "transformers"
}
// docUsingGeneratorCount counts the number of the kustomization files using generators // allReferredDocs includes all the documents referred in the field
docUsingGeneratorCount := 0 allReferredDocs := doc.NewUniqueDocuments()
// docUsingTransformerCount counts the number of the kustomization files using transformers // docUsingGeneratorCount counts the number of the kustomization files using generators or transformers
docUsingTransformerCount := 0 docCount := 0
// collect all the documents referred in the generators and transformers fields // collect all the documents referred in the field
for _, d := range generatorDocs { for _, d := range docs {
kdoc := doc.KustomizationDocument{ kdoc := doc.KustomizationDocument{
Document: *d, Document: *d,
} }
generators, err := kdoc.GetResources(false, false, true) referredDocs, err := kdoc.GetResources(false, !isGenerator, isGenerator)
if err != nil { if err != nil {
log.Printf("failed to parse the generators field of the Document (%s): %v", log.Printf("failed to parse the %s field of the Document (%s): %v",
d.Path(), err) fieldName, d.Path(), err)
} }
if len(generators) > 0 { if len(referredDocs) > 0 {
docUsingGeneratorCount++ docCount++
allGenerators.AddDocuments(generators) allReferredDocs.AddDocuments(referredDocs)
} }
} }
for _, d := range transformerDocs { fileCount, dirCount, fileTypeDocs, dirTypeDocs := DocumentTypeSummary(ctx, allReferredDocs.Documents())
kdoc := doc.KustomizationDocument{
Document: *d,
}
transformers, err := kdoc.GetResources(false, true, false)
if err != nil {
log.Printf("failed to parse the transformers field of the Document (%s): %v",
d.Path(), err)
}
if len(transformers) > 0 {
docUsingTransformerCount++
allTransformers.AddDocuments(transformers)
}
}
// fileGeneratorCount counts file-type generators // check whether any of the files are not in the index
// dirGeneratorCount counts dir-type generators nonExistFileCount := ExistInIndex(idx, fileTypeDocs, fieldName + " file ")
fileGeneratorCount, dirGeneratorCount, generatorFiles, generatorDirs := DocumentTypeSummary(ctx, allGenerators.Documents()) // check whether any of the dirs are not in the index
nonExistDirCount := ExistInIndex(idx, dirTypeDocs, fieldName + " dir ")
// fileTransformerCount counts file-type transformers GitRepositorySummary(fileTypeDocs, fieldName + " files")
// dirTransformerCount counts dir-type transformers GitRepositorySummary(dirTypeDocs, fieldName + " dirs")
fileTransformerCount, dirTransformerCount, transformerFiles, transformerDirs := DocumentTypeSummary(ctx, allTransformers.Documents())
// check whether any of the generator files are not in the index fmt.Printf("%d kustomization files use %s: %d %s are files and %d %s are dirs.\n",
nonExistGeneratorFileCount := ExistInIndex(idx, generatorFiles, "generator file ") docCount, fieldName, fileCount, fieldName, dirCount, fieldName)
// check whether any of the generator dirs are not in the index fmt.Printf("%d %s files do not exist in the index\n", nonExistFileCount, fieldName)
nonExistGeneratorDirCount := ExistInIndex(idx, generatorDirs, "generator dir ") fmt.Printf("%d %s dirs do not exist in the index\n", nonExistDirCount, fieldName)
// check whether any of the transformer files are not in the index
nonExistTransformerFileCount := ExistInIndex(idx, transformerFiles, "transformer file ")
// check whether any of the transformer dirs are not in the index
nonExistTransformerDirCount := ExistInIndex(idx, transformerDirs, "transformer dir ")
GitRepositorySummary(generatorFiles, "generator files")
GitRepositorySummary(generatorDirs, "generator dirs")
GitRepositorySummary(transformerFiles, "transformer files")
GitRepositorySummary(transformerDirs, "transformer dirs")
fmt.Printf(`%d kustomization files use generators: %d generators are files and %d generators are dirs.
%d kustomization files use tranformers: %d transformers are files and %d transformers are dirs.`,
docUsingGeneratorCount, fileGeneratorCount, dirGeneratorCount,
docUsingTransformerCount, fileTransformerCount, dirTransformerCount)
fmt.Printf("\n")
fmt.Printf("%d generator files do not exist in the index\n", nonExistGeneratorFileCount)
fmt.Printf("%d generator dirs do not exist in the index\n", nonExistGeneratorDirCount)
fmt.Printf("%d transformer files do not exist in the index\n", nonExistTransformerFileCount)
fmt.Printf("%d transformer dirs do not exist in the index\n", nonExistTransformerDirCount)
} }
// GitRepositorySummary counts the distribution of docs: // GitRepositorySummary counts the distribution of docs:
@@ -314,5 +280,6 @@ There are %d documents in the kustomize index.
} }
} }
GeneratorAndTransformerStats(ctx, generatorDocs, transformersDocs, idx) GeneratorOrTransformerStats(ctx, generatorDocs, true, idx)
GeneratorOrTransformerStats(ctx, transformersDocs, false, idx)
} }