File size: 2,714 Bytes
bdef324
39018b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdef324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287a59e
bdef324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39018b4
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
// lib/api.ts — All API calls to SIKOMO Forecast backend
const BASE = process.env.NEXT_PUBLIC_API_URL || ''

function getToken() {
  if (typeof window !== 'undefined') return localStorage.getItem('sikomo_token') || ''
  return ''
}

async function req(path: string, opts: RequestInit = {}) {
  const res = await fetch(`${BASE}${path}`, {
    ...opts,
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${getToken()}`,
      ...opts.headers,
    },
  })
  if (!res.ok) {
    const err = await res.json().catch(() => ({ detail: res.statusText }))
    throw new Error(err.detail || 'Request gagal')
  }
  return res.json()
}

export const api = {
  // Auth
  login: (password: string) => req('/auth/login', { method: 'POST', body: JSON.stringify({ password }) }),

  // Komoditas & Prediksi
  getKomoditas: () => req('/komoditas'),
  getPrediksi: (id: number) => req(`/prediksi/${id}`),

  // Forecast actions
  runForecast: (komoditas_id: number) => req('/forecast/run', { method: 'POST', body: JSON.stringify({ komoditas_id }) }),
  runForecastAll: () => req('/forecast/run-all', { method: 'POST' }),
  stopForecast: () => req('/forecast/stop', { method: 'POST' }),

  // Status & Logs (realtime)
  getStatus: () => req('/status'),
  getLogs: (limit = 200, level?: string) => req(`/logs?limit=${limit}${level && level !== 'ALL' ? `&level=${level}` : ''}`),
  clearLogs: () => req('/clear-logs', { method: 'POST' }),

  // Scheduler (multiple schedules)
  getSchedules: () => req('/schedules'),
  addSchedule: (cron_expression: string, label: string) => req('/schedules/add', { method: 'POST', body: JSON.stringify({ cron_expression, label }) }),
  removeSchedule: (jobId: string) => req(`/schedules/${jobId}/remove`, { method: 'DELETE' }),
  startScheduler: () => req('/schedule/start', { method: 'POST' }),
  stopScheduler: () => req('/schedule/stop', { method: 'POST' }),

  // Runtime settings
  getSettings: () => req('/settings'),
  updateSetting: (key: string, value: string) => req('/settings', { method: 'POST', body: JSON.stringify({ key, value }) }),

  // CRUD
  getInsights: (id: number) => req(`/insight/${id}`),
  updateInsight: (id: number, body: object) => req(`/insight/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
  addInsight: (id: number, body: object) => req(`/insight/${id}`, { method: 'POST', body: JSON.stringify(body) }),
  deleteInsight: (id: number) => req(`/insight/${id}`, { method: 'DELETE' }),
  updateRingkasan: (id: number, body: object) => req(`/ringkasan/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
  updateModelMl: (id: number, body: object) => req(`/model-ml/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
}