<commit_before>package main

import (
	"fmt"
	"os"

	"github.com/cloudfoundry/cli/plugin"
	"github.com/krujos/usagereport-plugin/apihelper"
)

//UsageReportCmd the plugin
type UsageReportCmd struct {
	apiHelper apihelper.CFAPIHelper
	cli       plugin.CliConnection
}

type org struct {
	name        string
	memoryQuota int
	memoryUsage int
	spaces      []space
}

type space struct {
	apps []app
	name string
}

type app struct {
	ram       int
	instances int
	running   bool
}

//GetMetadata returns metatada
func (cmd *UsageReportCmd) GetMetadata() plugin.PluginMetadata {
	return plugin.PluginMetadata{
		Name: "usage-report",
		Version: plugin.VersionType{
			Major: 1,
			Minor: 1,
			Build: 0,
		},
		Commands: []plugin.Command{
			{
				Name:     "usage-report",
				HelpText: "Report AI and memory usage for orgs and spaces",
				UsageDetails: plugin.Usage{
					Usage: "cf usage-report",
				},
			},
		},
	}
}

//UsageReportCommand doer
func (cmd *UsageReportCmd) UsageReportCommand(args []string) {
	fmt.Println("Gathering usage information")

	if nil == cmd.cli {
		fmt.Println("ERROR: CLI Connection is nil!")
		os.Exit(1)
	}

	orgs, err := cmd.getOrgs()
	if nil != err {
		fmt.Println(err)
		os.Exit(1)
	}

	totalApps := 0
	totalInstances := 0
	for _, org := range orgs {
		fmt.Printf("Org %s is consuming %d MB of %d MB.\n", org.name, org.memoryUsage, org.memoryQuota)
		for _, space := range org.spaces {
			consumed := 0
			instances := 0
			runningApps := 0
			runningInstances := 0
			for _, app := range space.apps {
				if app.running {
					consumed += int(app.instances * app.ram)
					runningApps++
					runningInstances += app.instances
				}
				instances += int(app.instances)
			}
			fmt.Printf("\tSpace %s is consuming %d MB memory (%d%%) of org quota.\n",
				space.name, consumed, (100 * consumed / org.memoryQuota))
			fmt.Printf("\t\t%d apps: %d running %d stopped\n", len(space.apps),
				runningApps, len(space.apps)-runningApps)
			fmt.Printf("\t\t%d instances: %d running, %d stopped\n", instances,
				runningInstances, instances-runningInstances)
			totalInstances += instances
			totalApps += len(space.apps)
		}
	}
	fmt.Printf("You are running %d apps in all orgs, with a total of %d instances.\n",
		totalApps, totalInstances)
}

func (cmd *UsageReportCmd) getOrgs() ([]org, error) {
	rawOrgs, err := cmd.apiHelper.GetOrgs(cmd.cli)
	if nil != err {
		return nil, err
	}

	var orgs = []org{}

	for _, o := range rawOrgs {
		usage, err := cmd.apiHelper.GetOrgMemoryUsage(cmd.cli, o)
		if nil != err {
			return nil, err
		}
		quota, err := cmd.apiHelper.GetQuotaMemoryLimit(cmd.cli, o.QuotaURL)
		if nil != err {
			return nil, err
		}
		spaces, err := cmd.getSpaces(o.SpacesURL)
		if nil != err {
			return nil, err
		}

		orgs = append(orgs, org{
			name:        o.Name,
			memoryQuota: int(quota),
			memoryUsage: int(usage),
			spaces:      spaces,<commit_msg>show back number of orgs<commit_after>package main

import (
	"fmt"
	"os"

	"github.com/cloudfoundry/cli/plugin"
	"github.com/krujos/usagereport-plugin/apihelper"
)

//UsageReportCmd the plugin
type UsageReportCmd struct {
	apiHelper apihelper.CFAPIHelper
	cli       plugin.CliConnection
}

type org struct {
	name        string
	memoryQuota int
	memoryUsage int
	spaces      []space
}

type space struct {
	apps []app
	name string
}

type app struct {
	ram       int
	instances int
	running   bool
}

//GetMetadata returns metatada
func (cmd *UsageReportCmd) GetMetadata() plugin.PluginMetadata {
	return plugin.PluginMetadata{
		Name: "usage-report",
		Version: plugin.VersionType{
			Major: 1,
			Minor: 1,
			Build: 0,
		},
		Commands: []plugin.Command{
			{
				Name:     "usage-report",
				HelpText: "Report AI and memory usage for orgs and spaces",
				UsageDetails: plugin.Usage{
					Usage: "cf usage-report",
				},
			},
		},
	}
}

//UsageReportCommand doer
func (cmd *UsageReportCmd) UsageReportCommand(args []string) {
	fmt.Println("Gathering usage information")

	if nil == cmd.cli {
		fmt.Println("ERROR: CLI Connection is nil!")
		os.Exit(1)
	}

	orgs, err := cmd.getOrgs()
	if nil != err {
		fmt.Println(err)
		os.Exit(1)
	}

	totalApps := 0
	totalInstances := 0
	for _, org := range orgs {
		fmt.Printf("Org %s is consuming %d MB of %d MB.\n", org.name, org.memoryUsage, org.memoryQuota)
		for _, space := range org.spaces {
			consumed := 0
			instances := 0
			runningApps := 0
			runningInstances := 0
			for _, app := range space.apps {
				if app.running {
					consumed += int(app.instances * app.ram)
					runningApps++
					runningInstances += app.instances
				}
				instances += int(app.instances)
			}
			fmt.Printf("\tSpace %s is consuming %d MB memory (%d%%) of org quota.\n",
				space.name, consumed, (100 * consumed / org.memoryQuota))
			fmt.Printf("\t\t%d apps: %d running %d stopped\n", len(space.apps),
				runningApps, len(space.apps)-runningApps)
			fmt.Printf("\t\t%d instances: %d running, %d stopped\n", instances,
				runningInstances, instances-runningInstances)
			totalInstances += instances
			totalApps += len(space.apps)
		}
	}
	fmt.Printf("You are running %d apps in %d orgs, with a total of %d instances.\n",
		totalApps, len(orgs), totalInstances)
}

func (cmd *UsageReportCmd) getOrgs() ([]org, error) {
	rawOrgs, err := cmd.apiHelper.GetOrgs(cmd.cli)
	if nil != err {
		return nil, err
	}

	var orgs = []org{}

	for _, o := range rawOrgs {
		usage, err := cmd.apiHelper.GetOrgMemoryUsage(cmd.cli, o)
		if nil != err {
			return nil, err
		}
		quota, err := cmd.apiHelper.GetQuotaMemoryLimit(cmd.cli, o.QuotaURL)
		if nil != err {
			return nil, err
		}
		spaces, err := cmd.getSpaces(o.SpacesURL)
		if nil != err {
			return nil, err
		}

		orgs = append(orgs, org{
			name:        o.Name,
			memoryQuota: int(quota),
			memoryUsage: int(usage),
			spaces:      spaces,
{'fraction_non_alphanumeric': 0.13141190598730484, 'fraction_numerical': 0.004803568365071196, 'mean_word_length': 3.1702432045779685, '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': '7411375', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2065, 'n_words': 548}