| 
									
										
										
										
											2019-08-17 15:55:25 +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 main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import "sort" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type analysis struct { | 
					
						
							|  |  |  | 	sum      map[string]result // metrics per domain | 
					
						
							|  |  |  | 	keys     []string          // unique keys | 
					
						
							|  |  |  | 	groupKey groupFunc | 
					
						
							|  |  |  | 	filter   filterFunc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newAnalysis() *analysis { | 
					
						
							|  |  |  | 	return &analysis{ | 
					
						
							|  |  |  | 		sum:      make(map[string]result), | 
					
						
							|  |  |  | 		groupKey: domainGrouper, | 
					
						
							|  |  |  | 		filter:   noopFilter, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 20:00:27 +03:00
										 |  |  | func (a *analysis) groupBy(g groupFunc) { | 
					
						
							|  |  |  | 	if g != nil { | 
					
						
							|  |  |  | 		a.groupKey = g | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (a *analysis) filterBy(f filterFunc) { | 
					
						
							|  |  |  | 	if f != nil { | 
					
						
							|  |  |  | 		a.filter = f | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 20:00:27 +03:00
										 |  |  | // analyse the given result | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | func (a *analysis) analyse(r result) { | 
					
						
							|  |  |  | 	if !a.filter(r) { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	key := a.groupKey(r) | 
					
						
							|  |  |  | 	if _, ok := a.sum[key]; !ok { | 
					
						
							|  |  |  | 		a.keys = append(a.keys, key) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a.sum[key] = r.add(a.sum[key]) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 20:00:27 +03:00
										 |  |  | // each sends an analysis result to `handle` | 
					
						
							|  |  |  | func (a *analysis) each(handle resultFn) { | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | 	sort.Strings(a.keys) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, domain := range a.keys { | 
					
						
							| 
									
										
										
										
											2019-08-19 20:00:27 +03:00
										 |  |  | 		handle(a.sum[domain]) | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |