feat: enhance Jira analytics with caching and force refresh

- Updated `getJiraAnalytics` to accept a `forceRefresh` parameter for optional cache bypass.
- Modified `getProjectAnalytics` to check the cache and return cached data unless forced to refresh.
- Adjusted `loadAnalytics` in `useJiraAnalytics` to trigger a forced refresh on manual updates.
- Improved UI in `JiraDashboardPageClient` to indicate when data is served from cache.
This commit is contained in:
Julien Froidefond
2025-09-18 22:28:34 +02:00
parent 5d73a6c279
commit 4c03ae946f
6 changed files with 309 additions and 26 deletions

View File

@@ -0,0 +1,155 @@
/**
* Service de cache pour les analytics Jira
* Cache en mémoire avec invalidation manuelle
*/
import { JiraAnalytics } from '@/lib/types';
interface CacheEntry {
data: JiraAnalytics;
timestamp: number;
projectKey: string;
configHash: string; // Hash de la config Jira pour détecter les changements
}
class JiraAnalyticsCacheService {
private cache = new Map<string, CacheEntry>();
private readonly CACHE_KEY_PREFIX = 'jira-analytics:';
/**
* Génère une clé de cache basée sur la config Jira
*/
private getCacheKey(projectKey: string, configHash: string): string {
return `${this.CACHE_KEY_PREFIX}${projectKey}:${configHash}`;
}
/**
* Génère un hash de la configuration Jira pour détecter les changements
*/
private generateConfigHash(config: { baseUrl: string; email: string; apiToken: string; projectKey: string }): string {
const configString = `${config.baseUrl}|${config.email}|${config.apiToken}|${config.projectKey}`;
// Simple hash (pour production, utiliser crypto.createHash)
let hash = 0;
for (let i = 0; i < configString.length; i++) {
const char = configString.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return hash.toString();
}
/**
* Récupère les analytics depuis le cache si disponible
*/
get(config: { baseUrl: string; email: string; apiToken: string; projectKey: string }): JiraAnalytics | null {
const configHash = this.generateConfigHash(config);
const cacheKey = this.getCacheKey(config.projectKey, configHash);
const entry = this.cache.get(cacheKey);
if (!entry) {
console.log(`📋 Cache MISS pour projet ${config.projectKey}`);
return null;
}
// Vérifier que la config n'a pas changé
if (entry.configHash !== configHash) {
console.log(`🔄 Config changée pour projet ${config.projectKey}, invalidation du cache`);
this.cache.delete(cacheKey);
return null;
}
console.log(`✅ Cache HIT pour projet ${config.projectKey} (${this.getAgeDescription(entry.timestamp)})`);
return entry.data;
}
/**
* Stocke les analytics dans le cache
*/
set(config: { baseUrl: string; email: string; apiToken: string; projectKey: string }, data: JiraAnalytics): void {
const configHash = this.generateConfigHash(config);
const cacheKey = this.getCacheKey(config.projectKey, configHash);
const entry: CacheEntry = {
data,
timestamp: Date.now(),
projectKey: config.projectKey,
configHash
};
this.cache.set(cacheKey, entry);
console.log(`💾 Analytics mises en cache pour projet ${config.projectKey}`);
}
/**
* Invalide le cache pour un projet spécifique
*/
invalidate(config: { baseUrl: string; email: string; apiToken: string; projectKey: string }): void {
const configHash = this.generateConfigHash(config);
const cacheKey = this.getCacheKey(config.projectKey, configHash);
const deleted = this.cache.delete(cacheKey);
if (deleted) {
console.log(`🗑️ Cache invalidé pour projet ${config.projectKey}`);
} else {
console.log(` Aucun cache à invalider pour projet ${config.projectKey}`);
}
}
/**
* Invalide tout le cache
*/
invalidateAll(): void {
const size = this.cache.size;
this.cache.clear();
console.log(`🗑️ Tout le cache analytics invalidé (${size} entrées supprimées)`);
}
/**
* Retourne les statistiques du cache
*/
getStats(): {
totalEntries: number;
projects: Array<{ projectKey: string; age: string; size: number }>;
} {
const projects = Array.from(this.cache.entries()).map(([key, entry]) => ({
projectKey: entry.projectKey,
age: this.getAgeDescription(entry.timestamp),
size: JSON.stringify(entry.data).length
}));
return {
totalEntries: this.cache.size,
projects
};
}
/**
* Formate l'âge d'une entrée de cache
*/
private getAgeDescription(timestamp: number): string {
const ageMs = Date.now() - timestamp;
const ageMinutes = Math.floor(ageMs / (1000 * 60));
const ageHours = Math.floor(ageMinutes / 60);
if (ageHours > 0) {
return `il y a ${ageHours}h${ageMinutes % 60}m`;
} else if (ageMinutes > 0) {
return `il y a ${ageMinutes}m`;
} else {
return 'maintenant';
}
}
/**
* Vérifie si une entrée existe pour un projet
*/
has(config: { baseUrl: string; email: string; apiToken: string; projectKey: string }): boolean {
const configHash = this.generateConfigHash(config);
const cacheKey = this.getCacheKey(config.projectKey, configHash);
return this.cache.has(cacheKey);
}
}
// Instance singleton
export const jiraAnalyticsCache = new JiraAnalyticsCacheService();

View File

@@ -4,14 +4,15 @@
*/
import { JiraService } from './jira';
import {
JiraAnalytics,
import { jiraAnalyticsCache } from './jira-analytics-cache';
import {
JiraAnalytics,
JiraTask,
AssigneeDistribution,
SprintVelocity,
CycleTimeByType,
StatusDistribution,
AssigneeWorkload
AssigneeDistribution,
SprintVelocity,
CycleTimeByType,
StatusDistribution,
AssigneeWorkload
} from '@/lib/types';
export interface JiraAnalyticsConfig {
@@ -24,18 +25,28 @@ export interface JiraAnalyticsConfig {
export class JiraAnalyticsService {
private jiraService: JiraService;
private projectKey: string;
private config: JiraAnalyticsConfig;
constructor(config: JiraAnalyticsConfig) {
this.jiraService = new JiraService(config);
this.projectKey = config.projectKey;
this.config = config;
}
/**
* Récupère toutes les analytics du projet
* Récupère toutes les analytics du projet avec cache
*/
async getProjectAnalytics(): Promise<JiraAnalytics> {
async getProjectAnalytics(forceRefresh = false): Promise<JiraAnalytics> {
try {
console.log(`📊 Début de l'analyse du projet ${this.projectKey}...`);
// Vérifier le cache d'abord (sauf si forceRefresh)
if (!forceRefresh) {
const cachedAnalytics = jiraAnalyticsCache.get(this.config);
if (cachedAnalytics) {
return cachedAnalytics;
}
}
console.log(`🔄 Calcul des analytics Jira pour projet ${this.projectKey} ${forceRefresh ? '(actualisation forcée)' : '(cache manquant)'}`);
// Récupérer les informations du projet
const projectInfo = await this.getProjectInfo();
@@ -57,7 +68,7 @@ export class JiraAnalyticsService {
this.calculateWorkInProgress(allIssues)
]);
return {
const analytics: JiraAnalytics = {
project: {
key: this.projectKey,
name: projectInfo.name,
@@ -69,12 +80,24 @@ export class JiraAnalyticsService {
workInProgress
};
// Mettre en cache le résultat
jiraAnalyticsCache.set(this.config, analytics);
return analytics;
} catch (error) {
console.error('Erreur lors du calcul des analytics:', error);
throw error;
}
}
/**
* Invalide le cache pour ce projet
*/
invalidateCache(): void {
jiraAnalyticsCache.invalidate(this.config);
}
/**
* Récupère les informations de base du projet
*/