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