This commit is contained in:
Emily
2024-11-14 18:23:56 +01:00
parent 9de299d841
commit 5ac43dec6b
5 changed files with 57 additions and 455 deletions

View File

@@ -1,157 +0,0 @@
<script lang="ts" setup>
import { sub, isSameDay, type Duration } from 'date-fns'
type ChartType = 'bar' | 'line';
const chartTypeOptions: { value: ChartType, label: string }[] = [
{ value: 'bar', label: 'Bar chart' },
{ value: 'line', label: 'Line chart' },
]
type yAxisMode = 'count';
const yAxisModeOptions: { value: yAxisMode, label: string }[] = [
{ value: 'count', label: 'Count fields' },
]
type Slice = 'day' | 'month';
const sliceOptions: Slice[] = ['day', 'month'];
const chartType = ref<ChartType>('line');
const tableName = ref<string>('');
const xAxis = ref<string>('');
const yAxisMode = ref<yAxisMode>('count');
const slice = ref<Slice>('day');
const visualizationName = ref<string>('');
const ranges = [
{ label: 'Last 7 days', duration: { days: 7 } },
{ label: 'Last 14 days', duration: { days: 14 } },
{ label: 'Last 30 days', duration: { days: 30 } },
{ label: 'Last 3 months', duration: { months: 3 } },
{ label: 'Last 6 months', duration: { months: 6 } },
{ label: 'Last year', duration: { years: 1 } }
]
const timeframe = ref<{ start: Date, end: Date }>({ start: sub(new Date(), { days: 14 }), end: new Date() })
function isRangeSelected(duration: Duration) {
return isSameDay(timeframe.value.start, sub(new Date(), duration)) && isSameDay(timeframe.value.end, new Date())
}
function selectRange(duration: Duration) {
timeframe.value = { start: sub(new Date(), duration), end: new Date() }
}
const { createAlert } = useAlert();
const { closeDialog } = useCustomDialog();
const activeProjectId = useActiveProjectId();
const { integrationsCredentials,testConnection } = useSupabase();
async function generate() {
const credentials = integrationsCredentials.data.value;
if (!credentials?.supabase) return createAlert('Credentials not found', 'Please add supabase credentials on the integration page', 'far fa-error', 5000);
const connectionStatus = await testConnection();
if (!connectionStatus) return createAlert('Invalid supabase credentials', 'Please check your supabase credentials on the integration page', 'far fa-error', 5000);
try {
const creation = await $fetch('/api/integrations/supabase/add', {
...signHeaders({
'x-pid': activeProjectId.data.value || '',
'Content-Type': 'application/json'
}),
method: 'POST',
body: JSON.stringify({
name: visualizationName.value,
chart_type: chartType.value,
table_name: tableName.value,
xField: xAxis.value,
yMode: yAxisMode.value,
from: timeframe.value.start,
to: timeframe.value.end,
slice: slice.value
})
})
createAlert('Integration generated', 'Integration generated successfully', 'far fa-check-circle', 5000);
closeDialog();
} catch (ex: any) {
createAlert('Error generating integrations', ex.response._data.message.toString(), 'far fa-error', 5000);
}
}
</script>
<template>
<div class="flex flex-col gap-4">
<div>
<div> Visualization name </div>
<div>
<LyxUiInput class="w-full px-2 py-1" v-model="visualizationName"></LyxUiInput>
</div>
</div>
<div>
<div> Chart type </div>
<USelect v-model="chartType" :options="chartTypeOptions" />
</div>
<div>
<div> Table name </div>
<div>
<LyxUiInput class="w-full px-2 py-1" v-model="tableName"></LyxUiInput>
</div>
</div>
<div>
<div> X axis field </div>
<div>
<LyxUiInput class="w-full px-2 py-1" v-model="xAxis"></LyxUiInput>
</div>
</div>
<div>
<div> Y axis mode </div>
<div>
<USelect v-model="yAxisMode" :options="yAxisModeOptions" />
</div>
</div>
<div>
<div> Timeframe </div>
<div>
<UPopover class="w-full" :popper="{ placement: 'bottom' }">
<UButton class="w-full" color="primary" variant="solid">
<div class="flex items-center justify-center w-full gap-2">
<i class="i-heroicons-calendar-days-20-solid"></i>
{{ timeframe.start.toLocaleDateString() }} - {{ timeframe.end.toLocaleDateString() }}
</div>
</UButton>
<template #panel="{ close }">
<div class="flex items-center sm:divide-x divide-gray-200 dark:divide-gray-800">
<div class="hidden sm:flex flex-col py-4">
<UButton v-for="(range, index) in ranges" :key="index" :label="range.label" color="gray"
variant="ghost" class="rounded-none px-6"
:class="[isRangeSelected(range.duration) ? 'bg-gray-100 dark:bg-gray-800' : 'hover:bg-gray-50 dark:hover:bg-gray-800/50']"
truncate @click="selectRange(range.duration)" />
</div>
<DatePicker v-model="timeframe" @close="close" />
</div>
</template>
</UPopover>
</div>
</div>
<div>
<div> View mode </div>
<div>
<USelect v-model="slice" :options="sliceOptions" />
</div>
</div>
<LyxUiButton type="primary" @click="generate()">
Generate
</LyxUiButton>
</div>
</template>

View File

