Repository navigation
Expand file tree
/
Copy pathModelUsageCounterScope.java
More file actions
171 lines (155 loc) · 7.83 KB
/
Copy pathModelUsageCounterScope.java
File metadata and controls
171 lines (155 loc) · 7.83 KB
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
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.engine.impl.model.usage;
import java.util.Objects;
import prerna.engine.impl.model.inferencetracking.ModelUsageLogFilter;
/**
* Whose use a usage limit counter counts. With a rule's metric and a window
* bucket it makes one counter.
*
* <ul>
* <li>{@link ScopeType#PERSON_MODEL}: all of a person's use of one model,
* whichever source paid; for direct limits and public use rules for each
* person.</li>
* <li>{@link ScopeType#TEAM_SHARED} and {@link ScopeType#TEAM_MEMBER}: the
* calls billed to a team on one model, together or for one member.</li>
* <li>{@link ScopeType#PUBLIC_SHARED}: the calls billed to a model's public
* use.</li>
* <li>{@link ScopeType#GRANTEE_SHARED} and {@link ScopeType#GRANTEE_MEMBER}: the
* calls billed to an app or engine on one model, together or for one
* person.</li>
* <li>{@link ScopeType#TEAM_TOTAL}: the calls billed to a team on any model; a
* router's own calls are left out so a routed call counts once.</li>
* <li>{@link ScopeType#MODEL_TOTAL}: every call a model answers.</li>
* <li>{@link ScopeType#PERSON_ALL_MODELS}: a person's use of every model, for
* the all-models and platform defaults; a router's own calls are left out so a
* routed call counts once.</li>
* </ul>
*
* @param scopeType whose use it counts
* @param engineId the model, or null across all models
* @param teamId the team, or null
* @param teamType the team's type, or null
* @param userId the person's primary login id, or null
* @param userType the person's primary login type, or null
* @param granteeEngineId the app or engine given the model's permission, or
* null
* @param granteeEngineType its catalog type, or null
*/
public record ModelUsageCounterScope(ScopeType scopeType, String engineId, String teamId, String teamType,
String userId, String userType, String granteeEngineId, String granteeEngineType) {
/** Whose use a counter counts */
public enum ScopeType {
PERSON_MODEL, TEAM_SHARED, TEAM_MEMBER, PUBLIC_SHARED, TEAM_TOTAL, MODEL_TOTAL, PERSON_ALL_MODELS,
GRANTEE_SHARED, GRANTEE_MEMBER
}
public static ModelUsageCounterScope personModel(String engineId, String userId, String userType) {
return new ModelUsageCounterScope(ScopeType.PERSON_MODEL, engineId, null, null, userId, userType, null, null);
}
public static ModelUsageCounterScope teamShared(String engineId, String teamId, String teamType) {
return new ModelUsageCounterScope(ScopeType.TEAM_SHARED, engineId, teamId, teamType, null, null, null, null);
}
public static ModelUsageCounterScope teamMember(String engineId, String teamId, String teamType, String userId,
String userType) {
return new ModelUsageCounterScope(ScopeType.TEAM_MEMBER, engineId, teamId, teamType, userId, userType, null,
null);
}
public static ModelUsageCounterScope publicShared(String engineId) {
return new ModelUsageCounterScope(ScopeType.PUBLIC_SHARED, engineId, null, null, null, null, null, null);
}
public static ModelUsageCounterScope teamTotal(String teamId, String teamType) {
return new ModelUsageCounterScope(ScopeType.TEAM_TOTAL, null, teamId, teamType, null, null, null, null);
}
public static ModelUsageCounterScope modelTotal(String engineId) {
return new ModelUsageCounterScope(ScopeType.MODEL_TOTAL, engineId, null, null, null, null, null, null);
}
public static ModelUsageCounterScope personAllModels(String userId, String userType) {
return new ModelUsageCounterScope(ScopeType.PERSON_ALL_MODELS, null, null, null, userId, userType, null,
null);
}
public static ModelUsageCounterScope granteeShared(String engineId, String granteeEngineId,
String granteeEngineType) {
return new ModelUsageCounterScope(ScopeType.GRANTEE_SHARED, engineId, null, null, null, null, granteeEngineId,
granteeEngineType);
}
public static ModelUsageCounterScope granteeMember(String engineId, String granteeEngineId,
String granteeEngineType, String userId, String userType) {
return new ModelUsageCounterScope(ScopeType.GRANTEE_MEMBER, engineId, null, null, userId, userType,
granteeEngineId, granteeEngineType);
}
/**
* The scope's name within a counter key: its type, then the model, team and
* person it covers.
*
* @return the scope's part of a counter key; each part is escaped, so an id
* that holds {@code |} cannot make two scopes share a key. An app's or
* engine's scope ends with its catalog type and id
*/
public String id() {
String id = scopeType.name() + "|" + part(engineId) + "|" + part(teamType) + "|" + part(teamId) + "|"
+ part(userType) + "|" + part(userId);
return granteeEngineId == null ? id : id + "|" + part(granteeEngineType) + "|" + part(granteeEngineId);
}
/**
* The rows match what the counter counts: billed rows for a team, an app, an
* engine or public use, every row of the person or model otherwise, and the scopes across
* all models leave out a router's own rows, as their counters do.
*
* @return the {@code MESSAGE} rows the counter is filled from
*/
public ModelUsageLogFilter logFilter() {
switch (scopeType) {
case PERSON_MODEL:
return new ModelUsageLogFilter(engineId, userId, userType, null, null, null, false);
case TEAM_SHARED:
return new ModelUsageLogFilter(engineId, null, null, ModelUsageBilledTo.TEAM, teamId, teamType, false);
case TEAM_MEMBER:
return new ModelUsageLogFilter(engineId, userId, userType, ModelUsageBilledTo.TEAM, teamId, teamType,
false);
case PUBLIC_SHARED:
return new ModelUsageLogFilter(engineId, null, null, ModelUsageBilledTo.PUBLIC, null, null, false);
case TEAM_TOTAL:
return new ModelUsageLogFilter(null, null, null, ModelUsageBilledTo.TEAM, teamId, teamType, true);
case MODEL_TOTAL:
return new ModelUsageLogFilter(engineId, null, null, null, null, null, false);
case PERSON_ALL_MODELS:
return new ModelUsageLogFilter(null, userId, userType, null, null, null, true);
case GRANTEE_SHARED:
return new ModelUsageLogFilter(engineId, null, null, ModelUsageBilledTo.ENGINE, granteeEngineId,
granteeEngineType, false);
case GRANTEE_MEMBER:
return new ModelUsageLogFilter(engineId, userId, userType, ModelUsageBilledTo.ENGINE, granteeEngineId,
granteeEngineType, false);
default:
throw new IllegalStateException("Unknown counter scope " + scopeType);
}
}
private static String part(String value) {
return Objects.toString(value, "").replace("%", "%25").replace("|", "%7C");
}
}