From 5ac43dec6bc4821994539072915b4133cd9b5ca5 Mon Sep 17 00:00:00 2001 From: Emily Date: Thu, 14 Nov 2024 18:23:56 +0100 Subject: [PATCH] . --- .../integrations/SupabaseChartDialog.vue | 157 ---------------- .../integrations/SupabaseLineChart.vue | 170 ------------------ dashboard/composables/useSnapshot.ts | 8 +- dashboard/composables/useSupabase.ts | 125 ------------- dashboard/utils/DateUtils.ts | 52 ++++++ 5 files changed, 57 insertions(+), 455 deletions(-) delete mode 100644 dashboard/components/integrations/SupabaseChartDialog.vue delete mode 100644 dashboard/components/integrations/SupabaseLineChart.vue delete mode 100644 dashboard/composables/useSupabase.ts diff --git a/dashboard/components/integrations/SupabaseChartDialog.vue b/dashboard/components/integrations/SupabaseChartDialog.vue deleted file mode 100644 index e006e41..0000000 --- a/dashboard/components/integrations/SupabaseChartDialog.vue +++ /dev/null @@ -1,157 +0,0 @@ - - - \ No newline at end of file diff --git a/dashboard/components/integrations/SupabaseLineChart.vue b/dashboard/components/integrations/SupabaseLineChart.vue deleted file mode 100644 index 3fed455..0000000 --- a/dashboard/components/integrations/SupabaseLineChart.vue +++ /dev/null @@ -1,170 +0,0 @@ - - - - diff --git a/dashboard/composables/useSnapshot.ts b/dashboard/composables/useSnapshot.ts index b84c236..c308141 100644 --- a/dashboard/composables/useSnapshot.ts +++ b/dashboard/composables/useSnapshot.ts @@ -1,5 +1,6 @@ import type { TProjectSnapshot } from "@schema/ProjectSnapshot"; +import fns from 'date-fns'; const { projectId, project } = useProject(); @@ -31,9 +32,10 @@ const snapshots = computed(() => { }, { project_id: project.value?._id as any, - _id: 'default1' as any, - name: 'Last month', - from: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30), + _id: 'current_month' as any, + name: 'Current month', + from: fns.startOfMonth() + new Date(Date.now() - 1000 * 60 * 60 * 24 * 30), to: new Date(Date.now()), color: '#00CC00' }, diff --git a/dashboard/composables/useSupabase.ts b/dashboard/composables/useSupabase.ts deleted file mode 100644 index 2f977f7..0000000 --- a/dashboard/composables/useSupabase.ts +++ /dev/null @@ -1,125 +0,0 @@ - - -import type { TSupabaseIntegration } from "@schema/integrations/SupabaseIntegrationSchema"; -import { createClient, SupabaseClient } from "@supabase/supabase-js"; - -import { format } from 'date-fns'; - -const activeProjectId = useActiveProjectId(); - - -const computedHeaders = computed>(() => { - const signedHeaders = signHeaders(); - return { - 'x-pid': activeProjectId.data.value || '', - 'Authorization': signedHeaders.headers.Authorization - } -}) - -const integrationsCredentials = useFetch(`/api/integrations/credentials/get`, { - headers: computedHeaders, - onResponse: (e) => { - supabaseUrl.value = e.response._data.supabase.url || ''; - supabaseAnonKey.value = e.response._data.supabase.anon_key || ''; - supabaseServiceRoleKey.value = e.response._data.supabase.service_role_key || ''; - } -}); - -const supabaseUrl = ref(''); -const supabaseAnonKey = ref(''); -const supabaseServiceRoleKey = ref(''); - -const supabaseIntegrations = useFetch('/api/integrations/supabase/list', { headers: computedHeaders }) - - -const subabaseClientData: { client: SupabaseClient | undefined } = { - client: undefined -} - -async function updateIntegrationsCredentails(data: { supabase_url: string, supabase_anon_key: string, supabase_service_role_key: string }) { - try { - await $fetch(`/api/integrations/credentials/${activeProjectId.data.value}/update`, { - ...signHeaders({ 'Content-Type': 'application/json' }), - method: 'POST', - body: JSON.stringify({ - supabase_url: data.supabase_url, - supabase_anon_key: data.supabase_anon_key, - supabase_service_role_key: data.supabase_service_role_key - }), - }); - integrationsCredentials.refresh(); - return { ok: true, error: '' } - } catch (ex: any) { - return { ok: false, error: ex.message.toString() }; - } - -} - -function createSupabaseUrl(supabaseUrl: string) { - let result = supabaseUrl; - if (!result.includes('https://')) result = `https://${result}`; - if (!result.endsWith('.supabase.co')) result = `${result}.supabase.co`; - return result; -} - - -async function testConnection() { - const url = createSupabaseUrl(supabaseUrl.value); - subabaseClientData.client = createClient(url, supabaseAnonKey.value); - const res = await subabaseClientData.client.from('_t_e_s_t_').select('*').limit(1); - if (res.error?.message.startsWith('TypeError')) return false; - return true; -} - - - -type GroupBy = 'day' | 'month'; - -const groupByDate = (data: string[], groupBy: GroupBy) => { - return data.reduce((acc, item) => { - const date = new Date(item); - const dateKey = groupBy === 'day' - ? format(date, 'yyyy-MM-dd') // Group by day - : format(date, 'yyyy-MM'); // Group by month - - if (!acc[dateKey]) { acc[dateKey] = []; } - - acc[dateKey].push(item); - return acc; - }, {} as Record); -} - -async function getRemoteData(table: string, xField: string, yMode: string, from: string, to: string, slice: string) { - const url = createSupabaseUrl(supabaseUrl.value); - subabaseClientData.client = createClient(url, supabaseAnonKey.value); - const res = await subabaseClientData.client.from(table).select(xField) - .filter(xField, 'gte', from) - .filter(xField, 'lte', to); - - if (res.error) return { error: res.error.message }; - - const grouped = groupByDate(res.data.map((e: any) => e.created_at) || [], slice as any); - - const result: { labels: string[], data: number[] } = { labels: [], data: [] } - - for (const key in grouped) { - result.labels.push(key); - result.data.push(grouped[key].length); - } - - - return { result }; -} - -export function useSupabase() { - - return { - getRemoteData, - testConnection, - supabaseIntegrations, integrationsCredentials, - supabaseUrl, supabaseAnonKey, - supabaseServiceRoleKey, - updateIntegrationsCredentails - } - -} \ No newline at end of file diff --git a/dashboard/utils/DateUtils.ts b/dashboard/utils/DateUtils.ts index a0de4db..4794ef0 100644 --- a/dashboard/utils/DateUtils.ts +++ b/dashboard/utils/DateUtils.ts @@ -1,5 +1,57 @@ import type { MetricsTimeline } from "~/server/api/metrics/[project_id]/timeline/generic"; + + +// Calcola date snapshot +// 1- Frontend +// 2- Backend +// 3- Data singola + +// 4- Aggregazione + +// ISO +// UTC UTENTE + +// Utility - per date snapshot + +// getStartDay: data => 00.00 della data +// getEndDay: data => 23.59 della data + +// getStartWeek: data => 00.00 del primo giorno +// getEndWeek: data => 23.59 dell ultimo giorno + +// getStartMonth: data => 00.00 del primo giorno del mese +// getEndMonth: data => 23.59 dell ulrimo giorno del mese + + + + +// Snapshot -> Current Week -> 11/11-00:00 - 17/11-23:59 +// Converti UTC UTENTE -> ISO + +// Backend -> Prendi dati da ISO_A a ISO_B + +// Funzioni da creare + +// UTC TO ISO +// Converte utc -> Iso +// UTC TO ISO Day + +// UTC TO ISO Month + +// UTC_IS_NEXT_DAY +// True se il giorno passa a quello successivo +// UTC_IS_PREV_DAY +// True se il giorno passa a quello precedente + + + + + + + + + export const slicesData = { hour: { fromOffset: 1000 * 60 * 60 * 24