116 lines
2.9 KiB
TypeScript
116 lines
2.9 KiB
TypeScript
'use server';
|
|
|
|
import {
|
|
jiraAnomalyDetection,
|
|
JiraAnomaly,
|
|
AnomalyDetectionConfig,
|
|
} from '@/services/integrations/jira/anomaly-detection';
|
|
import {
|
|
JiraAnalyticsService,
|
|
JiraAnalyticsConfig,
|
|
} from '@/services/integrations/jira/analytics';
|
|
import { userPreferencesService } from '@/services/core/user-preferences';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth';
|
|
|
|
export interface AnomalyDetectionResult {
|
|
success: boolean;
|
|
data?: JiraAnomaly[];
|
|
error?: string;
|
|
}
|
|
|
|
/**
|
|
* Détecte les anomalies dans les métriques Jira actuelles
|
|
*/
|
|
export async function detectJiraAnomalies(
|
|
forceRefresh = false
|
|
): Promise<AnomalyDetectionResult> {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non authentifié' };
|
|
}
|
|
|
|
// Récupérer la config Jira
|
|
const jiraConfig = await userPreferencesService.getJiraConfig(
|
|
session.user.id
|
|
);
|
|
|
|
if (
|
|
!jiraConfig?.baseUrl ||
|
|
!jiraConfig?.email ||
|
|
!jiraConfig?.apiToken ||
|
|
!jiraConfig?.projectKey
|
|
) {
|
|
return {
|
|
success: false,
|
|
error: 'Configuration Jira incomplète',
|
|
};
|
|
}
|
|
|
|
// Récupérer les analytics actuelles
|
|
if (!jiraConfig.baseUrl || !jiraConfig.projectKey) {
|
|
return { success: false, error: 'Configuration Jira incomplète' };
|
|
}
|
|
|
|
const analyticsService = new JiraAnalyticsService(
|
|
jiraConfig as JiraAnalyticsConfig
|
|
);
|
|
const analytics = await analyticsService.getProjectAnalytics(forceRefresh);
|
|
|
|
// Détecter les anomalies
|
|
const anomalies = await jiraAnomalyDetection.detectAnomalies(analytics);
|
|
|
|
return {
|
|
success: true,
|
|
data: anomalies,
|
|
};
|
|
} catch (error) {
|
|
console.error("❌ Erreur lors de la détection d'anomalies:", error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Erreur inconnue',
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Met à jour la configuration de détection d'anomalies
|
|
*/
|
|
export async function updateAnomalyDetectionConfig(
|
|
config: Partial<AnomalyDetectionConfig>
|
|
) {
|
|
try {
|
|
jiraAnomalyDetection.updateConfig(config);
|
|
|
|
return {
|
|
success: true,
|
|
data: jiraAnomalyDetection.getConfig(),
|
|
};
|
|
} catch (error) {
|
|
console.error('❌ Erreur lors de la mise à jour de la config:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Erreur inconnue',
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Récupère la configuration actuelle de détection d'anomalies
|
|
*/
|
|
export async function getAnomalyDetectionConfig() {
|
|
try {
|
|
return {
|
|
success: true,
|
|
data: jiraAnomalyDetection.getConfig(),
|
|
};
|
|
} catch (error) {
|
|
console.error('❌ Erreur lors de la récupération de la config:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Erreur inconnue',
|
|
};
|
|
}
|
|
}
|