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