admin / Bid-Sentinel
publicBid Scrape and Tracking Application with AI Capability
Bid-Sentinel / bid-sentinel-v2 / backend / app / intelligence.py
10865 B · main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | """Analyst intelligence: aggregate the per-opportunity assessments to surface capability/certification gaps, buyer profiles, win/loss patterns, trends, and data-driven sales angles. Pure functions over ORM Tender rows (no DB access).""" from __future__ import annotations from collections import Counter, defaultdict from datetime import datetime, timedelta, timezone from app.analysis import find_terms from app.scraper.keywords import CYBER_KEYWORDS def _aware(dt): """Coerce a datetime to timezone-aware (assume UTC) for safe comparisons.""" if dt is None: return None return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc) def management_stats(tenders) -> dict: """Pipeline KPIs for the management summary. Mirrors the dashboard rules: a Lost bid leaves the Bidding count and the pipeline value.""" now = datetime.now(timezone.utc) in14 = now + timedelta(days=14) open_now = bidding = closing_soon = 0 pipeline = won = lost = won_value = 0.0 by_status: dict[str, list] = {"Pending": [0, 0.0], "Bid": [0, 0.0], "No Bid": [0, 0.0]} for t in tenders: close = _aware(t.closing_date) is_lost = t.outcome == "Lost" val = t.value_amount or 0 if close and close > now: open_now += 1 if close and now <= close <= in14: closing_soon += 1 if t.bid_status == "Bid" and not is_lost: bidding += 1 if not is_lost: pipeline += val if t.outcome == "Won": won += 1 won_value += val if is_lost: lost += 1 key = t.bid_status.value if hasattr(t.bid_status, "value") else str(t.bid_status) if key in by_status: by_status[key][0] += 1 by_status[key][1] += val decided = won + lost return { "total": len(tenders), "open": open_now, "bidding": bidding, "closing_soon": closing_soon, "pipeline_value": round(pipeline), "won": int(won), "lost": int(lost), "won_value": round(won_value), "win_rate": round(100 * won / decided) if decided else None, "by_status": [ {"label": k, "count": v[0], "value": round(v[1])} for k, v in by_status.items() ], } def bidding_opportunities(tenders, limit: int = 15) -> list[dict]: """Opportunities actively being bid on (status Bid, not yet Lost), by value.""" active = [t for t in tenders if t.bid_status == "Bid" and t.outcome != "Lost"] active.sort(key=lambda t: (t.value_amount or 0), reverse=True) out = [] for t in active[:limit]: close = _aware(t.closing_date) out.append( { "title": t.title or "Untitled", "buyer": (t.buyer or "").strip() or "Unknown buyer", "value": round(t.value_amount or 0), "closing": close.strftime("%d %b %Y") if close else "—", "fit": t.fit_score, "outcome": t.outcome, } ) return out def demand_vocabulary(custom_keywords: list[str]) -> list[str]: """Terms that represent market demand (built-in cyber list + custom keywords).""" seen, vocab = set(), [] for term in [*CYBER_KEYWORDS, *custom_keywords]: if term.lower() not in seen: seen.add(term.lower()) vocab.append(term) return vocab def capability_gaps(tenders, capability_terms: list[str], demand_vocab: list[str]) -> list[dict]: """In-demand service terms appearing across opportunities that you don't offer.""" profile = {t.lower() for t in capability_terms} counts: dict[str, list] = defaultdict(lambda: [0, 0.0]) for t in tenders: text = f"{t.title or ''} {t.description or ''}" for term in find_terms(text, demand_vocab): if term.lower() in profile: continue counts[term][0] += 1 counts[term][1] += t.value_amount or 0 items = [ {"term": k, "opportunities": v[0], "total_value": round(v[1])} for k, v in counts.items() ] items.sort(key=lambda x: (-x["opportunities"], -x["total_value"])) return items def cert_gaps(tenders, accreditations_held: list[str]) -> list[dict]: """Certifications/clearances required across opportunities, flagged held/gap.""" held = {a.lower() for a in accreditations_held} counts: Counter = Counter() for t in tenders: for cert in (t.required_certs or "").split(", "): cert = cert.strip() if cert: counts[cert] += 1 total = len(tenders) or 1 items = [ { "name": name, "required_count": cnt, "demand_pct": round(100 * cnt / total), "held": name.lower() in held, } for name, cnt in counts.items() ] items.sort(key=lambda x: -x["required_count"]) return items def buyer_profiles(tenders, limit: int = 25) -> list[dict]: groups: dict[str, list] = defaultdict(list) for t in tenders: buyer = (t.buyer or "").strip() if buyer: groups[buyer].append(t) out = [] for buyer, ts in groups.items(): vals = [x.value_amount for x in ts if x.value_amount] fits = [x.fit_score for x in ts if x.fit_score is not None] won = sum(1 for x in ts if x.outcome == "Won") lost = sum(1 for x in ts if x.outcome == "Lost") bidding = sum(1 for x in ts if x.bid_status == "Bid") services = Counter(x.matched_keyword for x in ts if x.matched_keyword) certs: Counter = Counter() for x in ts: for c in (x.required_certs or "").split(", "): if c.strip(): certs[c.strip()] += 1 out.append( { "buyer": buyer, "opportunities": len(ts), "total_value": round(sum(vals)), "avg_fit": round(sum(fits) / len(fits)) if fits else None, "won": won, "lost": lost, "bidding": bidding, # A "target" = value in play but no engagement yet. "is_target": bidding == 0 and won == 0 and len(ts) >= 2, "top_services": [s for s, _ in services.most_common(3)], "top_certs": [c for c, _ in certs.most_common(3)], } ) out.sort(key=lambda x: (-x["total_value"], -x["opportunities"])) return out[:limit] def _band(items: dict[str, list]) -> list[dict]: bands = [] for label, (w, l) in items.items(): bands.append( {"label": label, "won": w, "lost": l, "win_rate": round(100 * w / (w + l)) if (w + l) else None} ) return bands def win_loss(tenders) -> dict: won = sum(1 for t in tenders if t.outcome == "Won") lost = sum(1 for t in tenders if t.outcome == "Lost") decided = won + lost by_service: dict[str, list] = defaultdict(lambda: [0, 0]) fit_bands: dict[str, list] = { "High (70-100)": [0, 0], "Medium (40-69)": [0, 0], "Low (0-39)": [0, 0], } for t in tenders: if t.outcome not in ("Won", "Lost"): continue is_won = t.outcome == "Won" svc = t.matched_keyword or "Other" by_service[svc][0 if is_won else 1] += 1 if t.fit_score is not None: band = "High (70-100)" if t.fit_score >= 70 else "Medium (40-69)" if t.fit_score >= 40 else "Low (0-39)" fit_bands[band][0 if is_won else 1] += 1 by_service_list = _band(by_service) by_service_list.sort(key=lambda x: -(x["won"] + x["lost"])) return { "overall_win_rate": round(100 * won / decided) if decided else None, "won": won, "lost": lost, "by_service": by_service_list[:8], "by_fit": _band(fit_bands), } def trends(tenders) -> list[dict]: monthly: dict[str, list] = defaultdict(lambda: [0, 0.0]) for t in tenders: d = t.published_date or t.closing_date if not d: continue key = f"{d.year}-{d.month:02d}" monthly[key][0] += 1 monthly[key][1] += t.value_amount or 0 return [ {"label": k, "count": monthly[k][0], "value": round(monthly[k][1])} for k in sorted(monthly) ] def summary(tenders, capability_terms, demand_vocab, accreditations_held) -> dict: wl = win_loss(tenders) gaps = capability_gaps(tenders, capability_terms, demand_vocab) certs = cert_gaps(tenders, accreditations_held) targets = [b for b in buyer_profiles(tenders) if b["is_target"]] pipeline = sum(t.value_amount or 0 for t in tenders) return { "opportunities": len(tenders), "pipeline_value": round(pipeline), "win_rate": wl["overall_win_rate"], "capability_gaps": len(gaps), "cert_gaps": sum(1 for c in certs if not c["held"]), "target_buyers": len(targets), } def _fmt_money(v) -> str: v = v or 0 if v >= 1_000_000: return f"£{v / 1_000_000:.1f}M" if v >= 1_000: return f"£{round(v / 1_000)}k" return f"£{round(v)}" def deterministic_angle(buyer: dict, accreditations_held: list[str]) -> str: held = {a.lower() for a in accreditations_held} missing = [c for c in buyer["top_certs"] if c.lower() not in held] n = buyer["opportunities"] parts = [ f"{buyer['buyer']} has {n} tracked cyber opportunit{'ies' if n != 1 else 'y'} " f"worth {_fmt_money(buyer['total_value'])}" ] if buyer["avg_fit"] is not None: parts[0] += f" at {buyer['avg_fit']}% average fit" if buyer["is_target"]: parts[0] += ", with no bids from you yet" lead = ", ".join(buyer["top_services"][:2]) if buyer["top_services"] else "your core services" angle = f"{parts[0]}. Lead with {lead}" if missing: angle += f". Gap to close: {', '.join(missing)} required but not held — partner or note as roadmap" return angle + "." def angle_context(buyer: dict, capability_terms: list[str], accreditations_held: list[str]) -> str: """Compact context string handed to the AI for a narrative angle.""" return ( f"Client organisation: {buyer['buyer']}\n" f"Tracked opportunities: {buyer['opportunities']} (value {_fmt_money(buyer['total_value'])})\n" f"Average fit: {buyer['avg_fit']}\n" f"Our engagement: {buyer['bidding']} bids, {buyer['won']} won, {buyer['lost']} lost\n" f"Most-requested services: {', '.join(buyer['top_services']) or 'n/a'}\n" f"Most-requested certifications: {', '.join(buyer['top_certs']) or 'n/a'}\n" f"Our capabilities: {', '.join(capability_terms) or 'n/a'}\n" f"Certifications we hold: {', '.join(accreditations_held) or 'none listed'}" ) |