taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

operations.py (3977B)


      1 # This file is part of GNU Taler
      2 # (C) 2026 Taler Systems S.A.
      3 #
      4 # GNU Taler is free software; you can redistribute it and/or modify it under the
      5 # terms of the GNU General Public License as published by the Free Software
      6 # Foundation; either version 3, or (at your option) any later version.
      7 #
      8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
      9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     10 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     11 #
     12 # You should have received a copy of the GNU General Public License along with
     13 # GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     14 """Which wallet-core operations the simulator has actually exercised.
     15 
     16 JaCoCo measures the Kotlin shell; the logic that produces "internal error"
     17 screens is TypeScript inside QuickJS and cannot be instrumented through qtart.
     18 The boundary between them can be measured though: `BackendManager` logs every
     19 request it sends and every reply it gets, so the operations exercised, and the
     20 error codes each one produced, can be counted from the logcats the episodes
     21 already keep.
     22 
     23 The denominator is the set of operations the app's own Kotlin code can issue
     24 (`app-operations.txt`, extracted from the sources), not everything wallet-core
     25 offers.
     26 
     27     python3 operations.py /opt/taler-sim/run/out
     28     python3 operations.py /opt/taler-sim/run/out --ops app-operations.txt
     29 """
     30 
     31 from __future__ import annotations
     32 
     33 import argparse
     34 import json
     35 import re
     36 from collections import Counter, defaultdict
     37 from pathlib import Path
     38 
     39 SENT = re.compile(r'"operation"\s*:\s*"([a-zA-Z]+)"')
     40 ERROR_REPLY = re.compile(
     41     r'\{"type":"error","operation":"([a-zA-Z]+)","id":\d+,"error":\{"code":(\d+)')
     42 
     43 
     44 def scan(root: Path) -> tuple[Counter, dict[str, Counter]]:
     45     operations: Counter = Counter()
     46     errors: dict[str, Counter] = defaultdict(Counter)
     47     for log in root.rglob("*logcat*.txt"):
     48         try:
     49             text = log.read_text(errors="replace")
     50         except OSError:
     51             continue
     52         for name in SENT.findall(text):
     53             operations[name] += 1
     54         for name, code in ERROR_REPLY.findall(text):
     55             errors[name][int(code)] += 1
     56     return operations, errors
     57 
     58 
     59 def main() -> int:
     60     ap = argparse.ArgumentParser(description=__doc__)
     61     ap.add_argument("root", type=Path, nargs="?",
     62                     default=Path("/opt/taler-sim/run/out"))
     63     ap.add_argument("--ops", type=Path,
     64                     default=Path(__file__).with_name("app-operations.txt"))
     65     ap.add_argument("--json", action="store_true")
     66     args = ap.parse_args()
     67 
     68     known = [line.strip() for line in args.ops.read_text().splitlines()
     69              if line.strip()] if args.ops.is_file() else []
     70     seen, errors = scan(args.root)
     71 
     72     exercised = [op for op in known if seen.get(op)]
     73     missing = [op for op in known if not seen.get(op)]
     74     extra = sorted(set(seen) - set(known))
     75 
     76     if args.json:
     77         print(json.dumps({
     78             "exercised": exercised, "missing": missing, "undeclared": extra,
     79             "counts": seen, "errors": {k: dict(v) for k, v in errors.items()},
     80         }, indent=2))
     81         return 0
     82 
     83     total = len(known) or len(seen)
     84     print(f"wallet-core operations exercised: {len(exercised)}/{total} "
     85           f"({100 * len(exercised) / total:.0f}%)\n")
     86     print("top operations by calls:")
     87     for name, count in seen.most_common(15):
     88         print(f"  {count:6}  {name}")
     89     print(f"\nnever exercised ({len(missing)}):")
     90     for name in missing:
     91         print(f"  {name}")
     92     if extra:
     93         print(f"\nseen but not in the app's list ({len(extra)}): "
     94               f"{', '.join(extra[:20])}")
     95     print("\noperations that returned an error, with codes:")
     96     for name, codes in sorted(errors.items(), key=lambda kv: -sum(kv[1].values())):
     97         print(f"  {name}: {dict(codes)}")
     98     return 0
     99 
    100 
    101 if __name__ == "__main__":
    102     raise SystemExit(main())