@alwaysai/device-agent 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/lib/application-control/environment-variables.d.ts +1 -1
  2. package/lib/application-control/environment-variables.d.ts.map +1 -1
  3. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +3 -2
  4. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  5. package/lib/cloud-connection/device-agent-cloud-connection.js +80 -59
  6. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  7. package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -1
  8. package/lib/cloud-connection/live-updates-handler.js +2 -4
  9. package/lib/cloud-connection/live-updates-handler.js.map +1 -1
  10. package/lib/cloud-connection/live-updates-handler.test.js +1 -1
  11. package/lib/cloud-connection/live-updates-handler.test.js.map +1 -1
  12. package/lib/cloud-connection/passthrough-handler.d.ts.map +1 -1
  13. package/lib/cloud-connection/passthrough-handler.js +3 -6
  14. package/lib/cloud-connection/passthrough-handler.js.map +1 -1
  15. package/lib/cloud-connection/publisher.d.ts +5 -4
  16. package/lib/cloud-connection/publisher.d.ts.map +1 -1
  17. package/lib/cloud-connection/publisher.js +9 -8
  18. package/lib/cloud-connection/publisher.js.map +1 -1
  19. package/lib/cloud-connection/shadow-handler.d.ts +1 -0
  20. package/lib/cloud-connection/shadow-handler.d.ts.map +1 -1
  21. package/lib/cloud-connection/shadow-handler.js +5 -6
  22. package/lib/cloud-connection/shadow-handler.js.map +1 -1
  23. package/lib/cloud-connection/shadow-handler.test.js +9 -0
  24. package/lib/cloud-connection/shadow-handler.test.js.map +1 -1
  25. package/lib/cloud-connection/transaction-manager.d.ts +10 -0
  26. package/lib/cloud-connection/transaction-manager.d.ts.map +1 -0
  27. package/lib/cloud-connection/transaction-manager.js +41 -0
  28. package/lib/cloud-connection/transaction-manager.js.map +1 -0
  29. package/lib/cloud-connection/transaction-manager.test.d.ts +2 -0
  30. package/lib/cloud-connection/transaction-manager.test.d.ts.map +1 -0
  31. package/lib/cloud-connection/transaction-manager.test.js +63 -0
  32. package/lib/cloud-connection/transaction-manager.test.js.map +1 -0
  33. package/lib/device-control/device-control.d.ts +35 -24
  34. package/lib/device-control/device-control.d.ts.map +1 -1
  35. package/lib/device-control/device-control.js +46 -11
  36. package/lib/device-control/device-control.js.map +1 -1
  37. package/lib/subcommands/app/env-vars.d.ts.map +1 -1
  38. package/lib/subcommands/app/env-vars.js +15 -9
  39. package/lib/subcommands/app/env-vars.js.map +1 -1
  40. package/lib/subcommands/device/clean.d.ts.map +1 -1
  41. package/lib/subcommands/device/clean.js +14 -12
  42. package/lib/subcommands/device/clean.js.map +1 -1
  43. package/lib/subcommands/device/device.d.ts +1 -0
  44. package/lib/subcommands/device/device.d.ts.map +1 -1
  45. package/lib/subcommands/device/device.js +23 -16
  46. package/lib/subcommands/device/device.js.map +1 -1
  47. package/lib/subcommands/device/index.js +1 -1
  48. package/lib/subcommands/device/index.js.map +1 -1
  49. package/lib/util/logger.d.ts.map +1 -1
  50. package/lib/util/logger.js +1 -1
  51. package/lib/util/logger.js.map +1 -1
  52. package/lib/util/safe-rimraf.d.ts +2 -0
  53. package/lib/util/safe-rimraf.d.ts.map +1 -0
  54. package/lib/util/safe-rimraf.js +16 -0
  55. package/lib/util/safe-rimraf.js.map +1 -0
  56. package/package.json +3 -2
  57. package/readme.md +1 -0
  58. package/src/application-control/environment-variables.ts +1 -1
  59. package/src/cloud-connection/device-agent-cloud-connection.ts +124 -80
  60. package/src/cloud-connection/live-updates-handler.test.ts +2 -2
  61. package/src/cloud-connection/live-updates-handler.ts +2 -8
  62. package/src/cloud-connection/passthrough-handler.ts +8 -7
  63. package/src/cloud-connection/publisher.ts +27 -10
  64. package/src/cloud-connection/shadow-handler.test.ts +9 -0
  65. package/src/cloud-connection/shadow-handler.ts +6 -14
  66. package/src/cloud-connection/transaction-manager.test.ts +73 -0
  67. package/src/cloud-connection/transaction-manager.ts +43 -0
  68. package/src/device-control/device-control.ts +50 -12
  69. package/src/subcommands/app/env-vars.ts +17 -10
  70. package/src/subcommands/device/clean.ts +22 -13
  71. package/src/subcommands/device/device.ts +31 -20
  72. package/src/subcommands/device/index.ts +2 -2
  73. package/src/util/logger.ts +15 -10
  74. package/src/util/safe-rimraf.ts +14 -0
  75. package/lib/cloud-connection/transaction-queue.d.ts +0 -12
  76. package/lib/cloud-connection/transaction-queue.d.ts.map +0 -1
  77. package/lib/cloud-connection/transaction-queue.js +0 -38
  78. package/lib/cloud-connection/transaction-queue.js.map +0 -1
  79. package/lib/cloud-connection/transaction-queue.test.d.ts +0 -2
  80. package/lib/cloud-connection/transaction-queue.test.d.ts.map +0 -1
  81. package/lib/cloud-connection/transaction-queue.test.js +0 -46
  82. package/lib/cloud-connection/transaction-queue.test.js.map +0 -1
  83. package/src/cloud-connection/transaction-queue.test.ts +0 -55
  84. package/src/cloud-connection/transaction-queue.ts +0 -40
