@@ -26,7 +26,7 @@ DECLARE
2626 v_rows_updated integer := 0 ;
2727BEGIN
2828 LOOP
29- WITH candidate AS (
29+ WITH RECURSIVE candidate AS (
3030 SELECT id
3131 FROM workflow_execution_logs
3232 WHERE cost IS NULL
@@ -110,14 +110,14 @@ BEGIN
110110 UPDATE workflow_execution_logs AS l
111111 SET cost = jsonb_strip_nulls(
112112 jsonb_build_object(
113- ' total' , COALESCE(l . total_cost , NULLIF(agg .agg_total ,0 )),
114- ' input' , COALESCE(l . total_input_cost , NULLIF(agg .agg_input ,0 )),
115- ' output' , COALESCE(l . total_output_cost , NULLIF(agg .agg_output ,0 )),
113+ ' total' , COALESCE((to_jsonb(l) - >> ' total_cost' ):: numeric , NULLIF(agg .agg_total ,0 )),
114+ ' input' , COALESCE((to_jsonb(l) - >> ' total_input_cost' ):: numeric , NULLIF(agg .agg_input ,0 )),
115+ ' output' , COALESCE((to_jsonb(l) - >> ' total_output_cost' ):: numeric , NULLIF(agg .agg_output ,0 )),
116116 ' tokens' , CASE
117- WHEN l . total_tokens IS NOT NULL OR tb .prompt IS NOT NULL OR tb .completion IS NOT NULL OR NULLIF(agg .agg_tokens_total ,0 ) IS NOT NULL THEN
117+ WHEN (to_jsonb(l) ? ' total_tokens ' ) OR tb .prompt IS NOT NULL OR tb .completion IS NOT NULL OR NULLIF(agg .agg_tokens_total ,0 ) IS NOT NULL THEN
118118 jsonb_strip_nulls(
119119 jsonb_build_object(
120- ' total' , COALESCE(l . total_tokens , NULLIF(agg .agg_tokens_total ,0 )),
120+ ' total' , COALESCE((to_jsonb(l) - >> ' total_tokens' ):: numeric , NULLIF(agg .agg_tokens_total ,0 )),
121121 ' prompt' , COALESCE(tb .prompt , NULLIF(agg .agg_tokens_prompt ,0 )),
122122 ' completion' , COALESCE(tb .completion , NULLIF(agg .agg_tokens_completion ,0 ))
123123 )
0 commit comments