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

Implement SQLite database #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# database
data/db.sqlite
data/db.sqlite-journal
data/db.sqlite-shm
data/db.sqlite-wal
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ owner=\"your_org\"
repo=\"your_repo\"
```

### SQLite Database Setup

The SQLite database stores the JSON data generated by the scripts into a more efficient format for querying. Most of the db related code is in `src/lib/data`. Here's how to set it up:

1. Initialize the database and ingest historical data:

```bash
bun run init-db
```

This will:

- Create a SQLite database in the `data/` directory
- Set up the required tables and schema
- Ingest historical contributor data

## Usage

### Manual Data Collection
Expand Down
Binary file modified bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit";

export default defineConfig({
out: "./drizzle",
schema: "./src/lib/data/schema.ts",
dialect: "sqlite",
dbCredentials: {
url: "file:data/db.sqlite",
},
verbose: true,
});
61 changes: 61 additions & 0 deletions drizzle/0000_aromatic_slipstream.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
CREATE TABLE `tags` (
`name` text PRIMARY KEY NOT NULL,
`description` text DEFAULT '',
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL
);
--> statement-breakpoint
CREATE TABLE `user_daily_summaries` (
`id` text PRIMARY KEY NOT NULL,
`username` text,
`date` text NOT NULL,
`score` real DEFAULT 0 NOT NULL,
`summary` text DEFAULT '',
`total_commits` integer DEFAULT 0 NOT NULL,
`total_prs` integer DEFAULT 0 NOT NULL,
`additions` integer DEFAULT 0 NOT NULL,
`deletions` integer DEFAULT 0 NOT NULL,
`changed_files` integer DEFAULT 0 NOT NULL,
`commits` text DEFAULT '[]' NOT NULL,
`pull_requests` text DEFAULT '[]' NOT NULL,
`issues` text DEFAULT '[]' NOT NULL,
FOREIGN KEY (`username`) REFERENCES `users`(`username`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_user_daily_summaries_username` ON `user_daily_summaries` (`username`);--> statement-breakpoint
CREATE INDEX `idx_user_daily_summaries_date` ON `user_daily_summaries` (`date`);--> statement-breakpoint
CREATE TABLE `user_stats` (
`username` text PRIMARY KEY NOT NULL,
`total_prs` integer DEFAULT 0 NOT NULL,
`merged_prs` integer DEFAULT 0 NOT NULL,
`closed_prs` integer DEFAULT 0 NOT NULL,
`total_files` integer DEFAULT 0 NOT NULL,
`total_additions` integer DEFAULT 0 NOT NULL,
`total_deletions` integer DEFAULT 0 NOT NULL,
`files_by_type` text DEFAULT '{}' NOT NULL,
`prs_by_month` text DEFAULT '{}' NOT NULL,
`focus_areas` text DEFAULT '[]' NOT NULL,
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`username`) REFERENCES `users`(`username`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `user_tag_scores` (
`id` text PRIMARY KEY NOT NULL,
`username` text,
`tag` text,
`score` real DEFAULT 0 NOT NULL,
`level` integer DEFAULT 0 NOT NULL,
`progress` real DEFAULT 0 NOT NULL,
`points_to_next` real DEFAULT 0 NOT NULL,
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`username`) REFERENCES `users`(`username`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`tag`) REFERENCES `tags`(`name`) ON UPDATE no action ON DELETE no action
);
Comment on lines +42 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add unique constraint and index for user_tag_scores.

  1. Add a unique constraint on (username, tag) to prevent duplicate scores
  2. Add an index on the tag column for faster lookups
+CREATE UNIQUE INDEX `idx_user_tag_scores_unique` ON `user_tag_scores` (`username`, `tag`);
+CREATE INDEX `idx_user_tag_scores_tag` ON `user_tag_scores` (`tag`);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
CREATE TABLE `user_tag_scores` (
`id` text PRIMARY KEY NOT NULL,
`username` text,
`tag` text,
`score` real DEFAULT 0 NOT NULL,
`level` integer DEFAULT 0 NOT NULL,
`progress` real DEFAULT 0 NOT NULL,
`points_to_next` real DEFAULT 0 NOT NULL,
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`username`) REFERENCES `users`(`username`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`tag`) REFERENCES `tags`(`name`) ON UPDATE no action ON DELETE no action
);
CREATE TABLE `user_tag_scores` (
`id` text PRIMARY KEY NOT NULL,
`username` text,
`tag` text,
`score` real DEFAULT 0 NOT NULL,
`level` integer DEFAULT 0 NOT NULL,
`progress` real DEFAULT 0 NOT NULL,
`points_to_next` real DEFAULT 0 NOT NULL,
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`username`) REFERENCES `users`(`username`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`tag`) REFERENCES `tags`(`name`) ON UPDATE no action ON DELETE no action
);
CREATE UNIQUE INDEX `idx_user_tag_scores_unique` ON `user_tag_scores` (`username`, `tag`);
CREATE INDEX `idx_user_tag_scores_tag` ON `user_tag_scores` (`tag`);

--> statement-breakpoint
CREATE INDEX `idx_user_tag_scores_username` ON `user_tag_scores` (`username`);--> statement-breakpoint
CREATE TABLE `users` (
`username` text PRIMARY KEY NOT NULL,
`avatar_url` text DEFAULT '',
`last_updated` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`score` real DEFAULT 0 NOT NULL
);
Loading