| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, count |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { meter } |
| 7 | |
| 8 | /** Instance totals, read once per metric collection so they can be plotted over time. Counts, not histories: a repository deleted today lowers the line. */ |
| 9 | @Injectable() |
| 10 | export class AppStatsService |
| 11 | constructor(@Inject( |
| 12 | |
| 13 | onModuleInit(): void |
| 14 | const repositories = |
| 15 | description: 'Repositories on this instance.', |
| 16 | }); |
| 17 | const commits = |
| 18 | description: 'Commits indexed on default branches.', |
| 19 | }); |
| 20 | const users = |
| 21 | description: 'Registered accounts.', |
| 22 | }); |
| 23 | const issues = |
| 24 | description: 'Issues, by state.', |
| 25 | }); |
| 26 | const pullRequests = |
| 27 | description: 'Pull requests, by state.', |
| 28 | }); |
| 29 | const stars = |
| 30 | description: 'Stars across every repository.', |
| 31 | }); |
| 32 | |
| 33 | meter.addBatchObservableCallback( |
| 34 | async (result) |
| 35 | const [ |
| 36 | repositoryRows, |
| 37 | [commitRow], |
| 38 | [userRow], |
| 39 | issueRows, |
| 40 | pullRequestRows, |
| 41 | [starRow], |
| 42 | ] = await Promise |
| 43 | this.db |
| 44 | .select({ |
| 45 | visibility: schema.repository |
| 46 | total: count(), |
| 47 | }) |
| 48 | .from(schema.repository |
| 49 | .groupBy(schema.repository |
| 50 | // ponytail: a full scan of the contribution index. One aggregate a minute; give it a rollup table if that stops being cheap. |
| 51 | this.db |
| 52 | .select({ total: |
| 53 | .from(schema.repositoryContribution |
| 54 | this.db.select |
| 55 | this.db |
| 56 | .select({ state: |
| 57 | .from(schema.issue |
| 58 | .where(eq(schema |
| 59 | .groupBy(schema.issue |
| 60 | this.db |
| 61 | .select({ state: |
| 62 | .from(schema.pullRequest |
| 63 | .groupBy(schema.pullRequest |
| 64 | this.db.select |
| 65 | ]); |
| 66 | |
| 67 | for (const row |
| 68 | result.observe(repositories |
| 69 | visibility: row.visibility |
| 70 | }); |
| 71 | } |
| 72 | for (const row |
| 73 | result.observe(issues |
| 74 | } |
| 75 | for (const row |
| 76 | result.observe(pullRequests |
| 77 | } |
| 78 | |
| 79 | result.observe(commits |
| 80 | result.observe(users |
| 81 | result.observe(stars |
| 82 | }, |
| 83 | [repositories, commits, |
| 84 | ); |
| 85 | |
| 86 | this.observeDelivery(); |
| 87 | } |
| 88 | |
| 89 | /** apps/delivery has no metrics of its own; its tables say how it is doing. */ |
| 90 | private observeDelivery() { |
| 91 | const jobs = |
| 92 | description: |
| 93 | 'Delivery jobs, by kind and status. Finished ones are pruned after 30 days.', |
| 94 | }); |
| 95 | const lag = |
| 96 | unit: 's', |
| 97 | description: |
| 98 | 'How overdue the most overdue pending job is: near zero while apps/delivery keeps up.', |
| 99 | }); |
| 100 | const attempts = |
| 101 | description: 'Webhook attempts in the last five minutes, by outcome.', |
| 102 | }); |
| 103 | const p99 = |
| 104 | unit: 'ms', |
| 105 | description: 'p99 webhook attempt duration over the last five minutes.', |
| 106 | }); |
| 107 | |
| 108 | meter.addBatchObservableCallback( |
| 109 | async (result) |
| 110 | const [jobRows, |
| 111 | this.db |
| 112 | .select({ |
| 113 | kind: schema.deliveryJob |
| 114 | status: schema.deliveryJob |
| 115 | total: count(), |
| 116 | }) |
| 117 | .from(schema.deliveryJob |
| 118 | .groupBy(schema.deliveryJob |
| 119 | this.db |
| 120 | .select({ |
| 121 | seconds: sql<number |
| 122 | }) |
| 123 | .from(schema.deliveryJob |
| 124 | .where( |
| 125 | and( |
| 126 | eq(schema.deliveryJob |
| 127 | lte(schema.deliveryJob |
| 128 | ), |
| 129 | ), |
| 130 | this.db |
| 131 | .select({ |
| 132 | succeeded: sql<number |
| 133 | failed: sql<number |
| 134 | p99: sql<number |
| 135 | }) |
| 136 | .from(schema.deliveryAttempt |
| 137 | .where( |
| 138 | gt( |
| 139 | schema.deliveryAttempt.startedAt |
| 140 | sql`now() - interval '5 minutes'`, |
| 141 | ), |
| 142 | ), |
| 143 | ]); |
| 144 | |
| 145 | for (const row |
| 146 | result.observe(jobs |
| 147 | kind: row.kind |
| 148 | status: row.status |
| 149 | }); |
| 150 | } |
| 151 | result.observe(lag |
| 152 | result.observe(attempts |
| 153 | outcome: 'succeeded', |
| 154 | }); |
| 155 | result.observe(attempts |
| 156 | outcome: 'failed', |
| 157 | }); |
| 158 | result.observe(p99 |
| 159 | }, |
| 160 | [jobs, lag, |
| 161 | ); |
| 162 | } |
| 163 | } |