Improved feature flag loading in frontend #446

This commit is contained in:
Sven Heidemann 2023-12-03 15:13:20 +01:00
parent 0e4419312b
commit 36887f1bdf
7 changed files with 56 additions and 27 deletions

View File

@ -52,4 +52,5 @@ type Query {
discord: Discord
hasFeatureFlag(flag: String): FeatureFlag
featureFlags: [FeatureFlag]
}

View File

@ -40,6 +40,7 @@ type Server implements TableWithHistoryQuery {
config: ServerConfig
hasFeatureFlag(flag: String): FeatureFlag
featureFlags: [FeatureFlag]
statistic(date: String): ServerStatistic

View File

@ -116,6 +116,10 @@ class ServerQuery(DataQueryWithHistoryABC):
"hasFeatureFlag",
lambda server, *_, **kwargs: self._resolve_has_feature_flag(server, *_, **kwargs),
)
self.set_field(
"featureFlags",
lambda server, *_, **kwargs: self._resolve_feature_flags(server, *_),
)
self.set_field("statistic", lambda server, *_, **kwargs: ServerStatistics(server, kwargs))
@staticmethod
@ -142,3 +146,13 @@ class ServerQuery(DataQueryWithHistoryABC):
"key": kwargs["flag"],
"value": FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum(kwargs["flag"])),
}
def _resolve_feature_flags(self, server: Server, *_) -> list[dict]:
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.discord_id}")
return [
{
"key": flag,
"value": FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum(flag)),
}
for flag in [e.value for e in FeatureFlagsEnum]
]

View File

@ -131,6 +131,10 @@ class Query(QueryABC):
"hasFeatureFlag",
lambda *_, **kwargs: self._resolve_has_feature_flag(*_, **kwargs),
)
self.set_field(
"featureFlags",
lambda *_, **kwargs: self._resolve_feature_flags(*_),
)
def _resolve_has_feature_flag(self, *_, **kwargs):
settings: TechnicianConfig = self._config.get_configuration(TechnicianConfig)
@ -140,3 +144,13 @@ class Query(QueryABC):
"key": kwargs["flag"],
"value": FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum(kwargs["flag"])),
}
def _resolve_feature_flags(self, *_) -> list[dict]:
settings: TechnicianConfig = self._config.get_configuration(TechnicianConfig)
return [
{
"key": flag,
"value": FeatureFlagsSettings.get_flag_from_dict(settings.feature_flags, FeatureFlagsEnum(flag)),
}
for flag in [e.value for e in FeatureFlagsEnum]
]

View File

@ -128,6 +128,17 @@ export class Queries {
}
`;
static serverFeatureFlags = `
query HasServerFeatureFlag($filter: ServerFilter) {
servers(filter: $filter) {
featureFlags {
key
value
}
}
}
`;
static gameServerQuery = `
query GameServersList($serverId: ID) {
servers(filter: {id: $serverId}) {

View File

@ -1,7 +1,7 @@
import { GameServer, Server } from "../data/server.model";
import { User } from "../data/user.model";
import { AutoRole, AutoRoleRule } from "../data/auto_role.model";
import { Discord, Guild } from "../data/discord.model";
import { Discord } from "../data/discord.model";
import { Level } from "../data/level.model";
import { Achievement, AchievementAttribute } from "../data/achievement.model";
import { TechnicianConfig } from "../config/technician-config.model";
@ -82,6 +82,10 @@ export interface HasServerFeatureFlagQuery {
hasFeatureFlag: FeatureFlag;
}
export interface FeatureFlagsQuery {
featureFlags: FeatureFlag[];
}
export interface ShortRoleNameListQuery {
shortRoleNameCount: number;
shortRoleNames: ShortRoleName[];

View File

@ -1,6 +1,6 @@
import { Injectable } from "@angular/core";
import { MenuItem } from "primeng/api";
import { BehaviorSubject, forkJoin, Observable } from "rxjs";
import { BehaviorSubject } from "rxjs";
import { AuthRoles } from "../../models/auth/auth-roles.enum";
import { AuthService } from "../auth/auth.service";
import { TranslateService } from "@ngx-translate/core";
@ -9,7 +9,7 @@ import { ThemeService } from "../theme/theme.service";
import { Server } from "../../models/data/server.model";
import { UserDTO } from "../../models/auth/auth-user.dto";
import { ServerService } from "../server.service";
import { HasServerFeatureFlagQuery, PossibleFeatureFlagsQuery, Query } from "../../models/graphql/query.model";
import { FeatureFlagsQuery, Query } from "../../models/graphql/query.model";
import { Queries } from "../../models/graphql/queries.model";
import { DataService } from "../data/data.service";
import { FeatureFlag } from "../../models/config/feature-flags.model";
@ -165,31 +165,15 @@ export class SidebarService {
if (server) {
this.featureFlags = [];
this.data.query<PossibleFeatureFlagsQuery>("{possibleFeatureFlags}"
).subscribe(data => {
let observables: Observable<HasServerFeatureFlagQuery>[] = [];
data.possibleFeatureFlags.forEach(flag => {
observables.push(
this.data.query<HasServerFeatureFlagQuery>(Queries.hasServerFeatureFlag, {
filter: { id: server.id },
flag: flag
},
function(data: Query) {
return data.servers[0];
}
)
);
});
forkJoin(observables).subscribe(data => {
data.forEach(flag => {
if (!flag.hasFeatureFlag.value) {
return;
}
this.featureFlags.push(flag.hasFeatureFlag);
});
this._setMenu(build);
});
this.data.query<FeatureFlagsQuery>(Queries.serverFeatureFlags, {
filter: { id: server.id }
},
function(data: Query) {
return data.servers[0];
}).subscribe(data => {
this.featureFlags = data.featureFlags;
this._setMenu(build);
});
} else {
this._setMenu(build);