Package io.jenkins.plugins.aiagentjob
Class AgentUsageStats
java.lang.Object
io.jenkins.plugins.aiagentjob.AgentUsageStats
- All Implemented Interfaces:
Serializable
Normalized token usage and cost statistics extracted from AI agent JSONL logs. Aggregates values
across all lines in the log (multiple turns, partial results, etc.) so the final object reflects
totals for the entire session.
Agent-specific extraction logic is delegated to AiAgentStatsExtractor implementations,
which are provided by each AiAgentTypeHandler via AiAgentTypeHandler.getStatsExtractor(). Shared/common extraction (system init, result lines) is
handled by the base class as a fallback.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaccumulateUsage(net.sf.json.JSONObject usage) Accumulates token counts from a standard usage object.voidaddApiDurationMs(long value) Set API duration (takes the max of current and new value).voidaddCacheReadTokens(long value) Accumulate cache read tokens (takes the max of current and new value).voidaddCacheWriteTokens(long value) Accumulate cache write tokens (takes the max of current and new value).voidaddCostUsd(double value) Set cost (takes the max of current and new value).voidaddDurationMs(long value) Set duration (takes the max of current and new value).voidaddInputTokens(long value) Accumulate input tokens (takes the max of current and new value).voidaddNumTurns(int value) Set number of turns (takes the max of current and new value).voidaddOutputTokens(long value) Accumulate output tokens (takes the max of current and new value).voidaddReasoningTokens(long value) Accumulate reasoning tokens (takes the max of current and new value).voidaddToolCalls(int value) Set tool call count (takes the max of current and new value).voidaddTotalTokens(long value) Accumulate total tokens (takes the max of current and new value).voidextractFrom(net.sf.json.JSONObject json) Extracts stats from a single JSON line without any agent-specific extractor.voidextractFrom(net.sf.json.JSONObject json, AiAgentStatsExtractor extractor) Extracts stats using the given extractor (if any), falling back to shared extraction.voidextractResultStats(net.sf.json.JSONObject json) Extracts stats from a "result" event (Claude Code / Gemini / Cursor shared structure).static AgentUsageStatsfromLogFile(File logFile) Parses the entire JSONL log file and returns aggregated stats.static AgentUsageStatsfromLogFile(File logFile, AiAgentStatsExtractor extractor) Parses the entire JSONL log file using the given extractor and returns aggregated stats.longlonglongFormatted cost string like "$0.30" or empty if no cost data.doubleModel name detected from system init or result lines, empty if not found.Formatted duration like "4.5s" or "2m 15s".longlongFormats a token count with comma grouping (e.g., "103,854").intlonglongintlongbooleanhasData()Returns true if any meaningful data was extracted.voidincrementCacheReadTokens(long value) Increment cache read tokens (additive).voidincrementCacheWriteTokens(long value) Increment cache write tokens (additive).voidincrementCostUsd(double value) Increment cost (additive).voidincrementInputTokens(long value) Increment input tokens (additive, for multi-step agents like OpenCode).voidincrementOutputTokens(long value) Increment output tokens (additive).voidincrementReasoningTokens(long value) Increment reasoning tokens (additive).voidincrementTotalTokens(long value) Increment total tokens (additive).voidsetDetectedModelIfEmpty(String model) Set the detected model name if not already set.
-
Constructor Details
-
AgentUsageStats
public AgentUsageStats()
-
-
Method Details
-
getInputTokens
public long getInputTokens() -
getOutputTokens
public long getOutputTokens() -
getCacheReadTokens
public long getCacheReadTokens() -
getCacheWriteTokens
public long getCacheWriteTokens() -
getTotalTokens
public long getTotalTokens() -
getReasoningTokens
public long getReasoningTokens() -
getCostUsd
public double getCostUsd() -
getCostDisplay
Formatted cost string like "$0.30" or empty if no cost data. -
getDurationMs
public long getDurationMs() -
getDurationDisplay
Formatted duration like "4.5s" or "2m 15s". -
getApiDurationMs
public long getApiDurationMs() -
getNumTurns
public int getNumTurns() -
getToolCalls
public int getToolCalls() -
getDetectedModel
Model name detected from system init or result lines, empty if not found. -
getInputTokensDisplay
Formats a token count with comma grouping (e.g., "103,854"). -
getOutputTokensDisplay
-
getCacheReadTokensDisplay
-
getCacheWriteTokensDisplay
-
getTotalTokensDisplay
-
getReasoningTokensDisplay
-
hasData
public boolean hasData()Returns true if any meaningful data was extracted. -
addInputTokens
public void addInputTokens(long value) Accumulate input tokens (takes the max of current and new value). -
addOutputTokens
public void addOutputTokens(long value) Accumulate output tokens (takes the max of current and new value). -
addCacheReadTokens
public void addCacheReadTokens(long value) Accumulate cache read tokens (takes the max of current and new value). -
addCacheWriteTokens
public void addCacheWriteTokens(long value) Accumulate cache write tokens (takes the max of current and new value). -
addTotalTokens
public void addTotalTokens(long value) Accumulate total tokens (takes the max of current and new value). -
addReasoningTokens
public void addReasoningTokens(long value) Accumulate reasoning tokens (takes the max of current and new value). -
incrementInputTokens
public void incrementInputTokens(long value) Increment input tokens (additive, for multi-step agents like OpenCode). -
incrementOutputTokens
public void incrementOutputTokens(long value) Increment output tokens (additive). -
incrementReasoningTokens
public void incrementReasoningTokens(long value) Increment reasoning tokens (additive). -
incrementTotalTokens
public void incrementTotalTokens(long value) Increment total tokens (additive). -
incrementCacheReadTokens
public void incrementCacheReadTokens(long value) Increment cache read tokens (additive). -
incrementCacheWriteTokens
public void incrementCacheWriteTokens(long value) Increment cache write tokens (additive). -
incrementCostUsd
public void incrementCostUsd(double value) Increment cost (additive). -
addCostUsd
public void addCostUsd(double value) Set cost (takes the max of current and new value). -
addDurationMs
public void addDurationMs(long value) Set duration (takes the max of current and new value). -
addApiDurationMs
public void addApiDurationMs(long value) Set API duration (takes the max of current and new value). -
addNumTurns
public void addNumTurns(int value) Set number of turns (takes the max of current and new value). -
addToolCalls
public void addToolCalls(int value) Set tool call count (takes the max of current and new value). -
setDetectedModelIfEmpty
Set the detected model name if not already set. -
fromLogFile
Parses the entire JSONL log file and returns aggregated stats. Each line that contains usage or stats information contributes to the totals.- Throws:
IOException
-
fromLogFile
public static AgentUsageStats fromLogFile(File logFile, AiAgentStatsExtractor extractor) throws IOException Parses the entire JSONL log file using the given extractor and returns aggregated stats. The extractor is tried first for each line; if it returnsfalse, the shared extractor handles the line.- Throws:
IOException
-
extractFrom
Extracts stats using the given extractor (if any), falling back to shared extraction. -
extractFrom
public void extractFrom(net.sf.json.JSONObject json) Extracts stats from a single JSON line without any agent-specific extractor. -
extractResultStats
public void extractResultStats(net.sf.json.JSONObject json) Extracts stats from a "result" event (Claude Code / Gemini / Cursor shared structure). -
accumulateUsage
public void accumulateUsage(net.sf.json.JSONObject usage) Accumulates token counts from a standard usage object.
-