Skip to content

Commit

Permalink
Implement fallback field list - closes #2. Refactor terminology.
Browse files Browse the repository at this point in the history
Signed-off-by: Utku Ozdemir <uoz@protonmail.com>
  • Loading branch information
utkuozdemir committed Jun 24, 2021
1 parent 8d58587 commit 02f89ba
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 147 deletions.
10 changes: 5 additions & 5 deletions cmd/nvidia_gpu_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func main() {
nvidiaSmiCommand = kingpin.Flag("nvidia-smi-command",
"Path or command to be used for the nvidia-smi executable").
Default(exporter.DefaultNvidiaSmiCommand).String()
queryFieldNames = kingpin.Flag("query-field-names",
qFields = kingpin.Flag("query-field-names",
fmt.Sprintf("Comma-separated list of the query fields. "+
"You can find out possible fields by running `nvidia-smi --help-query-gpus`. "+
"The value `%s` will automatically detect the fields to query.", exporter.DefaultQueryFieldNames)).
Default(exporter.DefaultQueryFieldNames).String()
"The value `%s` will automatically detect the fields to query.", exporter.DefaultQField)).
Default(exporter.DefaultQField).String()
)

promlogConfig := &promlog.Config{}
Expand All @@ -53,7 +53,7 @@ func main() {
kingpin.Parse()

logger := promlog.New(promlogConfig)
e, err := exporter.New(exporter.DefaultPrefix, *nvidiaSmiCommand, *queryFieldNames, logger)
e, err := exporter.New(exporter.DefaultPrefix, *nvidiaSmiCommand, *qFields, logger)
if err != nil {
_ = level.Error(logger).Log("msg", "Error on creating exporter", "err", err)
os.Exit(1)
Expand Down Expand Up @@ -90,6 +90,6 @@ func NewRootHandler(logger log.Logger, metricsPath string) *RootHandler {
func (r *RootHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write(r.response)
if err != nil {
_ = level.Error(r.logger).Log("Error writing redirect", "err", err)
_ = level.Error(r.logger).Log("msg", "Error writing redirect", "err", err)
}
}
2 changes: 1 addition & 1 deletion cmd/query_fields_parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
)

kingpin.Parse()
fields, err := exporter.ParseQueryFields(*nvidiaSmiCommand)
fields, err := exporter.ParseAutoQFields(*nvidiaSmiCommand)
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
Expand Down
50 changes: 25 additions & 25 deletions internal/exporter/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,67 @@ package exporter
import "strings"

type table struct {
rows []row
returnedFieldNames []string
queryFieldNameToCells map[string][]cell
rows []row
rFields []string
qFieldToCells map[string][]cell
}

type row struct {
queryFieldNameToCells map[string]cell
cells []cell
qFieldToCells map[string]cell
cells []cell
}

type cell struct {
queryFieldName string
returnedFieldName string
rawValue string
qField string
rField string
rawValue string
}

func parseCSVIntoTable(queryResult string, queryFieldNames []string) table {
func parseCSVIntoTable(queryResult string, qFields []string) table {
lines := strings.Split(strings.TrimSpace(queryResult), "\n")
titlesLine := lines[0]
valuesLines := lines[1:]
returnedFieldNames := parseCSVLine(titlesLine)

numCols := len(queryFieldNames)
numCols := len(qFields)
numRows := len(valuesLines)

rows := make([]row, numRows)

queryFieldNameToCells := make(map[string][]cell)
for _, queryFieldName := range queryFieldNames {
queryFieldNameToCells[queryFieldName] = make([]cell, numRows)
qFieldToCells := make(map[string][]cell)
for _, qField := range qFields {
qFieldToCells[qField] = make([]cell, numRows)
}

for rowIndex, valuesLine := range valuesLines {
queryFieldNameToCell := make(map[string]cell, numCols)
qFieldToCell := make(map[string]cell, numCols)
cells := make([]cell, numCols)
rawValues := parseCSVLine(valuesLine)
for colIndex, rawValue := range rawValues {
queryFieldName := queryFieldNames[colIndex]
qField := qFields[colIndex]
gm := cell{
queryFieldName: queryFieldName,
returnedFieldName: returnedFieldNames[colIndex],
rawValue: rawValue,
qField: qField,
rField: returnedFieldNames[colIndex],
rawValue: rawValue,
}
queryFieldNameToCell[queryFieldName] = gm
qFieldToCell[qField] = gm
cells[colIndex] = gm
queryFieldNameToCells[queryFieldName][rowIndex] = gm
qFieldToCells[qField][rowIndex] = gm
}

gmc := row{
queryFieldNameToCells: queryFieldNameToCell,
cells: cells,
qFieldToCells: qFieldToCell,
cells: cells,
}

rows[rowIndex] = gmc

}

return table{
rows: rows,
returnedFieldNames: returnedFieldNames,
queryFieldNameToCells: queryFieldNameToCells,
rows: rows,
rFields: returnedFieldNames,
qFieldToCells: qFieldToCells,
}
}

Expand Down
24 changes: 12 additions & 12 deletions internal/exporter/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ Some Dummy GPU, 12.34 W
func TestParseCsvIntoTable(t *testing.T) {
parsed := parseCSVIntoTable(testCsv, []string{"name", "power.draw"})
assert.Len(t, parsed.rows, 2)
assert.Equal(t, []string{"name", "power.draw [W]"}, parsed.returnedFieldNames)
assert.Equal(t, []string{"name", "power.draw [W]"}, parsed.rFields)

cell00 := cell{queryFieldName: "name", returnedFieldName: "name", rawValue: "NVIDIA GeForce RTX 2080 SUPER"}
cell01 := cell{queryFieldName: "power.draw", returnedFieldName: "power.draw [W]", rawValue: "30.14 W"}
cell10 := cell{queryFieldName: "name", returnedFieldName: "name", rawValue: "Some Dummy GPU"}
cell11 := cell{queryFieldName: "power.draw", returnedFieldName: "power.draw [W]", rawValue: "12.34 W"}
cell00 := cell{qField: "name", rField: "name", rawValue: "NVIDIA GeForce RTX 2080 SUPER"}
cell01 := cell{qField: "power.draw", rField: "power.draw [W]", rawValue: "30.14 W"}
cell10 := cell{qField: "name", rField: "name", rawValue: "Some Dummy GPU"}
cell11 := cell{qField: "power.draw", rField: "power.draw [W]", rawValue: "12.34 W"}

row0 := row{
queryFieldNameToCells: map[string]cell{"name": cell00, "power.draw": cell01},
cells: []cell{cell00, cell01},
qFieldToCells: map[string]cell{"name": cell00, "power.draw": cell01},
cells: []cell{cell00, cell01},
}

row1 := row{
queryFieldNameToCells: map[string]cell{"name": cell10, "power.draw": cell11},
cells: []cell{cell10, cell11},
qFieldToCells: map[string]cell{"name": cell10, "power.draw": cell11},
cells: []cell{cell10, cell11},
}

expected := table{
rows: []row{row0, row1},
returnedFieldNames: []string{"name", "power.draw [W]"},
queryFieldNameToCells: map[string][]cell{
rows: []row{row0, row1},
rFields: []string{"name", "power.draw [W]"},
qFieldToCells: map[string][]cell{
"name": {cell00, cell10},
"power.draw": {cell01, cell11},
},
Expand Down
Loading

0 comments on commit 02f89ba

Please sign in to comment.