@@ -1,170 +0,0 @@
<script setup lang="ts">
import type { TSupabaseIntegration } from '@schema/integrations/SupabaseIntegrationSchema';
import type { ChartData, ChartOptions } from 'chart.js';
import { useLineChart, LineChart } from 'vue-chart-3';
const props = defineProps<{ integration_id: string }>();
const activeProjectId = useActiveProjectId();
const supabaseData = ref<{ labels: string[], data: number[] }>();
const supabaseError = ref<string | undefined>(undefined);
const supabaseFetching = ref<boolean>(false);
const { getRemoteData } = useSupabase();
function createGradient() {
const c = document.createElement('canvas');
const ctx = c.getContext("2d");
let gradient: any = `#34B67C22`;
if (ctx) {
gradient = ctx.createLinearGradient(0, 25, 0, 300);
gradient.addColorStop(0, `#34B67C99`);
gradient.addColorStop(0.35, `#34B67C66`);
gradient.addColorStop(1, `#34B67C22`);
} else {
console.warn('Cannot get context for gradient');
}
chartData.value.datasets[0].backgroundColor = [gradient];
}
const chartOptions = ref<ChartOptions<'line'>>({
responsive: true,
maintainAspectRatio: false,
interaction: {
intersect: false,
mode: 'nearest',
axis: 'x',
includeInvisible: true
},
scales: {
y: {
ticks: { display: true },
grid: {
display: true,
drawBorder: false,
color: '#CCCCCC22',
// borderDash: [5, 10]
},
},
x: {
ticks: { display: true },
grid: {
display: true,
drawBorder: false,
color: '#CCCCCC22',
}
}
},
plugins: {
legend: { display: false },
title: { display: false },
tooltip: {
enabled: true,
backgroundColor: 'rgba(0, 0, 0, 0.8)',
titleFont: { size: 16, weight: 'bold' },
bodyFont: { size: 14 },
padding: 10,
cornerRadius: 4,
boxPadding: 10,
caretPadding: 20,
yAlign: 'bottom',
xAlign: 'center',
}
},
});
const chartData = ref<ChartData<'line'>>({
labels: [],
datasets: [
{
data: [],
backgroundColor: ['#34B67C' + '77'],
borderColor: '#34B67C',
borderWidth: 4,
fill: true,
tension: 0.45,
pointRadius: 0,
pointHoverRadius: 10,
hoverBackgroundColor: '#34B67C',
hoverBorderColor: 'white',
hoverBorderWidth: 2,
},
],
});
onMounted(async () => {
supabaseFetching.value = true;
supabaseError.value = undefined;
const integrationData = await $fetch<TSupabaseIntegration>('/api/integrations/supabase/get', {
...signHeaders({
'x-pid': activeProjectId.data.value || '',
'x-integration': props.integration_id
})
});
if (!integrationData) {
supabaseError.value = 'Cannot get integration data';
supabaseFetching.value = false;
return;
}
try {
const data = await getRemoteData(
integrationData.table_name,
integrationData.xField,
integrationData.yMode,
integrationData.from.toString(),
integrationData.to.toString(),
integrationData.slice,
);
if (data.error) {
supabaseError.value = data.error;
supabaseFetching.value = false;
return;
}
supabaseFetching.value = false;
supabaseData.value = data.result;
chartData.value.labels = data.result?.labels || [];
chartData.value.datasets[0].data = data.result?.data || [];
console.log(data.result);
createGradient();
} catch (ex: any) {
if (!ex.response._data) {
supabaseError.value = ex.message.toString();
supabaseFetching.value = false;
} else {
supabaseError.value = ex.response._data.message.toString();
supabaseFetching.value = false;
}
}
});
const { lineChartProps, lineChartRef } = useLineChart({ chartData: chartData, options: chartOptions });
</script>
<template>
<div v-if="!supabaseFetching">
<div v-if="!supabaseError">
<LineChart ref="lineChartRef" v-bind="lineChartProps"> </LineChart>
</div>
<div v-if="supabaseError"> {{ supabaseError }} </div>
</div>
<div v-if="supabaseFetching">
Getting remote data...
</div>
</template>

View File

@@ -1,5 +1,6 @@
import type { TProjectSnapshot } from "@schema/ProjectSnapshot"; import type { TProjectSnapshot } from "@schema/ProjectSnapshot";
import fns from 'date-fns';
const { projectId, project } = useProject(); const { projectId, project } = useProject();
@@ -31,9 +32,10 @@ const snapshots = computed(() => {
}, },
{ {
project_id: project.value?._id as any, project_id: project.value?._id as any,
_id: 'default1' as any, _id: 'current_month' as any,
name: 'Last month', name: 'Current month',
from: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30), from: fns.startOfMonth()
new Date(Date.now() - 1000 * 60 * 60 * 24 * 30),
to: new Date(Date.now()), to: new Date(Date.now()),
color: '#00CC00' color: '#00CC00'
}, },

View File

@@ -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<Record<string, string>>(() => {
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<string>('');
const supabaseAnonKey = ref<string>('');
const supabaseServiceRoleKey = ref<string>('');
const supabaseIntegrations = useFetch<TSupabaseIntegration[]>('/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<string, string[]>);
}
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
}
}

View File

@@ -1,5 +1,57 @@
import type { MetricsTimeline } from "~/server/api/metrics/[project_id]/timeline/generic"; 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 = { export const slicesData = {
hour: { hour: {
fromOffset: 1000 * 60 * 60 * 24 fromOffset: 1000 * 60 * 60 * 24