userContent.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import type { UntrustedSqlFragment } from '@supabase/pg-meta'
  2. import { ChartConfig } from '@/components/interfaces/SQLEditor/UtilityPanel/ChartConfig'
  3. export interface UserContent<
  4. T = Dashboards.Content | SqlSnippets.Content | LogSqlSnippets.Content,
  5. > {
  6. id?: string
  7. name: string
  8. description?: string
  9. type: 'sql' | 'report' | 'log_sql'
  10. visibility: 'user' | 'project' | 'org' | 'public'
  11. content: T
  12. owner_id?: number // user id
  13. last_updated_by?: number // user id
  14. inserted_at?: string // '2021-08-26T08:24:52.040695+00:00'
  15. owner?: Owner
  16. project_id?: number
  17. updated_at?: string // '2021-08-26T08:24:52.040695+00:00'
  18. updated_by?: Owner
  19. favorite?: boolean
  20. }
  21. export interface UserContentMap {
  22. [key: string]: UserContent
  23. }
  24. export namespace SqlSnippets {
  25. /**
  26. * To be stored in the database: public.user_content.content
  27. * In this case there is only one thing to store, but it's good to
  28. * nest it in an object for future expansion.
  29. */
  30. export interface Content {
  31. // unique id of the sql snippet, possibly to used so snippets can support versioning
  32. content_id: string
  33. // A full SQL query - this will be hashed on the /content endpoint
  34. // Named unchecked_sql to highlight that this SQL must never be run automatically
  35. // without user confirmation — it may originate from untrusted sources like URL params.
  36. unchecked_sql: UntrustedSqlFragment
  37. // we can add some versioning to this schema in case we need to change the format.
  38. schema_version: string
  39. chart?: {
  40. type: 'bar' | 'line'
  41. cumulative: boolean
  42. xKey: string
  43. yKey: string
  44. showLabels?: boolean
  45. showGrid?: boolean
  46. }
  47. }
  48. }
  49. export interface Owner {
  50. id: number
  51. username: string
  52. }
  53. /**
  54. * User generated content: Dashboards
  55. */
  56. export namespace Dashboards {
  57. /**
  58. * To be stored in the database: public.user_content.content
  59. * Time periods are considered as historical dates and can be provided as
  60. * an interval ("1w" = 1 week ago) or an exact date.
  61. */
  62. export interface Content {
  63. schema_version: 1 // we can add some versioning to this schema in case we need to change the format.
  64. period_start: {
  65. time_period?: string // "0m", "1m", "5m", "1h", "1d", "1w", "1M", "1y"
  66. date?: string // "2017-01-01T00:00:00.000Z"
  67. }
  68. period_end: {
  69. time_period?: string // "0m", "1m", "5m", "1h", "1d", "1w", "1M", "1y"
  70. date?: string // "2017-01-01T00:00:00.000Z"
  71. }
  72. interval: '1m' | '5m' | '1h' | '1d' | '1w' | '1M' | '1y' // this is the data interval
  73. layout: Chart[]
  74. }
  75. /**
  76. * Predefined charts
  77. */
  78. export type ChartType =
  79. | 'total_get_requests'
  80. | 'total_auth_patch_requests'
  81. | 'total_auth_requests'
  82. | 'total_egress'
  83. | 'total_delete_requests'
  84. | 'total_auth_requests'
  85. | 'combined_bar'
  86. // | 'bar'- should we include simple types as well?
  87. // | 'line'
  88. /**
  89. * An individual instance of a chart, with title and position
  90. */
  91. export interface Chart {
  92. id: string // uuid
  93. x: number
  94. y: number
  95. w: number
  96. h: number
  97. label: string
  98. attribute: ChartType
  99. provider: 'daily-stats' | 'infra-monitoring'
  100. chart_type: 'bar' | 'line'
  101. chartConfig?: Partial<ChartConfig>
  102. }
  103. }
  104. export namespace LogSqlSnippets {
  105. /**
  106. * To be stored in the database: public.user_content.content
  107. * In this case there is only one thing to store, but it's good to
  108. * nest it in an object for future expansion.
  109. */
  110. export interface Content {
  111. // unique id of the sql snippet, possibly to used so snippets can support versioning
  112. content_id: string
  113. // A full SQL query - this will be hashed on the /content endpoint
  114. sql: string
  115. // we can add some versioning to this schema in case we need to change the format.
  116. schema_version: string
  117. }
  118. }