Skip to content

Commit c93490e

Browse files
committed
⭐ show assessments inside of blocks
Signed-off-by: Dominik Richter <[email protected]>
1 parent 53a692f commit c93490e

File tree

5 files changed

+136
-72
lines changed

5 files changed

+136
-72
lines changed

apps/cnquery/cmd/plugin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (c *cnqueryPlugin) RunQuery(conf *run.RunQueryConfig, runtime *providers.Ru
8181
return errors.Wrap(err, "failed to compile command")
8282
}
8383

84-
out.WriteString(logger.PrettyJSON((b)) + "\n" + printer.DefaultPrinter.CodeBundle(b))
84+
out.WriteString(logger.PrettyJSON((b)) + "\n" + printer.DefaultPrinter.CodeBundle(b.Expand()))
8585
return nil
8686
}
8787

cli/printer/mql.go

+110-45
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"golang.org/x/exp/slices"
1818
)
1919

20-
func (print *Printer) Datas(bundle *llx.CodeBundle, results map[string]*llx.RawResult) string {
20+
func (print *Printer) Datas(bundle *llx.ExpandedCodeBundle, results map[string]*llx.RawResult) string {
2121
var res strings.Builder
2222
i := 0
2323
for _, v := range results {
@@ -33,17 +33,18 @@ func (print *Printer) Datas(bundle *llx.CodeBundle, results map[string]*llx.RawR
3333
// Results prints a full query with all data points
3434
// NOTE: ensure that results only contains results that match the bundle!
3535
func (print *Printer) Results(bundle *llx.CodeBundle, results map[string]*llx.RawResult) string {
36+
ebundle := bundle.Expand()
3637
assessment := llx.Results2Assessment(bundle, results)
3738

3839
if assessment != nil {
39-
return print.Assessment(bundle, assessment)
40+
return print.Assessment(ebundle, assessment)
4041
}
4142

42-
return print.Datas(bundle, results)
43+
return print.Datas(ebundle, results)
4344
}
4445

4546
// Assessment prints a complete comparable assessment
46-
func (print *Printer) Assessment(bundle *llx.CodeBundle, assessment *llx.Assessment) string {
47+
func (print *Printer) Assessment(bundle *llx.ExpandedCodeBundle, assessment *llx.Assessment) string {
4748
var res strings.Builder
4849

4950
var indent string
@@ -74,7 +75,7 @@ func isBooleanOp(op string) bool {
7475
return op == "&&" || op == "||"
7576
}
7677

77-
func (print *Printer) assessment(bundle *llx.CodeBundle, assessment *llx.AssessmentItem, indent string) string {
78+
func (print *Printer) assessment(bundle *llx.ExpandedCodeBundle, assessment *llx.AssessmentItem, indent string) string {
7879
var codeID string
7980
if bundle.CodeV2 != nil {
8081
codeID = bundle.CodeV2.Id
@@ -161,15 +162,15 @@ func (print *Printer) assessment(bundle *llx.CodeBundle, assessment *llx.Assessm
161162
}
162163

163164
// Result prints the llx raw result into a string
164-
func (print *Printer) Result(result *llx.RawResult, bundle *llx.CodeBundle) string {
165+
func (print *Printer) Result(result *llx.RawResult, bundle *llx.ExpandedCodeBundle) string {
165166
if result == nil {
166167
return "< no result? >"
167168
}
168169

169170
return print.DataWithLabel(result.Data, result.CodeID, bundle, "")
170171
}
171172

172-
func (print *Printer) label(ref string, bundle *llx.CodeBundle, isResource bool) string {
173+
func (print *Printer) label(ref string, bundle *llx.ExpandedCodeBundle, isResource bool) string {
173174
if bundle == nil {
174175
return ""
175176
}
@@ -187,7 +188,7 @@ func (print *Printer) label(ref string, bundle *llx.CodeBundle, isResource bool)
187188
return print.Primary(label) + ": "
188189
}
189190

190-
func (print *Printer) defaultLabel(ref string, bundle *llx.CodeBundle) string {
191+
func (print *Printer) defaultLabel(ref string, bundle *llx.ExpandedCodeBundle) string {
191192
if bundle == nil {
192193
return ""
193194
}
@@ -205,7 +206,7 @@ func (print *Printer) defaultLabel(ref string, bundle *llx.CodeBundle) string {
205206
return print.Primary(label) + "="
206207
}
207208

208-
func isDefaultField(ref string, bundle *llx.CodeBundle, defaultFields []string) bool {
209+
func isDefaultField(ref string, bundle *llx.ExpandedCodeBundle, defaultFields []string) bool {
209210
if bundle == nil {
210211
return false
211212
}
@@ -223,7 +224,7 @@ func isDefaultField(ref string, bundle *llx.CodeBundle, defaultFields []string)
223224
return slices.Contains(defaultFields, label)
224225
}
225226

226-
func (print *Printer) array(typ types.Type, data []interface{}, codeID string, bundle *llx.CodeBundle, indent string) string {
227+
func (print *Printer) array(typ types.Type, data []interface{}, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
227228
if len(data) == 0 {
228229
return "[]"
229230
}
@@ -265,7 +266,7 @@ func (print *Printer) assessmentTemplate(template string, data []string) string
265266
return res
266267
}
267268

268-
func (print *Printer) dataAssessment(codeID string, data map[string]interface{}, bundle *llx.CodeBundle) string {
269+
func (print *Printer) dataAssessment(codeID string, data map[string]interface{}, bundle *llx.ExpandedCodeBundle) string {
269270
var assertion *llx.AssertionMessage
270271
var ok bool
271272

@@ -309,31 +310,57 @@ func (print *Printer) dataAssessment(codeID string, data map[string]interface{},
309310
return print.assessmentTemplate(assertion.Template, fields)
310311
}
311312

312-
func (print *Printer) refMap(typ types.Type, data map[string]interface{}, codeID string, bundle *llx.CodeBundle, indent string) string {
313+
func (print *Printer) block(typ types.Type, data map[string]interface{}, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
313314
if len(data) == 0 {
314315
return "{}"
315316
}
316317

317318
var res strings.Builder
318319

319-
// we need to separate entries that are unlabelled (eg part of an assertion)
320-
labeledKeys := []string{}
321-
keys := sortx.Keys(data)
322-
for i := range keys {
323-
if _, ok := bundle.Labels.Labels[keys[i]]; ok {
324-
labeledKeys = append(labeledKeys, keys[i])
325-
}
326-
}
327-
328-
code := bundle.CodeV2
329-
ep := code.Blocks[0].Entrypoints[0]
330-
chunk := code.Chunk(ep)
331320
listType := ""
332-
switch chunk.Id {
333-
case "$one", "$all", "$none", "$any":
334-
ref := chunk.Function.Binding
335-
listChunk := code.Chunk(ref)
336-
listType = types.Type(listChunk.Type()).Child().Label()
321+
code := bundle.CodeV2
322+
var block *llx.Block
323+
var funBlock *llx.Block
324+
if blockRef, ok := bundle.Ref2CodeID[codeID]; ok {
325+
block = code.Block(blockRef)
326+
}
327+
if len(block.Entrypoints) > 0 {
328+
ep := block.Entrypoints[0]
329+
chunk := code.Chunk(ep)
330+
switch chunk.Id {
331+
case "{}":
332+
arg := chunk.Function.Args[0]
333+
fref := arg.RawData().Value.(uint64)
334+
funBlock = bundle.CodeV2.Block(fref)
335+
case "$one", "$all", "$none", "$any":
336+
ref := chunk.Function.Binding
337+
listChunk := code.Chunk(ref)
338+
listType = types.Type(listChunk.Type()).Child().Label()
339+
}
340+
}
341+
342+
// collect all checksums that will be considered for printing of this
343+
// block's data
344+
dataChecksums := []string{}
345+
346+
// We generally prefer to directly access the function block and
347+
// collect only the entrypoints. The raw data contains both entrypoints
348+
// and datapoints and the latter are only needed to provide additional
349+
// information on the output.
350+
if funBlock != nil && len(funBlock.Entrypoints) > 0 {
351+
for _, ep := range funBlock.Entrypoints {
352+
csum := code.Checksums[ep]
353+
if _, ok := bundle.Labels.Labels[csum]; ok {
354+
dataChecksums = append(dataChecksums, csum)
355+
}
356+
}
357+
} else {
358+
keys := sortx.Keys(data)
359+
for i := range keys {
360+
if _, ok := bundle.Labels.Labels[keys[i]]; ok {
361+
dataChecksums = append(dataChecksums, keys[i])
362+
}
363+
}
337364
}
338365

339366
nonDefaultFields := []string{}
@@ -345,7 +372,7 @@ func (print *Printer) refMap(typ types.Type, data map[string]interface{}, codeID
345372
}
346373
}
347374

348-
for _, k := range labeledKeys {
375+
for _, k := range dataChecksums {
349376
if k == "_" {
350377
continue
351378
}
@@ -371,8 +398,34 @@ func (print *Printer) refMap(typ types.Type, data map[string]interface{}, codeID
371398

372399
if len(nonDefaultFields) > 0 {
373400
res.WriteString("{\n")
401+
skipFields := map[string]struct{}{}
402+
for _, k := range nonDefaultFields {
403+
if ref, ok := bundle.Ref2CodeID[k]; ok {
404+
skip := []string{}
405+
assessment := code.Entrypoint2Assessment(bundle.CodeBundle, ref, func(s string) (*llx.RawResult, bool) {
406+
found, ok := data[s]
407+
if !ok {
408+
return nil, false
409+
}
410+
skip = append(skip, s)
411+
return &llx.RawResult{Data: found.(*llx.RawData), CodeID: codeID}, true
412+
})
413+
if assessment != nil {
414+
res.WriteString(indent)
415+
res.WriteString(" ")
416+
res.WriteString(print.assessment(bundle, assessment, indent+" "))
417+
res.WriteByte('\n')
418+
for _, v := range skip {
419+
skipFields[v] = struct{}{}
420+
}
421+
422+
continue
423+
}
424+
}
425+
}
426+
374427
for _, k := range nonDefaultFields {
375-
if k == "_" {
428+
if _, ok := skipFields[k]; ok {
376429
continue
377430
}
378431

@@ -404,7 +457,7 @@ func (print *Printer) refMap(typ types.Type, data map[string]interface{}, codeID
404457
return res.String()
405458
}
406459

407-
func (print *Printer) stringMap(typ types.Type, data map[string]interface{}, codeID string, bundle *llx.CodeBundle, indent string) string {
460+
func (print *Printer) stringMap(typ types.Type, data map[string]interface{}, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
408461
if len(data) == 0 {
409462
return "{}"
410463
}
@@ -423,7 +476,7 @@ func (print *Printer) stringMap(typ types.Type, data map[string]interface{}, cod
423476
return res.String()
424477
}
425478

426-
func (print *Printer) dict(typ types.Type, raw interface{}, codeID string, bundle *llx.CodeBundle, indent string) string {
479+
func (print *Printer) dict(typ types.Type, raw interface{}, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
427480
if raw == nil {
428481
return print.Secondary("null")
429482
}
@@ -488,7 +541,7 @@ func (print *Printer) dict(typ types.Type, raw interface{}, codeID string, bundl
488541
}
489542
}
490543

491-
func (print *Printer) intMap(typ types.Type, data map[int]interface{}, codeID string, bundle *llx.CodeBundle, indent string) string {
544+
func (print *Printer) intMap(typ types.Type, data map[int]interface{}, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
492545
var res strings.Builder
493546
res.WriteString("{\n")
494547

@@ -515,7 +568,7 @@ func isCodeBlock(codeID string, bundle *llx.CodeBundle) bool {
515568
return ok
516569
}
517570

518-
func (print *Printer) resourceContext(data any, checksum string, bundle *llx.CodeBundle, indent string) (string, bool) {
571+
func (print *Printer) resourceContext(data any, checksum string, bundle *llx.ExpandedCodeBundle, indent string) (string, bool) {
519572
m, ok := data.(map[string]any)
520573
if !ok {
521574
return "", false
@@ -577,7 +630,7 @@ func (print *Printer) resourceContext(data any, checksum string, bundle *llx.Cod
577630
return r, true
578631
}
579632

580-
func (print *Printer) autoExpand(blockRef uint64, data interface{}, bundle *llx.CodeBundle, indent string) string {
633+
func (print *Printer) autoExpand(blockRef uint64, data interface{}, bundle *llx.ExpandedCodeBundle, indent string) string {
581634
var res strings.Builder
582635

583636
if arr, ok := data.([]interface{}); ok {
@@ -675,7 +728,7 @@ func (print *Printer) autoExpand(blockRef uint64, data interface{}, bundle *llx.
675728
return res.String()
676729
}
677730

678-
func (print *Printer) Data(typ types.Type, data interface{}, codeID string, bundle *llx.CodeBundle, indent string) string {
731+
func (print *Printer) Data(typ types.Type, data interface{}, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
679732
if typ.NotSet() {
680733
return "no data available"
681734
}
@@ -781,7 +834,7 @@ func (print *Printer) Data(typ types.Type, data interface{}, codeID string, bund
781834
return llx.ScoreString(data.([]byte))
782835

783836
case types.Block:
784-
return print.refMap(typ, data.(map[string]interface{}), codeID, bundle, indent)
837+
return print.block(typ, data.(map[string]any), codeID, bundle, indent)
785838

786839
case types.Version:
787840
return print.Secondary(data.(string))
@@ -837,7 +890,7 @@ func (print *Printer) Data(typ types.Type, data interface{}, codeID string, bund
837890
}
838891

839892
// DataWithLabel prints RawData into a string
840-
func (print *Printer) DataWithLabel(r *llx.RawData, codeID string, bundle *llx.CodeBundle, indent string) string {
893+
func (print *Printer) DataWithLabel(r *llx.RawData, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
841894
b := strings.Builder{}
842895
if r.Error != nil {
843896
b.WriteString(print.Error(strings.TrimSpace(r.Error.Error())))
@@ -871,7 +924,7 @@ func (print *Printer) CompilerStats(stats mqlc.CompilerStats) string {
871924
}
872925

873926
// CodeBundle prints the contents of the MQL query
874-
func (print *Printer) CodeBundle(bundle *llx.CodeBundle) string {
927+
func (print *Printer) CodeBundle(bundle *llx.ExpandedCodeBundle) string {
875928
var res strings.Builder
876929

877930
res.WriteString(print.CodeV2(bundle.CodeV2, bundle, ""))
@@ -884,7 +937,7 @@ func (print *Printer) CodeBundle(bundle *llx.CodeBundle) string {
884937
return res.String()
885938
}
886939

887-
func (print *Printer) CodeV2(code *llx.CodeV2, bundle *llx.CodeBundle, indent string) string {
940+
func (print *Printer) CodeV2(code *llx.CodeV2, bundle *llx.ExpandedCodeBundle, indent string) string {
888941
var res strings.Builder
889942
indent += " "
890943

@@ -903,6 +956,18 @@ func (print *Printer) CodeV2(code *llx.CodeV2, bundle *llx.CodeBundle, indent st
903956
}
904957
res.WriteString("]\n")
905958

959+
if len(block.Datapoints) > 0 {
960+
res.WriteString(indent)
961+
res.WriteString("datapoints: [")
962+
for idx, ep := range block.Datapoints {
963+
res.WriteString(fmt.Sprintf("<%d,%d>", ep>>32, ep&0xFFFFFFFF))
964+
if idx != len(block.Datapoints)-1 {
965+
res.WriteString(" ")
966+
}
967+
}
968+
res.WriteString("]\n")
969+
}
970+
906971
for j := range block.Chunks {
907972
res.WriteString(" ")
908973
print.chunkV2(j, block.Chunks[j], block, bundle, indent, &res)
@@ -912,7 +977,7 @@ func (print *Printer) CodeV2(code *llx.CodeV2, bundle *llx.CodeBundle, indent st
912977
return res.String()
913978
}
914979

915-
func (print *Printer) chunkV2(idx int, chunk *llx.Chunk, block *llx.Block, bundle *llx.CodeBundle, indent string, w *strings.Builder) {
980+
func (print *Printer) chunkV2(idx int, chunk *llx.Chunk, block *llx.Block, bundle *llx.ExpandedCodeBundle, indent string, w *strings.Builder) {
916981
sidx := strconv.Itoa(idx+1) + ": "
917982

918983
switch chunk.Call {
@@ -948,7 +1013,7 @@ func (print *Printer) chunkV2(idx int, chunk *llx.Chunk, block *llx.Block, bundl
9481013
w.WriteString("\n")
9491014
}
9501015

951-
func (print *Printer) functionV2(f *llx.Function, codeID string, bundle *llx.CodeBundle, indent string) string {
1016+
func (print *Printer) functionV2(f *llx.Function, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
9521017
argsStr := ""
9531018
if len(f.Args) > 0 {
9541019
argsStr = " (" + strings.TrimSpace(print.primitives(f.Args, codeID, bundle, indent)) + ")"
@@ -960,7 +1025,7 @@ func (print *Printer) functionV2(f *llx.Function, codeID string, bundle *llx.Cod
9601025
argsStr
9611026
}
9621027

963-
func (print *Printer) primitives(list []*llx.Primitive, codeID string, bundle *llx.CodeBundle, indent string) string {
1028+
func (print *Printer) primitives(list []*llx.Primitive, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
9641029
if len(list) == 0 {
9651030
return ""
9661031
}
@@ -978,7 +1043,7 @@ func (print *Printer) primitives(list []*llx.Primitive, codeID string, bundle *l
9781043
}
9791044

9801045
// Primitive prints the llx primitive to a string
981-
func (print *Printer) Primitive(primitive *llx.Primitive, codeID string, bundle *llx.CodeBundle, indent string) string {
1046+
func (print *Printer) Primitive(primitive *llx.Primitive, codeID string, bundle *llx.ExpandedCodeBundle, indent string) string {
9821047
if primitive == nil {
9831048
return "?"
9841049
}

llx/code.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (l *CodeV2) returnValues(bundle *CodeBundle, lookup func(s string) (*RawRes
307307
return res
308308
}
309309

310-
func (l *CodeV2) entrypoint2assessment(bundle *CodeBundle, ref uint64, lookup func(s string) (*RawResult, bool)) *AssessmentItem {
310+
func (l *CodeV2) Entrypoint2Assessment(bundle *CodeBundle, ref uint64, lookup func(s string) (*RawResult, bool)) *AssessmentItem {
311311
code := bundle.CodeV2
312312
checksum := code.Checksums[ref]
313313

0 commit comments

Comments
 (0)