Skip to content

Commit

Permalink
Add TopN query
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Darbord committed Oct 28, 2024
1 parent d0866be commit 1b3d721
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Famix-OpenTelemetry-Importer/OTelTopNQuery.class.st
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
"
I group spans with my `selector` block, sort them with my `comparator` (or do not sort if not provided), and keep only the top `count`.
"
Class {
#name : 'OTelTopNQuery',
#superclass : 'OTelQuery',
#instVars : [
'comparator',
'count'
],
#category : 'Famix-OpenTelemetry-Importer',
#package : 'Famix-OpenTelemetry-Importer'
}

{ #category : 'accessing' }
OTelTopNQuery >> comparator [
"If not specified, do not sort"

^ comparator
]

{ #category : 'accessing' }
OTelTopNQuery >> comparator: aTwoArgsBlock [

comparator := aTwoArgsBlock
]

{ #category : 'transforming' }
OTelTopNQuery >> count [

^ count ifNil: [ count := 1 ]
]

{ #category : 'transforming' }
OTelTopNQuery >> count: anInteger [

count := anInteger max: 1
]

{ #category : 'transforming' }
OTelTopNQuery >> value: model [
"Only keep the top n of each group in the model."

(model allSpans groupedBy: self selector) do: [ :spans |
| sortedSpans |
sortedSpans := self comparator
ifNil: [ spans ]
ifNotNil: [ :comp | spans sorted: comp ].
self count + 1 to: spans size do: [ :i |
self removeSpan: (sortedSpans at: i) ] ].
^ model
]

0 comments on commit 1b3d721

Please sign in to comment.