| 
									
										
										
										
											2019-04-27 13:19:24 +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/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | // result stores the parsed result for a domain | 
					
						
							|  |  |  | type result struct { | 
					
						
							|  |  |  | 	domain string | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 	visits int | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	// add more metrics if needed | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | // parser keep tracks of the parsing | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | type parser struct { | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	sum     map[string]result // metrics per domain | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 	domains []string          // unique domain names | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	total   int               // total visits for all domains | 
					
						
							|  |  |  | 	lines   int               // number of parsed lines (for the error messages) | 
					
						
							|  |  |  | 	lerr    error             // the last error occurred | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | // newParser constructs, initializes and returns a new parser | 
					
						
							|  |  |  | func newParser() parser { | 
					
						
							|  |  |  | 	return parser{sum: make(map[string]result)} | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | // parse parses a log line and returns the parsed result with an error | 
					
						
							|  |  |  | func parse(p *parser, line string) (parsed result) { | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 	if p.lerr != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p.lines++ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fields := strings.Fields(line) | 
					
						
							|  |  |  | 	if len(fields) != 2 { | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 		p.lerr = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines) | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	parsed.domain = fields[0] | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	parsed.visits, err = strconv.Atoi(fields[1]) | 
					
						
							|  |  |  | 	if parsed.visits < 0 || err != nil { | 
					
						
							|  |  |  | 		p.lerr = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 18:14:53 +03:00
										 |  |  | // update updates all the parsing results using the given parsing result | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | func update(p *parser, parsed result) { | 
					
						
							|  |  |  | 	if p.lerr != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	domain, visits := parsed.domain, parsed.visits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Collect the unique domains | 
					
						
							|  |  |  | 	if _, ok := p.sum[domain]; !ok { | 
					
						
							|  |  |  | 		p.domains = append(p.domains, domain) | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 13:19:24 +03:00
										 |  |  | 	// Keep track of total and per domain visits | 
					
						
							|  |  |  | 	p.total += visits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create and assign a new copy of `visit` | 
					
						
							|  |  |  | 	p.sum[domain] = result{ | 
					
						
							|  |  |  | 		domain: domain, | 
					
						
							|  |  |  | 		visits: visits + p.sum[domain].visits, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | } |