Debian Codegraph @

helpers/processing_stats.go

raw
1 // Copyright 2017 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package helpers
15
16 import (
17 "io"
18 "strconv"
19 "sync/atomic"
20
21 "github.com/olekukonko/tablewriter"
22 "github.com/olekukonko/tablewriter/renderer"
23 "github.com/olekukonko/tablewriter/tw"
24 )
25
26 // ProcessingStats represents statistics about a site build.
27 type ProcessingStats struct {
28 Name string
29
30 Pages uint64
31 PaginatorPages uint64
32 Static uint64
33 ProcessedImages uint64
34 Files uint64
35 Aliases uint64
36 Cleaned uint64
37 }
38
39 type processingStatsTitleVal struct {
40 name string
41 val uint64
42 }
43
44 func (s *ProcessingStats) toVals() []processingStatsTitleVal {
45 return []processingStatsTitleVal{
46 {"Pages", s.Pages},
47 {"Paginator pages", s.PaginatorPages},
48 {"Non-page files", s.Files},
49 {"Static files", s.Static},
50 {"Processed images", s.ProcessedImages},
51 {"Aliases", s.Aliases},
52 {"Cleaned", s.Cleaned},
53 }
54 }
55
56 // NewProcessingStats returns a new ProcessingStats instance.
57 func NewProcessingStats(name string) *ProcessingStats {
58 return &ProcessingStats{Name: name}
59 }
60
61 // Incr increments a given counter.
62 func (s *ProcessingStats) Incr(counter *uint64) {
63 atomic.AddUint64(counter, 1)
64 }
65
66 // ProcessingStatsTable writes a table-formatted representation of stats to w.
67 func ProcessingStatsTable(w io.Writer, stats ...*ProcessingStats) {
68 names := make([]string, len(stats)+1)
69
70 var data [][]string
71
72 for i := range stats {
73 stat := stats[i]
74 names[i+1] = stat.Name
75
76 titleVals := stat.toVals()
77
78 if i == 0 {
79 data = make([][]string, len(titleVals))
80 }
81
82 for j, tv := range titleVals {
83 if i == 0 {
84 data[j] = []string{tv.name, strconv.Itoa(int(tv.val))}
85 } else {
86 data[j] = append(data[j], strconv.Itoa(int(tv.val)))
87 }
88 }
89 }
90
91 table := tablewriter.NewTable(
92 w,
93 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
94 Borders: tw.BorderNone,
95 Symbols: tw.NewSymbols(tw.StyleLight),
96 Settings: tw.Settings{
97 Separators: tw.Separators{BetweenRows: tw.Off},
98 Lines: tw.Lines{ShowFooterLine: tw.On},
99 },
100 })),
101 tablewriter.WithConfig(
102 tablewriter.Config{
103 MaxWidth: 70,
104 Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight, PerColumn: []tw.Align{tw.AlignLeft}}},
105 }),
106 )
107
108 table.Bulk(data)
109 table.Header(names)
110 table.Render()
111 }