<commit_before>/**
 * report.go - provides basic reporting features for stnparse output.
 * @author <NAME>, <<EMAIL>>
 * copyright (c) 2015 all rights reserved.
 * Released under the BSD 2-Clause license
 * See: http://opensource.org/licenses/BSD-2-Clause
 */

// Package report provides basic report rendering for Simple Timesheet Notation.
package report

import (
	"../../stn"
	"fmt"
	"strings"
)

// EntryAggregation
type EntryAggregation struct {
	Entries []stn.Entry
}

// Aggregate - add an entry to the EntryAggregate
func (a *EntryAggregation) Aggregate(entry *stn.Entry) bool {
	i := len(a.Entries)
	a.Entries = append(a.Entries, *entry)
	if len(a.Entries) == (i + 1) {
		return true
	}
	return false
}

// Summarize - give the output of stnparse or stnfilter aggregate the results
// by the first notation and the durration of time.
func (e *EntryAggregation) Summarize() string {
	var outText []string

	summary := make(map[string]float64)
	for _, item := range e.Entries {
		// Calc duration
		duration := item.End.Sub(item.Start)
		// Calc key
		key := item.Annotations[0]
		// if map entry does not exist create one with key and duration
		// else add the new duration to old and update map
		val, ok := summary[key]
		if ok == true {
			summary[key] = val + duration.Hours()
		} else {
			summary[key] = duration.Hours()
		}
	}
	outText = append(outText, "Hours\tProject")
	total := 0.0
	for k, v := range summary {
		total += v
		outText = append(outText, fmt.Sprintf("%5.2f\t%s", v, k))
	}
	outText = append(outText, "")
	outText = append(outText, fmt.Sprintf("%5.2f\tTotal Hours", total))
	outText = append(outText, "")
	return strings.Join(outText, "\n")
}<commit_msg>Adding sorting of project names in report.<commit_after>/**
 * report.go - provides basic reporting features for stnparse output.
 * @author <NAME>, <<EMAIL>>
 * copyright (c) 2015 all rights reserved.
 * Released under the BSD 2-Clause license
 * See: http://opensource.org/licenses/BSD-2-Clause
 */

// Package report provides basic report rendering for Simple Timesheet Notation.
package report

import (
	"../../stn"
	"fmt"
	"strings"
	"sort"
)

// EntryAggregation
type EntryAggregation struct {
	Entries []stn.Entry
}

// Aggregate - add an entry to the EntryAggregate
func (a *EntryAggregation) Aggregate(entry *stn.Entry) bool {
	i := len(a.Entries)
	a.Entries = append(a.Entries, *entry)
	if len(a.Entries) == (i + 1) {
		return true
	}
	return false
}

// Summarize - give the output of stnparse or stnfilter aggregate the results
// by the first notation and the durration of time.
func (e *EntryAggregation) Summarize() string {
	var outText []string

	summary := make(map[string]float64)
	for _, item := range e.Entries {
		// Calc duration
		duration := item.End.Sub(item.Start)
		// Calc key
		key := item.Annotations[0]
		// if map entry does not exist create one with key and duration
		// else add the new duration to old and update map
		val, ok := summary[key]
		if ok == true {
			summary[key] = val + duration.Hours()
		} else {
			summary[key] = duration.Hours()
		}
	}
	outText = append(outText, "Hours\tProject")
	total := 0.0
	keys := make([]string, len(summary))
	for k := range summary {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
		v, ok := summary[k]
		if ok == true {
			total += v
			outText = append(outText, fmt.Sprintf("%5.2f\t%s", v, k))
		}
	}
	outText = append(outText, "")
	outText = append(outText, fmt.Sprintf("%5.2f\tTotal Hours", total))
	outText = append(outText, "")
	return strings.Join(outText, "\n")
}
{'fraction_non_alphanumeric': 0.13789237668161436, 'fraction_numerical': 0.008968609865470852, 'mean_word_length': 3.5349428208386278, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6137609', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1204, 'n_words': 381}