fix(home): fix homepage edge heartbeat judgement [EE-2041] (#6624)

* fix(home): judge LastCheckInDate with QueryDate for heartbeat

* refactor(environments): remove deprecated variable homepageLoadTime

* style(environments): run yarn format

Co-authored-by: sam@gemibook <huapox@126.com>
pull/6651/head
Oscar Zhou 2022-03-14 14:53:23 +13:00 committed by GitHub
parent f1ea2b5c02
commit 5188ead870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 15 additions and 41 deletions

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
@ -125,6 +126,7 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht
if paginatedEndpoints[idx].EdgeCheckinInterval == 0 { if paginatedEndpoints[idx].EdgeCheckinInterval == 0 {
paginatedEndpoints[idx].EdgeCheckinInterval = settings.EdgeAgentCheckinInterval paginatedEndpoints[idx].EdgeCheckinInterval = settings.EdgeAgentCheckinInterval
} }
paginatedEndpoints[idx].QueryDate = time.Now().Unix()
} }
w.Header().Set("X-Total-Count", strconv.Itoa(filteredEndpointCount)) w.Header().Set("X-Total-Count", strconv.Itoa(filteredEndpointCount))

View File

@ -325,6 +325,8 @@ type (
AMTDeviceGUID string `json:"AMTDeviceGUID,omitempty" example:"4c4c4544-004b-3910-8037-b6c04f504633"` AMTDeviceGUID string `json:"AMTDeviceGUID,omitempty" example:"4c4c4544-004b-3910-8037-b6c04f504633"`
// LastCheckInDate mark last check-in date on checkin // LastCheckInDate mark last check-in date on checkin
LastCheckInDate int64 LastCheckInDate int64
// QueryDate of each query with the endpoints list
QueryDate int64
// IsEdgeDevice marks if the environment was created as an EdgeDevice // IsEdgeDevice marks if the environment was created as an EdgeDevice
IsEdgeDevice bool IsEdgeDevice bool
// Whether the device has been trusted or not by the user // Whether the device has been trusted or not by the user

View File

@ -63,6 +63,7 @@ export type Environment = {
GroupId: EnvironmentGroupId; GroupId: EnvironmentGroupId;
EdgeID?: string; EdgeID?: string;
EdgeCheckinInterval?: number; EdgeCheckinInterval?: number;
QueryDate?: number;
LastCheckInDate?: number; LastCheckInDate?: number;
Name: string; Name: string;
Status: EnvironmentStatus; Status: EnvironmentStatus;

View File

@ -21,14 +21,14 @@ function renderComponent(
edgeId = '', edgeId = '',
lastCheckInDate = 0, lastCheckInDate = 0,
checkInInterval = 0, checkInInterval = 0,
homepageLoadTime = 0 queryDate = 0
) { ) {
return render( return render(
<EdgeIndicator <EdgeIndicator
edgeId={edgeId} edgeId={edgeId}
lastCheckInDate={lastCheckInDate} lastCheckInDate={lastCheckInDate}
checkInInterval={checkInInterval} checkInInterval={checkInInterval}
homepageLoadTime={homepageLoadTime} queryDate={queryDate}
/> />
); );
} }

View File

@ -5,7 +5,7 @@ import { isoDateFromTimestamp } from '@/portainer/filters/filters';
interface Props { interface Props {
checkInInterval?: number; checkInInterval?: number;
edgeId?: string; edgeId?: string;
homepageLoadTime?: number; queryDate?: number;
lastCheckInDate?: number; lastCheckInDate?: number;
} }
@ -13,7 +13,7 @@ export function EdgeIndicator({
edgeId, edgeId,
lastCheckInDate, lastCheckInDate,
checkInInterval, checkInInterval,
homepageLoadTime, queryDate,
}: Props) { }: Props) {
if (!edgeId) { if (!edgeId) {
return ( return (
@ -25,9 +25,8 @@ export function EdgeIndicator({
// give checkIn some wiggle room // give checkIn some wiggle room
let isCheckValid = false; let isCheckValid = false;
if (checkInInterval && homepageLoadTime && lastCheckInDate) { if (checkInInterval && queryDate && lastCheckInDate) {
isCheckValid = isCheckValid = queryDate - lastCheckInDate <= checkInInterval * 2 + 20;
homepageLoadTime - lastCheckInDate <= checkInInterval * 2 + 20;
} }
return ( return (

View File

@ -15,17 +15,10 @@ export default {
interface Args { interface Args {
environment: Environment; environment: Environment;
homepageLoadTime: number;
} }
function Template({ environment, homepageLoadTime = 1 }: Args) { function Template({ environment }: Args) {
return ( return <EnvironmentItem environment={environment} onClick={() => {}} />;
<EnvironmentItem
environment={environment}
homepageLoadTime={homepageLoadTime}
onClick={() => {}}
/>
);
} }
export const DockerEnvironment: Story<Args> = Template.bind({}); export const DockerEnvironment: Story<Args> = Template.bind({});

View File

@ -67,7 +67,6 @@ function renderComponent(
onClick={() => {}} onClick={() => {}}
environment={env} environment={env}
groupName={group.Name} groupName={group.Name}
homepageLoadTime={0}
/> />
</UserContext.Provider> </UserContext.Provider>
); );

View File

@ -25,18 +25,12 @@ import styles from './EnvironmentItem.module.css';
import { EnvironmentStatusBadge } from './EnvironmentStatusBadge'; import { EnvironmentStatusBadge } from './EnvironmentStatusBadge';
interface Props { interface Props {
homepageLoadTime?: number;
environment: Environment; environment: Environment;
groupName?: string; groupName?: string;
onClick(environment: Environment): void; onClick(environment: Environment): void;
} }
export function EnvironmentItem({ export function EnvironmentItem({ environment, onClick, groupName }: Props) {
environment,
onClick,
homepageLoadTime,
groupName,
}: Props) {
const isAdmin = useIsAdmin(); const isAdmin = useIsAdmin();
const isEdge = isEdgeEnvironment(environment.Type); const isEdge = isEdgeEnvironment(environment.Type);
@ -77,7 +71,7 @@ export function EnvironmentItem({
edgeId={environment.EdgeID} edgeId={environment.EdgeID}
checkInInterval={environment.EdgeCheckinInterval} checkInInterval={environment.EdgeCheckinInterval}
lastCheckInDate={environment.LastCheckInDate} lastCheckInDate={environment.LastCheckInDate}
homepageLoadTime={homepageLoadTime} queryDate={environment.QueryDate}
/> />
) : ( ) : (
<> <>

View File

@ -31,8 +31,6 @@ interface Props {
} }
export function EnvironmentList({ onClickItem, onRefresh }: Props) { export function EnvironmentList({ onClickItem, onRefresh }: Props) {
const homepageLoadTime = usePageLoadingTime();
const isAdmin = useIsAdmin(); const isAdmin = useIsAdmin();
const storageKey = 'home_endpoints'; const storageKey = 'home_endpoints';
@ -98,7 +96,6 @@ export function EnvironmentList({ onClickItem, onRefresh }: Props) {
groupsQuery.data?.find((g) => g.Id === env.GroupId)?.Name groupsQuery.data?.find((g) => g.Id === env.GroupId)?.Name
} }
onClick={onClickItem} onClick={onClickItem}
homepageLoadTime={homepageLoadTime}
/> />
)) ))
)} )}
@ -145,15 +142,3 @@ function renderItems(
return items; return items;
} }
function usePageLoadingTime() {
const [homepageLoadTime, setHomepageLoadTime] = useState<
number | undefined
>();
useEffect(() => {
setHomepageLoadTime(Math.floor(Date.now() / 1000));
}, []);
return homepageLoadTime;
}

View File

@ -8,6 +8,5 @@ export const EnvironmentListAngular = react2angular(EnvironmentList, [
'tags', 'tags',
'onClickItem', 'onClickItem',
'onRefresh', 'onRefresh',
'homepageLoadTime',
'groups', 'groups',
]); ]);