fix(analytics): load public settings [EE-3590] (#7128)

pull/7134/head
Chaim Lev-Ari 2022-06-27 19:29:17 +03:00 committed by GitHub
parent 1551b02fde
commit 95f706aabe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 16 deletions

View File

@ -30,6 +30,19 @@ type publicSettingsResponse struct {
KubeconfigExpiry string `example:"24h" default:"0"` KubeconfigExpiry string `example:"24h" default:"0"`
// Whether team sync is enabled // Whether team sync is enabled
TeamSync bool `json:"TeamSync" example:"true"` TeamSync bool `json:"TeamSync" example:"true"`
Edge struct {
// Whether the device has been started in edge async mode
AsyncMode bool
// The ping interval for edge agent - used in edge async mode [seconds]
PingInterval int `json:"PingInterval" example:"60"`
// The snapshot interval for edge agent - used in edge async mode [seconds]
SnapshotInterval int `json:"SnapshotInterval" example:"60"`
// The command list interval for edge agent - used in edge async mode [seconds]
CommandInterval int `json:"CommandInterval" example:"60"`
// The check in interval for edge agent (in seconds) - used in non async mode [seconds]
CheckinInterval int `example:"60"`
}
} }
// @id SettingsPublic // @id SettingsPublic
@ -61,6 +74,13 @@ func generatePublicSettings(appSettings *portainer.Settings) *publicSettingsResp
KubeconfigExpiry: appSettings.KubeconfigExpiry, KubeconfigExpiry: appSettings.KubeconfigExpiry,
Features: appSettings.FeatureFlagSettings, Features: appSettings.FeatureFlagSettings,
} }
publicSettings.Edge.AsyncMode = appSettings.Edge.AsyncMode
publicSettings.Edge.PingInterval = appSettings.Edge.PingInterval
publicSettings.Edge.SnapshotInterval = appSettings.Edge.SnapshotInterval
publicSettings.Edge.CommandInterval = appSettings.Edge.CommandInterval
publicSettings.Edge.CheckinInterval = appSettings.EdgeAgentCheckinInterval
//if OAuth authentication is on, compose the related fields from application settings //if OAuth authentication is on, compose the related fields from application settings
if publicSettings.AuthenticationMethod == portainer.AuthenticationOAuth { if publicSettings.AuthenticationMethod == portainer.AuthenticationOAuth {
publicSettings.OAuthLogoutURI = appSettings.OAuthSettings.LogoutURI publicSettings.OAuthLogoutURI = appSettings.OAuthSettings.LogoutURI

View File

@ -1,6 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import { useSettings } from '@/portainer/settings/queries'; import { usePublicSettings } from '@/portainer/settings/queries';
const categories = [ const categories = [
'docker', 'docker',
@ -64,7 +64,9 @@ export function push(
} }
export function useAnalytics() { export function useAnalytics() {
const telemetryQuery = useSettings((settings) => settings.EnableTelemetry); const telemetryQuery = usePublicSettings({
select: (settings) => settings.EnableTelemetry,
});
return { trackEvent: handleTrackEvent }; return { trackEvent: handleTrackEvent };

View File

@ -2,8 +2,8 @@ import clsx from 'clsx';
import { isoDateFromTimestamp } from '@/portainer/filters/filters'; import { isoDateFromTimestamp } from '@/portainer/filters/filters';
import { Environment } from '@/portainer/environments/types'; import { Environment } from '@/portainer/environments/types';
import { useSettings } from '@/portainer/settings/queries'; import { usePublicSettings } from '@/portainer/settings/queries';
import { Settings } from '@/portainer/settings/types'; import { PublicSettingsViewModel } from '@/portainer/models/settings';
interface Props { interface Props {
showLastCheckInDate?: boolean; showLastCheckInDate?: boolean;
@ -58,7 +58,7 @@ export function EdgeIndicator({
} }
function useHasHeartbeat(environment: Environment, associated: boolean) { function useHasHeartbeat(environment: Environment, associated: boolean) {
const settingsQuery = useSettings(undefined, associated); const settingsQuery = usePublicSettings({ enabled: associated });
if (!associated) { if (!associated) {
return false; return false;
@ -81,7 +81,10 @@ function useHasHeartbeat(environment: Environment, associated: boolean) {
return false; return false;
} }
function getCheckinInterval(environment: Environment, settings: Settings) { function getCheckinInterval(
environment: Environment,
settings: PublicSettingsViewModel
) {
const asyncMode = environment.Edge.AsyncMode; const asyncMode = environment.Edge.AsyncMode;
if (asyncMode) { if (asyncMode) {
@ -104,7 +107,7 @@ function getCheckinInterval(environment: Environment, settings: Settings) {
!environment.EdgeCheckinInterval || !environment.EdgeCheckinInterval ||
environment.EdgeCheckinInterval === 0 environment.EdgeCheckinInterval === 0
) { ) {
return settings.EdgeAgentCheckinInterval; return settings.Edge.CheckinInterval;
} }
return environment.EdgeCheckinInterval; return environment.EdgeCheckinInterval;

View File

@ -30,9 +30,9 @@ export function KubeconfigPrompt({
const [page, setPage] = useState(1); const [page, setPage] = useState(1);
const [pageLimit, setPageLimit] = usePaginationLimitState(storageKey); const [pageLimit, setPageLimit] = usePaginationLimitState(storageKey);
const expiryQuery = usePublicSettings((settings) => const expiryQuery = usePublicSettings({
expiryMessage(settings.KubeconfigExpiry) select: (settings) => expiryMessage(settings.KubeconfigExpiry),
); });
const { selection, toggle: toggleSelection, selectionSize } = useSelection(); const { selection, toggle: toggleSelection, selectionSize } = useSelection();
const { environments, totalCount } = useEnvironmentList({ const { environments, totalCount } = useEnvironmentList({

View File

@ -34,6 +34,8 @@ export function PublicSettingsViewModel(settings) {
this.EnableTelemetry = settings.EnableTelemetry; this.EnableTelemetry = settings.EnableTelemetry;
this.OAuthLogoutURI = settings.OAuthLogoutURI; this.OAuthLogoutURI = settings.OAuthLogoutURI;
this.KubeconfigExpiry = settings.KubeconfigExpiry; this.KubeconfigExpiry = settings.KubeconfigExpiry;
this.Features = settings.Features;
this.Edge = new EdgeSettingsViewModel(settings.Edge);
} }
export function InternalAuthSettingsViewModel(data) { export function InternalAuthSettingsViewModel(data) {
@ -75,3 +77,11 @@ export function OAuthSettingsViewModel(data) {
this.SSO = data.SSO; this.SSO = data.SSO;
this.LogoutURI = data.LogoutURI; this.LogoutURI = data.LogoutURI;
} }
export function EdgeSettingsViewModel(data = {}) {
this.CheckinInterval = data.CheckinInterval;
this.PingInterval = data.PingInterval;
this.SnapshotInterval = data.SnapshotInterval;
this.CommandInterval = data.CommandInterval;
this.AsyncMode = data.AsyncMode;
}

View File

@ -15,12 +15,17 @@ import {
} from './settings.service'; } from './settings.service';
import { Settings } from './types'; import { Settings } from './types';
export function usePublicSettings<T = PublicSettingsViewModel>( export function usePublicSettings<T = PublicSettingsViewModel>({
select?: (settings: PublicSettingsViewModel) => T enabled,
) { select,
}: {
select?: (settings: PublicSettingsViewModel) => T;
enabled?: boolean;
} = {}) {
return useQuery(['settings', 'public'], () => getPublicSettings(), { return useQuery(['settings', 'public'], () => getPublicSettings(), {
select, select,
...withError('Unable to retrieve public settings'), ...withError('Unable to retrieve public settings'),
enabled,
}); });
} }

View File

@ -8,9 +8,9 @@ interface Props {
} }
export function SettingsSidebar({ isAdmin }: Props) { export function SettingsSidebar({ isAdmin }: Props) {
const teamSyncQuery = usePublicSettings<boolean>( const teamSyncQuery = usePublicSettings<boolean>({
(settings) => settings.TeamSync select: (settings) => settings.TeamSync,
); });
const showUsersSection = const showUsersSection =
!window.ddExtension && (isAdmin || teamSyncQuery.data); !window.ddExtension && (isAdmin || teamSyncQuery.data);