refactor: log parser text writer and func types
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| package main | ||||
|  | ||||
| type filterFunc func(result) (include bool) | ||||
|  | ||||
| func filterBy(results []result, filterer filterFunc) []result { | ||||
| 	out := results[:0] | ||||
|  | ||||
|   | ||||
| @@ -6,7 +6,7 @@ func noopFilter(r result) bool { | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func not(filter filterFunc) filterFunc { | ||||
| func notUsing(filter filterFunc) filterFunc { | ||||
| 	return func(r result) bool { | ||||
| 		return !filter(r) | ||||
| 	} | ||||
|   | ||||
| @@ -1,5 +1,7 @@ | ||||
| package main | ||||
|  | ||||
| type groupFunc func(result) (key string) | ||||
|  | ||||
| func groupBy(results []result, keyer groupFunc) []result { | ||||
| 	grouped := make(map[string]result, len(results)) | ||||
|  | ||||
|   | ||||
| @@ -20,7 +20,7 @@ func main() { | ||||
| 		to(os.Stdout). | ||||
| 		retrieveFrom(textReader). | ||||
| 		filterBy(orgDomainsFilter). | ||||
| 		// filterBy(not(domainExtFilter("org", "io"))). | ||||
| 		// filterBy(notUsing(domainExtFilter("org", "io"))). | ||||
| 		// groupBy(pageGrouper). | ||||
| 		groupBy(domainGrouper). | ||||
| 		writeTo(textWriter). | ||||
| @@ -44,6 +44,17 @@ func recoverErr() { | ||||
| } | ||||
|  | ||||
| /* | ||||
| newReport -> stats.NewReport(). | ||||
| Result    -> stats.Record | ||||
| newReport -> report.New(). | ||||
| Result    -> report.Line | ||||
|  | ||||
| notUsing = report.Not | ||||
|  | ||||
| _, err := stats.Parse(). | ||||
| 	From(os.Stdin). | ||||
| 	To(os.Stdout). | ||||
| 	RetrieveFrom(stats.TextReader). | ||||
| 	FilterBy(notUsing(stats.DomainExtFilter("org", "io"))). | ||||
| 	GroupBy(stats.DomainGrouper). | ||||
| 	WriteTo(stats.TextWriter). | ||||
| 	Run() | ||||
| */ | ||||
|   | ||||
| @@ -4,8 +4,6 @@ import "io" | ||||
|  | ||||
| type ( | ||||
| 	retrieveFunc func(io.Reader) ([]result, error) | ||||
| 	filterFunc   func(result) bool | ||||
| 	groupFunc    func(result) string | ||||
| 	writeFunc    func(io.Writer, []result) error | ||||
| ) | ||||
|  | ||||
|   | ||||
| @@ -8,23 +8,28 @@ import ( | ||||
|  | ||||
| // TODO: sort by result key interfaces section | ||||
|  | ||||
| func textWriter(w io.Writer, results []result) error { | ||||
| 	fmt.Fprintf(w, "%-25s %-10s %10s %10s\n", | ||||
| 		"DOMAINS", "PAGES", "VISITS", "UNIQUES") | ||||
| const ( | ||||
|  | ||||
| 	fmt.Fprintln(w, strings.Repeat("-", 58)) | ||||
| 	//          DOMAINS PAGES VISITS UNIQUES | ||||
| 	//             ^      ^     ^    ^ | ||||
| 	//             |      |     |    | | ||||
| 	header     = "%-25s %-10s %10s %10s\n" | ||||
| 	line       = "%-25s %-10s %10d %10d\n" | ||||
| 	footer     = "\n%-36s %10d %10d\n" // -> "" VISITS UNIQUES | ||||
| 	dashLength = 58 | ||||
| ) | ||||
|  | ||||
| func textWriter(w io.Writer, results []result) error { | ||||
| 	fmt.Fprintf(w, header, "DOMAINS", "PAGES", "VISITS", "UNIQUES") | ||||
| 	fmt.Fprintln(w, strings.Repeat("-", dashLength)) | ||||
|  | ||||
| 	var total result | ||||
|  | ||||
| 	for _, r := range results { | ||||
| 		total = total.add(r) | ||||
|  | ||||
| 		fmt.Fprintf(w, "%-25s %-10s %10d %10d\n", | ||||
| 			r.domain, r.page, r.visits, r.uniques) | ||||
| 		fmt.Fprintf(w, line, r.domain, r.page, r.visits, r.uniques) | ||||
| 	} | ||||
|  | ||||
| 	fmt.Fprintf(w, "\n%-36s %10d %10d\n", | ||||
| 		"", total.visits, total.uniques) | ||||
| 	fmt.Fprintf(w, footer, "", total.visits, total.uniques) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user