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