Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to dump all resource data to text file #981

Merged
merged 43 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
39760f3
Adds 'scout' command and 'resources' sub command.
jasonhawkharris Apr 28, 2023
5da36f4
Merge branch 'main' into jhh/src-resource
jasonhawkharris Apr 28, 2023
718560a
This commit changes output pattern from unformatted lines to a table
jasonhawkharris Apr 28, 2023
5340cff
adds unit test for ResroucesK8s
jasonhawkharris May 1, 2023
0bb6274
formatting/retabbing
jasonhawkharris May 1, 2023
2bd386d
Adds --docker flag to print resources of docker deployment
jasonhawkharris May 2, 2023
6dfdfda
edits docs to reflect which sub commands are currently available to use
jasonhawkharris May 2, 2023
5831881
refactor
jasonhawkharris May 2, 2023
a66ed4c
fix build issues
jasonhawkharris May 2, 2023
bd01abc
replace hard coded file path with path generated by homedir package
jasonhawkharris May 2, 2023
3263141
Update cmd/src/scout_resources.go
jasonhawkharris May 2, 2023
94642d9
Update cmd/src/scout_resources.go
jasonhawkharris May 2, 2023
94bcd30
Update internal/scout/resources/resources.go
jasonhawkharris May 2, 2023
3b95e21
Update internal/scout/resources/resources.go
jasonhawkharris May 2, 2023
a1768d4
Update internal/scout/resources/resources.go
jasonhawkharris May 2, 2023
0a9d3c1
don't capitalize error messages
jasonhawkharris May 2, 2023
2780bcc
remove capitalization on error message
jasonhawkharris May 2, 2023
36f2b7a
capitalization
jasonhawkharris May 2, 2023
3163878
add defer functions for closing tabwriter
jasonhawkharris May 2, 2023
3698d1f
capitalization
jasonhawkharris May 2, 2023
faf2878
change function invocations to reflect new function names and retab
jasonhawkharris May 2, 2023
60b8c73
reformats output for --docker flag
jasonhawkharris May 2, 2023
24d2fa5
retab
jasonhawkharris May 2, 2023
d6c9acf
Requested changes
jasonhawkharris May 4, 2023
79eec1a
test skeleton
jasonhawkharris May 11, 2023
c11677c
test for getMemUnits added
jasonhawkharris May 12, 2023
29225df
replace 'spy' with 'advise' in command instructions
jasonhawkharris May 12, 2023
8dc2bab
Add bubbles/bubble tea components to CLI for better UI/UX
jasonhawkharris May 15, 2023
d49544b
remove unnecessary tests. Formatting
jasonhawkharris May 15, 2023
1b4b89d
formatting and removes placeholder code/comments
jasonhawkharris May 15, 2023
21f66e9
remove redundant return statement
jasonhawkharris May 15, 2023
ee1e64e
export to file now works for docker as well.
jasonhawkharris May 15, 2023
575a290
refactor
jasonhawkharris May 15, 2023
941bc31
add file export functionality for docker
jasonhawkharris May 15, 2023
20a6c47
fix errors linter caught
jasonhawkharris May 16, 2023
69f4216
fix import order (linter caught)
jasonhawkharris May 16, 2023
e3dddef
remove unnecessary integration test
jasonhawkharris May 16, 2023
3ee7c08
import order
jasonhawkharris May 16, 2023
a0695a0
fix merge conflict
jasonhawkharris May 16, 2023
d5ce0af
add the ability to dump all container resource data txt file
jasonhawkharris May 16, 2023
cee0234
fix merge conflicts
jasonhawkharris May 16, 2023
dbc69f0
Merge branch 'main' into jhh/src-resource
jasonhawkharris May 16, 2023
992739c
Merge branch 'jhh/copy-all' into jhh/src-resource
jasonhawkharris May 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/src/scout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
var scoutCommands commander

func init() {
usage := `'src scout' is a tool that provides monitoring for Sourcegraph resource allocations
usage := `'src scout' is a tool that provides monitoring for Sourcegraph resources

EXPERIMENTAL: 'scout' is an experimental command in the 'src' tool. To use, you must
point your .kube config to your Sourcegraph instance.
Expand Down
130 changes: 89 additions & 41 deletions internal/scout/resource/style/Table.go
jasonhawkharris marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package style
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/tabwriter"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sourcegraph/sourcegraph/lib/errors"
"golang.design/x/clipboard"
)

