File size: 5,987 Bytes
1802f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * BIST Predictor β€” API Δ°letişim ModΓΌlΓΌ
 * Backend REST API ve SSE stream yΓΆnetimi.
 */

const API = {
    BASE_URL: window.location.origin,
    
    /** SSE event source instance */
    _eventSource: null,
    _sseListeners: [],
    
    // ─── HTTP Δ°stekleri ─────────────────────────────────────────────────────
    
    async _fetch(endpoint, options = {}) {
        try {
            const url = `${this.BASE_URL}/api${endpoint}`;
            const response = await fetch(url, {
                headers: { 'Content-Type': 'application/json' },
                ...options,
            });
            
            if (!response.ok) {
                const error = await response.json().catch(() => ({ detail: response.statusText }));
                throw new Error(error.detail || `HTTP ${response.status}`);
            }
            
            return await response.json();
        } catch (error) {
            console.error(`API Error [${endpoint}]:`, error);
            throw error;
        }
    },
    
    async get(endpoint) {
        return this._fetch(endpoint);
    },
    
    async post(endpoint, data = null) {
        const options = { method: 'POST' };
        if (data) options.body = JSON.stringify(data);
        return this._fetch(endpoint, options);
    },
    
    // ─── Dashboard ──────────────────────────────────────────────────────────
    
    async getDashboard(horizon = 10) {
        return this.get(`/dashboard?horizon=${horizon}`);
    },
    
    async getStocks() {
        return this.get('/stocks');
    },
    
    async getStockDetail(symbol, horizon = 10) {
        return this.get(`/stock/${symbol}?horizon=${horizon}`);
    },
    
    // ─── Tahminler ──────────────────────────────────────────────────────────
    
    async getPredictions(symbol, horizon = 10) {
        return this.get(`/predictions/${symbol}?horizon=${horizon}`);
    },
    
    async triggerPrediction(symbol, horizons = '10') {
        return this.post(`/predict/${symbol}?horizons=${horizons}`);
    },
    
    async triggerAllPredictions() {
        return this.post('/predict-all');
    },
    
    // ─── GΓΌven PuanΔ± ────────────────────────────────────────────────────────
    
    async getConfidence(symbol, horizon = 10) {
        return this.get(`/confidence/${symbol}?horizon=${horizon}`);
    },
    
    async getConfidenceRanking(horizon = 10) {
        return this.get(`/confidence-ranking?horizon=${horizon}`);
    },
    
    // ─── KarşılaştΔ±rma ──────────────────────────────────────────────────────
    
    async getComparison(symbol, horizon = 10) {
        return this.get(`/comparison/${symbol}?horizon=${horizon}`);
    },
    
    // ─── Veri YΓΆnetimi ──────────────────────────────────────────────────────
    
    async loadInitialData() {
        return this.post('/data/load');
    },
    
    async updateStockData(symbol) {
        return this.post(`/data/update/${symbol}`);
    },
    
    async triggerComparison() {
        return this.post('/data/compare');
    },
    
    // ─── Sistem ─────────────────────────────────────────────────────────────
    
    async getSystemStatus() {
        return this.get('/system/status');
    },
    
    async getSystemLogs(limit = 50) {
        return this.get(`/system/logs?limit=${limit}`);
    },
    
    // ─── SSE (Server-Sent Events) ────────────────────────────────────────────
    
    connectSSE() {
        if (this._eventSource) {
            this._eventSource.close();
        }
        
        this._eventSource = new EventSource(`${this.BASE_URL}/api/stream`);
        
        this._eventSource.onopen = () => {
            console.log('SSE bağlantısı kuruldu');
            this._notifyListeners('connected', {});
        };
        
        this._eventSource.onmessage = (event) => {
            try {
                const parsed = JSON.parse(event.data);
                const { type, data, timestamp } = parsed;
                this._notifyListeners(type, data, timestamp);
            } catch (e) {
                console.warn('SSE parse hatasΔ±:', e);
            }
        };
        
        this._eventSource.onerror = (error) => {
            console.warn('SSE bağlantı hatası, yeniden bağlanılıyor...');
            this._notifyListeners('disconnected', {});
            
            // Otomatik yeniden bağlanma (EventSource bunu otomatik yapar)
        };
    },
    
    disconnectSSE() {
        if (this._eventSource) {
            this._eventSource.close();
            this._eventSource = null;
        }
    },
    
    onSSE(callback) {
        this._sseListeners.push(callback);
    },
    
    offSSE(callback) {
        this._sseListeners = this._sseListeners.filter(cb => cb !== callback);
    },
    
    _notifyListeners(type, data, timestamp) {
        for (const listener of this._sseListeners) {
            try {
                listener(type, data, timestamp);
            } catch (e) {
                console.error('SSE listener hatasΔ±:', e);
            }
        }
    },
};