Skip to content

Commit

Permalink
get basic db working.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgarant committed Nov 17, 2023
1 parent fa204a1 commit b239406
Show file tree
Hide file tree
Showing 24 changed files with 10,533 additions and 10,118 deletions.
29 changes: 21 additions & 8 deletions agent/agentWS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ping from 'ping'
import os from 'os'
import { PingTest } from '../types'
import { type PingTest } from '../types'

const port = 1002

Expand All @@ -25,7 +25,9 @@ export function setupAgentWs() {
.then((result) => {
ws.send(JSON.stringify({ test: result }))
})
.catch((err) => ws.close(1011, `Error pinging ip: ${err}`))
.catch((err) =>
ws.close(1011, `Error pinging ip: ${err}`)
)
})
} catch (error) {
ws.close(1011, 'Error parsing JSON from message')
Expand All @@ -50,15 +52,24 @@ function tryConnects(tests: PingTest[]) {
console.log(`Host IP: ${hostIP}`)

return tests.map((x) =>
isConnected(x.destIp).then((isAlive) => ({
isConnected(x.dest_ip).then((isAlive) => ({
...x,
status: getStatusText(isAlive, x.shouldFail),
status: getStatusText(isAlive, x.should_fail),
}))
)
}

function getStatusText(isAlive: boolean, shouldFail: boolean): PingTest['status'] {
return shouldFail ? (isAlive ? 'failed' : 'success') : isAlive ? 'success' : 'failed'
function getStatusText(
isAlive: boolean,
should_fail: boolean
): PingTest['status'] {
return should_fail
? isAlive
? 'failed'
: 'success'
: isAlive
? 'success'
: 'failed'
}

function isConnected(ip: string): Promise<boolean> {
Expand All @@ -67,10 +78,12 @@ function isConnected(ip: string): Promise<boolean> {

function getLocalIP() {
const networkInterfaces = os.networkInterfaces()
const localInterface = networkInterfaces['en0'] || networkInterfaces['Wi-Fi']
const localInterface = networkInterfaces.en0 || networkInterfaces['Wi-Fi']

if (localInterface) {
const localIPv4 = localInterface.find((iface) => iface.family === 'IPv4')
const localIPv4 = localInterface.find(
(iface) => iface.family === 'IPv4'
)
if (localIPv4) {
return localIPv4.address
}
Expand Down
24 changes: 12 additions & 12 deletions coordinator/data/tests-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ export const rowsData = [
id: 'd7d4c1c4-7c5d-4d5d-9c3f-9d5d6d7d8d9e',
status: undefined,
description: 'Can access internet',
agent: 'f7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
destIp: ips.internet,
shouldFail: false,
agent_id: 'f7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
dest_ip: ips.internet,
should_fail: false,
},
{
id: 'a9b8c7d6-e5f4-4d3c-2b1a-1a2b3c4d5e6f',
status: undefined,
description: 'Cannot access device on a separate vlan',
agent: 'f7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
destIp: ips.iotdevice,
shouldFail: true,
agent_id: 'f7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
dest_ip: ips.iotdevice,
should_fail: true,
},
{
id: 'e9b8c7d6-e5f4-4d3c-2b1a-1a2b3c4d5e6f',
status: undefined,
description: 'Can access device on same vlan',
agent: 'f7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
destIp: ips.fail,
shouldFail: false,
agent_id: 'f7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
dest_ip: ips.fail,
should_fail: false,
},
{
id: 'f3c6b2e9-7e3c-4e5d-8c1f-9a8b7c6d5e4f',
status: undefined,
description: 'IoT thermostat can access the internet',
agent: 'e7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
destIp: '1.1.1.1',
shouldFail: false,
agent_id: 'e7c5d6e4-8a2b-4c1d-9e3f-0d5a6b7c8d9e',
dest_ip: '1.1.1.1',
should_fail: false,
},
]

Expand Down
10 changes: 6 additions & 4 deletions coordinator/db/schema/pingtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { text, sqliteTable, integer } from 'drizzle-orm/sqlite-core'
export const pingTest = sqliteTable('tests', {
id: integer('id').primaryKey(),
description: text('description'),
agent: integer('agent'),
destIp: text('destIp'),
shouldFail: integer('shouldFail', { mode: 'boolean' }),
status: text('status', { enum: ['success', 'failed', 'running', 'not ran', 'error'] }),
agent_id: integer('agent_id'),
dest_ip: text('dest_ip'),
should_fail: integer('should_fail', { mode: 'boolean' }),
status: text('status', {
enum: ['success', 'failed', 'running', 'not ran', 'error'],
}),
})

export type PingTest = typeof pingTest.$inferSelect
2 changes: 1 addition & 1 deletion coordinator/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { Config } from 'drizzle-kit'
export default {
schema: './db/schema/pingtest.ts',
out: './db/drizzle',
driver: '',
driver: 'pg',
} satisfies Config
12 changes: 7 additions & 5 deletions coordinator/setupDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function seedDB() {

// Create a new table
db.exec(
'CREATE TABLE tests (id TEXT, status TEXT NULL, description TEXT, agent TEXT, destIp TEXT, shouldFail BOOLEAN)'
'CREATE TABLE tests (id TEXT, status TEXT NULL, description TEXT, agent_id TEXT, dest_ip TEXT, should_fail BOOLEAN)'
)

// Insert your data into the table
Expand All @@ -37,15 +37,17 @@ function seedDB() {
rowsData[i].id,
rowsData[i].status || null,
rowsData[i].description,
rowsData[i].agent,
rowsData[i].destIp,
rowsData[i].shouldFail,
rowsData[i].agent_id,
rowsData[i].dest_ip,
rowsData[i].should_fail,
])
}

// Read the data back to verify it was inserted correctly
const rows = db
.prepare('SELECT id, status, description, agent, destIp, CAST(shouldFail AS BOOLEAN) AS shouldFail FROM tests')
.prepare(
'SELECT id, status, description, agent_id, dest_ip, CAST(should_fail AS BOOLEAN) AS should_fail FROM tests'
)
.all()

console.log('Seeded DB: ', rows)
Expand Down
10 changes: 10 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from 'drizzle-kit'

export default {
schema: './src/server/db/schema/index.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: {
connectionString: process.env.POSTGRES_URL + '?sslmode=require',
},
} satisfies Config
15 changes: 15 additions & 0 deletions drizzle/0000_open_captain_midlands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DO $$ BEGIN
CREATE TYPE "status" AS ENUM('success', 'failed', 'running', 'not ran', 'error');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "tests" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"description" text,
"agent_id" uuid DEFAULT gen_random_uuid() NOT NULL,
"dest_ip" varchar(15) NOT NULL,
"should_fail" boolean NOT NULL,
"status" "status" NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL
);
81 changes: 81 additions & 0 deletions drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"version": "5",
"dialect": "pg",
"id": "ef28e0c3-509a-4464-8593-4c19ca4ce475",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"tests": {
"name": "tests",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"agent_id": {
"name": "agent_id",
"type": "uuid",
"primaryKey": false,
"notNull": true,
"default": "gen_random_uuid()"
},
"dest_ip": {
"name": "dest_ip",
"type": "varchar(15)",
"primaryKey": false,
"notNull": true
},
"should_fail": {
"name": "should_fail",
"type": "boolean",
"primaryKey": false,
"notNull": true
},
"status": {
"name": "status",
"type": "status",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {
"status": {
"name": "status",
"values": {
"success": "success",
"failed": "failed",
"running": "running",
"not ran": "not ran",
"error": "error"
}
}
},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
13 changes: 13 additions & 0 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1700069038118,
"tag": "0000_open_captain_midlands",
"breakpoints": true
}
]
}
Loading

0 comments on commit b239406

Please sign in to comment.