Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1/* eslint-disable max-lines */
2import { inject } from '../../iocContainer/utils.js';
3import {
4 type IActionStatisticsAdapter,
5 type ActionCountsInput,
6} from '../../plugins/warehouse/queries/IActionStatisticsAdapter.js';
7import { YEAR_MS } from '../../utils/time.js';
8
9class ActionStatisticsService {
10 constructor(
11 private readonly adapter: IActionStatisticsAdapter,
12 ) {}
13
14 /**
15 * Returns the total number of content submissions actioned on each day,
16 * for the given org. Looks across all rules and includes actions triggered
17 * manually.
18 *
19 * Even with the functions that return the number of actioned submissions by
20 * policy or tag, this function is necessary because the same content
21 * submission may be actioned on under multiple policies/tags, so you'll get
22 * the wrong result if you just add up the number of actioned submissions
23 * (e.g.) across all policies.
24 */
25 async getActionedSubmissionCountsByDay(
26 orgId: string,
27 startAt: Date = new Date(Date.now() - YEAR_MS),
28 ) {
29 return this.adapter.getActionedSubmissionCountsByDay(orgId, startAt);
30 }
31
32 async getActionedSubmissionCountsByTagByDay(
33 orgId: string,
34 startAt: Date = new Date(Date.now() - YEAR_MS),
35 ) {
36 return this.adapter.getActionedSubmissionCountsByTagByDay(
37 orgId,
38 startAt,
39 );
40 }
41
42 async getActionedSubmissionCountsByPolicyByDay(
43 orgId: string,
44 startAt: Date = new Date(Date.now() - YEAR_MS),
45 ) {
46 return this.adapter.getActionedSubmissionCountsByPolicyByDay(
47 orgId,
48 startAt,
49 );
50 }
51
52 /**
53 * NB: this technically returns just the number of actions of each type taken
54 * each day, _not_ the number of submissions that received each action each
55 * day. However, those two quantities are the same, except if the same content
56 * submission passed to the same action twice (in the same day), which should
57 * almost never happen, so sticking with this simpler query is good enough.
58 *
59 * If we did approx_count_distinct(item_submission_id), instead of count(*),
60 * then this would be measuring what's implied by the function name, except
61 * that it might be slower and would likely have more error (given how rare
62 * it'll be for the same content submission to get the same action twice in a
63 * day).
64 */
65 async getActionedSubmissionCountsByActionByDay(
66 orgId: string,
67 startAt: Date = new Date(Date.now() - YEAR_MS),
68 ) {
69 return this.adapter.getActionedSubmissionCountsByActionByDay(
70 orgId,
71 startAt,
72 );
73 }
74
75 /**
76 * Returns the number of actions taken total across an org by day
77 */
78 async getActionCountsPerDay(
79 orgId: string,
80 startAt: Date = new Date(Date.now() - YEAR_MS),
81 ) {
82 return this.adapter.getActionCountsPerDay(orgId, startAt);
83 }
84
85 async getPoliciesSortedByViolationCount(input: {
86 filterBy: {
87 startDate: Date;
88 endDate: Date;
89 };
90 timeZone: string;
91 orgId: string;
92 }) {
93 return this.adapter.getPoliciesSortedByViolationCount(input);
94 }
95 async getAllActionCountsGroupByPolicy(input: ActionCountsInput) {
96 return this.adapter.getAllActionCountsGroupByPolicy(input);
97 }
98
99 async getAllActionCountsGroupByActionId(input: ActionCountsInput) {
100 return this.adapter.getAllActionCountsGroupByActionId(input);
101 }
102 async getAllActionCountsGroupBySource(input: ActionCountsInput) {
103 return this.adapter.getAllActionCountsGroupBySource(input);
104 }
105 async getAllActionCountsGroupByItemTypeId(input: ActionCountsInput) {
106 return this.adapter.getAllActionCountsGroupByItemTypeId(input);
107 }
108
109 async getAllActionCountsGroupByRule(input: ActionCountsInput) {
110 return this.adapter.getAllActionCountsGroupByRule(input);
111 }
112
113 async getAllActionCountsGroupBy(input: ActionCountsInput) {
114 return this.adapter.getAllActionCountsGroupBy(input);
115 }
116}
117
118export default inject(['ActionStatisticsAdapter'], ActionStatisticsService);
119export { type ActionStatisticsService };