Skip to content

Commit

Permalink
Add the device.last_api_heartbeat_change_date term to the model
Browse files Browse the repository at this point in the history
  • Loading branch information
thgreasi committed Aug 5, 2024
1 parent afc83fe commit da753e6
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 88 deletions.
2 changes: 2 additions & 0 deletions src/balena-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ export interface Device {
id: Types['Serial']['Read'];
actor: { __id: Actor['Read']['id'] } | [Actor['Read']];
api_heartbeat_state: 'online' | 'offline' | 'timeout' | 'unknown';
last_api_heartbeat_change_date: Types['Date Time']['Read'] | null;
uuid: Types['Text']['Read'];
local_id: Types['Short Text']['Read'] | null;
device_name: Types['Short Text']['Read'] | null;
Expand Down Expand Up @@ -777,6 +778,7 @@ export interface Device {
id: Types['Serial']['Write'];
actor: Actor['Write']['id'];
api_heartbeat_state: 'online' | 'offline' | 'timeout' | 'unknown';
last_api_heartbeat_change_date: Types['Date Time']['Write'] | null;
uuid: Types['Text']['Write'];
local_id: Types['Short Text']['Write'] | null;
device_name: Types['Short Text']['Write'] | null;
Expand Down
8 changes: 7 additions & 1 deletion src/balena.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ Term: alias
Term: api heartbeat state
Concept Type: Short Text (Type)

Term: last api heartbeat change date
Concept Type: Date Time (Type)

Term: api port
Concept Type: Integer (Type)

Expand Down Expand Up @@ -368,6 +371,9 @@ Term: device
Necessity: each device has exactly one api heartbeat state
Definition: "online" or "offline" or "timeout" or "unknown"

Fact type: device has last api heartbeat change date
Necessity: each device has at most one last api heartbeat change date.

Fact type: device has env var name
Term Form: device environment variable
Database Table Name: device environment variable
Expand Down Expand Up @@ -445,7 +451,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
13 changes: 10 additions & 3 deletions src/features/device-heartbeat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,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 @@ -268,10 +268,17 @@ export class DeviceOnlineStateManager extends EventEmitter<{
id: deviceId,
options: {
$filter: {
$not: body,
$not: baseBody,
},
},
body,
body: {
...baseBody,
// Since the heartbeat manager is the only place that we update the heartbeat state
// we are updating the heartbeat's change date in here rather than a hook, so that
// we can avoid the extra DB request that a generic hook would require for checking
// whether the value actually changed or not.
last_api_heartbeat_change_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 api heartbeat change date" TIMESTAMP NULL;
6 changes: 6 additions & 0 deletions src/translations/v6/v6.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ Term: alias
Term: api heartbeat state
Concept Type: Short Text (Type)

Term: last api heartbeat change date
Concept Type: Date Time (Type)

Term: api port
Concept Type: Integer (Type)

Expand Down Expand Up @@ -378,6 +381,9 @@ Term: device
Necessity: each device has exactly one api heartbeat state
Definition: "online" or "offline" or "timeout" or "unknown"

Fact type: device has last api heartbeat change date
Necessity: each device has at most one last api heartbeat change date.

Fact type: device has env var name
Term Form: device environment variable
Database Table Name: device environment variable
Expand Down
Loading

0 comments on commit da753e6

Please sign in to comment.