gitee.com/mirrors/Hugo-Go@v0.47.1/helpers/processing_stats.go (about)

     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  )
    23  
    24  type ProcessingStats struct {
    25  	Name string
    26  
    27  	Pages           uint64
    28  	PaginatorPages  uint64
    29  	Static          uint64
    30  	ProcessedImages uint64
    31  	Files           uint64
    32  	Aliases         uint64
    33  	Sitemaps        uint64
    34  	Cleaned         uint64
    35  }
    36  
    37  type processingStatsTitleVal struct {
    38  	name string
    39  	val  uint64
    40  }
    41  
    42  func (s *ProcessingStats) toVals() []processingStatsTitleVal {
    43  	return []processingStatsTitleVal{
    44  		{"Pages", s.Pages},
    45  		{"Paginator pages", s.PaginatorPages},
    46  		{"Non-page files", s.Files},
    47  		{"Static files", s.Static},
    48  		{"Processed images", s.ProcessedImages},
    49  		{"Aliases", s.Aliases},
    50  		{"Sitemaps", s.Sitemaps},
    51  		{"Cleaned", s.Cleaned},
    52  	}
    53  }
    54  
    55  func NewProcessingStats(name string) *ProcessingStats {
    56  	return &ProcessingStats{Name: name}
    57  }
    58  
    59  func (s *ProcessingStats) Incr(counter *uint64) {
    60  	atomic.AddUint64(counter, 1)
    61  }
    62  
    63  func (s *ProcessingStats) Add(counter *uint64, amount int) {
    64  	atomic.AddUint64(counter, uint64(amount))
    65  }
    66  
    67  func (s *ProcessingStats) Table(w io.Writer) {
    68  	titleVals := s.toVals()
    69  	data := make([][]string, len(titleVals))
    70  	for i, tv := range titleVals {
    71  		data[i] = []string{tv.name, strconv.Itoa(int(tv.val))}
    72  	}
    73  
    74  	table := tablewriter.NewWriter(w)
    75  
    76  	table.AppendBulk(data)
    77  	table.SetHeader([]string{"", s.Name})
    78  	table.SetBorder(false)
    79  	table.Render()
    80  
    81  }
    82  
    83  func ProcessingStatsTable(w io.Writer, stats ...*ProcessingStats) {
    84  	names := make([]string, len(stats)+1)
    85  
    86  	var data [][]string
    87  
    88  	for i := 0; i < len(stats); i++ {
    89  		stat := stats[i]
    90  		names[i+1] = stat.Name
    91  
    92  		titleVals := stat.toVals()
    93  
    94  		if i == 0 {
    95  			data = make([][]string, len(titleVals))
    96  		}
    97  
    98  		for j, tv := range titleVals {
    99  			if i == 0 {
   100  				data[j] = []string{tv.name, strconv.Itoa(int(tv.val))}
   101  			} else {
   102  				data[j] = append(data[j], strconv.Itoa(int(tv.val)))
   103  			}
   104  
   105  		}
   106  
   107  	}
   108  
   109  	table := tablewriter.NewWriter(w)
   110  
   111  	table.AppendBulk(data)
   112  	table.SetHeader(names)
   113  	table.SetBorder(false)
   114  	table.Render()
   115  
   116  }