MCPcopy Index your code
hub / github.com/AstrBotDevs/AstrBot / insert_provider_stat

Method insert_provider_stat

astrbot/core/db/sqlite.py:197–240  ·  view source on GitHub ↗

Insert a provider stat record for a single agent response.

(
        self,
        *,
        umo: str,
        provider_id: str,
        provider_model: str | None = None,
        conversation_id: str | None = None,
        status: str = "completed",
        stats: dict | None = None,
        agent_type: str = "internal",
    )

Source from the content-addressed store, hash-verified

195 return list(result.scalars().all())
196
197 async def insert_provider_stat(
198 self,
199 *,
200 umo: str,
201 provider_id: str,
202 provider_model: str | None = None,
203 conversation_id: str | None = None,
204 status: str = "completed",
205 stats: dict | None = None,
206 agent_type: str = "internal",
207 ) -> ProviderStat:
208 """Insert a provider stat record for a single agent response."""
209 stats = stats or {}
210 token_usage = stats.get("token_usage", {})
211
212 token_input_other = int(token_usage.get("input_other", 0) or 0)
213 token_input_cached = int(token_usage.get("input_cached", 0) or 0)
214 token_output = int(token_usage.get("output", 0) or 0)
215
216 start_time = float(stats.get("start_time", 0.0) or 0.0)
217 end_time = float(stats.get("end_time", 0.0) or 0.0)
218 time_to_first_token = float(stats.get("time_to_first_token", 0.0) or 0.0)
219
220 async with self.get_db() as session:
221 session: AsyncSession
222 async with session.begin():
223 record = ProviderStat(
224 agent_type=agent_type,
225 status=status,
226 umo=umo,
227 conversation_id=conversation_id,
228 provider_id=provider_id,
229 provider_model=provider_model,
230 token_input_other=token_input_other,
231 token_input_cached=token_input_cached,
232 token_output=token_output,
233 start_time=start_time,
234 end_time=end_time,
235 time_to_first_token=time_to_first_token,
236 )
237 session.add(record)
238 await session.flush()
239 await session.refresh(record)
240 return record
241
242 # ====
243 # Conversation Management

Callers

nothing calls this directly

Calls 5

ProviderStatClass · 0.90
addMethod · 0.80
getMethod · 0.45
get_dbMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected