All files / src/services/api/routers ActivityRouter.tsx

94.11% Statements 48/51
92.3% Branches 36/39
100% Functions 7/7
92.3% Lines 36/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                      2219x                   2219x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x     13x 12x   1x       2219x 8x 8x 8x 8x     8x 8x                   2219x     8x 8x 8x 8x 8x 8x 8x     8x 8x           2219x 4x 4x 4x          
import {
  ActivityEventDetail,
  BackgroundEventList,
  BackgroundQuery,
  ActivityEventList,
  ActivityQuery,
  ActivitySummary,
} from "@/objects/Activity";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { APIRouter } from "../ApiRouter";
 
/**
 * The account-wide activity feed — the audit trail read across resources
 * rather than down one.
 *
 * A resource's own `/history` answers "what happened to this?"; this answers
 * "what happened, across everything?" Read-only, like the generations
 * router: the rows are written by the flows that perform the changes.
 */
export class ActivityRouter extends APIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("activity", host, token, default_config, headers);
  }
 
  /**
   * Resource changes, newest first.
   *
   * `resource_type` repeats as `resource_type=a&resource_type=b` — FastAPI's
   * shape for a list query parameter, which axios' default serialiser does
   * not produce, so the params are built by hand.
   */
  list = async (query: ActivityQuery = {}): Promise<ActivityEventList | null> => {
    const params = new URLSearchParams();
    query.resource_type?.forEach((t) => params.append("resource_type", t));
    if (query.action) params.set("action", query.action);
    if (query.generated_only) params.set("generated_only", "true");
    if (query.since) params.set("since", query.since);
    if (query.search) params.set("search", query.search);
    if (query.limit != null) params.set("limit", String(query.limit));
    if (query.offset != null) params.set("offset", String(query.offset));
 
    const qs = params.toString();
    const res = await this.api.get<ActivityEventList>(
      qs ? `${this.prefix}?${qs}` : this.prefix,
    );
    if (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /** Counts over the same window the feed is showing. */
  summary = async (since?: string): Promise<ActivitySummary | null> => {
    const params = new URLSearchParams();
    if (since) params.set("since", since);
    const qs = params.toString();
    const res = await this.api.get<ActivitySummary>(
      qs ? `${this.prefix}/summary?${qs}` : `${this.prefix}/summary`,
    );
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /**
   * Work done for the caller in the background — tile refreshes, website
   * crawls, retention purges — newest first. `kind` repeats per value, like
   * `resource_type` above.
   */
  background = async (
    query: BackgroundQuery = {},
  ): Promise<BackgroundEventList | null> => {
    const params = new URLSearchParams();
    query.kind?.forEach((k) => params.append("kind", k));
    if (query.since) params.set("since", query.since);
    if (query.limit != null) params.set("limit", String(query.limit));
    if (query.offset != null) params.set("offset", String(query.offset));
    const qs = params.toString();
    const res = await this.api.get<BackgroundEventList>(
      qs ? `${this.prefix}/background?${qs}` : `${this.prefix}/background`,
    );
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /** One event, with its snapshot and the diff against the version before. */
  get = async (eventId: string): Promise<ActivityEventDetail | null> => {
    const res = await this.api.get<ActivityEventDetail>(super.endpoint(eventId));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
}