OpenAF processor examples
π₯ Basic
OUTPUT JSON: Creating ndjson file from json files
Command:
# Creates a data.ndjson file where each record is formatted from json files in /some/data
find /some/data -name "*.json" -exec oafp {} output=json \; > data.ndjson
FILTER PATH: Manipulate text in a field
Command:
# Get a json with the lyrics of a song
curl -s https://api.lyrics.ovh/v1/Coldplay/Viva%20La%20Vida | oafp path="substring(lyrics,index_of(lyrics, '\n'),length(lyrics))"
Results:
I used to rule the world
[...]
Oooooh Oooooh Oooooh
FILTER PATH: Docker ps formatting
Command:
oafp cmd="docker ps --format json" input=ndjson ndjsonjoin=true path="[].{id:ID,name:Names,state:State,image:Image,networks:Networks,ports:Ports,Status:Status}" sql="select * order by networks,state,name" output=ctable
Result:
id β name β state β image β networks β ports β Status
βββββββββββββΌβββββββββββββββββββββββββΌββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββ
af3adb5b8349βregistry βrunningβregistry:2 βbridge,k3d-k3s-defaultβ0.0.0.0:5000->5000/tcp βUp 2 hours
cba6e3807b44βk3d-k3s-default-server-0βrunningβrancher/k3s:v1.27.4-k3s1 βk3d-k3s-default β βUp 2 hours
b775ad480764βk3d-k3s-default-serverlbβrunningβghcr.io/k3d-io/k3d-proxy:5.6.0βk3d-k3s-default β80/tcp, 0.0.0.0:1080->1080/tcp, 0.0.0.0:45693->6443/tcpβUp 2 hours
[#3 rows]
π₯ Medium
FILTER PATH: Kubectl get pods formatting
Command:
oafp cmd="kubectl get pods -A -o json" path="items[].{ns:metadata.namespace,kind:metadata.ownerReferences[].kind,name:metadata.name,status:status.phase,restarts:sum(status.containerStatuses[].restartCount),node:spec.nodeName,age:timeago(status.startTime)}" sql="select * order by status,name" output=ctable
Result:
ns β kind β name β status βrestartsβ node β age
ββββββββββββΌβββββββββββΌβββββββββββββββββββββββββββββββββββββββΌββββββββββΌβββββββββΌβββββββββββββββββββββββββΌββββββββββββββ
kube-systemβReplicaSetβcoredns-77ccd57875-5m44t βRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβReplicaSetβlocal-path-provisioner-957fdf8bc-24hmfβRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβReplicaSetβmetrics-server-648b5df564-hzbwb βRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβReplicaSetβsocks-server-d7c8c4d78-r6jc9 βRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβDaemonSet βsvclb-socks-server-78b973ca-zvf58 βRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβDaemonSet βsvclb-traefik-e1776788-7z2gf βRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβReplicaSetβtraefik-64f55bb67d-g2vps βRunning β0 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβJob βhelm-install-traefik-6j5zx βSucceededβ1 βk3d-k3s-default-server-0β66 minutes ago
kube-systemβJob βhelm-install-traefik-crd-z59fs βSucceededβ0 βk3d-k3s-default-server-0β66 minutes ago
[#9 rows]
FILTER PATH:
Command:
oafp cmd="kubectl get nodes -o json" path="items[].{node:metadata.name,totalCPU:status.capacity.cpu,allocCPU:status.allocatable.cpu,totalMem:to_bytesAbbr(from_bytesAbbr(status.capacity.memory)),allocMem:to_bytesAbbr(from_bytesAbbr(status.allocatable.memory)),totalStorage:to_bytesAbbr(from_bytesAbbr(status.capacity.\"ephemeral-storage\")),allocStorage:to_bytesAbbr(to_number(status.allocatable.\"ephemeral-storage\")),conditions:join(\`, \`,status.conditions[].reason)}" output=ctable
Result:
node βtotalCPUβallocCPUβtotalMemβallocMemβtotalStorageβallocStorageβ conditions
βββββββββββββββββββββββββΌβββββββββΌβββββββββΌβββββββββΌβββββββββΌβββββββββββββΌβββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
k3d-k3s-default-server-0β4 β4 β3.85 GB β3.85 GB β77.6 GB β73.8 GB βKubeletHasSufficientMemory, KubeletHasNoDiskPressure, KubeletHasSufficientPID, KubeletReady
[#1 row]
INPUT LLM: Using a LLM to generate a table
Setting up the LLM model and gather the data into a data.json file:
Command:
export OAFP_MODEL="(type: openai, model: gpt-3.5-turbo, key: ..., timeout: 900000)"
echo "list all United Nations secretaries with their corresponding 'name', their mandate 'begin date', their mandate 'end date' and their corresponding secretary 'numeral'" | oafp input=llm output=json > data.json
Checking the obtain data:
Result:
oafp data.json
β secretaries β [0] β name : Trygve Lie
β β begin date: 1946-02-01
β β end date : 1952-11-10
β β° numeral : 1
β [1] β name : Dag HammarskjΓΆld
β β begin date: 1953-04-10
β β end date : 1961-09-18
β β° numeral : 2
β [2] β name : U Thant
β β begin date: 1961-11-30
β β end date : 1971-12-31
β β° numeral : 3
β [3] β name : Kurt Waldheim
β β begin date: 1972-01-01
β β end date : 1981-12-31
β β° numeral : 4
β [4] β name : Javier PΓ©rez de CuΓ©llar
β β begin date: 1982-01-01
β β end date : 1991-12-31
β β° numeral : 5
β [5] β name : Boutros Boutros-Ghali
β β begin date: 1992-01-01
β β end date : 1996-12-31
β β° numeral : 6
β [6] β name : Kofi Annan
β β begin date: 1997-01-01
β β end date : 2006-12-31
β β° numeral : 7
β [7] β name : Ban Ki-moon
β β begin date: 2007-01-01
β β end date : 2016-12-31
β β° numeral : 8
β° [8] β name : AntΓ³nio Guterres
β begin date: 2017-01-01
β end date : present
β° numeral : 9
Checking the data in a table format:
Result:
oafp data.json path=secretaries output=ctable
name βbegin dateβ end date βnumeral
ββββββββββββββββββββββββΌβββββββββββΌβββββββββββΌβββββββ
Trygve Lie β1946-02-01β1952-11-10β1
Dag HammarskjΓΆld β1953-04-10β1961-09-18β2
U Thant β1961-11-30β1971-12-31β3
Kurt Waldheim β1972-01-01β1981-12-31β4
Javier PΓ©rez de CuΓ©llarβ1982-01-01β1991-12-31β5
Boutros Boutros-Ghali β1992-01-01β1996-12-31β6
Kofi Annan β1997-01-01β2006-12-31β7
Ban Ki-moon β2007-01-01β2016-12-31β8
AntΓ³nio Guterres β2017-01-01βpresent β9
[#9 rows]
TRANSFORM LLM: Using a LLM to transform an input
Using the data gather in βUsing a LLM to generate a tableβ use a LLM to transform it:
Command:
export OAFP_MODEL="(type: openai, model: gpt-3.5-turbo, key: ..., timeout: 900000)"
oafp data.json llmprompt="convert the numeral number into a roman number" path=secretaries output=ctable
Result:
name βbegin dateβ end date βnumeral
ββββββββββββββββββββββββΌβββββββββββΌβββββββββββΌβββββββ
Trygve Lie β1946-02-01β1952-11-10βI
Dag HammarskjΓΆld β1953-04-10β1961-09-18βII
U Thant β1961-11-30β1971-12-31βIII
Kurt Waldheim β1972-01-01β1981-12-31βIV
Javier PΓ©rez de CuΓ©llarβ1982-01-01β1991-12-31βV
Boutros Boutros-Ghali β1992-01-01β1996-12-31βVI
Kofi Annan β1997-01-01β2006-12-31βVII
Ban Ki-moon β2007-01-01β2016-12-31βVIII
AntΓ³nio Guterres β2017-01-01βpresent βIX
[#9 rows]
INPUT LLM: Using a private LLM to generate sample data
Command:
export OAFP_MODEL="(type: ollama, model: 'mistral:instruct', url: 'https://models.local', timeout: 900000)"
echo "Output a JSON array with 15 cities where each entry has the 'city' name, the estimated population and the corresponding 'country'" | oafp input=llm output=json > data.json
oafp data.json output=ctable sql="select * order by population desc"
Result:
city βpopulationβ country
ββββββββββββΌβββββββββββΌββββββββββ
Shanghai β270584000 βChina
Tokyo β37436958 βJapan
Delhi β30290936 βIndia
SΓ£o Paulo β21935296 βBrazil
Beijing β21516000 βChina
Mexico Cityβ21402981 βMexico
Mumbai β20712874 βIndia
Cairo β20636449 βEgypt
Osaka β19365701 βJapan
Dhaka β18568373 βBangladesh
[#10 rows]
π₯ Advanced
OUTPUT XLS: Creating an XLSx file with multiple sheets
Command:
# Processes each json file in /some/data creating and updating the data.xlsx file with a sheet for each file
find /some/data -name "*.json" | xargs -I '{}' /bin/sh -c 'oafp file={} output=xls xlsfile=data.xlsx xlsopen=false xlssheet=$(echo {} | sed "s/.*\/\(.*\)\.json/\1/g" )'