Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(insights): correct DB migration script #5024

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/insights/drizzle/0012_omniscient_leader.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ CREATE TABLE `symbolDetailTmp` (
FOREIGN KEY (`public_api_key`) REFERENCES `applications`(`public_api_key`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`public_api_key`,`manifest_hash`) REFERENCES `manifests`(`public_api_key`,`hash`) ON UPDATE no action ON DELETE no action
);--> statement-breakpoint
INSERT INTO `symbolDetailTmp` SELECT * FROM `symbolDetail`;--> statement-breakpoint
INSERT INTO `symbolDetailTmp` (
`hash`,
`public_api_key`,
`manifest_hash`,
`full_name`,
`origin`,
`lo`,
`hi`
) SELECT
`hash`,
`public_api_key`,
`manifest_hash`,
`full_name`,
`origin`,
`lo`,
`hi`
FROM `symbolDetail`;--> statement-breakpoint
DROP TABLE `symbolDetail`;--> statement-breakpoint
ALTER TABLE `symbolDetailTmp` RENAME TO `symbolDetail`;
6 changes: 3 additions & 3 deletions packages/insights/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"@typescript/analyze-trace": "^0.10.0",
"density-clustering": "^1.3.0",
"dotenv": "^16.3.1",
"drizzle-kit": "^0.19.12",
"drizzle-orm": "^0.28.2",
"drizzle-kit": "^0.19.13",
"drizzle-orm": "0.28.3",
"install": "^0.13.0"
},
"devDependencies": {
Expand All @@ -29,7 +29,7 @@
"eslint-plugin-qwik": "1.2.6",
"netlify-cli": "^15.9.1",
"prettier": "3.0.0",
"typescript": "5.1.6",
"typescript": "5.2.2",
"undici": "5.22.1",
"uvu": "0.5.6",
"vite": "4.4.7",
Expand Down
8 changes: 4 additions & 4 deletions packages/insights/src/db/query-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sql, type InferInsertModel } from 'drizzle-orm';
import { edgeTable, type routesTable } from './schema';
import { sql } from 'drizzle-orm';
import { type EdgeRowSansId, edgeTable, type RouteRowSansId } from './schema';
import { BUCKETS } from '~/stats/vector';

