Fix formatting
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -23,3 +23,4 @@ yarn-error.log
|
||||
|
||||
# Platform specific
|
||||
.DS_Store
|
||||
*compose-prod.yml
|
||||
|
@ -53,7 +53,7 @@ export default defineConfig({
|
||||
() => import('@adonisjs/lucid/database_provider'),
|
||||
() => import('@adonisjs/auth/auth_provider'),
|
||||
() => import('@adonisjs/inertia/inertia_provider'),
|
||||
() => import('@adonisjs/redis/redis_provider')
|
||||
() => import('@adonisjs/redis/redis_provider'),
|
||||
],
|
||||
|
||||
/*
|
||||
|
@ -5,25 +5,25 @@ import env from '#start/env'
|
||||
let recordsUpdated = 0
|
||||
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
|
||||
interface ApiResponse<T> {
|
||||
data: T;
|
||||
data: T
|
||||
// optionally, you can define `meta`, `errors`, etc. if your API returns them
|
||||
}
|
||||
export async function fetchBatch(url: string) {
|
||||
const options: RequestInit = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${SENTRY_TOKEN}`
|
||||
}
|
||||
Authorization: `Bearer ${SENTRY_TOKEN}`,
|
||||
},
|
||||
}
|
||||
const req = await fetch(url, options)
|
||||
if (!req.ok) {
|
||||
throw new Error(`Request failed with status ${req.status}`);
|
||||
throw new Error(`Request failed with status ${req.status}`)
|
||||
}
|
||||
|
||||
const resp = await req.json() as ApiResponse<Replay[]>;
|
||||
const replays = resp.data;
|
||||
const resp = (await req.json()) as ApiResponse<Replay[]>
|
||||
const replays = resp.data
|
||||
const headers = req.headers
|
||||
|
||||
const cleanedData = replays.map(record => sanitizeInput(record, Replay.allowedFields))
|
||||
const cleanedData = replays.map((record) => sanitizeInput(record, Replay.allowedFields))
|
||||
|
||||
let updated = await Replay.updateOrCreateMany('id', cleanedData)
|
||||
recordsUpdated = recordsUpdated + updated.length
|
||||
@ -39,12 +39,14 @@ export async function fetchBatch(url: string) {
|
||||
}
|
||||
console.log('no more results')
|
||||
return { recordsUpdated }
|
||||
|
||||
}
|
||||
|
||||
function sanitizeInput(data: Record<string, any>, allowedFields: string[]) {
|
||||
return allowedFields.reduce((acc, key) => {
|
||||
return allowedFields.reduce(
|
||||
(acc, key) => {
|
||||
if (key in data) acc[key] = data[key]
|
||||
return acc
|
||||
}, {} as Record<string, any>)
|
||||
},
|
||||
{} as Record<string, any>
|
||||
)
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
|
||||
export interface SentryPagination {
|
||||
previous: string;
|
||||
hasPreviousResults: boolean;
|
||||
hasNextResults: boolean;
|
||||
previous: string
|
||||
hasPreviousResults: boolean
|
||||
hasNextResults: boolean
|
||||
next: string
|
||||
}
|
||||
export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||
const links = header.split(',').map(part => part.trim())
|
||||
const links = header.split(',').map((part) => part.trim())
|
||||
|
||||
let result = {} as SentryPagination
|
||||
for (const link of links) {
|
||||
|
@ -1,18 +1,20 @@
|
||||
import env from '#start/env'
|
||||
|
||||
export async function sendDataToWebhook(responseData:{ version: number, updatedAt: Date, numberOfRecords: number, data: unknown}) {
|
||||
export async function sendDataToWebhook(responseData: {
|
||||
version: number
|
||||
updatedAt: Date
|
||||
numberOfRecords: number
|
||||
data: unknown
|
||||
}) {
|
||||
try {
|
||||
console.log('syncing to webhook')
|
||||
await fetch(env.get('WEBHOOK_URL'),
|
||||
{
|
||||
headers:
|
||||
{
|
||||
'content-type': 'application/json'
|
||||
await fetch(env.get('WEBHOOK_URL'), {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
method: 'POST',
|
||||
body: JSON.stringify(responseData)
|
||||
}
|
||||
)
|
||||
body: JSON.stringify(responseData),
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('error sending webhook data', e)
|
||||
}
|
||||
|
@ -6,9 +6,7 @@ import redis from '@adonisjs/redis/services/main'
|
||||
import { fetchBatch } from '../Helpers/Replays.js'
|
||||
import { sendDataToWebhook } from '../Helpers/Webhook.js'
|
||||
|
||||
|
||||
export default class ReplaysController {
|
||||
|
||||
public async stats({ request, response }: HttpContext) {
|
||||
const { sendToWebhook } = request.qs()
|
||||
const latestVersion = await redis.get(`replays:stats:latest_version`)
|
||||
@ -24,7 +22,12 @@ export default class ReplaysController {
|
||||
}
|
||||
}
|
||||
|
||||
let responseData = { version: results.version, updatedAt: results.updatedAt, numberOfRecords: results.rows.length, data: results.rows }
|
||||
let responseData = {
|
||||
version: results.version,
|
||||
updatedAt: results.updatedAt,
|
||||
numberOfRecords: results.rows.length,
|
||||
data: results.rows,
|
||||
}
|
||||
if (sendToWebhook) {
|
||||
await sendDataToWebhook(responseData)
|
||||
}
|
||||
@ -40,7 +43,7 @@ export default class ReplaysController {
|
||||
let paginated, meta, replays
|
||||
|
||||
if (data) {
|
||||
({ paginated, meta, replays } = JSON.parse(data))
|
||||
;({ paginated, meta, replays } = JSON.parse(data))
|
||||
} else {
|
||||
paginated = await Replay.query().paginate(page, perPage)
|
||||
paginated.baseUrl('/list')
|
||||
@ -49,7 +52,7 @@ export default class ReplaysController {
|
||||
|
||||
meta = {
|
||||
...json.meta,
|
||||
links: buildPaginationLinks(json.meta)
|
||||
links: buildPaginationLinks(json.meta),
|
||||
}
|
||||
|
||||
replays = json.data
|
||||
@ -60,16 +63,13 @@ export default class ReplaysController {
|
||||
return inertia.render('Replays/Index', {
|
||||
data: {
|
||||
replays,
|
||||
meta
|
||||
}
|
||||
meta,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
async index({ request, response }: HttpContext) {
|
||||
const { statsPeriod, start, end } = request.qs()
|
||||
|
||||
let queryString: string = '?statsPeriod=24h' // Default in case none is provided
|
||||
if (statsPeriod) {
|
||||
queryString = `?statsPeriod=${statsPeriod}`
|
||||
@ -82,25 +82,28 @@ export default class ReplaysController {
|
||||
|
||||
return response.json({ version: queryResults.latestVersion, ...queryResults })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number; currentPage: number; nextPageUrl: string }) {
|
||||
function buildPaginationLinks(meta: {
|
||||
previousPageUrl: string
|
||||
lastPage: number
|
||||
currentPage: number
|
||||
nextPageUrl: string
|
||||
}) {
|
||||
const links = []
|
||||
|
||||
// Previous
|
||||
links.push({
|
||||
url: meta.previousPageUrl,
|
||||
label: '« Prev',
|
||||
active: false
|
||||
active: false,
|
||||
})
|
||||
|
||||
for (let page = 1; page <= meta.lastPage; page++) {
|
||||
links.push({
|
||||
url: `/list?page=${page}`,
|
||||
label: page.toString(),
|
||||
active: page === meta.currentPage
|
||||
active: page === meta.currentPage,
|
||||
})
|
||||
}
|
||||
|
||||
@ -108,7 +111,7 @@ function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number;
|
||||
links.push({
|
||||
url: meta.nextPageUrl,
|
||||
label: 'Next »',
|
||||
active: false
|
||||
active: false,
|
||||
})
|
||||
|
||||
return links
|
||||
|
@ -54,8 +54,7 @@ export default class Replay extends BaseModel {
|
||||
) r ON true
|
||||
|
||||
ORDER BY
|
||||
u.total_time_seconds DESC;`
|
||||
)
|
||||
u.total_time_seconds DESC;`)
|
||||
const updatedVersion = await redis.incr('replays:stats:latest_version')
|
||||
results.version = updatedVersion
|
||||
results.updatedAt = Date.now()
|
||||
@ -72,14 +71,14 @@ export default class Replay extends BaseModel {
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare trace_ids: string[]
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare error_ids: string[]
|
||||
|
||||
@ -90,26 +89,22 @@ export default class Replay extends BaseModel {
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare tags: string[]
|
||||
|
||||
@column()
|
||||
declare user: string[]
|
||||
|
||||
|
||||
@column()
|
||||
declare sdk: any
|
||||
|
||||
|
||||
@column()
|
||||
declare os: any
|
||||
|
||||
|
||||
@column()
|
||||
declare browser: any
|
||||
|
||||
|
||||
@column()
|
||||
declare device: any
|
||||
|
||||
@ -119,21 +114,19 @@ export default class Replay extends BaseModel {
|
||||
@column()
|
||||
declare is_archived: boolean | null
|
||||
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare urls: any
|
||||
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare clicks: any
|
||||
|
||||
@ -170,12 +163,11 @@ export default class Replay extends BaseModel {
|
||||
@column()
|
||||
declare platform: string | null
|
||||
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare releases: any
|
||||
|
||||
|
32
compose.yml
32
compose.yml
@ -13,12 +13,12 @@ services:
|
||||
- traefik
|
||||
scraper:
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.docker.network=sentry_traefik"
|
||||
- "traefik.http.routers.scraper.rule=Host(`sentry.docker.localhost`)"
|
||||
- "traefik.http.services.scraper.loadbalancer.server.port=3333"
|
||||
- "traefik.http.routers.scraper.entrypoints=http"
|
||||
- "traefik.http.routers.scraper.service=scraper"
|
||||
- 'traefik.enable=true'
|
||||
- 'traefik.docker.network=sentry_traefik'
|
||||
- 'traefik.http.routers.scraper.rule=Host(`sentry.docker.localhost`)'
|
||||
- 'traefik.http.services.scraper.loadbalancer.server.port=3333'
|
||||
- 'traefik.http.routers.scraper.entrypoints=http'
|
||||
- 'traefik.http.routers.scraper.service=scraper'
|
||||
networks:
|
||||
- traefik
|
||||
- redis
|
||||
@ -31,7 +31,7 @@ services:
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=password
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready", "-d", "postgres"]
|
||||
test: ['CMD-SHELL', 'pg_isready', '-d', 'postgres']
|
||||
interval: 5s
|
||||
timeout: 60s
|
||||
retries: 5
|
||||
@ -43,17 +43,21 @@ services:
|
||||
grafana:
|
||||
image: grafana/grafana:latest
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.docker.network=sentry_traefik"
|
||||
- "traefik.http.routers.grafana.rule=Host(`grafana.docker.localhost`)"
|
||||
- "traefik.http.routers.grafana.entrypoints=http"
|
||||
- "traefik.http.services.grafana.loadbalancer.server.port=3000"
|
||||
- "traefik.http.routers.grafana.service=grafana"
|
||||
- 'traefik.enable=true'
|
||||
- 'traefik.docker.network=sentry_traefik'
|
||||
- 'traefik.http.routers.grafana.rule=Host(`grafana.docker.localhost`)'
|
||||
- 'traefik.http.routers.grafana.entrypoints=http'
|
||||
- 'traefik.http.services.grafana.loadbalancer.server.port=3000'
|
||||
- 'traefik.http.routers.grafana.service=grafana'
|
||||
networks:
|
||||
- traefik
|
||||
- database
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"]
|
||||
test:
|
||||
[
|
||||
'CMD-SHELL',
|
||||
'wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1',
|
||||
]
|
||||
interval: 10s
|
||||
timeout: 30s
|
||||
retries: 5
|
||||
|
@ -4,10 +4,10 @@
|
||||
import '../css/app.css'
|
||||
import { createSSRApp, h } from 'vue'
|
||||
import type { DefineComponent } from 'vue'
|
||||
import { createInertiaApp } from '@inertiajs/vue3'
|
||||
import { createInertiaApp, Link } from '@inertiajs/vue3'
|
||||
import { resolvePageComponent } from '@adonisjs/inertia/helpers'
|
||||
|
||||
const appName = import.meta.env.VITE_APP_NAME || 'AdonisJS'
|
||||
Vue.component('inertia-link', Link)
|
||||
|
||||
createInertiaApp({
|
||||
progress: { color: '#5468FF' },
|
||||
|
@ -9,7 +9,6 @@
|
||||
<th class="p-2">Email</th>
|
||||
<th class="p-2">Date</th>
|
||||
<th class="p-2">Location</th>
|
||||
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -17,13 +16,22 @@
|
||||
<td class="p-2">{{ replay.id }}</td>
|
||||
<td class="p-2">{{ replay.user.email ?? replay.user.display_name }}</td>
|
||||
<td class="p-2">{{ replay.finished_at }}</td>
|
||||
<td class="p-2">{{ replay.user.geo ? `${replay.user.geo.city} ${replay.user.geo.subdivision}, ${replay.user.geo.region}` : 'unknown' }}</td>
|
||||
<td class="p-2">
|
||||
{{
|
||||
replay.user.geo
|
||||
? `${replay.user.geo.city} ${replay.user.geo.subdivision}, ${replay.user.geo.region}`
|
||||
: 'unknown'
|
||||
}}
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="mt-4 flex flex-wrap items-center gap-2" v-if="data.meta && data.meta.links && data.meta.links.length > 1">
|
||||
<div
|
||||
class="mt-4 flex flex-wrap items-center gap-2"
|
||||
v-if="data.meta && data.meta.links && data.meta.links.length > 1"
|
||||
>
|
||||
<!-- First -->
|
||||
<Link
|
||||
v-if="firstPageUrl && !isFirstPage"
|
||||
@ -34,11 +42,7 @@
|
||||
</Link>
|
||||
|
||||
<!-- Previous -->
|
||||
<Link
|
||||
v-if="prevPageUrl"
|
||||
:href="prevPageUrl"
|
||||
class="px-3 py-1 border rounded text-sm"
|
||||
>
|
||||
<Link v-if="prevPageUrl" :href="prevPageUrl" class="px-3 py-1 border rounded text-sm">
|
||||
‹ Prev
|
||||
</Link>
|
||||
|
||||
@ -50,7 +54,7 @@
|
||||
class="px-3 py-1 border rounded text-sm"
|
||||
:class="{
|
||||
'font-bold bg-gray-300': link.active,
|
||||
'text-gray-400 cursor-not-allowed': !link.url
|
||||
'text-gray-400 cursor-not-allowed': !link.url,
|
||||
}"
|
||||
>
|
||||
<span v-html="link.label" />
|
||||
@ -58,11 +62,7 @@
|
||||
</template>
|
||||
|
||||
<!-- Next -->
|
||||
<Link
|
||||
v-if="nextPageUrl"
|
||||
:href="nextPageUrl"
|
||||
class="px-3 py-1 border rounded text-sm"
|
||||
>
|
||||
<Link v-if="nextPageUrl" :href="nextPageUrl" class="px-3 py-1 border rounded text-sm">
|
||||
Next ›
|
||||
</Link>
|
||||
|
||||
@ -83,12 +83,12 @@ import { computed } from 'vue'
|
||||
import { Link } from '@inertiajs/vue3'
|
||||
|
||||
const props = defineProps({
|
||||
data: Object
|
||||
data: Object,
|
||||
})
|
||||
|
||||
// Core pagination values
|
||||
const links = computed(() => props.data.meta.links || [])
|
||||
const currentIndex = computed(() => links.value.findIndex(link => link.active))
|
||||
const currentIndex = computed(() => links.value.findIndex((link) => link.active))
|
||||
|
||||
const maxVisible = 10
|
||||
const half = Math.floor(maxVisible / 2)
|
||||
@ -115,5 +115,7 @@ const nextPageUrl = computed(() => links.value[currentIndex.value + 1]?.url)
|
||||
const lastPageUrl = computed(() => links.value[links.value.length - 2]?.url) // last item is "Next »", second-last is last numbered
|
||||
|
||||
const isFirstPage = computed(() => links.value[currentIndex.value]?.label === '1')
|
||||
const isLastPage = computed(() => links.value[currentIndex.value]?.label === props.data.meta.last_page)
|
||||
const isLastPage = computed(
|
||||
() => links.value[currentIndex.value]?.label === props.data.meta.last_page
|
||||
)
|
||||
</script>
|
||||
|
@ -34,5 +34,5 @@ export default await Env.create(new URL('../', import.meta.url), {
|
||||
PG_USER: Env.schema.string(),
|
||||
PG_PASSWORD: Env.schema.string(),
|
||||
|
||||
WEBHOOK_URL: Env.schema.string()
|
||||
WEBHOOK_URL: Env.schema.string(),
|
||||
})
|
||||
|
@ -11,6 +11,5 @@ import ReplaysController from '#controllers/replays_controller'
|
||||
import router from '@adonisjs/core/services/router'
|
||||
router.on('/').renderInertia('home')
|
||||
router.get('/replays', [ReplaysController, 'index'])
|
||||
router.get('/list', [ReplaysController, 'list'
|
||||
])
|
||||
router.get('/list', [ReplaysController, 'list'])
|
||||
router.get('/stats', [ReplaysController, 'stats'])
|
Reference in New Issue
Block a user