-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlagsmithClient.ts
More file actions
349 lines (299 loc) · 9.12 KB
/
FlagsmithClient.ts
File metadata and controls
349 lines (299 loc) · 9.12 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { DiscoveryApi, FetchApi } from '@backstage/core-plugin-api';
export interface FlagsmithOrganization {
id: number;
name: string;
created_date: string;
}
export interface FlagsmithProject {
id: number;
name: string;
organisation: number;
created_date: string;
}
export interface FlagsmithTag {
id: number;
label: string;
color?: string;
}
export interface FlagsmithEnvironment {
id: number;
name: string;
api_key: string;
project: number;
use_v2_feature_versioning?: boolean;
}
export interface FlagsmithFeature {
id: number;
name: string;
description?: string;
created_date: string;
project: number;
environment_state?: Array<{
id: number;
enabled: boolean;
feature_segment?: number | null;
}> | null;
num_segment_overrides?: number | null;
num_identity_overrides?: number | null;
live_version?: {
is_live: boolean;
live_from?: string | null;
published: boolean;
published_by?: string | null;
uuid?: string;
} | null;
owners?: Array<{
id: number;
name: string;
email: string;
}>;
group_owners?: Array<{
id: number;
name: string;
}>;
created_by?: {
id: number;
email: string;
first_name?: string;
last_name?: string;
} | null;
tags?: Array<number>;
is_server_key_only?: boolean;
type?: string;
default_enabled?: boolean;
is_archived?: boolean;
initial_value?: string | null;
multivariate_options?: Array<{
id: number;
type: string;
integer_value?: number | null;
string_value?: string | null;
boolean_value?: boolean | null;
default_percentage_allocation: number;
}>;
}
export interface FlagsmithFeatureVersion {
uuid: string;
is_live: boolean;
live_from?: string | null;
published: boolean;
published_by?: string | null;
}
export interface FlagsmithFeatureStateValue {
string_value?: string | null;
integer_value?: number | null;
boolean_value?: boolean | null;
}
export interface FlagsmithFeatureSegment {
segment: number;
priority: number;
}
export interface FlagsmithFeatureState {
id: number;
enabled: boolean;
environment?: number;
feature_segment?: FlagsmithFeatureSegment | null;
feature_state_value?: FlagsmithFeatureStateValue | null;
updated_at?: string | null;
}
export interface FlagsmithFeatureDetails {
liveVersion: FlagsmithFeatureVersion | null;
featureState: FlagsmithFeatureState[] | null;
segmentOverrides: number;
scheduledVersion: FlagsmithFeatureVersion | null;
}
export interface FlagsmithUsageData {
flags: number | null;
identities: number;
traits: number;
environment_document: number;
day: string;
labels: {
client_application_name: string | null;
client_application_version: string | null;
user_agent: string | null;
};
}
export class FlagsmithClient {
constructor(
private readonly discoveryApi: DiscoveryApi,
private readonly fetchApi: FetchApi,
) {}
private async getBaseUrl(): Promise<string> {
const proxyUrl = await this.discoveryApi.getBaseUrl('proxy');
return `${proxyUrl}/flagsmith`;
}
async getOrganizations(): Promise<FlagsmithOrganization[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(`${baseUrl}/organisations/`);
if (!response.ok) {
throw new Error(`Failed to fetch organizations: ${response.statusText}`);
}
const data = await response.json();
return data.results || data;
}
async getProjectsInOrg(orgId: number): Promise<FlagsmithProject[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/organisations/${orgId}/projects/`,
);
if (!response.ok) {
throw new Error(`Failed to fetch projects: ${response.statusText}`);
}
const data = await response.json();
return data.results || data;
}
async getProjectFeatures(projectId: string): Promise<FlagsmithFeature[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/projects/${projectId}/features/`,
);
if (!response.ok) {
throw new Error(`Failed to fetch features: ${response.statusText}`);
}
const data = await response.json();
return data.results || data;
}
async getProjectEnvironments(
projectId: number,
): Promise<FlagsmithEnvironment[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/projects/${projectId}/environments/`,
);
if (!response.ok) {
throw new Error(`Failed to fetch environments: ${response.statusText}`);
}
const data = await response.json();
return data.results || data;
}
async getProject(projectId: number): Promise<FlagsmithProject> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/projects/${projectId}/`,
);
if (!response.ok) {
throw new Error(`Failed to fetch project: ${response.statusText}`);
}
return await response.json();
}
async getProjectTags(projectId: number): Promise<FlagsmithTag[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/projects/${projectId}/tags/`,
);
if (!response.ok) {
throw new Error(`Failed to fetch project tags: ${response.statusText}`);
}
const data = await response.json();
return data.results || data;
}
async getUsageData(
orgId: number,
projectId?: number,
environmentId?: number,
): Promise<FlagsmithUsageData[]> {
const baseUrl = await this.getBaseUrl();
const url = new URL(`${baseUrl}/organisations/${orgId}/usage-data/`);
if (projectId) {
url.searchParams.set('project_id', projectId.toString());
}
if (environmentId) {
url.searchParams.set('environment_id', environmentId.toString());
}
const response = await this.fetchApi.fetch(url.toString());
if (!response.ok) {
throw new Error(`Failed to fetch usage data: ${response.statusText}`);
}
return await response.json();
}
/**
* Fetch usage data for multiple environments in parallel
*/
async getUsageDataByEnvironments(
orgId: number,
projectId: number,
environments: Pick<FlagsmithEnvironment, 'id' | 'name'>[],
): Promise<Map<string, FlagsmithUsageData[]>> {
const results = new Map<string, FlagsmithUsageData[]>();
// Fetch usage data for each environment in parallel
const promises = environments.map(async env => {
try {
const data = await this.getUsageData(orgId, projectId, env.id);
return { envName: env.name, data };
} catch {
// If environment-level filtering isn't supported, return empty
return { envName: env.name, data: [] };
}
});
const responses = await Promise.all(promises);
responses.forEach(({ envName, data }) => {
results.set(envName, data);
});
return results;
}
// Lazy loading methods for feature details
async getFeatureVersions(
environmentId: number,
featureId: number,
): Promise<FlagsmithFeatureVersion[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/environments/${environmentId}/features/${featureId}/versions/`,
);
if (!response.ok) {
throw new Error(
`Failed to fetch feature versions: ${response.statusText}`,
);
}
const data = await response.json();
return data.results || data;
}
async getFeatureStates(
environmentId: number,
featureId: number,
versionUuid: string,
): Promise<FlagsmithFeatureState[]> {
const baseUrl = await this.getBaseUrl();
const response = await this.fetchApi.fetch(
`${baseUrl}/environments/${environmentId}/features/${featureId}/versions/${versionUuid}/featurestates/`,
);
if (!response.ok) {
throw new Error(`Failed to fetch feature states: ${response.statusText}`);
}
return await response.json();
}
// Helper to load full feature details (called on accordion expand)
async getFeatureDetails(
environmentId: number,
featureId: number,
): Promise<FlagsmithFeatureDetails> {
const versions = await this.getFeatureVersions(environmentId, featureId);
const liveVersion = versions.find(v => v.is_live) || null;
// Find next scheduled version (future live_from date, not yet live)
// If multiple versions are scheduled, pick the earliest one
const now = new Date();
const scheduledVersions = versions
.filter(v => !v.is_live && v.live_from && new Date(v.live_from) > now)
.sort((a, b) => new Date(a.live_from!).getTime() - new Date(b.live_from!).getTime());
const scheduledVersion = scheduledVersions[0] || null;
let featureState: FlagsmithFeatureState[] | null = null;
let segmentOverrides = 0;
if (liveVersion) {
featureState = await this.getFeatureStates(
environmentId,
featureId,
liveVersion.uuid,
);
segmentOverrides = (featureState || []).filter(
s => s.feature_segment !== null,
).length;
}
return {
liveVersion,
featureState,
segmentOverrides,
scheduledVersion,
};
}
}