| 1 | import { TABLE_NAMES, chQuery } from '@openpanel/db'; |
| 2 | |
| 3 | export async function ping() { |
| 4 | if (process.env.DISABLE_PING) { |
| 5 | return; |
| 6 | } |
| 7 | |
| 8 | const [res] = await chQuery<{ count: number }>( |
| 9 | `SELECT COUNT(*) as count FROM ${TABLE_NAMES.events}`, |
| 10 | ); |
| 11 | |
| 12 | if (typeof res?.count === 'number') { |
| 13 | const response = await fetch('https://api.openpanel.dev/misc/ping', { |
| 14 | method: 'POST', |
| 15 | headers: { |
| 16 | 'Content-Type': 'application/json', |
| 17 | }, |
| 18 | body: JSON.stringify({ |
| 19 | domain: |
| 20 | process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL, |
| 21 | count: res?.count, |
| 22 | }), |
| 23 | }); |
| 24 | |
| 25 | if (response.ok) { |
| 26 | return await response.json(); |
| 27 | } |
| 28 | |
| 29 | throw new Error('Failed to ping the server'); |
| 30 | } |
| 31 | } |