<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/cezarsa/form"
	"github.com/ghodss/yaml"
	"github.com/tsuru/gnuflag"
	"github.com/tsuru/tsuru/cmd"
	"github.com/tsuru/tsuru/event"
)

type eventList struct {
	fs     *gnuflag.FlagSet
	filter eventFilter
}

type eventFilter struct {
	KindName    string
	Target      string
	TargetValue string
	OwnerName   string
	Running     bool
}

func (f *eventFilter) queryString(client *cmd.Client) (url.Values, error) {
	values, err := form.EncodeToValues(f)
	if err != nil {
		return nil, err
	}
	for k, v := range values {
		values.Del(k)
		values[strings.ToLower(k)] = v
	}
	if !f.Running {
		values.Del("running")
	}
	return values, nil
}

func (f *eventFilter) flags(fs *gnuflag.FlagSet) {
	name := "Filter events by kind name"
	fs.StringVar(&f.KindName, "kind", "", name)
	fs.StringVar(&f.KindName, "k", "", name)
	name = "Filter events by target name"
	fs.StringVar(&f.Target, "target", "", name)
	fs.StringVar(&f.Target, "t", "", name)
	name = "Filter events by target value"
	fs.StringVar(&f.TargetValue, "target-value", "", name)
	fs.StringVar(&f.TargetValue, "v", "", name)
	name = "Filter events by owner name"
	fs.StringVar(&f.OwnerName, "owner", "", name)
	fs.StringVar(&f.OwnerName, "o", "", name)
	name = "Shows only currently running events"
	fs.BoolVar(&f.Running, "running", false, name)
	fs.BoolVar(&f.Running, "r", false, name)
}

func (c *eventList) Info() *cmd.Info {
	return &cmd.Info{
		Name:  "event-list",
		Usage: "event-list [-k kindName]",
		Desc:  `Lists events possibly filtering them.`,
	}
}

func (c *eventList) Flags() *gnuflag.FlagSet {
	if c.fs == nil {
		c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
		c.filter.flags(c.fs)
	}
	return c.fs
}

func (c *eventList) Run(context *cmd.Context, client *cmd.Client) error {
	qs, err := c.filter.queryString(client)
	if err != nil {
		return err
	}
	u, err := cmd.GetURLVersion("1.1", fmt.Sprintf("/events?%s", qs.Encode()))
	if err != nil {
		return err
	}
	request, err := http.NewRequest("GET", u, nil)
	if err != nil {
		return err
	}
	response, err := client.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	if response.StatusCode == http.StatusNoContent {
		return nil
	}
	result, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return err
	}
	var evts []event.Event
	err = json.Unmarshal(result, &evts)
	if err != nil {
		return fmt.Errorf("unable to unmarshal %q: %s", string(result), err)
	}
	return c.Show(evts, context)
}

var reEmailShort = regexp.MustCompile(`@.*$`)

func (c *eventList) Show(evts []event.Event, context *cmd.Context) error {
	tbl := cmd.NewTable()
	tbl.Headers = cmd.Row{"ID", "Start (duration)", "Success", "Owner", "Kind", "Target"}
	for i := range evts {
		evt := &evts[i]
		if evt.Target.Type == "container" {
			evt.Target.Value = evt.Target.Value[:12]
		}
		fullTarget := fmt.Sprintf("%s: %s", evt.Target.Type, evt.Target.Value)
		startFmt := evt.StartTime.Format(time.RFC822Z)
		owner := reEmailShort.ReplaceAllString(evt.Owner.Name, "@…")
		var ts, success string
		if evt.Running {
			ts = fmt.Sprintf("%s (…)", startFmt)
			success = "…"
		} else {
			ts = fmt.Sprintf("%s (%v)", startFmt, evt.EndTime.Sub(evt.StartTime))
			success = fmt.Sprintf("%v", evt.Error == "")
			if evt.CancelInfo.Canceled {
				success += " ✗"
			}
		}
		row := cmd.Row{evt.UniqueID.Hex(), ts, success, owner, evt.Kind.Name, fullTarget}
		var color string
		if evt.CancelInfo.Canceled {
			color = "magenta"
		} else if evt.Error != "" {
			color = "red"
		}
		if color != "" {
			for i, v := range row {
				if v != "" {
					row[i] = cmd.Colorfy(v, color, "", "")
				}
			}
		}
		tbl.AddRow(row)
	}
	fmt.Fprintf(context.Stdout, "%s", tbl.String())
	return nil
}

type eventInfo struct{}
<commit_msg>event: different color for running events<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/cezarsa/form"
	"github.com/ghodss/yaml"
	"github.com/tsuru/gnuflag"
	"github.com/tsuru/tsuru/cmd"
	"github.com/tsuru/tsuru/event"
)