@@ -14,7 +14,9 @@ import { JsSpawner } from 'alwaysai/lib/util';
14
14
  import {
15
15
  getCpuDetails,
16
16
  getDiskDetails,
17
- getMemDetails
17
+ getMemDetails,
18
+ getSystemInformation,
19
+ reboot
18
20
  } from '../../device-control/device-control';
19
21
  import { writeTokenAndDeviceCfg } from '../../infrastructure/tokens-and-device-cfg';
20
22
  import { logger } from '../../util/logger';
@@ -109,24 +111,33 @@ export const getInfoCliLeaf = CliLeaf({
109
111
  const cpuDetails = await getCpuDetails();
110
112
  const diskDetails = await getDiskDetails();
111
113
  const memDetails = await getMemDetails();
112
- const deviceInfo = [
113
- [
114
- 'CPU Utilization',
115
- `Used ${cpuDetails.usedPerc.toFixed(2)}%, Temperature: ${
116
- cpuDetails.temperature
117
- } °C`
118
- ],
119
- [
120
- 'Disk Utilization',
121
- `${diskDetails.usedGb} GB / ${
122
- diskDetails.usedGb + diskDetails.freeGb
123
- } GB`
124
- ],
125
- [
126
- 'Memory Utilization',
127
- `${memDetails.usedMb} MB / ${memDetails.usedMb + memDetails.freeMb} MB`
128
- ]
129
- ];
130
- console.table(deviceInfo);
114
+ const out = {
115
+ 'CPU Utilization': `Used ${cpuDetails.usedPerc.toFixed(
116
+ 2
117
+ )}%, Temperature ${cpuDetails.temperature} °C`,
118
+ 'Disk Utilization': `${diskDetails.usedGb} GB / ${
119
+ diskDetails.usedGb + diskDetails.freeGb
120
+ } GB`,
121
+ 'Memory Utilization': `${memDetails.usedMb} MB / ${
122
+ memDetails.usedMb + memDetails.freeMb
123
+ } MB`
124
+ };
125
+ console.table(out);
126
+ const systemInfo = await getSystemInformation();
127
+ console.table(systemInfo.os);
128
+ console.table(systemInfo.cpu);
129
+ console.table(systemInfo.disk?.drives);
130
+ console.table(systemInfo.device);
131
+ console.table(systemInfo.network);
132
+ console.table(systemInfo.versions);
133
+ }
134
+ });
135
+
136
+ export const restartCliLeaf = CliLeaf({
137
+ name: 'restart',
138
+ description: 'Restart the device',
139
+ namedInputs: {},
140
+ async action(_, opts) {
141
+ await reboot();
131
142
  }
132
143
  });
