'use server'; import { z, ZodError } from 'zod'; import { actionClient } from '../../actions/actionClient'; import { getSDKForCurrentUser } from '../../getSDKForCurrentUser'; import { CreateScheduledJobSchema, DeleteExecutionsSchema, DeleteJobSchema, GetDailyExecutionTrendSchema, GetJobStatsSchema, GetScheduledJobSchema, GetTopFailedJobsSchema, ListAvgExecutionsSchema, ListExecutionsSchema, ListScheduledJobsSchema, QueueJobSchema, UpdateJobSchema } from './schema'; import { AvgExecution, Execution, ExecutionTrend, FailedJob, JobStats, ScheduledJob } from './types'; import { AppcondaException } from '../../client'; import { ActionResult } from '../../lib/types'; export const CreateScheduledJob = async ({ parsedInput }: { parsedInput: z.infer }): Promise<{ data?: ScheduledJob, serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.CreateScheduledJob(parsedInput) }; } catch (error) { console.error('Error in CreateScheduledJob:', error); return { serverError: error.toString() }; } }; export const ListScheduledJobs = async (parsedInput: z.infer): Promise<{ data?: ScheduledJob[], serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.ListScheduledJobs(parsedInput) }; } catch (error) { console.error('Error in ListScheduledJobs:', error); return { serverError: error.toString() }; } }; export const GetScheduledJob = async (parsedInput: z.infer): Promise<{ data?: ScheduledJob, serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.GetScheduledJob(parsedInput) }; } catch (error) { console.error('Error in GetScheduledJob:', error); return { serverError: error.toString() }; } }; export const ListExecutions = async (parsedInput: z.infer): Promise<{ data?: Execution[], serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.ListExecutions(parsedInput) }; } catch (error) { console.error('Error in ListExecutions:', error); return { serverError: error.toString() }; } }; export const ListAvgExecutions = async (parsedInput: z.infer): Promise<{ data?: AvgExecution[], serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.ListAvgExecutions(parsedInput) }; } catch (error) { console.error('Error in ListAvgExecutions:', error); return { serverError: error.toString() }; } }; export const UpdateJob = async (params: z.infer): Promise> => { try { console.log('params', params); const parsedInput = UpdateJobSchema.parse(params); const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.UpdateJob(parsedInput) }; } catch (error) { if (error instanceof AppcondaException) { return { serverError: error.message }; } if (error instanceof ZodError) { return { validationError: error.toString() }; } console.error('Error in UpdateJob:', error); return { serverError: error.toString() }; } }; export const GetDailyExecutionTrend = async (parsedInput: z.infer): Promise<{ data?: ExecutionTrend[], serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.GetDailyExecutionTrend(parsedInput) }; } catch (error) { console.error('Error in GetDailyExecutionTrend:', error); return { serverError: error.toString() }; } }; export const GetTopFailedJobs = async (parsedInput: z.infer): Promise<{ data?: FailedJob[], serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); //@ts-ignore return { data: await scheduledJob.GetTopFailedJobs(parsedInput) }; } catch (error) { console.error('Error in GetTopFailedJobs:', error); return { serverError: error.toString() }; } }; export const GetJobStats = async (parsedInput: z.infer): Promise<{ data?: JobStats, serverError?: string }> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.GetJobStats(parsedInput) }; } catch (error) { console.error('Error in GetJobStats:', error); return { serverError: error.toString() }; } }; export const DeleteJob = async (parsedInput: z.infer): Promise> => { try { const { scheduledJob } = await getSDKForCurrentUser(); await scheduledJob.DeleteJob(parsedInput); return {}; } catch (error) { console.error('Error in DeleteJob:', error); return { serverError: error.toString() }; } }; export const ListJobsWithStats = async () => { try { const jobs = await ListScheduledJobs({}); const avg = await ListAvgExecutions({}); // Merge jobs with average execution data const mergedJobs = jobs?.data?.map(job => { const jobAvg = avg?.data?.find(a => a.scheduledJobId === job.id); return { ...job, totalExecutions: jobAvg?.totalExecutions || 0, activeDays: jobAvg?.activeDays || 0, avgExecutionsPerDay: jobAvg?.avgExecutionsPerDay || 0, completed: jobAvg?.completed || 0, failed: jobAvg?.failed || 0, queued: jobAvg?.queued || 0, }; }) || []; return { data: mergedJobs }; } catch (error) { console.error('Error in ListJobsWithStats:', error); return { serverError: error.toString() }; } }; export const GetJobMetrics = async () => { try { const { scheduledJob } = await getSDKForCurrentUser(); return await scheduledJob.GetJobMetrics(); } catch (error) { console.error('Error in GetJobMetrics:', error); throw new Error(error.toString()); } }; export const QueueJob = async (parsedInput: z.infer): Promise => { try { const { scheduledJob } = await getSDKForCurrentUser(); await scheduledJob.QueueJob(parsedInput); return true; } catch (error) { if (error instanceof AppcondaException) { console.error('Error in QueueJob:', error.message); } return false; } }; export const DeleteExecutions = async ( parsedInput: z.infer ): Promise<{data?: void, serverError?: string}> => { try { const { scheduledJob } = await getSDKForCurrentUser(); return { data: await scheduledJob.DeleteExecutions(parsedInput) }; } catch (error) { console.error('Error in DeleteExecutions:', error); return { serverError: error.toString() }; } };