File size: 10,727 Bytes
d362646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**

 * Fallback API Client with Hierarchical Retry System

 * سیستم fallback سلسله مراتبی با 10 پشتیبان

 */

class FallbackAPIClient {
  constructor() {
    // لیست 10 endpoint پشتیبان به ترتیب اولویت
    this.endpoints = [
      'https://Really-amin-crypto-api-clean.hf.space',
      'https://really-amin-datasourceforcryptocurrency-2.hf.space',
      'https://really-amin-datasourceforcryptocurrency.hf.space',
      'http://localhost:7860',
      'http://localhost:8000',
      'https://api.coingecko.com/api/v3',
      'https://api.coincap.io/v2',
      'https://api.binance.com/api/v3',
      'https://api.kraken.com/0/public',
      'https://api.coinbase.com/v2'
    ];

    // Cache برای نتایج موفق
    this.cache = new Map();
    this.cacheTimeout = 60000; // 1 دقیقه

    // آمار برای monitoring
    this.stats = {
      totalRequests: 0,
      successfulRequests: 0,
      failedRequests: 0,
      endpointStats: {},
      lastSuccessfulEndpoint: null
    };

    // Initialize endpoint stats
    this.endpoints.forEach(endpoint => {
      this.stats.endpointStats[endpoint] = {
        requests: 0,
        successes: 0,
        failures: 0,
        avgResponseTime: 0,
        lastUsed: null
      };
    });
  }

  /**

   * درخواست با fallback سلسله مراتبی

   */
  async request(path, options = {}) {
    const {
      method = 'GET',
      body = null,
      headers = {},
      timeout = 10000,
      retryCount = 3,
      useCache = true
    } = options;

    // بررسی cache
    const cacheKey = `${method}:${path}:${JSON.stringify(body)}`;
    if (useCache && method === 'GET') {
      const cached = this.getFromCache(cacheKey);
      if (cached) {
        console.log('✅ Cache hit:', path);
        return cached;
      }
    }

    this.stats.totalRequests++;
    const errors = [];

    // تلاش با هر endpoint به ترتیب
    for (let i = 0; i < this.endpoints.length; i++) {
      const endpoint = this.endpoints[i];
      const endpointStats = this.stats.endpointStats[endpoint];

      try {
        console.log(`🔄 Trying endpoint ${i + 1}/${this.endpoints.length}: ${endpoint}`);
        
        const startTime = Date.now();
        const result = await this.makeRequest(endpoint, path, {
          method,
          body,
          headers,
          timeout
        });
        const responseTime = Date.now() - startTime;

        // به‌روزرسانی آمار موفق
        endpointStats.requests++;
        endpointStats.successes++;
        endpointStats.lastUsed = new Date().toISOString();
        endpointStats.avgResponseTime = 
          (endpointStats.avgResponseTime * (endpointStats.successes - 1) + responseTime) / 
          endpointStats.successes;

        this.stats.successfulRequests++;
        this.stats.lastSuccessfulEndpoint = endpoint;

        // ذخیره در cache
        if (useCache && method === 'GET') {
          this.saveToCache(cacheKey, result);
        }

        console.log(`✅ Success with endpoint ${i + 1}: ${endpoint} (${responseTime}ms)`);
        return result;

      } catch (error) {
        // به‌روزرسانی آمار خطا
        endpointStats.requests++;
        endpointStats.failures++;
        
        errors.push({
          endpoint,
          error: error.message,
          index: i + 1
        });

        console.warn(`❌ Failed endpoint ${i + 1}/${this.endpoints.length}: ${endpoint}`, error.message);

        // اگر آخرین endpoint بود، خطا بده
        if (i === this.endpoints.length - 1) {
          this.stats.failedRequests++;
          throw new Error(
            `All ${this.endpoints.length} endpoints failed:\n` +
            errors.map(e => `${e.index}. ${e.endpoint}: ${e.error}`).join('\n')
          );
        }

        // صبر کوتاه قبل از تلاش بعدی
        await this.sleep(500);
      }
    }
  }

