ukaf-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ukaf/__init__.py +8 -0
- ukaf/__main__.py +4 -0
- ukaf/cli.py +88 -0
- ukaf/data/cursor.toml +148 -0
- ukaf/delivery.py +268 -0
- ukaf/dora.py +62 -0
- ukaf/github_api.py +286 -0
- ukaf/i18n.py +119 -0
- ukaf/mcp_server.py +70 -0
- ukaf/money.py +34 -0
- ukaf/pricing.py +172 -0
- ukaf/recommend.py +357 -0
- ukaf/reconcile.py +185 -0
- ukaf/render_html.py +584 -0
- ukaf/reporting.py +50 -0
- ukaf/serialize.py +340 -0
- ukaf/story.py +459 -0
- ukaf/timeutil.py +65 -0
- ukaf/usage.py +150 -0
- ukaf_cli-0.1.0.dist-info/METADATA +213 -0
- ukaf_cli-0.1.0.dist-info/RECORD +25 -0
- ukaf_cli-0.1.0.dist-info/WHEEL +5 -0
- ukaf_cli-0.1.0.dist-info/entry_points.txt +3 -0
- ukaf_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- ukaf_cli-0.1.0.dist-info/top_level.txt +1 -0
ukaf/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""ukaf turns coding-agent usage and pull requests into one additive story."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
|
|
5
|
+
SPONSOR_URL = "https://github.com/sponsors/UriBer"
|
|
6
|
+
|
|
7
|
+
# One line, used by --version and by report footers. Not an interstitial.
|
|
8
|
+
SPONSOR_LINE = f"ukaf {__version__} · sponsor the work: {SPONSOR_URL}"
|
ukaf/__main__.py
ADDED
ukaf/cli.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Command line. `ukaf report` writes HTML, JSON, and Markdown."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from ukaf import SPONSOR_LINE, __version__
|
|
10
|
+
from ukaf.delivery import load_fixture
|
|
11
|
+
from ukaf.github_api import GitHubError, fetch_pulls
|
|
12
|
+
from ukaf.reconcile import ReconciliationError
|
|
13
|
+
from ukaf.reporting import analyze, load_events, write_report
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def main(argv: list[str] | None = None) -> int:
|
|
17
|
+
parser = argparse.ArgumentParser(
|
|
18
|
+
prog="ukaf",
|
|
19
|
+
description="Turn coding-agent usage and pull requests into one additive cost story.",
|
|
20
|
+
)
|
|
21
|
+
parser.add_argument("--version", action="store_true", help="print version and the sponsorship line")
|
|
22
|
+
sub = parser.add_subparsers(dest="command")
|
|
23
|
+
|
|
24
|
+
report = sub.add_parser("report", help="write the HTML, JSON, and Markdown story")
|
|
25
|
+
report.add_argument("--usage", required=True, type=Path, help="Cursor usage CSV export")
|
|
26
|
+
report.add_argument("--pricing", type=Path, default=None, help="pricing table (TOML)")
|
|
27
|
+
report.add_argument("--github-fixture", type=Path, help="offline pull-request JSON")
|
|
28
|
+
report.add_argument("--repo", action="append", default=[], help="owner/name, repeatable; uses the GitHub API")
|
|
29
|
+
report.add_argument("--since", help="only load pull requests updated on or after this ISO date")
|
|
30
|
+
report.add_argument("--until", help="only load pull requests created on or before this ISO date")
|
|
31
|
+
report.add_argument("--max-prs", type=int, default=30)
|
|
32
|
+
report.add_argument("--team-token-rate", action="store_true", help="add the Teams Cursor Token Rate")
|
|
33
|
+
report.add_argument("--lang", choices=("en", "he"), default="en")
|
|
34
|
+
report.add_argument("--out", type=Path, required=True, help="directory for report.html, report.json, report.md")
|
|
35
|
+
|
|
36
|
+
sub.add_parser("mcp", help="run the MCP server over stdio")
|
|
37
|
+
|
|
38
|
+
args = parser.parse_args(argv)
|
|
39
|
+
if args.version or args.command is None and argv == ["--version"]:
|
|
40
|
+
print(SPONSOR_LINE)
|
|
41
|
+
return 0
|
|
42
|
+
if args.command is None:
|
|
43
|
+
parser.print_help()
|
|
44
|
+
print()
|
|
45
|
+
print(SPONSOR_LINE)
|
|
46
|
+
return 0
|
|
47
|
+
if args.command == "mcp":
|
|
48
|
+
try:
|
|
49
|
+
from ukaf.mcp_server import run
|
|
50
|
+
except ModuleNotFoundError as exc:
|
|
51
|
+
if exc.name != "mcp":
|
|
52
|
+
raise
|
|
53
|
+
print('ukaf: MCP support requires pip install "ukaf-cli[mcp]"', file=sys.stderr)
|
|
54
|
+
return 2
|
|
55
|
+
run()
|
|
56
|
+
return 0
|
|
57
|
+
try:
|
|
58
|
+
return _report(args)
|
|
59
|
+
except (OSError, ValueError, GitHubError, ReconciliationError) as exc:
|
|
60
|
+
print(f"ukaf: {exc}", file=sys.stderr)
|
|
61
|
+
return 2
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _report(args: argparse.Namespace) -> int:
|
|
65
|
+
if bool(args.github_fixture) == bool(args.repo):
|
|
66
|
+
raise ValueError("pass exactly one of --github-fixture or --repo")
|
|
67
|
+
events = load_events(args.usage, args.pricing, team_token_rate=args.team_token_rate)
|
|
68
|
+
if args.github_fixture:
|
|
69
|
+
pulls = load_fixture(args.github_fixture)
|
|
70
|
+
else:
|
|
71
|
+
pulls = fetch_pulls(
|
|
72
|
+
list(args.repo),
|
|
73
|
+
since=args.since,
|
|
74
|
+
until=args.until,
|
|
75
|
+
max_pulls=args.max_prs,
|
|
76
|
+
)
|
|
77
|
+
analysis = analyze(events, pulls)
|
|
78
|
+
paths, document = write_report(analysis, args.out, lang=args.lang)
|
|
79
|
+
print(f"wrote {paths['html']}")
|
|
80
|
+
print(f"list {document['totals']['list_usd']} billed {document['totals']['billed_usd']}")
|
|
81
|
+
print(SPONSOR_LINE)
|
|
82
|
+
if not document["reconciliation"]["ok"]:
|
|
83
|
+
return 2
|
|
84
|
+
return 0
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def version_text() -> str:
|
|
88
|
+
return f"{__version__}\n{SPONSOR_LINE}"
|
ukaf/data/cursor.toml
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# List prices for ukaf, USD per million tokens.
|
|
2
|
+
# Source: https://cursor.com/docs/models-and-pricing (captured 2026-12-03).
|
|
3
|
+
# A dash for cache write in those docs means there is no separate cache-write fee.
|
|
4
|
+
# Those tokens are billed at the input rate. Fast variants are their own rows at the
|
|
5
|
+
# published fast rates (about 2x standard for Grok 4.7), not an extra multiplier on top.
|
|
6
|
+
# Names that do not appear here fall through [aliases], then [default].
|
|
7
|
+
# Max Mode, when the CSV says Yes, is applied in code as +20% (legacy Cursor rule).
|
|
8
|
+
# The Cursor Token Rate ($0.25 / MTok on eligible third-party models for Teams) is
|
|
9
|
+
# not included; individual plans do not pay it. Pass --team-token-rate to add it.
|
|
10
|
+
|
|
11
|
+
[default]
|
|
12
|
+
input = 2.0
|
|
13
|
+
cache_write = 2.0
|
|
14
|
+
cache_read = 0.5
|
|
15
|
+
output = 6.0
|
|
16
|
+
fast = false
|
|
17
|
+
note = "Unlisted model. Priced as Grok 4.7 standard so the row still has a list price."
|
|
18
|
+
|
|
19
|
+
# --- Cursor / Grok pool -------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
[models."grok-4.7"]
|
|
22
|
+
input = 2.0
|
|
23
|
+
cache_write = 2.0
|
|
24
|
+
cache_read = 0.5
|
|
25
|
+
output = 6.0
|
|
26
|
+
fast = false
|
|
27
|
+
note = "Grok 4.7 standard."
|
|
28
|
+
|
|
29
|
+
[models."grok-4.7-fast"]
|
|
30
|
+
input = 4.0
|
|
31
|
+
cache_write = 4.0
|
|
32
|
+
cache_read = 1.0
|
|
33
|
+
output = 12.0
|
|
34
|
+
fast = true
|
|
35
|
+
non_fast = "grok-4.7"
|
|
36
|
+
note = "Grok 4.7 Fast, published 2x rates."
|
|
37
|
+
|
|
38
|
+
[models."grok-4.6"]
|
|
39
|
+
input = 2.0
|
|
40
|
+
cache_write = 2.0
|
|
41
|
+
cache_read = 0.5
|
|
42
|
+
output = 6.0
|
|
43
|
+
fast = false
|
|
44
|
+
|
|
45
|
+
[models."grok-4.6-fast"]
|
|
46
|
+
input = 4.0
|
|
47
|
+
cache_write = 4.0
|
|
48
|
+
cache_read = 1.0
|
|
49
|
+
output = 12.0
|
|
50
|
+
fast = true
|
|
51
|
+
non_fast = "grok-4.6"
|
|
52
|
+
|
|
53
|
+
[models."grok-4.5"]
|
|
54
|
+
input = 2.0
|
|
55
|
+
cache_write = 2.0
|
|
56
|
+
cache_read = 0.5
|
|
57
|
+
output = 6.0
|
|
58
|
+
fast = false
|
|
59
|
+
|
|
60
|
+
[models."grok-4.5-fast"]
|
|
61
|
+
input = 4.0
|
|
62
|
+
cache_write = 4.0
|
|
63
|
+
cache_read = 1.0
|
|
64
|
+
output = 18.0
|
|
65
|
+
fast = true
|
|
66
|
+
non_fast = "grok-4.5"
|
|
67
|
+
note = "Grok 4.5 Fast output is $18/MTok in the Cursor table (not 2x of $6)."
|
|
68
|
+
|
|
69
|
+
[models."composer-2.5"]
|
|
70
|
+
input = 0.5
|
|
71
|
+
cache_write = 0.5
|
|
72
|
+
cache_read = 0.2
|
|
73
|
+
output = 2.5
|
|
74
|
+
fast = false
|
|
75
|
+
|
|
76
|
+
[models."composer-2.5-fast"]
|
|
77
|
+
input = 3.0
|
|
78
|
+
cache_write = 3.0
|
|
79
|
+
cache_read = 0.5
|
|
80
|
+
output = 15.0
|
|
81
|
+
fast = true
|
|
82
|
+
non_fast = "composer-2.5"
|
|
83
|
+
|
|
84
|
+
# --- Third-party (API list price) --------------------------------------------
|
|
85
|
+
|
|
86
|
+
[models."claude-4.5-sonnet"]
|
|
87
|
+
input = 3.0
|
|
88
|
+
cache_write = 3.75
|
|
89
|
+
cache_read = 0.3
|
|
90
|
+
output = 15.0
|
|
91
|
+
fast = false
|
|
92
|
+
|
|
93
|
+
[models."claude-4.6-opus"]
|
|
94
|
+
input = 5.0
|
|
95
|
+
cache_write = 6.25
|
|
96
|
+
cache_read = 0.5
|
|
97
|
+
output = 25.0
|
|
98
|
+
fast = false
|
|
99
|
+
|
|
100
|
+
[models."claude-sonnet-5.5"]
|
|
101
|
+
input = 2.0
|
|
102
|
+
cache_write = 2.5
|
|
103
|
+
cache_read = 0.1
|
|
104
|
+
output = 10.0
|
|
105
|
+
fast = false
|
|
106
|
+
|
|
107
|
+
[models."gemini-2.5-flash"]
|
|
108
|
+
input = 0.3
|
|
109
|
+
cache_write = 0.3
|
|
110
|
+
cache_read = 0.03
|
|
111
|
+
output = 2.5
|
|
112
|
+
fast = false
|
|
113
|
+
|
|
114
|
+
[models."gemini-3.1-pro"]
|
|
115
|
+
input = 2.0
|
|
116
|
+
cache_write = 2.0
|
|
117
|
+
cache_read = 0.2
|
|
118
|
+
output = 12.0
|
|
119
|
+
fast = false
|
|
120
|
+
|
|
121
|
+
# Export model ids that are not the docs' canonical names.
|
|
122
|
+
# auto and the grok-bot-* skus do not publish a separate rate card. They draw the
|
|
123
|
+
# included Cursor pool; ukaf prices that draw as Grok 4.7 standard so list price
|
|
124
|
+
# is "what the tokens would cost outside the pool", and billed stays whatever the
|
|
125
|
+
# CSV charged (usually zero while the pool covers them).
|
|
126
|
+
|
|
127
|
+
[aliases]
|
|
128
|
+
auto = "grok-4.7"
|
|
129
|
+
grok-bot-default = "grok-4.7"
|
|
130
|
+
grok-bot-cua = "grok-4.7"
|
|
131
|
+
grok-bot-automation = "grok-4.7"
|
|
132
|
+
grok-bot-knowledge-work = "grok-4.7"
|
|
133
|
+
"grok-4.7-high-fast" = "grok-4.7-fast"
|
|
134
|
+
"grok-4.7-medium-fast" = "grok-4.7-fast"
|
|
135
|
+
"grok-4.7-low-fast" = "grok-4.7-fast"
|
|
136
|
+
"cursor-grok-4.5-high" = "grok-4.5"
|
|
137
|
+
"cursor-grok-4.5-low-fast" = "grok-4.5-fast"
|
|
138
|
+
"claude-4.5-sonnet-thinking" = "claude-4.5-sonnet"
|
|
139
|
+
"claude-4.6-opus-high-thinking" = "claude-4.6-opus"
|
|
140
|
+
claude-sonnet-5-5-high = "claude-sonnet-5.5"
|
|
141
|
+
|
|
142
|
+
# Third-party models whose cache-read price is strictly below $0.10/MTok on every
|
|
143
|
+
# tier are exempt from the Cursor Token Rate. Everyone else on this list is eligible
|
|
144
|
+
# when --team-token-rate is set. Cursor-native models are never eligible.
|
|
145
|
+
[token_rate]
|
|
146
|
+
usd_per_mtok = 0.25
|
|
147
|
+
exempt = ["gemini-2.5-flash"]
|
|
148
|
+
native_prefixes = ["grok-", "composer-", "auto", "cursor-grok-", "grok-bot-"]
|
ukaf/delivery.py
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"""Pull-request facts, whether they came from a JSON fixture or the GitHub API.
|
|
2
|
+
|
|
3
|
+
Both paths normalize into the same records. SLOC excludes lockfiles and generated
|
|
4
|
+
paths so a dependency bump does not become an XL feature.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import re
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from ukaf.timeutil import parse_time
|
|
16
|
+
|
|
17
|
+
# Filenames (case-insensitive) that are lockfiles, not hand-written code.
|
|
18
|
+
LOCKFILES = {
|
|
19
|
+
"package-lock.json",
|
|
20
|
+
"yarn.lock",
|
|
21
|
+
"pnpm-lock.yaml",
|
|
22
|
+
"npm-shrinkwrap.json",
|
|
23
|
+
"cargo.lock",
|
|
24
|
+
"poetry.lock",
|
|
25
|
+
"pipfile.lock",
|
|
26
|
+
"uv.lock",
|
|
27
|
+
"bun.lock",
|
|
28
|
+
"gemfile.lock",
|
|
29
|
+
"composer.lock",
|
|
30
|
+
"go.sum",
|
|
31
|
+
"packages.lock.json",
|
|
32
|
+
"podfile.lock",
|
|
33
|
+
"cartfile.resolved",
|
|
34
|
+
"mix.lock",
|
|
35
|
+
"flake.lock",
|
|
36
|
+
"pubspec.lock",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
# Substrings matched against the full path, lowercased.
|
|
40
|
+
GENERATED_MARKERS = (
|
|
41
|
+
"/node_modules/",
|
|
42
|
+
"/vendor/",
|
|
43
|
+
"/dist/",
|
|
44
|
+
"/build/",
|
|
45
|
+
"/generated/",
|
|
46
|
+
"/coverage/",
|
|
47
|
+
".min.js",
|
|
48
|
+
".min.css",
|
|
49
|
+
".pb.go",
|
|
50
|
+
".pb.ts",
|
|
51
|
+
"_pb2.py",
|
|
52
|
+
".generated.",
|
|
53
|
+
".g.dart",
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
AGENT_RE = re.compile(r"\b(bc-[0-9a-fA-F-]{8,}|agent-[A-Za-z0-9][A-Za-z0-9-]*)\b")
|
|
57
|
+
|
|
58
|
+
# Complexity rubric. A feature takes the higher tier of the two columns.
|
|
59
|
+
# Hand-written SLOC is additions + deletions on files that are not lockfiles
|
|
60
|
+
# or generated. Counted files are those same paths.
|
|
61
|
+
#
|
|
62
|
+
# | Tier | Hand-written SLOC | Counted files |
|
|
63
|
+
# | S | under 50 | 3 or fewer |
|
|
64
|
+
# | M | 50–299 | 4–10 |
|
|
65
|
+
# | L | 300–999 | 11–30 |
|
|
66
|
+
# | XL | 1000 or more | 31 or more |
|
|
67
|
+
TIER_ORDER = ("S", "M", "L", "XL")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True)
|
|
71
|
+
class FileChange:
|
|
72
|
+
filename: str
|
|
73
|
+
additions: int
|
|
74
|
+
deletions: int
|
|
75
|
+
status: str = "modified"
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def excluded(self) -> bool:
|
|
79
|
+
return is_excluded(self.filename)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True)
|
|
83
|
+
class CheckRun:
|
|
84
|
+
name: str
|
|
85
|
+
conclusion: str
|
|
86
|
+
started_at: datetime | None
|
|
87
|
+
completed_at: datetime | None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class Deployment:
|
|
92
|
+
environment: str
|
|
93
|
+
state: str
|
|
94
|
+
created_at: datetime
|
|
95
|
+
updated_at: datetime
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass(frozen=True)
|
|
99
|
+
class Incident:
|
|
100
|
+
started_at: datetime | None
|
|
101
|
+
resolved_at: datetime | None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass(frozen=True)
|
|
105
|
+
class Pull:
|
|
106
|
+
repo: str
|
|
107
|
+
number: int
|
|
108
|
+
title: str
|
|
109
|
+
url: str
|
|
110
|
+
author: str
|
|
111
|
+
created_at: datetime
|
|
112
|
+
merged_at: datetime | None
|
|
113
|
+
closed_at: datetime | None
|
|
114
|
+
base_ref: str
|
|
115
|
+
issue_created_at: datetime | None
|
|
116
|
+
commits: tuple[datetime, ...]
|
|
117
|
+
checks: tuple[CheckRun, ...]
|
|
118
|
+
deployments: tuple[Deployment, ...]
|
|
119
|
+
files: tuple[FileChange, ...]
|
|
120
|
+
agent_ids: frozenset[str]
|
|
121
|
+
reverted: bool
|
|
122
|
+
incident: Incident | None
|
|
123
|
+
body: str = ""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@dataclass(frozen=True)
|
|
127
|
+
class Sloc:
|
|
128
|
+
hand_written: int
|
|
129
|
+
excluded: int
|
|
130
|
+
files_counted: int
|
|
131
|
+
files_excluded: int
|
|
132
|
+
tier: str
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def is_excluded(filename: str) -> bool:
|
|
136
|
+
path = filename.replace("\\", "/")
|
|
137
|
+
name = path.rsplit("/", 1)[-1].lower()
|
|
138
|
+
if name in LOCKFILES:
|
|
139
|
+
return True
|
|
140
|
+
lowered = f"/{path.lower()}"
|
|
141
|
+
return any(marker in lowered for marker in GENERATED_MARKERS)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def measure(files: tuple[FileChange, ...]) -> Sloc:
|
|
145
|
+
hand = excluded = files_counted = files_excluded = 0
|
|
146
|
+
for change in files:
|
|
147
|
+
churn = max(change.additions, 0) + max(change.deletions, 0)
|
|
148
|
+
if change.excluded:
|
|
149
|
+
excluded += churn
|
|
150
|
+
files_excluded += 1
|
|
151
|
+
else:
|
|
152
|
+
hand += churn
|
|
153
|
+
files_counted += 1
|
|
154
|
+
return Sloc(
|
|
155
|
+
hand_written=hand,
|
|
156
|
+
excluded=excluded,
|
|
157
|
+
files_counted=files_counted,
|
|
158
|
+
files_excluded=files_excluded,
|
|
159
|
+
tier=tier_for(hand, files_counted),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def tier_for(sloc: int, files: int) -> str:
|
|
164
|
+
if sloc < 50:
|
|
165
|
+
by_sloc = 0
|
|
166
|
+
elif sloc < 300:
|
|
167
|
+
by_sloc = 1
|
|
168
|
+
elif sloc < 1000:
|
|
169
|
+
by_sloc = 2
|
|
170
|
+
else:
|
|
171
|
+
by_sloc = 3
|
|
172
|
+
if files <= 3:
|
|
173
|
+
by_files = 0
|
|
174
|
+
elif files <= 10:
|
|
175
|
+
by_files = 1
|
|
176
|
+
elif files <= 30:
|
|
177
|
+
by_files = 2
|
|
178
|
+
else:
|
|
179
|
+
by_files = 3
|
|
180
|
+
return TIER_ORDER[max(by_sloc, by_files)]
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def load_fixture(path: Path) -> list[Pull]:
|
|
184
|
+
doc = json.loads(path.read_text(encoding="utf-8"))
|
|
185
|
+
pulls: list[Pull] = []
|
|
186
|
+
for repo in doc.get("repos") or []:
|
|
187
|
+
full_name = str(repo["full_name"])
|
|
188
|
+
for raw in repo.get("pulls") or []:
|
|
189
|
+
pulls.append(pull_from_dict(full_name, raw))
|
|
190
|
+
return pulls
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def pull_from_dict(repo: str, raw: dict) -> Pull:
|
|
194
|
+
commits = tuple(parse_time(item["committed_at"]) for item in raw.get("commits") or [])
|
|
195
|
+
checks = tuple(
|
|
196
|
+
CheckRun(
|
|
197
|
+
name=str(item.get("name") or "ci"),
|
|
198
|
+
conclusion=str(item.get("conclusion") or ""),
|
|
199
|
+
started_at=_opt_time(item.get("started_at")),
|
|
200
|
+
completed_at=_opt_time(item.get("completed_at")),
|
|
201
|
+
)
|
|
202
|
+
for item in raw.get("check_runs") or []
|
|
203
|
+
)
|
|
204
|
+
deployments = tuple(
|
|
205
|
+
Deployment(
|
|
206
|
+
environment=str(item.get("environment") or "production"),
|
|
207
|
+
state=str(item.get("state") or ""),
|
|
208
|
+
created_at=parse_time(item["created_at"]),
|
|
209
|
+
updated_at=parse_time(item.get("updated_at") or item["created_at"]),
|
|
210
|
+
)
|
|
211
|
+
for item in raw.get("deployments") or []
|
|
212
|
+
)
|
|
213
|
+
files = tuple(
|
|
214
|
+
FileChange(
|
|
215
|
+
filename=str(item["filename"]),
|
|
216
|
+
additions=int(item.get("additions") or 0),
|
|
217
|
+
deletions=int(item.get("deletions") or 0),
|
|
218
|
+
status=str(item.get("status") or "modified"),
|
|
219
|
+
)
|
|
220
|
+
for item in raw.get("files") or []
|
|
221
|
+
)
|
|
222
|
+
incident_raw = raw.get("incident")
|
|
223
|
+
incident = None
|
|
224
|
+
if incident_raw:
|
|
225
|
+
incident = Incident(
|
|
226
|
+
started_at=_opt_time(incident_raw.get("started_at")),
|
|
227
|
+
resolved_at=_opt_time(incident_raw.get("resolved_at")),
|
|
228
|
+
)
|
|
229
|
+
explicit = {str(item) for item in raw.get("agent_ids") or [] if str(item).strip()}
|
|
230
|
+
texts = [str(raw.get("body") or "")]
|
|
231
|
+
texts.extend(str(item.get("message") or "") for item in raw.get("commits") or [])
|
|
232
|
+
explicit |= _agents_in_text("\n".join(texts))
|
|
233
|
+
number = int(raw["number"])
|
|
234
|
+
return Pull(
|
|
235
|
+
repo=repo,
|
|
236
|
+
number=number,
|
|
237
|
+
title=str(raw.get("title") or f"PR {number}"),
|
|
238
|
+
url=str(raw.get("url") or f"https://github.com/{repo}/pull/{number}"),
|
|
239
|
+
author=str(raw.get("author") or "unknown"),
|
|
240
|
+
created_at=parse_time(raw["created_at"]),
|
|
241
|
+
merged_at=_opt_time(raw.get("merged_at")),
|
|
242
|
+
closed_at=_opt_time(raw.get("closed_at")),
|
|
243
|
+
base_ref=str(raw.get("base_ref") or "main"),
|
|
244
|
+
issue_created_at=_opt_time(raw.get("issue_created_at")),
|
|
245
|
+
commits=commits,
|
|
246
|
+
checks=checks,
|
|
247
|
+
deployments=deployments,
|
|
248
|
+
files=files,
|
|
249
|
+
agent_ids=frozenset(explicit),
|
|
250
|
+
reverted=bool(raw.get("reverted")),
|
|
251
|
+
incident=incident,
|
|
252
|
+
body=str(raw.get("body") or ""),
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _agents_in_text(text: str) -> set[str]:
|
|
257
|
+
found: set[str] = set()
|
|
258
|
+
for match in AGENT_RE.finditer(text):
|
|
259
|
+
token = match.group(1)
|
|
260
|
+
if token.lower().startswith("bc-") or token.lower().startswith("agent-"):
|
|
261
|
+
found.add(token)
|
|
262
|
+
return found
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _opt_time(value: object) -> datetime | None:
|
|
266
|
+
if value is None or value == "":
|
|
267
|
+
return None
|
|
268
|
+
return parse_time(str(value))
|
ukaf/dora.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""DORA metrics that reuse the stage clock instead of inventing another one.
|
|
2
|
+
|
|
3
|
+
- Deploy frequency is a count per week, not a duration.
|
|
4
|
+
- Lead time for one feature is testing + deploy (first push → acceptance).
|
|
5
|
+
- Change failure rate is failed changes / changes that shipped.
|
|
6
|
+
- Time to restore is the fixes stage on the changes that failed.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from statistics import median
|
|
13
|
+
|
|
14
|
+
from ukaf.story import Story
|
|
15
|
+
from ukaf.timeutil import delta_us
|
|
16
|
+
|
|
17
|
+
WEEK_US = 7 * 86_400 * 1_000_000
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class Dora:
|
|
22
|
+
deploys: int
|
|
23
|
+
deploy_source: str
|
|
24
|
+
weeks: float
|
|
25
|
+
deploys_per_week: float
|
|
26
|
+
lead_time_us: int | None
|
|
27
|
+
change_failure_rate: float | None
|
|
28
|
+
failed_changes: int
|
|
29
|
+
shipped_changes: int
|
|
30
|
+
time_to_restore_us: int | None
|
|
31
|
+
restore_samples: int
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def dora_for(story: Story) -> Dora:
|
|
35
|
+
shipped = [feature for feature in story.features if feature.merged or feature.deployed]
|
|
36
|
+
prod_deploys = sum(feature.prod_deploys for feature in story.features)
|
|
37
|
+
if prod_deploys:
|
|
38
|
+
deploys = prod_deploys
|
|
39
|
+
source = "successful production deployments"
|
|
40
|
+
else:
|
|
41
|
+
deploys = sum(1 for feature in shipped if feature.merged)
|
|
42
|
+
source = "merged pull requests, because the input has no production deployment"
|
|
43
|
+
if story.window_start and story.window_end:
|
|
44
|
+
span = max(delta_us(story.window_start, story.window_end), 1)
|
|
45
|
+
else:
|
|
46
|
+
span = WEEK_US
|
|
47
|
+
weeks = span / WEEK_US
|
|
48
|
+
leads = [feature.lead_time_us for feature in shipped]
|
|
49
|
+
failed = [feature for feature in shipped if feature.failed]
|
|
50
|
+
restores = [feature.stages[4].duration_us for feature in failed]
|
|
51
|
+
return Dora(
|
|
52
|
+
deploys=deploys,
|
|
53
|
+
deploy_source=source,
|
|
54
|
+
weeks=weeks,
|
|
55
|
+
deploys_per_week=(deploys / weeks) if weeks else 0.0,
|
|
56
|
+
lead_time_us=int(median(leads)) if leads else None,
|
|
57
|
+
change_failure_rate=(len(failed) / len(shipped)) if shipped else None,
|
|
58
|
+
failed_changes=len(failed),
|
|
59
|
+
shipped_changes=len(shipped),
|
|
60
|
+
time_to_restore_us=int(median(restores)) if restores else None,
|
|
61
|
+
restore_samples=len(restores),
|
|
62
|
+
)
|