schema.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { bigint, char, datetime, decimal, index, int, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
  2. export const modelStat = mysqlTable(
  3. "model_stat",
  4. {
  5. ...periodColumns(),
  6. provider: varchar({ length: 128 }).notNull(),
  7. model: varchar({ length: 256 }).notNull(),
  8. provider_model: varchar({ length: 256 }).notNull().default(""),
  9. ...metricColumns(),
  10. rank_by_tokens: int(),
  11. rank_by_requests: int(),
  12. rank_by_cost: int(),
  13. ...timestampColumns(),
  14. },
  15. (table) => [
  16. uniqueIndex("uniq_model_period").on(
  17. table.grain,
  18. table.period_start,
  19. table.dataset,
  20. table.tier,
  21. table.client,
  22. table.source,
  23. table.provider,
  24. table.model,
  25. ),
  26. index("idx_leaderboard_tokens").on(table.grain, table.period_start, table.dataset, table.tier, table.total_tokens),
  27. index("idx_model").on(table.model, table.grain, table.period_start),
  28. ],
  29. )
  30. export const providerStat = mysqlTable(
  31. "provider_stat",
  32. {
  33. ...periodColumns(),
  34. provider: varchar({ length: 128 }).notNull(),
  35. ...metricColumns(),
  36. ...marketShareColumns(),
  37. rank_by_tokens: int(),
  38. rank_by_requests: int(),
  39. rank_by_sessions: int(),
  40. rank_by_cost: int(),
  41. ...timestampColumns(),
  42. },
  43. (table) => [
  44. uniqueIndex("uniq_provider_period").on(
  45. table.grain,
  46. table.period_start,
  47. table.dataset,
  48. table.tier,
  49. table.client,
  50. table.source,
  51. table.provider,
  52. ),
  53. index("idx_provider_leaderboard_tokens").on(
  54. table.grain,
  55. table.period_start,
  56. table.dataset,
  57. table.tier,
  58. table.total_tokens,
  59. ),
  60. index("idx_provider_market_share").on(
  61. table.grain,
  62. table.period_start,
  63. table.dataset,
  64. table.tier,
  65. table.market_share_tokens,
  66. ),
  67. index("idx_provider_rank").on(table.grain, table.period_start, table.dataset, table.tier, table.rank_by_tokens),
  68. index("idx_provider").on(table.provider, table.grain, table.period_start),
  69. ],
  70. )
  71. export const geoStat = mysqlTable(
  72. "geo_stat",
  73. {
  74. ...periodColumns(),
  75. country: char({ length: 2 }).notNull(),
  76. continent: varchar({ length: 8 }).notNull().default(""),
  77. ...metricColumns(),
  78. ...marketShareColumns(),
  79. rank_by_tokens: int(),
  80. rank_by_requests: int(),
  81. rank_by_sessions: int(),
  82. rank_by_cost: int(),
  83. ...timestampColumns(),
  84. },
  85. (table) => [
  86. uniqueIndex("uniq_country_period").on(
  87. table.grain,
  88. table.period_start,
  89. table.dataset,
  90. table.tier,
  91. table.client,
  92. table.source,
  93. table.country,
  94. ),
  95. index("idx_country_map_tokens").on(table.grain, table.period_start, table.dataset, table.tier, table.total_tokens),
  96. index("idx_country_rank").on(table.grain, table.period_start, table.dataset, table.tier, table.rank_by_tokens),
  97. index("idx_country").on(table.country, table.grain, table.period_start),
  98. index("idx_continent").on(table.continent, table.grain, table.period_start),
  99. ],
  100. )
  101. function periodColumns() {
  102. return {
  103. id: bigint({ mode: "number" }).autoincrement().primaryKey(),
  104. grain: varchar({ length: 16 }).notNull(),
  105. period_start: datetime({ mode: "date" }).notNull(),
  106. period_end: datetime({ mode: "date" }).notNull(),
  107. dataset: varchar({ length: 64 }).notNull().default("all"),
  108. tier: varchar({ length: 64 }).notNull().default("all"),
  109. client: varchar({ length: 64 }).notNull().default("all"),
  110. source: varchar({ length: 64 }).notNull().default("all"),
  111. }
  112. }
  113. function metricColumns() {
  114. return {
  115. sessions: bigint({ mode: "number" }).notNull().default(0),
  116. requests: bigint({ mode: "number" }).notNull().default(0),
  117. input_tokens: bigint({ mode: "number" }).notNull().default(0),
  118. output_tokens: bigint({ mode: "number" }).notNull().default(0),
  119. reasoning_tokens: bigint({ mode: "number" }).notNull().default(0),
  120. cache_read_tokens: bigint({ mode: "number" }).notNull().default(0),
  121. total_tokens: bigint({ mode: "number" }).notNull().default(0),
  122. input_cost_microcents: bigint({ mode: "number" }).notNull().default(0),
  123. output_cost_microcents: bigint({ mode: "number" }).notNull().default(0),
  124. total_cost_microcents: bigint({ mode: "number" }).notNull().default(0),
  125. avg_duration_ms: decimal({ precision: 12, scale: 2, mode: "number" }),
  126. p50_duration_ms: int(),
  127. p95_duration_ms: int(),
  128. avg_ttfb_ms: decimal({ precision: 12, scale: 2, mode: "number" }),
  129. p50_ttfb_ms: int(),
  130. p95_ttfb_ms: int(),
  131. avg_output_tps: decimal({ precision: 12, scale: 4, mode: "number" }),
  132. success_count: bigint({ mode: "number" }).notNull().default(0),
  133. error_count: bigint({ mode: "number" }).notNull().default(0),
  134. sample_count: bigint({ mode: "number" }).notNull().default(0),
  135. }
  136. }
  137. function marketShareColumns() {
  138. return {
  139. market_share_tokens: decimal({ precision: 10, scale: 6, mode: "number" }),
  140. market_share_requests: decimal({ precision: 10, scale: 6, mode: "number" }),
  141. market_share_sessions: decimal({ precision: 10, scale: 6, mode: "number" }),
  142. }
  143. }
  144. function timestampColumns() {
  145. return {
  146. created_at: datetime({ mode: "date" }).notNull().defaultNow(),
  147. updated_at: datetime({ mode: "date" }).notNull().defaultNow().onUpdateNow(),
  148. }
  149. }