export type VectorKeys<PREFIX extends string> =
Expand Down Expand Up @@ -81,7 +81,7 @@ export function createEdgeRow({
interaction: boolean;
delayBucket: number;
latencyBucket: number;
}): InferInsertModel<typeof edgeTable> {
}): EdgeRowSansId {
return {
publicApiKey,
manifestHash,
Expand Down Expand Up @@ -630,7 +630,7 @@ export function createRouteRow({
manifestHash: string;
route: string;
symbol: string;
}): InferInsertModel<typeof routesTable> {
}): RouteRowSansId {
return {
publicApiKey,
manifestHash,
Expand Down
31 changes: 28 additions & 3 deletions packages/insights/src/db/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { and, eq, isNull, sql, inArray } from 'drizzle-orm';
import { type AppDatabase } from '.';
import { applicationTable, edgeTable, routesTable, symbolDetailTable, symbolTable } from './schema';
import {
type SymbolDetailRow,
applicationTable,
edgeTable,
routesTable,
symbolDetailTable,
symbolTable,
} from './schema';
import {
createEdgeRow,
delayBucketField,
Expand Down Expand Up @@ -39,7 +46,17 @@ export async function getEdges(
}));
}

export async function getSlowEdges(db: AppDatabase, publicApiKey: string, manifests: string[]) {
export interface SlowEdge {
manifestHash: string;
to: string;
latency: number[];
}

export async function getSlowEdges(
db: AppDatabase,
publicApiKey: string,
manifests: string[]
): Promise<SlowEdge[]> {
let where = eq(edgeTable.publicApiKey, publicApiKey);
if (manifests.length) {
where = and(where, inArray(edgeTable.manifestHash, manifests))!;
Expand All @@ -62,7 +79,15 @@ export async function getSlowEdges(db: AppDatabase, publicApiKey: string, manife
}));
}

export function getSymbolDetails(db: AppDatabase, publicApiKey: string) {
export type SymbolDetailForApp = Pick<
SymbolDetailRow,
'hash' | 'fullName' | 'origin' | 'lo' | 'hi'
>;

export async function getSymbolDetails(
db: AppDatabase,
publicApiKey: string
): Promise<SymbolDetailForApp[]> {
return db
.select({
hash: symbolDetailTable.hash,
Expand Down
23 changes: 23 additions & 0 deletions packages/insights/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type InferSelectModel, type InferInsertModel } from 'drizzle-orm';
import {
integer,
sqliteTable,
Expand All @@ -9,7 +10,10 @@ import {

export type DatabaseSchema = {
applicationTable: typeof applicationTable;
manifestTabes: typeof manifestTable;
symbolTable: typeof symbolTable;
symbolDetailTable: typeof symbolDetailTable;
errorTable: typeof errorTable;
};

export const applicationTable = sqliteTable(
Expand All @@ -25,6 +29,9 @@ export const applicationTable = sqliteTable(
})
);

export type ApplicationRow = InferSelectModel<typeof applicationTable>;
export type ApplicationRowSansId = InferInsertModel<typeof applicationTable>;

export const symbolTable = sqliteTable('symbols', {
id: integer('id').primaryKey(),
publicApiKey: text('public_api_key').references(() => applicationTable.publicApiKey),
Expand All @@ -37,6 +44,9 @@ export const symbolTable = sqliteTable('symbols', {
loadDelay: integer('load_delay_ms').notNull(),
});

export type SymbolRow = InferSelectModel<typeof symbolTable>;
export type SymbolRowSansId = InferInsertModel<typeof symbolTable>;

// event, source, lineno, colno, error
export const errorTable = sqliteTable('errors', {
id: integer('id').primaryKey(),
Expand All @@ -53,6 +63,8 @@ export const errorTable = sqliteTable('errors', {
stack: text('stack').notNull(),
});

export type ErrorRow = InferSelectModel<typeof errorTable>;

export const manifestTable = sqliteTable(
'manifests',
{
Expand All @@ -66,6 +78,8 @@ export const manifestTable = sqliteTable(
})
);

export type ManifestRow = InferSelectModel<typeof manifestTable>;

export const symbolDetailTable = sqliteTable(
'symbolDetail',
{
Expand All @@ -92,6 +106,9 @@ export const symbolDetailTable = sqliteTable(
}
);

export type SymbolDetailRow = InferSelectModel<typeof symbolDetailTable>;
export type SymbolDetailRowSansId = InferInsertModel<typeof symbolDetailTable>;

export const edgeTable = sqliteTable(
'edges',
{
Expand Down Expand Up @@ -217,6 +234,9 @@ export const edgeTable = sqliteTable(
})
);

export type EdgeRow = InferSelectModel<typeof edgeTable>;
export type EdgeRowSansId = InferInsertModel<typeof edgeTable>;

export const routesTable = sqliteTable(
'routes',
{
Expand Down Expand Up @@ -285,3 +305,6 @@ export const routesTable = sqliteTable(
),
})
);

export type RouteRow = InferSelectModel<typeof routesTable>;
export type RouteRowSansId = InferInsertModel<typeof routesTable>;
9 changes: 8 additions & 1 deletion packages/insights/src/db/sql-edges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { type AppDatabase } from './index';
import { edgeTableDelayCount, latencyColumnSums, delayColumnSums, toVector } from './query-helpers';
import { edgeTable } from './schema';

export interface OutgoingEdge {
manifestHash: string;
to: string;
latency: number[];
delay: number[];
}

export async function dbGetOutgoingEdges(
db: AppDatabase,
publicApiKey: string,
symbol: string,
manifests: string[]
) {
): Promise<OutgoingEdge[]> {
let where = and(eq(edgeTable.publicApiKey, publicApiKey), eq(edgeTable.from, symbol));
if (manifests.length) {
where = and(where, inArray(edgeTable.manifestHash, manifests))!;
Expand Down
19 changes: 14 additions & 5 deletions packages/insights/src/db/sql-manifest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { eq, and, type InferSelectModel, sql } from 'drizzle-orm';
import { eq, and, sql } from 'drizzle-orm';
import { type AppDatabase } from './index';
import { edgeTable, manifestTable } from './schema';
import { type ManifestRow, edgeTable, manifestTable } from './schema';
import { latencyColumnSums, toVector } from './query-helpers';

export async function dbGetManifests(
db: AppDatabase,
publicApiKey: string
): Promise<InferSelectModel<typeof manifestTable>[]> {
): Promise<ManifestRow[]> {
const manifests = await db
.select()
.from(manifestTable)
Expand All @@ -16,7 +16,16 @@ export async function dbGetManifests(
return manifests;
}

export async function dbGetManifestStats(db: AppDatabase, publicApiKey: string) {
export interface ManifestStatsRow {
hash: string;
timestamp: Date;
latency: number[];
}

export async function dbGetManifestStats(
db: AppDatabase,
publicApiKey: string
): Promise<ManifestStatsRow[]> {
const manifests = await db
.select({ hash: manifestTable.hash, timestamp: manifestTable.timestamp, ...latencyColumnSums })
.from(manifestTable)
Expand All @@ -38,7 +47,7 @@ export async function dbGetManifestInfo(
db: AppDatabase,
publicApiKey: string,
manifestHash: string
): Promise<InferSelectModel<typeof manifestTable>> {
): Promise<ManifestRow> {
const manifest = await db
.select()
.from(manifestTable)
Expand Down
24 changes: 12 additions & 12 deletions packages/insights/src/routes.gen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,53 @@ export interface AppRouteParamsFunction {
(route: "/", params?: {}): string;
(
route: "/api/v1/[publicApiKey]/bundles/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/api/v1/[publicApiKey]/post/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/api/v1/[publicApiKey]/post/error/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/api/v1/[publicApiKey]/post/manifest/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(route: "/app/", params?: {}): string;
(route: "/app/[publicApiKey]/", params: { publicApiKey: string }): string;
(
route: "/app/[publicApiKey]/edit/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/errors/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/manifests/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/symbols/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/symbols/bundles/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/symbols/edge/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/symbols/outgoing/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(
route: "/app/[publicApiKey]/symbols/slow/",
params: { publicApiKey: string }
params: { publicApiKey: string },
): string;
(route: "/app/add/", params?: {}): string;
(route: "/test/", params?: {}): string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { component$ } from '@builder.io/qwik';
import { type ReadonlySignal, component$ } from '@builder.io/qwik';
import { routeLoader$ } from '@builder.io/qwik-city';
import { getDB, errorTable } from '~/db';
import { getDB, errorTable, type ErrorRow } from '~/db';
import { eq, sql } from 'drizzle-orm';
import { css, cx } from '~/styled-system/css';
import { ErrorIcon } from '~/components/icons/error';
import { type PopupEvent } from '~/components/popup-manager';

export const useErrors = routeLoader$(async ({ params }) => {
const db = getDB();
const errors = await db
const errors: ErrorRow[] = await db
.select()
.from(errorTable)
.where(eq(errorTable.publicApiKey, params.publicApiKey))
Expand Down Expand Up @@ -60,7 +60,7 @@ const columnMessageCell = cx(
);

export default component$(() => {
const errors = useErrors();
const errors: ReadonlySignal<ErrorRow[]> = useErrors();
return (
<div>
<h1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { component$ } from '@builder.io/qwik';
import { type ReadonlySignal, component$ } from '@builder.io/qwik';
import { routeLoader$ } from '@builder.io/qwik-city';
import Histogram, { latencyColors } from '~/components/histogram';
import { ManifestTile } from '~/components/minifest-tile';
import { getDB } from '~/db';
import { dbGetManifestStats } from '~/db/sql-manifest';
import { type ManifestStatsRow, dbGetManifestStats } from '~/db/sql-manifest';
import { vectorAvg, vectorSum, BUCKETS } from '~/stats/vector';
import { css, cx } from '~/styled-system/css';

Expand Down Expand Up @@ -60,7 +60,7 @@ const columnHistogram = cx(
);

export default component$(() => {
const data = useData();
const data: ReadonlySignal<ManifestStatsRow[]> = useData();
return (
<div>
<h1>Manifests</h1>
Expand Down
Loading
Loading