| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | // For more tutorials: https://blog.learngoprogramming.com | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package pipe | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"text/tabwriter" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	minWidth = 0 | 
					
						
							|  |  |  | 	tabWidth = 4 | 
					
						
							|  |  |  | 	padding  = 4 | 
					
						
							|  |  |  | 	flags    = 0 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TextReport report generator. | 
					
						
							|  |  |  | type TextReport struct { | 
					
						
							|  |  |  | 	w io.Writer | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewTextReport returns a TextReport report generator. | 
					
						
							|  |  |  | func NewTextReport(w io.Writer) *TextReport { | 
					
						
							|  |  |  | 	return &TextReport{w: w} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Consume generates a text report. | 
					
						
							|  |  |  | func (t *TextReport) Consume(records Iterator) error { | 
					
						
							|  |  |  | 	w := tabwriter.NewWriter(t.w, minWidth, tabWidth, padding, ' ', flags) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 16:08:46 +03:00
										 |  |  | 	fmt.Fprintf(w, "DOMAINS\tPAGES\tVISITS\tUNIQUES\n") | 
					
						
							|  |  |  | 	fmt.Fprintf(w, "-------\t-----\t------\t-------\n") | 
					
						
							| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 01:50:04 +03:00
										 |  |  | 	var total record | 
					
						
							| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 16:08:46 +03:00
										 |  |  | 	printLine := func(r Record) error { | 
					
						
							| 
									
										
										
										
											2019-08-29 01:50:04 +03:00
										 |  |  | 		total = r.sum(total) | 
					
						
							| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 16:08:46 +03:00
										 |  |  | 		fmt.Fprintf(w, "%s\t%s\t%d\t%d\n", | 
					
						
							| 
									
										
										
										
											2019-08-29 01:32:25 +03:00
										 |  |  | 			r.domain, r.page, | 
					
						
							|  |  |  | 			r.visits, r.uniques, | 
					
						
							| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | 		) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2019-08-29 16:08:46 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := records.Each(printLine); err != nil { | 
					
						
							| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 16:08:46 +03:00
										 |  |  | 	fmt.Fprintf(w, "\t\t\t\n") | 
					
						
							|  |  |  | 	fmt.Fprintf(w, "%s\t%s\t%d\t%d\n", "TOTAL", "", | 
					
						
							| 
									
										
										
										
											2019-08-29 01:32:25 +03:00
										 |  |  | 		total.visits, | 
					
						
							|  |  |  | 		total.uniques, | 
					
						
							| 
									
										
										
										
											2019-08-28 23:46:42 +03:00
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return w.Flush() | 
					
						
							|  |  |  | } |