| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { count, eq |
| 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 | } |