Expand All @@ -32,14 +36,29 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
copiedMessage := lipgloss.NewStyle().
Foreground(lipgloss.Color("#32CD32")).
Render(fmt.Sprintf(
"Copied resource allocations for %s to clipboard",
"Copied resource allocations for '%s' to clipboard",
m.table.SelectedRow()[0],
))
return m, tea.Batch(
tea.Printf(
copiedMessage,
),
)
case "C":
tmpDir := os.TempDir()
filePath := filepath.Join(tmpDir, "resource-dump.txt")
m.dumpResources(m.table.Rows(), filePath)
savedMessage := lipgloss.NewStyle().
Foreground(lipgloss.Color("#32CD32")).
Render(fmt.Sprintf(
"saved resource allocations to %s",
filePath,
))
return m, tea.Batch(
tea.Printf(
savedMessage,
),
)
}
}
m.table, cmd = m.table.Update(msg)
Expand All @@ -49,54 +68,83 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string {
s := "\n > Press 'j' and 'k' to go up and down\n"
s += " > Press 'c' to copy highlighted row to clipboard\n"
s += " > Press 'C' to copy all rows to a file\n"
s += " > Press 'q' to quit\n\n"
s += baseStyle.Render(m.table.View()) + "\n"
return s
}

func (m model) dumpResources(rows []table.Row, filePath string) error {
dumpFile, err := os.Create(filePath)
if err != nil {
return errors.Wrap(err, "error while creating new file")
}
defer dumpFile.Close()

tw := tabwriter.NewWriter(dumpFile, 0, 0, 3, ' ', 0)
defer tw.Flush()

// default to docker terms
headers := []string{
"NAME",
"CPU CORES",
"CPU SHARES",
"MEM LIMITS",
"MEM RESERVATIONS",
}

// kubernetes rows will always have 6 items
// change column headers to reflect k8s terms
if len(rows[0]) == 6 {
headers = []string{
"NAME",
"CPU LIMITS",
"CPU REQUESTS",
"MEM LIMITS",
"MEM REQUESTS",
"CAPACITY",
}
}

fmt.Fprintf(tw, strings.Join(headers, "\t")+"\n")

for _, row := range rows {
values := []string{row[0], row[1], row[2], row[3], row[4]}
if len(row) == 6 {
values = append(values, row[5])
}
fmt.Fprintf(tw, strings.Join(values, "\t")+"\n")
}
return nil
}

func (m model) copyRowToClipboard(row table.Row) {
var containerInfo string

// change output based on the length of row
// docker rows will always be length of 5
// kubernetes rows will always be length of 6
if len(row) == 5 {
name := row[0]
cpuCores := row[1]
cpuShares := row[2]
memLimits := row[3]
memReservations := row[4]
containerInfo = fmt.Sprintf(`container: %s
cpu cores: %s
cpu shares: %s
mem limits: %s
mem reservations: %s`,
name,
cpuCores,
cpuShares,
memLimits,
memReservations,
)
} else if len(row) == 6 {
name := row[0]
cpuLimits := row[1]
cpuRequests := row[2]
memLimits := row[3]
memRequests := row[4]
capacity := row[5]
containerInfo = fmt.Sprintf(`container: %s
cpu limits: %s
cpu requests: %s
mem limits: %s
mem requests: %s
disk capacity: %s`,
name,
cpuLimits,
cpuRequests,
memLimits,
memRequests,
capacity,
)
// default to docker headers
headers := []string{
"NAME",
"CPU CORES",
"CPU SHARES",
"MEM LIMITS",
"MEM RESERVATIONS",
}

// kubernetes rows will always have 6 items
// change column headers to reflect k8s terms
if len(row) == 6 {
headers = []string{
"NAME",
"CPU LIMITS",
"CPU REQUESTS",
"MEM LIMITS",
"MEM REQUESTS",
"CAPACITY",
}
}

for i, header := range headers {
containerInfo += fmt.Sprintf("%s: %s\n", header, row[i])
}

clipboard.Write(clipboard.FmtText, []byte(containerInfo))
Expand Down