@@ -1,9 +1,9 @@
1
1
  import { CliBranch } from '@alwaysai/alwayscli';
2
- import { getInfoCliLeaf, initCliLeaf } from './device';
2
+ import { getInfoCliLeaf, initCliLeaf, restartCliLeaf } from './device';
3
3
  import { cleanCliLeaf } from './clean';
4
4
 
5
5
  export const deviceCliBranch = CliBranch({
6
6
  name: 'device',
7
7
  description: 'Manage current device',
8
- subcommands: [initCliLeaf, getInfoCliLeaf, cleanCliLeaf]
8
+ subcommands: [initCliLeaf, getInfoCliLeaf, cleanCliLeaf, restartCliLeaf]
9
9
  });
@@ -2,19 +2,24 @@ import * as winston from 'winston';
2
2
  import 'winston-daily-rotate-file';
3
3
  import * as path from 'path';
4
4
  import { AAI_DIR } from 'alwaysai/lib/paths';
5
- import { ALWAYSAI_LOG_LEVEL, ALWAYSAI_LOG_TO_CONSOLE } from '../environment';
5
+ import {
6
+ ALWAYSAI_DEVICE_AGENT_MODE,
7
+ ALWAYSAI_LOG_LEVEL,
8
+ ALWAYSAI_LOG_TO_CONSOLE
9
+ } from '../environment';
6
10
 
7
11
  const LOG_LEVEL = ALWAYSAI_LOG_LEVEL || 'info';
8
12
 
9
- const transports = ALWAYSAI_LOG_TO_CONSOLE
10
- ? [new winston.transports.Console({ level: LOG_LEVEL })]
11
- : [
12
- new winston.transports.DailyRotateFile({
13
- filename: path.join(AAI_DIR, 'agent-logs', 'agent-logs.txt'),
14
- maxSize: '5m',
15
- maxFiles: '2d'
16
- })
17
- ];
13
+ const transports =
14
+ ALWAYSAI_LOG_TO_CONSOLE || ALWAYSAI_DEVICE_AGENT_MODE !== 'cloud'
15
+ ? [new winston.transports.Console({ level: LOG_LEVEL })]
16
+ : [
17
+ new winston.transports.DailyRotateFile({
18
+ filename: path.join(AAI_DIR, 'agent-logs', 'agent-logs.txt'),
19
+ maxSize: '5m',
20
+ maxFiles: '2d'
21
+ })
22
+ ];
18
23
 
