Skip to content

Commit 92aefd3

Browse files
committed
refactor: remove jank fuzzy find
1 parent 577df06 commit 92aefd3

File tree

3 files changed

+2
-87
lines changed

3 files changed

+2
-87
lines changed

alpacloud/promls/cli.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import click
99

1010
from alpacloud.promls.fetch import FetcherURL, Parser
11-
from alpacloud.promls.filter import MetricsTree, filter_any, filter_ish, filter_name, filter_path
11+
from alpacloud.promls.filter import MetricsTree, filter_any, filter_name, filter_path
1212
from alpacloud.promls.metrics import Metric
1313
from alpacloud.promls.util import paths_to_tree
1414
from alpacloud.promls.vis import PromlsVisApp
@@ -136,15 +136,6 @@ def path(url, filter: str, display: PrintMode):
136136
do_print(filtered, display)
137137

138138

139-
@search.command()
140-
@common_args()
141-
def ish(url, filter: str, display: PrintMode):
142-
"""Filter metrics using a fuzzy match"""
143-
tree = do_fetch(url)
144-
filtered = tree.filter(filter_ish(filter))
145-
do_print(filtered, display)
146-
147-
148139
@search.command()
149140
@arg_url
150141
@opt_filter

alpacloud/promls/filter.py

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -61,73 +61,3 @@ def predicate(metric: Metric) -> bool:
6161
return pattern.match(metric.name) is not None
6262

6363
return predicate
64-
65-
66-
def filter_ish(pattern: str) -> Predicate:
67-
"""
68-
Filter metrics for this that are kindof like what you want.
69-
Uses difflib
70-
"""
71-
72-
def predicate(metric: Metric) -> bool:
73-
mismatch = letter_mismatch(metric.name, pattern)
74-
if mismatch > len(pattern) / 2:
75-
l.debug(f"{metric.name} letter mismatch too high: {mismatch}")
76-
return False
77-
distance = query_levenshtein(metric.name, pattern, 0, 0, (len(metric.name) + len(pattern)) / 4, False)
78-
ratio = distance / len(pattern)
79-
allowable_distance = max(0.1, 1 / len(pattern)) # 10% of the length of the pattern or 1 character
80-
val = ratio - allowable_distance < EPSILON
81-
l.debug(f"{metric.name} ratio: {ratio}, allowable_distance: {allowable_distance}, val: {val}")
82-
return val # epsilon comparison for floating point inexactness`
83-
84-
return predicate
85-
86-
87-
def letter_mismatch(s, q):
88-
"""Quickly eliminate metrics that will never match."""
89-
90-
def letter_dict(v: str) -> dict[str, int]:
91-
d: dict[str, int] = {}
92-
for c in v:
93-
d[c] = d.get(c, 0) + 1
94-
return d
95-
96-
s = letter_dict(s)
97-
q = letter_dict(q)
98-
99-
misses = 0
100-
for k, v in q.items():
101-
delta = v - s.get(k, 0)
102-
if delta > 0:
103-
misses += abs(delta)
104-
105-
return misses
106-
107-
108-
def query_levenshtein(s, q, si, qi, badness: float, started):
109-
"""
110-
Modified Levenshtein distance.
111-
Tries to not impose penalties for substring matches:
112-
- do not penalise advancing
113-
- do not penalise differences after a complete match
114-
115-
args:
116-
- si: start index of s
117-
- qi: start index of q
118-
"""
119-
if badness <= 0:
120-
return 1e6
121-
if len(s) == si: # unprocessed query
122-
return len(q)
123-
elif len(q) == qi: # entire query is processed, so we're happy
124-
return 0
125-
elif s[si] == q[qi]:
126-
return query_levenshtein(s, q, si + 1, qi + 1, badness, True)
127-
else:
128-
skip_s = query_levenshtein(s, q, si + 1, qi, badness - 1, started)
129-
return min(
130-
skip_s + 1 if started else skip_s,
131-
1 + query_levenshtein(s, q, si, qi + 1, badness - 1, True),
132-
1 + query_levenshtein(s, q, si + 1, qi + 1, badness - 1, True),
133-
)

alpacloud/promls/vis.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from textual.widget import Widget
88
from textual.widgets import Footer, Header, Input, Label, Static, Tree
99

10-
from alpacloud.promls.filter import MetricsTree, PredicateFactory, filter_any, filter_ish, filter_name
10+
from alpacloud.promls.filter import MetricsTree, PredicateFactory, filter_any, filter_name
1111
from alpacloud.promls.metrics import Metric
1212
from alpacloud.promls.util import TreeT, paths_to_tree
1313

@@ -51,7 +51,6 @@ class PromlsVisApp(App):
5151
BINDINGS = [
5252
Binding("ctrl+f", "find", "find", priority=True),
5353
Binding("ctrl+g", "goto", "goto", priority=True),
54-
Binding("ctrl+z", "fuzzy_find", "fuzzy find", priority=True),
5554
Binding("greater_than_sign", "expand_all", "Expand all", show=False),
5655
Binding("less_than_sign", "collapse_all", "Collapse all", show=False),
5756
]
@@ -85,11 +84,6 @@ async def action_find(self) -> None:
8584
self.load_metrics()
8685
self.focus_findbox()
8786

88-
async def action_fuzzy_find(self) -> None:
89-
self.predicate_factory = filter_ish
90-
self.load_metrics()
91-
self.focus_findbox()
92-
9387
async def action_goto(self):
9488
self.predicate_factory = lambda s: filter_name(re.compile(s))
9589
self.load_metrics()

0 commit comments

Comments
 (0)