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" )'