usage
Spend: noclick.usage(group_by="user"), noclick.Usage.series(group_by="agent"),
noclick.Usage.limits().
noclick.usage(
period='month', *, group_by=None, agent=None, user=..., client=None
) -> UsageSpend for period ("month"), optionally split group_by
"user", "key", "agent" or "project" (each app), and narrowed to one agent (an Agent or its id). Needs the usage scope.
'month'NoneNoneWho the call acts for. Defaults to the user set by noclick.as_user (none: the account itself).
The client to send with; defaults to the module client built from NOCLICK_API_KEY.
None)The period's total_usd and, with group_by, one group per user, key, agent or app
that spent, largest first. Without group_by, groups is empty.
See this month's spend per user:
month = noclick.usage(group_by="user")
print(month.total_usd)
for group in month.groups:
print(group.id, group.usd, group.events)Inside as_user, it covers only that user:
with noclick.as_user("user_42"):
print(noclick.usage().total_usd)What one agent cost this month, per user it talked to:
support = noclick.Agent.from_name("support")
by_user = noclick.usage(group_by="user", agent=support)
print(by_user.total_usd, by_user["user_42"].usd)What each app spent, to bill its user on:
booking = noclick.Project.from_name("booking", user="user_42")
by_app = noclick.usage(group_by="project")
print(by_app[booking.id].usd)Usage
class UsageWhat the account (or the current user) spent in period.
| period | Any | |
| total_usd | float | |
| groups | list[UsageGroup] | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
__getitem__
usage[group_id] -> UsageGroupThe spend of one user, key, agent or app id in a grouped usage (None is the account's own).
That id's spend and event count; an id that spent nothing reads as $0 with no events.
user = noclick.User.from_id("user_42")
month = noclick.usage(group_by="user")
print(month[user.id].usd) # 0.0 for a user who spent nothingget
usage.get(group_id, default=None) -> AnyThe group of an id that spent something in a grouped usage, else default.
NoneThe UsageGroup, or default when the id spent nothing in the period.
support = noclick.Agent.from_name("support")
month = noclick.usage(group_by="agent")
if month.get(support.id) is None:
print("support has cost nothing this month")seriesstaticasync
noclick.Usage.series(
*, from_=None, to=None, interval=None, group_by=None, key=None, agent=None,
user_filter=None, user=..., client=None
) -> UsageSeriesDaily spend between from_ and to (default: the current month,
at most 92 days), one line per group_by key, user,
agent, type or project (an app). key
is a key id or prefix.
NoneNoneNoneNoneNoneNoneWhich users a read covers: noclick.ALL_USERS, noclick.NO_USER, a User, or your id for one. It filters without acting as that user.
None)Who the call acts for. Defaults to the user set by noclick.as_user (none: the account itself).
The client to send with; defaults to the module client built from NOCLICK_API_KEY.
None)The lines, largest first, each with its label, total_usd and daily points.
A day with no spend has no point.
Chart this month's spend by what it was for:
series = noclick.Usage.series(group_by="type")
for line in series.series:
print(line.label, line.total_usd, [point.usd for point in line.points])Or one agent's last 30 days, one line per user:
from datetime import datetime, timedelta, timezone
support = noclick.Agent.from_name("support")
now = datetime.now(timezone.utc)
series = noclick.Usage.series(from_=now - timedelta(days=30), to=now, group_by="user", agent=support)limitsstaticasync
noclick.Usage.limits(*, client=None) -> UsageLimitsThis month's spend of every capped key, user and agent (account principals), and of every app with a budget.
The client to send with; defaults to the module client built from NOCLICK_API_KEY.
None)Each capped key and user with its cap and spent, each budgeted agent with its
budget, spent and subtree_spent, each budgeted app (projects) with its
budget and spent, and when the caps reset (resets_at).
limits = noclick.Usage.limits()
for cap in limits.users:
print(cap.id, cap.spent, cap.cap)
for app in limits.projects:
print(app.name, app.user_id, app.spent, app.budget)
print(limits.resets_at)UsageGroup
class UsageGroupSpend of one user, key, agent or app (id) in the period.
| id | str | None | |
| usd | float | |
| events | int | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
UsageSeries
class UsageSeries| interval | str | |
| from_ | Any | |
| to | Any | |
| total_usd | float | |
| series | list[UsageSeriesLine] | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
UsageSeriesLine
class UsageSeriesLineOne key, user, agent or usage type's spend over the period. label is
a key name (Console for the console key), your id for a user, an
agent name or a type label.
| id | str | None | |
| label | str | None | |
| total_usd | float | |
| events | int | |
| points | list[UsagePoint] | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
UsagePoint
class UsagePointSpend in one interval starting at t.
| t | Any | |
| usd | float | |
| events | int |
UsageLimits
class UsageLimitsEvery capped key, user and agent with this month's spend, and every app with a budget.
| period_start | Any | |
| resets_at | Any | |
| keys | list[KeyCap] | |
| users | list[UserCap] | |
| agents | list[AgentBudget] | |
| projects | list[ProjectBudget] | Each app with a |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
KeyCap
class KeyCapA capped key's spend this month.
| id | str | |
| name | str | None | |
| cap | float | None | |
| spent | float | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
UserCap
class UserCapA capped user's spend this month; id is your id for the user.
| id | str | |
| cap | float | None | |
| spent | float | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
AgentBudget
class AgentBudgetA budgeted agent's spend this month; subtree_spent adds the agents it spawned.
| id | str | |
| name | str | None | |
| budget | float | None | |
| spent | float | |
| subtree_spent | float | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |
ProjectBudget
class ProjectBudgetA budgeted app's spend this month: id and name are the app's, user_id the
user it belongs to (None for your account's own).
| id | str | |
| name | str | None | |
| user_id | str | None | |
| budget | float | None | |
| spent | float | |
| raw | dict[str, Any] | The response JSON exactly as the API sent it. |