type eventList struct {
	fs     *gnuflag.FlagSet
	filter eventFilter
}

type eventFilter struct {
	KindName    string
	Target      string
	TargetValue string
	OwnerName   string
	Running     bool
}

func (f *eventFilter) queryString(client *cmd.Client) (url.Values, error) {
	values, err := form.EncodeToValues(f)
	if err != nil {
		return nil, err
	}
	for k, v := range values {
		values.Del(k)
		values[strings.ToLower(k)] = v
	}
	if !f.Running {
		values.Del("running")
	}
	return values, nil
}

func (f *eventFilter) flags(fs *gnuflag.FlagSet) {
	name := "Filter events by kind name"
	fs.StringVar(&f.KindName, "kind", "", name)
	fs.StringVar(&f.KindName, "k", "", name)
	name = "Filter events by target name"
	fs.StringVar(&f.Target, "target", "", name)
	fs.StringVar(&f.Target, "t", "", name)
	name = "Filter events by target value"
	fs.StringVar(&f.TargetValue, "target-value", "", name)
	fs.StringVar(&f.TargetValue, "v", "", name)
	name = "Filter events by owner name"
	fs.StringVar(&f.OwnerName, "owner", "", name)
	fs.StringVar(&f.OwnerName, "o", "", name)
	name = "Shows only currently running events"
	fs.BoolVar(&f.Running, "running", false, name)
	fs.BoolVar(&f.Running, "r", false, name)
}

func (c *eventList) Info() *cmd.Info {
	return &cmd.Info{
		Name:  "event-list",
		Usage: "event-list [-k kindName]",
		Desc:  `Lists events possibly filtering them.`,
	}
}

func (c *eventList) Flags() *gnuflag.FlagSet {
	if c.fs == nil {
		c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
		c.filter.flags(c.fs)
	}
	return c.fs
}

func (c *eventList) Run(context *cmd.Context, client *cmd.Client) error {
	qs, err := c.filter.queryString(client)
	if err != nil {
		return err
	}
	u, err := cmd.GetURLVersion("1.1", fmt.Sprintf("/events?%s", qs.Encode()))
	if err != nil {
		return err
	}
	request, err := http.NewRequest("GET", u, nil)
	if err != nil {
		return err
	}
	response, err := client.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	if response.StatusCode == http.StatusNoContent {
		return nil
	}
	result, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return err
	}
	var evts []event.Event
	err = json.Unmarshal(result, &evts)
	if err != nil {
		return fmt.Errorf("unable to unmarshal %q: %s", string(result), err)
	}
	return c.Show(evts, context)
}

var reEmailShort = regexp.MustCompile(`@.*$`)

func (c *eventList) Show(evts []event.Event, context *cmd.Context) error {
	tbl := cmd.NewTable()
	tbl.Headers = cmd.Row{"ID", "Start (duration)", "Success", "Owner", "Kind", "Target"}
	for i := range evts {
		evt := &evts[i]
		if evt.Target.Type == "container" {
			evt.Target.Value = evt.Target.Value[:12]
		}
		fullTarget := fmt.Sprintf("%s: %s", evt.Target.Type, evt.Target.Value)
		startFmt := evt.StartTime.Format(time.RFC822Z)
		owner := reEmailShort.ReplaceAllString(evt.Owner.Name, "@…")
		var ts, success string
		if evt.Running {
			ts = fmt.Sprintf("%s (…)", startFmt)
			success = "…"
		} else {
			ts = fmt.Sprintf("%s (%v)", startFmt, evt.EndTime.Sub(evt.StartTime))
			success = fmt.Sprintf("%v", evt.Error == "")
			if evt.CancelInfo.Canceled {
				success += " ✗"
			}
		}
		row := cmd.Row{evt.UniqueID.Hex(), ts, success, owner, evt.Kind.Name, fullTarget}
		var color string
		if evt.Running {
			color = "yellow"
		} else if evt.CancelInfo.Canceled {
			color = "magenta"
		} else if evt.Error != "" {
			color = "red"
		}
		if color != "" {
			for i, v := range row {
				if v != "" {
					row[i] = cmd.Colorfy(v, color, "", "")
				}
			}
		}
		tbl.AddRow(row)
	}
	fmt.Fprintf(context.Stdout, "%s", tbl.String())
	return nil
}

type eventInfo struct{}

{'fraction_non_alphanumeric': 0.17577227470002552, 'fraction_numerical': 0.0017870819504723003, 'mean_word_length': 3.6142520612485276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6561864', 'n_tokens_mistral': 3155, 'n_tokens_neox': 3035, 'n_words': 716}