Skip to content

Commit

Permalink
feat: enable show udf to print content
Browse files Browse the repository at this point in the history
  • Loading branch information
a-spiker committed Jan 27, 2025
1 parent 4e60d41 commit e982936
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
15 changes: 15 additions & 0 deletions lib/live_cluster/client/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,21 @@ async def info_udf_list(self):
return client_util.info_to_dict_multi_level(
udf_data, "filename", delimiter2=","
)

@async_return_exceptions
async def info_udf_get(self, filename):
"""
Get list of UDFs stored on the node.
Returns:
dict -- {<file-name>: {"content": <content>, "type": 'LUA'}, . . .}
"""
udf_data = await self._info("udf-get:filename={}".format(filename))

if not udf_data:
return {}

return client_util.info_to_dict(udf_data)

@async_return_exceptions
async def info_udf_put(self, udf_file_name, udf_str, udf_type="LUA"):
Expand Down
4 changes: 3 additions & 1 deletion lib/live_cluster/get_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,9 @@ def __init__(self, cluster):

async def get_udfs(self, nodes="all"):
return await self.cluster.info_udf_list(nodes=nodes)


async def get_udf_content(self, nodes="all", filename=None):
return await self.cluster.info_udf_get(nodes=nodes, filename=filename)

class GetSIndexController:
def __init__(self, cluster):
Expand Down
42 changes: 35 additions & 7 deletions lib/live_cluster/show_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,20 +1587,48 @@ async def _do_default(self, line):
"Displays UDF modules along with metadata.",
modifiers=(
ModifierHelp(Modifiers.LIKE, "Filter UDFs by name using a substring match"),
ModifierHelp(
"--name",
"Show UDF by exact name match",
),
ModifierHelp(
"-n",
"Show UDF by exact name match",
),
),
usage=f"[{ModifierUsage.LIKE}]",
usage=f"[{ModifierUsage.LIKE}] [--name <udf-name>] [-n <udf-name>]",
)
class ShowUdfsController(LiveClusterCommandController):
def __init__(self):
self.modifiers = set([Modifiers.LIKE])
self.modifiers = set([Modifiers.LIKE, '-v', '--name', '-n'])
self.getter = GetUdfController(self.cluster)

async def _do_default(self, line):
udfs_data = await self.getter.get_udfs(nodes="principal")
resp = list(udfs_data.values())[0]

return self.view.show_udfs(resp, **self.mods)

udf_name = util.get_arg_and_delete_from_mods(
line=line,
arg="--filename",
default="",
return_type=str,
modifiers=self.modifiers,
mods=self.mods,
) or util.get_arg_and_delete_from_mods(
line=line,
arg="-f",
default="",
return_type=str,
modifiers=self.modifiers,
mods=self.mods,
)

if udf_name != "":
udfs_data = await self.getter.get_udf_content(nodes="principal", filename=udf_name)
resp = list(udfs_data.values())[0]
return self.view.show_udf_content(resp, udf_name, **self.mods)
else:
udfs_data = await self.getter.get_udfs(nodes="principal")
resp = list(udfs_data.values())[0]
self.view.show_udfs(resp, **self.mods)


@CommandHelp(
"Displays secondary indexes and static metadata.",
Expand Down
25 changes: 25 additions & 0 deletions lib/view/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime
import locale
import logging
import base64
from os import path
import sys
import time
Expand Down Expand Up @@ -1121,6 +1122,30 @@ def show_roles(roles_data, like=None, timestamp="", **ignore):

CliView.print_result(sheet.render(templates.show_roles, title, sources))

@staticmethod
def show_udf_content(udf_data, udf_name="", timestamp="", **ignore):
if not udf_data:
return

if 'error' in udf_data:
CliView.print_result("Failed to find the udf {}".format(udf_name))
return

title_timestamp = CliView._get_timestamp_suffix(timestamp)
title = "UDF Module Content {}".format(title_timestamp)

CliView.print_result(title)
CliView.print_result("=" * len(title))


# decode the base64 encoded content
content = base64.b64decode(udf_data['content']).decode('utf-8')
radius = (max([ len(line) for line in content.split('\n') ]) + 1 ) // 2
CliView.print_result("~" * radius + " Type:" + udf_data['type'] + " " + "~" * radius + "\n")
CliView.print_result(content)
CliView.print_result("~" * radius * 2)


@staticmethod
def show_udfs(udfs_data, like, timestamp="", **ignore):
if not udfs_data:
Expand Down

0 comments on commit e982936

Please sign in to comment.