File size: 3,911 Bytes
42f149e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { Challenge, ChallengeSeries, TimeSeriesData, ForecastsResponse, ModelsResponse } from '@/src/types/challenge';

export interface ChallengeApiFilters {
  domains?: string[];
  categories?: string[];
  frequencies?: string[];
  horizons?: string[];
  endDateFrom?: string;
  endDateTo?: string;
  status?: string;
  searchTerm?: string;
}

export async function getChallenges(filters?: ChallengeApiFilters): Promise<Challenge[]> {
  const params = new URLSearchParams();
  
  if (filters) {
    // Add array filters
    filters.domains?.forEach(d => params.append('domain', d));
    filters.categories?.forEach(c => params.append('category', c));
    filters.frequencies?.forEach(f => params.append('frequency', f));
    filters.horizons?.forEach(h => params.append('horizon', h));
    
    // Add date filters - convert to ISO format
    if (filters.endDateFrom) {
      const fromDate = new Date(filters.endDateFrom);
      params.append('from', fromDate.toISOString());
    }
    if (filters.endDateTo) {
      const toDate = new Date(filters.endDateTo);
      // Set to end of day for 'to' date
      toDate.setHours(23, 59, 59, 999);
      params.append('to', toDate.toISOString());
    }
    
    // Add status filter
    if (filters.status && filters.status !== 'all') params.append('status', filters.status);
    
    // Add search term
    if (filters.searchTerm) params.append('search', filters.searchTerm);
  }
  
  const queryString = params.toString();
  const url = `/api/v1/rounds${queryString ? '?' + queryString : ''}`;
  
  console.log('Fetching challenges with filters:', url);
  
  const response = await fetch(url);
  if (!response.ok) {
    throw new Error('Failed to fetch challenges');
  }
  return response.json();
}

export async function getRoundsMetadata(): Promise<{
  domains: string[];
  categories: string[];
  frequencies: string[];
  horizons: string[];
  statuses: string[];
  subcategories: string[];
}> {
  const response = await fetch('/api/v1/rounds/metadata');
  if (!response.ok) {
    throw new Error('Failed to fetch rounds metadata');
  }
  return response.json();
}

export async function getChallengeSeries(roundId: number): Promise<ChallengeSeries[]> {
  const response = await fetch(`/api/v1/rounds/${roundId}/series`);
  if (!response.ok) {
    throw new Error('Failed to fetch round series');
  }
  return response.json();
}

export async function getSeriesData(
  roundId: number,
  seriesId: number,
  startTime: string,
  endTime: string
): Promise<TimeSeriesData> {
  const params = new URLSearchParams({
    start_time: startTime,
    end_time: endTime,
  });
  const url = `/api/v1/rounds/${roundId}/series/${seriesId}/data?${params.toString()}`;
  console.log('getSeriesData calling:', url);
  const response = await fetch(url);
  if (!response.ok) {
    const errorText = await response.text();
    console.error('getSeriesData failed:', response.status, errorText);
    throw new Error('Failed to fetch series data');
  }
  return response.json();
}

export async function getSeriesForecasts(
  roundId: number,
  seriesId: number
): Promise<ForecastsResponse> {
  const url = `/api/v1/rounds/${roundId}/series/${seriesId}/forecasts`;
  console.log('getSeriesForecasts calling:', url);
  const response = await fetch(url);
  if (!response.ok) {
    const errorText = await response.text();
    console.error('getSeriesForecasts failed:', response.status, errorText);
    throw new Error('Failed to fetch forecasts');
  }
  return response.json();
}

export async function getRoundModels(
  roundId: number
): Promise<ModelsResponse> {
  const url = `/api/v1/rounds/${roundId}/models`;
  const response = await fetch(url);
  if (!response.ok) {
    const errorText = await response.text();
    console.error('getRoundModels failed:', response.status, errorText);
    throw new Error('Failed to fetch models');
  }
  return response.json();
}