github.com/wtfutil/wtf@v0.43.0/modules/googleanalytics/display.go (about)

     1  package googleanalytics
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  func (widget *Widget) createTable(websiteReports []websiteReport) string {
    10  	content := ""
    11  
    12  	if len(websiteReports) == 0 {
    13  		return content
    14  	}
    15  
    16  	if websiteReports[0].RealtimeReport != nil {
    17  		content += "Realtime Visitor Counts\n"
    18  		for _, websiteReport := range websiteReports {
    19  			websiteRow := fmt.Sprintf(" %-20s", websiteReport.Name)
    20  
    21  			if websiteReport.RealtimeReport == nil {
    22  				websiteRow += "No data found for given ViewId"
    23  			} else {
    24  				if len(websiteReport.RealtimeReport.Rows) == 0 {
    25  					websiteRow += "-"
    26  				} else {
    27  					websiteRow += fmt.Sprintf("%-10s", websiteReport.RealtimeReport.Rows[0][0])
    28  				}
    29  			}
    30  
    31  			content += websiteRow + "\n"
    32  		}
    33  
    34  		content += "\n"
    35  		content += "Historical Visitor Counts\n"
    36  	}
    37  
    38  	content += widget.createHeader()
    39  
    40  	for _, websiteReport := range websiteReports {
    41  		websiteRow := ""
    42  
    43  		for _, report := range websiteReport.Report.Reports {
    44  			websiteRow += fmt.Sprintf(" %-20s", websiteReport.Name)
    45  			reportRows := report.Data.Rows
    46  			noDataMonth := widget.settings.months - len(reportRows)
    47  
    48  			// Fill in requested months with no data from query
    49  			if noDataMonth > 0 {
    50  				websiteRow += strings.Repeat("-         ", noDataMonth)
    51  			}
    52  
    53  			if reportRows == nil {
    54  				websiteRow += "No data found for given ViewId"
    55  			} else {
    56  				for _, row := range reportRows {
    57  					metrics := row.Metrics
    58  
    59  					for _, metric := range metrics {
    60  						websiteRow += fmt.Sprintf("%-10s", metric.Values[0])
    61  					}
    62  				}
    63  			}
    64  
    65  			content += websiteRow + "\n"
    66  		}
    67  	}
    68  
    69  	return content
    70  }
    71  
    72  func (widget *Widget) createHeader() string {
    73  	// Creates the table header of consisting of Months
    74  	currentMonth := int(time.Now().Month())
    75  	widgetStartMonth := currentMonth - widget.settings.months + 1
    76  	header := "                     "
    77  
    78  	for i := widgetStartMonth; i < currentMonth+1; i++ {
    79  		header += fmt.Sprintf("%-10s", time.Month(i))
    80  	}
    81  	header += "\n"
    82  
    83  	return header
    84  }