  /**

   * ساخت درخواست به یک endpoint

   */
  async makeRequest(baseUrl, path, options) {
    const { method, body, headers, timeout } = options;

    // ساخت URL کامل
    const url = this.buildUrl(baseUrl, path);

    // ساخت AbortController برای timeout
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), timeout);

    try {
      const response = await fetch(url, {
        method,
        headers: {
          'Content-Type': 'application/json',
          ...headers
        },
        body: body ? JSON.stringify(body) : null,
        signal: controller.signal
      });

      clearTimeout(timeoutId);

      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }

      const data = await response.json();
      return data;

    } catch (error) {
      clearTimeout(timeoutId);
      
      if (error.name === 'AbortError') {
        throw new Error(`Timeout after ${timeout}ms`);
      }
      
      throw error;
    }
  }

  /**

   * ساخت URL کامل

   */
  buildUrl(baseUrl, path) {
    // حذف slash اضافی
    baseUrl = baseUrl.replace(/\/$/, '');
    path = path.replace(/^\//, '');

    // تطبیق path با endpoint های مختلف
    if (baseUrl.includes('coingecko')) {
      return this.adaptToCoinGecko(baseUrl, path);
    } else if (baseUrl.includes('coincap')) {
      return this.adaptToCoinCap(baseUrl, path);
    } else if (baseUrl.includes('binance')) {
      return this.adaptToBinance(baseUrl, path);
    }

    // پیش‌فرض
    return `${baseUrl}/${path}`;
  }

  /**

   * تطبیق با CoinGecko API

   */
  adaptToCoinGecko(baseUrl, path) {
    if (path.includes('/api/coins/top')) {
      return `${baseUrl}/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=50`;
    }
    if (path.includes('/api/trending')) {
      return `${baseUrl}/search/trending`;
    }
    if (path.includes('/api/market')) {
      return `${baseUrl}/global`;
    }
    return `${baseUrl}/${path}`;
  }

  /**

   * تطبیق با CoinCap API

   */
  adaptToCoinCap(baseUrl, path) {
    if (path.includes('/api/coins/top')) {
      return `${baseUrl}/assets?limit=50`;
    }
    return `${baseUrl}/${path}`;
  }

  /**

   * تطبیق با Binance API

   */
  adaptToBinance(baseUrl, path) {
    if (path.includes('/api/coins/top')) {
      return `${baseUrl}/ticker/24hr`;
    }
    return `${baseUrl}/${path}`;
  }

  /**

   * Cache management

   */
  getFromCache(key) {
    const cached = this.cache.get(key);
    if (!cached) return null;

    const now = Date.now();
    if (now - cached.timestamp > this.cacheTimeout) {
      this.cache.delete(key);
      return null;
    }

    return cached.data;
  }

  saveToCache(key, data) {
    this.cache.set(key, {
      data,
      timestamp: Date.now()
    });

    // پاکسازی cache قدیمی
    if (this.cache.size > 100) {
      const oldestKey = this.cache.keys().next().value;
      this.cache.delete(oldestKey);
    }
  }

  /**

   * Helper: sleep

   */
  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  /**

   * دریافت آمار

   */
  getStats() {
    return {
      ...this.stats,
      successRate: this.stats.totalRequests > 0 
        ? (this.stats.successfulRequests / this.stats.totalRequests * 100).toFixed(2) + '%'
        : '0%',
      cacheSize: this.cache.size
    };
  }

  /**

   * ریست آمار

   */
  resetStats() {
    this.stats.totalRequests = 0;
    this.stats.successfulRequests = 0;
    this.stats.failedRequests = 0;
    
    this.endpoints.forEach(endpoint => {
      this.stats.endpointStats[endpoint] = {
        requests: 0,
        successes: 0,
        failures: 0,
        avgResponseTime: 0,
        lastUsed: null
      };
    });
  }

  /**

   * پاکسازی cache

   */
  clearCache() {
    this.cache.clear();
  }

  /**

   * تغییر ترتیب endpoints بر اساس عملکرد

   */
  optimizeEndpoints() {
    // مرتب‌سازی بر اساس نرخ موفقیت و سرعت
    this.endpoints.sort((a, b) => {
      const statsA = this.stats.endpointStats[a];
      const statsB = this.stats.endpointStats[b];

      const successRateA = statsA.requests > 0 ? statsA.successes / statsA.requests : 0;
      const successRateB = statsB.requests > 0 ? statsB.successes / statsB.requests : 0;

      if (successRateA !== successRateB) {
        return successRateB - successRateA; // بیشترین موفقیت اول
      }

      return statsA.avgResponseTime - statsB.avgResponseTime; // سریع‌تر اول
    });

    console.log('✅ Endpoints optimized based on performance');
  }
}

// ============================================================================
// API Methods با Fallback
// ============================================================================

class CryptoAPI {
  constructor() {
    this.client = new FallbackAPIClient();
  }

  // Health & Status
  async health() {
    return this.client.request('/api/health');
  }

  async status() {
    return this.client.request('/api/status');
  }

  // Market Data
  async getTopCoins(limit = 50) {
    return this.client.request(`/api/coins/top?limit=${limit}`);
  }

  async getTrending() {
    return this.client.request('/api/trending');
  }

  async getMarket() {
    return this.client.request('/api/market');
  }

  // Sentiment
  async getGlobalSentiment(timeframe = '1D') {
    return this.client.request(`/api/sentiment/global?timeframe=${timeframe}`);
  }

  async getAssetSentiment(symbol) {
    return this.client.request(`/api/sentiment/asset/${symbol}`);
  }

  // News
  async getNews(limit = 50) {
    return this.client.request(`/api/news?limit=${limit}`);
  }

  // Resources
  async getResources() {
    return this.client.request('/api/resources/summary');
  }

  async getCategories() {
    return this.client.request('/api/categories');
  }

  // Models
  async getModels() {
    return this.client.request('/api/models/list');
  }

  // Stats
  getStats() {
    return this.client.getStats();
  }

  optimizeEndpoints() {
    this.client.optimizeEndpoints();
  }
}

// Export
if (typeof module !== 'undefined' && module.exports) {
  module.exports = { FallbackAPIClient, CryptoAPI };
}