Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
taskylizard committed Feb 16, 2024
0 parents commit a4f93e8
Show file tree
Hide file tree
Showing 36 changed files with 1,350 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Code quality

on:
push:
pull_request:

jobs:
quality:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Biome
uses: biomejs/setup-biome@v2
with:
version: latest
- name: Run Biome
run: biome ci .
174 changes: 174 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# wrangler project

.dev.vars
.wrangler/
worker-configuration.d.ts
assets/built.css
18 changes: 18 additions & 0 deletions assets/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
;(() => {
const v = localStorage.getItem('color-scheme')
const a = window.matchMedia('(prefers-color-scheme: dark)').matches
const cl = document.documentElement.classList
const setColorScheme = (v) =>
(!v || v === 'auto' ? a : v === 'dark') ? cl.add('dark') : cl.remove('dark')
setColorScheme(v)
window.setColorScheme = (v) => {
setColorScheme(v)
localStorage.setItem('color-scheme', v)
}
window.toggleColorScheme = () => {
const cl = document.documentElement.classList
const currentScheme = cl.contains('dark') ? 'light' : 'dark'
cl.toggle('dark')
localStorage.setItem('color-scheme', currentScheme)
}
})()
33 changes: 33 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"organizeImports": {
"enabled": true
},
"vcs": {
"enabled": true,
"clientKind": "git"
},
"javascript": {
"formatter": {
"enabled": true,
"quoteStyle": "single",
"semicolons": "asNeeded",
"trailingComma": "none"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"lineWidth": 80
},
"css": {
"formatter": {
"enabled": true
}
}
}
Binary file added bun.lockb
Binary file not shown.
8 changes: 8 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pre-commit:
parallel: true
commands:
check:
glob: '*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}'
run:
bunx biome check --apply --no-errors-on-unmatched
--files-ignore-unknown=true {staged_files} && git update-index --again
22 changes: 22 additions & 0 deletions migrations/0000_create_events_and_probe_statuses_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Migration number: 0000

CREATE TABLE events (
id INTEGER PRIMARY KEY,
created_at TEXT NOT NULL,
probe_id TEXT,
webhook_id TEXT,
duration INTEGER,
result TEXT NOT NULL,
category TEXT NOT NULL,
description TEXT
) STRICT;

CREATE TABLE probe_statuses (
id TEXT PRIMARY KEY,
last_result TEXT NOT NULL,
last_started_at TEXT NOT NULL,
last_success_at TEXT,
last_failure_at TEXT,
same_result_since TEXT
) STRICT;

7 changes: 7 additions & 0 deletions migrations/0001_create_log_lines_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE log_lines (
id INTEGER PRIMARY KEY,
created_at TEXT NOT NULL,
level TEXT NOT NULL,
message TEXT NOT NULL
) STRICT;

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "simulation",
"version": "1.0.0",
"private": true,
"scripts": {
"db:insights": "wrangler d1 insights simulation",
"db:migrate:local": "wrangler d1 migrations apply --local simulation",
"db:migrate:prod": "wrangler d1 migrations apply simulation",
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"postinstall": "wrangler types && lefthook install",
"tail": "wrangler tail",
"typecheck": "wrangler types && tsc"
},
"dependencies": {
"clsx": "^2.1.0",
"d1-sql-tag": "^0.3.0",
"hono": "^3.11.2"
},
"devDependencies": {
"@biomejs/biome": "1.5.3",
"@cloudflare/workers-types": "^4.20240208.0",
"lefthook": "^1.6.1",
"tailwindcss": "^3.4.1",
"typescript": "^5.3.3",
"wrangler": "^3.28.1"
}
}
5 changes: 5 additions & 0 deletions src/db/conversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function convertDate(date: string): Date
export function convertDate(date: string | null): Date | null
export function convertDate(date: string | null) {
return date ? new Date(date) : null
}
35 changes: 35 additions & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type Brand<K, T> = K & { __brand: T }

export type EventId = Brand<number, 'EventId'>
export type LogLineId = Brand<number, 'LogLineId'>
export type ProbeId = Brand<string, 'ProbeId'>
export type WebhookId = Brand<string, 'WebhookId'>

export interface DatabaseSchema {
events: {
id: EventId
created_at: string
probe_id: ProbeId | null
webhook_id: WebhookId | null
duration: number | null
result: 'success' | 'failure'
category: string
description: string | null
}

log_lines: {
id: LogLineId
created_at: string
level: 'info' | 'warn' | 'error'
message: string
}

probe_statuses: {
id: ProbeId
last_started_at: string
last_result: 'success' | 'failure' | ''
last_success_at: string | null
last_failure_at: string | null
same_result_since: string | null
}
}
27 changes: 27 additions & 0 deletions src/db/sql-tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createD1SqlTag, logQueryResults } from 'd1-sql-tag'
import { type Context } from 'hono'
import { endTime, setMetric, startTime } from 'hono/timing'
import type { Bindings } from '..'

export function createSqlTag(c: Context<{ Bindings: Bindings }>) {
return createD1SqlTag(c.env.DB, {
beforeQuery(batchId, queries) {
startTime(c, `db-${batchId}`)
},
afterQuery(batchId, queries, results, duration) {
endTime(c, `db-${batchId}`)
results.forEach((result, i) => {
setMetric(c, `db-${batchId}-query-${i + 1}`, result.meta.duration)
})
logQueryResults(queries, results, duration)
}
})
}

export function createCronSqlTag(env: Bindings) {
return createD1SqlTag(env.DB, {
afterQuery(batchId, queries, results, duration) {
logQueryResults(queries, results, duration)
}
})
}
Loading

0 comments on commit a4f93e8

Please sign in to comment.