-
Notifications
You must be signed in to change notification settings - Fork 16
Sparkline #110
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
Merged
Sparkline #110
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cbd7330
Initial sparkline graph
zix99 bf47a50
Trim table and CSV support
zix99 b3dcea6
Merge branch 'master' into sparkline
zix99 fc30745
Add row/col sorter arguments
zix99 4804aa6
Fix default sort
zix99 e9c5edc
Remove todo
zix99 a7d4aa8
Test new command
zix99 6600e6d
Spark docs
zix99 6a24ced
Switch separate min/max compute to single function
zix99 bc5981c
Clarify docs
zix99 021a48b
Swap backwards comparison
zix99 aa38416
Unit test minmax
zix99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "rare/cmd/helpers" | ||
| "rare/pkg/aggregation" | ||
| "rare/pkg/color" | ||
| "rare/pkg/csv" | ||
| "rare/pkg/expressions" | ||
| "rare/pkg/multiterm" | ||
| "rare/pkg/multiterm/termrenderers" | ||
|
|
||
| "github.com/urfave/cli/v2" | ||
| ) | ||
|
|
||
| func sparkFunction(c *cli.Context) error { | ||
| var ( | ||
| delim = c.String("delim") | ||
| numRows = c.Int("num") | ||
| numCols = c.Int("cols") | ||
| noTruncate = c.Bool("notruncate") | ||
| scalerName = c.String(helpers.ScaleFlag.Name) | ||
| sortRows = c.String("sort-rows") | ||
| sortCols = c.String("sort-cols") | ||
| ) | ||
|
|
||
| counter := aggregation.NewTable(delim) | ||
|
|
||
| batcher := helpers.BuildBatcherFromArguments(c) | ||
| ext := helpers.BuildExtractorFromArguments(c, batcher) | ||
| rowSorter := helpers.BuildSorterOrFail(sortRows) | ||
| colSorter := helpers.BuildSorterOrFail(sortCols) | ||
|
|
||
| vt := helpers.BuildVTermFromArguments(c) | ||
| writer := termrenderers.NewSpark(vt, numRows, numCols) | ||
| writer.Scaler = helpers.BuildScalerOrFail(scalerName) | ||
|
|
||
| helpers.RunAggregationLoop(ext, counter, func() { | ||
|
|
||
| // Trim unused data from the data store (keep memory tidy!) | ||
| if !noTruncate { | ||
| if keepCols := counter.OrderedColumns(colSorter); len(keepCols) > numCols { | ||
| keepCols = keepCols[len(keepCols)-numCols:] | ||
| keepLookup := make(map[string]struct{}) | ||
| for _, item := range keepCols { | ||
| keepLookup[item] = struct{}{} | ||
| } | ||
| counter.Trim(func(col, row string, val int64) bool { | ||
| _, ok := keepLookup[col] | ||
| return !ok | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Write spark | ||
| writer.WriteTable(counter, rowSorter, colSorter) | ||
| writer.WriteFooter(0, helpers.FWriteExtractorSummary(ext, counter.ParseErrors(), | ||
| fmt.Sprintf("(R: %v; C: %v)", color.Wrapi(color.Yellow, counter.RowCount()), color.Wrapi(color.BrightBlue, counter.ColumnCount())))) | ||
| writer.WriteFooter(1, batcher.StatusString()) | ||
| }) | ||
|
|
||
| // Not deferred intentionally | ||
| writer.Close() | ||
|
|
||
| if err := helpers.TryWriteCSV(c, counter, csv.WriteTable); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return helpers.DetermineErrorState(batcher, ext, counter) | ||
| } | ||
|
|
||
| func sparkCommand() *cli.Command { | ||
| return helpers.AdaptCommandForExtractor(cli.Command{ | ||
| Name: "spark", | ||
| Aliases: []string{"sparkline", "s"}, | ||
| Usage: "Create rows of sparkline graphs", | ||
| Description: `Create rows of a sparkkline graph, all scaled equally | ||
| based on a table like input`, | ||
| Category: cmdCatVisualize, | ||
| Action: sparkFunction, | ||
| Flags: []cli.Flag{ | ||
| &cli.StringFlag{ | ||
| Name: "delim", | ||
| Usage: "Character to tabulate on. Use {$} helper by default", | ||
| Value: expressions.ArraySeparatorString, | ||
| }, | ||
| &cli.IntFlag{ | ||
| Name: "num", | ||
| Aliases: []string{"rows", "n"}, | ||
| Usage: "Number of elements (rows) to display", | ||
| Value: 20, | ||
| }, | ||
| &cli.IntFlag{ | ||
| Name: "cols", | ||
| Usage: "Number of columns to display", | ||
| Value: multiterm.TermCols() - 15, | ||
| }, | ||
| &cli.BoolFlag{ | ||
| Name: "notruncate", | ||
| Usage: "Disable truncating data that doesnt fit in the sparkline", | ||
| Value: false, | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "sort-rows", | ||
| Usage: helpers.DefaultSortFlag.Usage, | ||
| Value: "value", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "sort-cols", | ||
| Usage: helpers.DefaultSortFlag.Usage, | ||
| Value: "numeric", | ||
| }, | ||
| helpers.SnapshotFlag, | ||
| helpers.NoOutFlag, | ||
| helpers.CSVFlag, | ||
| helpers.ScaleFlag, | ||
| }, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSparkline(t *testing.T) { | ||
| testCommandSet(t, sparkCommand(), | ||
| `-m "(.+) (\d+)" -e "{$ {1} {2}}" testdata/graph.txt`, | ||
| `-o - -m "(.+) (\d+)" -e "{$ {1} {2}}" testdata/graph.txt`, | ||
| ) | ||
| } | ||
|
|
||
| func TestSparklineWithTrim(t *testing.T) { | ||
| out, eout, err := testCommandCapture(sparkCommand(), `--snapshot -m "(.+) (.+)" -e {1} -e {2} --cols 2 testdata/heat.txt`) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Empty(t, eout) | ||
| assert.Contains(t, out, " First bc Last \ny 1 _█ 2 \nx 1 __ 1 \nMatched: 10 / 10 (R: 2; C: 2)") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.