19
24
  export const logger = winston.createLogger({
20
25
  level: LOG_LEVEL,
@@ -0,0 +1,14 @@
1
+ import { rimraf } from 'rimraf';
2
+ import { logger } from '../util/logger';
3
+
4
+ export default async function safeRimraf(path: string) {
5
+ logger.debug(`Removing ${path}`);
6
+ try {
7
+ await rimraf(path);
8
+ } catch (e) {
9
+ logger.error(
10
+ `Failed to remove ${path}. Please manually delete the file or directory.`
11
+ );
12
+ logger.debug(`Error removing ${path}: ${e}`);
13
+ }
14
+ }
@@ -1,12 +0,0 @@
1
- export declare class TransactionQueue {
2
- private transactionList;
3
- private txIdToProjectIdMap;
4
- constructor();
5
- addTxIdToQueue(txId: string, projectId: string): void;
6
- getCurrentTxId(): string;
7
- checkTxnInQueue(txId: string): boolean;
8
- getProjectIdForTxnId(txId: string): string;
9
- completeTxn(txId: string): void;
10
- completeCurrentTxn(): void;
11
- }
12
- //# sourceMappingURL=transaction-queue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transaction-queue.d.ts","sourceRoot":"","sources":["../../src/cloud-connection/transaction-queue.ts"],"names":[],"mappings":"AAAA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,eAAe,CAAW;IAClC,OAAO,CAAC,kBAAkB,CAAkC;;IAOrD,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IAS9C,cAAc;IAId,eAAe,CAAC,IAAI,EAAE,MAAM;IAI5B,oBAAoB,CAAC,IAAI,EAAE,MAAM;IAIjC,WAAW,CAAC,IAAI,EAAE,MAAM;IAKxB,kBAAkB;CAI1B"}
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransactionQueue = void 0;
4
- class TransactionQueue {
5
- constructor() {
6
- this.txIdToProjectIdMap = {};
7
- this.transactionList = [];
8
- this.txIdToProjectIdMap = {};
9
- }
10
- addTxIdToQueue(txId, projectId) {
11
- if (!this.checkTxnInQueue(txId)) {
12
- this.transactionList.push(txId);
13
- this.txIdToProjectIdMap[txId] = projectId;
14
- }
15
- else {
16
- throw new Error(`txId ${txId} is already added to the queue!`);
17
- }
18
- }
19
- getCurrentTxId() {
20
- return this.transactionList[0];
21
- }
22
- checkTxnInQueue(txId) {
23
- return this.transactionList.includes(txId);
24
- }
25
- getProjectIdForTxnId(txId) {
26
- return this.txIdToProjectIdMap[txId];
27
- }
28
- completeTxn(txId) {
29
- delete this.txIdToProjectIdMap[txId];
30
- this.transactionList.splice(this.transactionList.indexOf(txId), 1);
31
- }
32
- completeCurrentTxn() {
33
- delete this.txIdToProjectIdMap[this.transactionList[0]];
34
- this.transactionList.shift();
35
- }
36
- }
37
- exports.TransactionQueue = TransactionQueue;
38
- //# sourceMappingURL=transaction-queue.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transaction-queue.js","sourceRoot":"","sources":["../../src/cloud-connection/transaction-queue.ts"],"names":[],"mappings":";;;AAAA,MAAa,gBAAgB;IAI3B;QAFQ,uBAAkB,GAA+B,EAAE,CAAC;QAG1D,IAAI,CAAC,eAAe,GAAG,EAAE,CAAC;QAC1B,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;IAC/B,CAAC;IAEM,cAAc,CAAC,IAAY,EAAE,SAAiB;QACnD,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE;YAC/B,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAChC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;SAC3C;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,QAAQ,IAAI,iCAAiC,CAAC,CAAC;SAChE;IACH,CAAC;IAEM,cAAc;QACnB,OAAO,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IACjC,CAAC;IAEM,eAAe,CAAC,IAAY;QACjC,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC;IAEM,oBAAoB,CAAC,IAAY;QACtC,OAAO,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;IAEM,WAAW,CAAC,IAAY;QAC7B,OAAO,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACrE,CAAC;IAEM,kBAAkB;QACvB,OAAO,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;QACxD,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;IAC/B,CAAC;CACF;AAvCD,4CAuCC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=transaction-queue.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transaction-queue.test.d.ts","sourceRoot":"","sources":["../../src/cloud-connection/transaction-queue.test.ts"],"names":[],"mappings":""}
@@ -1,46 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const device_agent_schemas_1 = require("@alwaysai/device-agent-schemas");
4
- const transaction_queue_1 = require("./transaction-queue");
5
- describe('Test Transaction Queue', () => {
6
- let txnQueue;
7
- beforeEach(() => {
8
- txnQueue = new transaction_queue_1.TransactionQueue();
9
- jest.clearAllMocks();
10
- });
11
- test('Add new to transaction queue', async () => {
12
- const txId = (0, device_agent_schemas_1.generateTxId)();
13
- const projectId = '1241fad-agah-gfadg-2352dgzg';
14
- txnQueue.addTxIdToQueue(txId, projectId);
15
- expect(txnQueue.checkTxnInQueue(txId)).toBeTruthy();
16
- });
17
- test('Attempt to add existing to transaction queue, results in failure', async () => {
18
- const txId = (0, device_agent_schemas_1.generateTxId)();
19
- const projectId = '1241fad-agah-gfadg-2352dgzg';
20
- txnQueue.addTxIdToQueue(txId, projectId);
21
- expect(() => txnQueue.addTxIdToQueue(txId, projectId)).toThrow(`txId ${txId} is already added to the queue!`);
22
- });
23
- test('Check current transaction', async () => {
24
- const txId = (0, device_agent_schemas_1.generateTxId)();
25
- const projectId = '1241fad-agah-gfadg-2352dgzg';
26
- txnQueue.addTxIdToQueue(txId, projectId);
27
- const txId2 = (0, device_agent_schemas_1.generateTxId)();
28
- const projectId2 = '1241faegta-agah-gagadg-2352dgzg';
29
- txnQueue.addTxIdToQueue(txId2, projectId2);
30
- expect(txnQueue.getCurrentTxId()).toEqual(txId);
31
- });
32
- test('Test remove transaction from queue', async () => {
33
- const txId = (0, device_agent_schemas_1.generateTxId)();
34
- const projectId = '1241fad-agah-gfadg-2352dgzg';
35
- txnQueue.addTxIdToQueue(txId, projectId);
36
- txnQueue.completeCurrentTxn();
37
- expect(txnQueue.getCurrentTxId()).toBeUndefined();
38
- });
39
- test('Test get project ID for a given txId', async () => {
40
- const txId = (0, device_agent_schemas_1.generateTxId)();
41
- const projectId = '1241fad-agah-gfadg-2352dgzg';
42
- txnQueue.addTxIdToQueue(txId, projectId);
43
- expect(txnQueue.getProjectIdForTxnId(txId)).toEqual(projectId);
44
- });
45
- });
46
- //# sourceMappingURL=transaction-queue.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transaction-queue.test.js","sourceRoot":"","sources":["../../src/cloud-connection/transaction-queue.test.ts"],"names":[],"mappings":";;AAAA,yEAA8D;AAC9D,2DAAuD;AAEvD,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;IACtC,IAAI,QAA0B,CAAC;IAE/B,UAAU,CAAC,GAAG,EAAE;QACd,QAAQ,GAAG,IAAI,oCAAgB,EAAE,CAAC;QAClC,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,8BAA8B,EAAE,KAAK,IAAI,EAAE;QAC9C,MAAM,IAAI,GAAG,IAAA,mCAAY,GAAE,CAAC;QAC5B,MAAM,SAAS,GAAG,6BAA6B,CAAC;QAChD,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACzC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACtD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,kEAAkE,EAAE,KAAK,IAAI,EAAE;QAClF,MAAM,IAAI,GAAG,IAAA,mCAAY,GAAE,CAAC;QAC5B,MAAM,SAAS,GAAG,6BAA6B,CAAC;QAChD,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACzC,MAAM,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,OAAO,CAC5D,QAAQ,IAAI,iCAAiC,CAC9C,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,2BAA2B,EAAE,KAAK,IAAI,EAAE;QAC3C,MAAM,IAAI,GAAG,IAAA,mCAAY,GAAE,CAAC;QAC5B,MAAM,SAAS,GAAG,6BAA6B,CAAC;QAChD,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAEzC,MAAM,KAAK,GAAG,IAAA,mCAAY,GAAE,CAAC;QAC7B,MAAM,UAAU,GAAG,iCAAiC,CAAC;QACrD,QAAQ,CAAC,cAAc,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QAC3C,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;QACpD,MAAM,IAAI,GAAG,IAAA,mCAAY,GAAE,CAAC;QAC5B,MAAM,SAAS,GAAG,6BAA6B,CAAC;QAChD,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACzC,QAAQ,CAAC,kBAAkB,EAAE,CAAC;QAE9B,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,CAAC,CAAC,aAAa,EAAE,CAAC;IACpD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;QACtD,MAAM,IAAI,GAAG,IAAA,mCAAY,GAAE,CAAC;QAC5B,MAAM,SAAS,GAAG,6BAA6B,CAAC;QAChD,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAEzC,MAAM,CAAC,QAAQ,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,55 +0,0 @@
1
- import { generateTxId } from '@alwaysai/device-agent-schemas';
2
- import { TransactionQueue } from './transaction-queue';
3
-
4
- describe('Test Transaction Queue', () => {
5
- let txnQueue: TransactionQueue;
6
-
7
- beforeEach(() => {
8
- txnQueue = new TransactionQueue();
9
- jest.clearAllMocks();
10
- });
11
-
12
- test('Add new to transaction queue', async () => {
13
- const txId = generateTxId();
14
- const projectId = '1241fad-agah-gfadg-2352dgzg';
15
- txnQueue.addTxIdToQueue(txId, projectId);
16
- expect(txnQueue.checkTxnInQueue(txId)).toBeTruthy();
17
- });
18
-
19
- test('Attempt to add existing to transaction queue, results in failure', async () => {
20
- const txId = generateTxId();
21
- const projectId = '1241fad-agah-gfadg-2352dgzg';
22
- txnQueue.addTxIdToQueue(txId, projectId);
23
- expect(() => txnQueue.addTxIdToQueue(txId, projectId)).toThrow(
24
- `txId ${txId} is already added to the queue!`
25
- );
26
- });
27
-
28
- test('Check current transaction', async () => {
29
- const txId = generateTxId();
30
- const projectId = '1241fad-agah-gfadg-2352dgzg';
31
- txnQueue.addTxIdToQueue(txId, projectId);
32
-
33
- const txId2 = generateTxId();
34
- const projectId2 = '1241faegta-agah-gagadg-2352dgzg';
35
- txnQueue.addTxIdToQueue(txId2, projectId2);
36
- expect(txnQueue.getCurrentTxId()).toEqual(txId);
37
- });
38
-
39
- test('Test remove transaction from queue', async () => {
40
- const txId = generateTxId();
41
- const projectId = '1241fad-agah-gfadg-2352dgzg';
42
- txnQueue.addTxIdToQueue(txId, projectId);
43
- txnQueue.completeCurrentTxn();
44
-
45
- expect(txnQueue.getCurrentTxId()).toBeUndefined();
46
- });
47
-
48
- test('Test get project ID for a given txId', async () => {
49
- const txId = generateTxId();
50
- const projectId = '1241fad-agah-gfadg-2352dgzg';
51
- txnQueue.addTxIdToQueue(txId, projectId);
52
-
53
- expect(txnQueue.getProjectIdForTxnId(txId)).toEqual(projectId);
54
- });
55
- });
@@ -1,40 +0,0 @@
1
- export class TransactionQueue {
2
- private transactionList: string[];
3
- private txIdToProjectIdMap: { [txId: string]: string } = {};
4
-
5
- constructor() {
6
- this.transactionList = [];
7
- this.txIdToProjectIdMap = {};
8
- }
9
-
10
- public addTxIdToQueue(txId: string, projectId: string) {
11
- if (!this.checkTxnInQueue(txId)) {
12
- this.transactionList.push(txId);
13
- this.txIdToProjectIdMap[txId] = projectId;
14
- } else {
15
- throw new Error(`txId ${txId} is already added to the queue!`);
16
- }
17
- }
18
-
19
- public getCurrentTxId() {
20
- return this.transactionList[0];
21
- }
22
-
23
- public checkTxnInQueue(txId: string) {
24
- return this.transactionList.includes(txId);
25
- }
26
-
27
- public getProjectIdForTxnId(txId: string) {
28
- return this.txIdToProjectIdMap[txId];
29
- }
30
-
31
- public completeTxn(txId: string) {
32
- delete this.txIdToProjectIdMap[txId];
33
- this.transactionList.splice(this.transactionList.indexOf(txId), 1);
34
- }
35
-
36
- public completeCurrentTxn() {
37
- delete this.txIdToProjectIdMap[this.transactionList[0]];
38
- this.transactionList.shift();
39
- }
40
- }