fix: log parser project
This commit is contained in:
		| @@ -11,54 +11,63 @@ import ( | |||||||
| 	"bufio" | 	"bufio" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"sort" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func main() { | func main() { | ||||||
| 	var ( | 	var ( | ||||||
| 		// Stores the visits per unique domain | 		sum     map[string]int // total visits per domain | ||||||
| 		sum = make(map[string]int) | 		domains []string       // unique domain names | ||||||
|  | 		total   int            // total visits to all domains | ||||||
| 		// Stores the unique domain names for printing | 		lines   int            // number of parsed lines (for the error messages) | ||||||
| 		domains []string |  | ||||||
|  |  | ||||||
| 		in = bufio.NewScanner(os.Stdin) |  | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
|  | 	sum = make(map[string]int) | ||||||
|  |  | ||||||
| 	// Scan the standard-in line by line | 	// Scan the standard-in line by line | ||||||
| 	for line := 1; in.Scan(); line++ { | 	in := bufio.NewScanner(os.Stdin) | ||||||
|  | 	for in.Scan() { | ||||||
|  | 		lines++ | ||||||
|  |  | ||||||
| 		// Parse the fields | 		// Parse the fields | ||||||
| 		fields := strings.Fields(in.Text()) | 		fields := strings.Fields(in.Text()) | ||||||
| 		if len(fields) != 2 { | 		if len(fields) != 2 { | ||||||
| 			fmt.Printf("wrong input: %v (line #%d)\n", fields, line) | 			fmt.Printf("wrong input: %v (line #%d)\n", fields, lines) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		domain, visits := fields[0], fields[1] |  | ||||||
|  |  | ||||||
| 		// Collect the unique domains |  | ||||||
| 		if _, ok := sum[domain]; !ok { |  | ||||||
| 			domains = append(domains, domain) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Sum the total visits per domain | 		// Sum the total visits per domain | ||||||
| 		n, err := strconv.Atoi(visits) | 		visits, err := strconv.Atoi(fields[1]) | ||||||
| 		if n < 0 || err != nil { | 		if visits < 0 || err != nil { | ||||||
| 			fmt.Printf("wrong input: %q (line #%d)\n", visits, line) | 			fmt.Printf("wrong input: %q (line #%d)\n", fields[1], lines) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		sum[domain] += n |  | ||||||
|  | 		name := fields[0] | ||||||
|  |  | ||||||
|  | 		// Collect the unique domains | ||||||
|  | 		if _, ok := sum[name]; !ok { | ||||||
|  | 			domains = append(domains, name) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// Keep track of total and per domain visits | ||||||
|  | 		total += visits | ||||||
|  | 		sum[name] += visits | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Print the visits per domain | 	// Print the visits per domain | ||||||
| 	var total int | 	sort.Strings(domains) | ||||||
| 	for _, domain := range domains { |  | ||||||
| 		hits := sum[domain] |  | ||||||
|  |  | ||||||
| 		fmt.Printf("%-25s -> %d\n", domain, hits) | 	fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS") | ||||||
| 		total += hits | 	fmt.Println(strings.Repeat("-", 45)) | ||||||
|  |  | ||||||
|  | 	for _, name := range domains { | ||||||
|  | 		visits := sum[name] | ||||||
|  | 		fmt.Printf("%-30s %10d\n", name, visits) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Print the total visits for all domains | 	// Print the total visits for all domains | ||||||
| 	fmt.Printf("\n%-25s -> %d\n", "TOTAL", total) | 	fmt.Printf("\n%-30s %10d\n", "TOTAL", total) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user