Skip to content

Commit

Permalink
Add last_changed_api_heartbeat_state_on__date to the device model
Browse files Browse the repository at this point in the history
Resolves: #644
Change-type: minor
  • Loading branch information
thgreasi committed Sep 28, 2023
1 parent cd99532 commit f4251ac
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 88 deletions.
1 change: 1 addition & 0 deletions src/balena-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ export interface Device {
id: number;
actor: { __id: number } | [Actor];
api_heartbeat_state: 'online' | 'offline' | 'timeout' | 'unknown';
last_changed_api_heartbeat_state_on__date: DateString | null;
uuid: string;
local_id: string | null;
device_name: string | null;
Expand Down
5 changes: 4 additions & 1 deletion src/balena.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ Term: device
Necessity: each device has exactly one api heartbeat state
Definition: "online" or "offline" or "timeout" or "unknown"

Fact type: device [last changed api heartbeat state on] date
Necessity: each device [last changed api heartbeat state on] at most one date.

Fact type: device has env var name
Term Form: device environment variable
Database Table Name: device environment variable
Expand Down Expand Up @@ -437,7 +440,7 @@ Fact type: user (Auth) has public key
-- user public key

Fact type: user public key has title
Necessity: each user public key has exactly one title
Necessity: each user public key has exactly one title


-- application type
Expand Down
9 changes: 6 additions & 3 deletions src/features/device-heartbeat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class DeviceOnlineStateManager extends EventEmitter<{

try {
// patch the api_heartbeat_state value to the new state...
const body = {
const baseBody = {
api_heartbeat_state: newState,
};
await api.resin.patch({
Expand All @@ -255,10 +255,13 @@ export class DeviceOnlineStateManager extends EventEmitter<{
id: deviceId,
options: {
$filter: {
$not: body,
$not: baseBody,
},
},
body,
body: {
...baseBody,
last_changed_api_heartbeat_state_on__date: Date.now(),
},
});
} catch ($err) {
err = $err;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "device"
ADD COLUMN IF NOT EXISTS "last changed api heartbeat state on-date" TIMESTAMP NULL;
Loading

0 comments on commit f4251ac

Please sign in to comment.