File size: 2,532 Bytes
6c3af4e
 
 
 
 
 
 
 
 
af13576
 
 
 
 
 
 
 
 
 
 
 
 
6c3af4e
 
 
 
af13576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af13576
6c3af4e
 
 
af13576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c3af4e
 
 
af13576
 
 
 
6c3af4e
 
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
/**
 * Aggregate counters for the reranking stage.
 *
 * The reranker sees the query and every snippet, and none of that is kept: how
 * many results came in, how many survived the score filter, how long the model
 * took, and how often it was unavailable. Those are the numbers the filter's
 * two thresholds should be moved against, and today they have none.
 */

import type { SearchType } from "./searchesSinceLastRestart.ts";

interface RerankCounts {
  reranks: number;
  totalMs: number;
  considered: number;
  kept: number;
}

const byType: Record<SearchType, RerankCounts> = {
  text: { reranks: 0, totalMs: 0, considered: 0, kept: 0 },
  images: { reranks: 0, totalMs: 0, considered: 0, kept: 0 },
};
let fallbackApplied = 0;
let skippedUnhealthy = 0;
let failed = 0;

export function recordRerank(
  searchType: SearchType,
  {
    considered,
    kept,
    durationMs,
    usedPercentageFallback,
  }: {
    considered: number;
    kept: number;
    durationMs: number;
    usedPercentageFallback: boolean;
  },
): void {
  const counts = byType[searchType];
  counts.reranks++;
  counts.totalMs += durationMs;
  counts.considered += considered;
  counts.kept += kept;
  if (usedPercentageFallback) fallbackApplied++;
}

/** The model was not loaded, so the endpoint served SearXNG's own order. */
export function recordRerankSkipped(): void {
  skippedUnhealthy++;
}

/** The model was loaded and reranking threw anyway. */
export function recordRerankFailed(): void {
  failed++;
}

/**
 * `keptRate` is a share rather than a count for the same reason
 * `pageReads.excerptKeptRate` is: a count of searches whose results were
 * filtered reads ~100% whatever the threshold is, and settles nothing.
 */
function summarize({ reranks, totalMs, considered, kept }: RerankCounts) {
  return {
    reranks,
    averageMs: Math.round(totalMs / reranks || 0),
    considered,
    kept,
    keptRate: Number(((kept / considered) * 100 || 0).toFixed(1)),
  };
}

export function getRerankingStats() {
  const totals = Object.values(byType).reduce(
    (total, counts) => ({
      reranks: total.reranks + counts.reranks,
      totalMs: total.totalMs + counts.totalMs,
      considered: total.considered + counts.considered,
      kept: total.kept + counts.kept,
    }),
    { reranks: 0, totalMs: 0, considered: 0, kept: 0 },
  );
  return {
    ...summarize(totals),
    fallbackApplied,
    skippedUnhealthy,
    failed,
    byType: {
      text: summarize(byType.text),
      images: summarize(byType.images),
    },
  };
}