Repository navigation
Expand file tree
/
Copy pathexample-basic.js
More file actions
111 lines (96 loc) · 3.6 KB
/
Copy pathexample-basic.js
File metadata and controls
111 lines (96 loc) · 3.6 KB
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
/**
* Basic Usage Example for TESAIoT Analytics API (JavaScript)
*
* This example demonstrates:
* - Client initialization
* - Making API requests
* - Handling responses
* - Error handling
*
* Author: TESAIoT Team
* License: Apache 2.0
*/
import { AnalyticsClient, TimeRange } from './analytics-client.js';
async function basicExample() {
console.log('='.repeat(60));
console.log('TESAIoT Analytics API - Basic Example (JavaScript)');
console.log('='.repeat(60));
const client = new AnalyticsClient({
baseUrl: process.env.TESAIOT_API_URL || 'https://admin.tesaiot.com/api/v1/bdh-ai',
apiToken: process.env.TESAIOT_API_TOKEN
});
// 1. Get recent anomalies
console.log('\n1. Fetching recent anomalies...');
try {
const anomalies = await client.getAnomalies({
limit: 5
// Note: severity values are 'critical', 'medium', 'low'
});
console.log(` Found ${anomalies.anomalies?.length || 0} anomalies`);
for (const a of (anomalies.anomalies || []).slice(0, 3)) {
console.log(` - ${a.device_name}: ${a.metric} (${a.severity})`);
}
} catch (e) {
console.log(` Error: ${e.message}`);
}
// 2. Get latency statistics
console.log('\n2. Fetching latency statistics...');
try {
const latency = await client.getLatencyStats({ hours: 24 });
const stats = latency.summary || latency;
console.log(` Average latency: ${stats.average_ms || stats.avg_latency_ms || 'N/A'} ms`);
console.log(` P95 latency: ${stats.p95_ms || stats.p95_latency_ms || 'N/A'} ms`);
} catch (e) {
console.log(` Error: ${e.message}`);
}
// 3. Get throughput statistics
console.log('\n3. Fetching throughput statistics...');
try {
const throughput = await client.getThroughputStats({ hours: 24 });
const stats = throughput.summary || throughput;
console.log(` Total messages: ${stats.total_messages || 'N/A'}`);
console.log(` Messages per hour: ${stats.messages_per_hour || 'N/A'}`);
} catch (e) {
console.log(` Error: ${e.message}`);
}
// 4. Get connectivity status
console.log('\n4. Fetching connectivity status...');
try {
const status = await client.getConnectivityStatus();
const summary = status.summary || {};
console.log(` Total devices: ${summary.total || 0}`);
console.log(` Online: ${summary.online || 0}`);
console.log(` Offline: ${summary.offline || 0}`);
} catch (e) {
console.log(` Error: ${e.message}`);
}
console.log('\n' + '='.repeat(60));
console.log('Done!');
}
function timeRangeExamples() {
console.log('='.repeat(60));
console.log('Time Range Examples');
console.log('='.repeat(60));
// Using TimeRange helper
const last7Days = TimeRange.lastDays(7);
console.log('\nLast 7 days:');
console.log(` Start: ${last7Days.start}`);
console.log(` End: ${last7Days.end}`);
const last24Hours = TimeRange.lastHours(24);
console.log('\nLast 24 hours:');
console.log(` Start: ${last24Hours.start}`);
console.log(` End: ${last24Hours.end}`);
// Custom time range
const customStart = new Date('2025-12-01T00:00:00Z');
const customEnd = new Date('2025-12-29T23:59:59Z');
console.log('\nCustom range:');
console.log(` Start: ${customStart.toISOString()}`);
console.log(` End: ${customEnd.toISOString()}`);
}
// Run examples
const args = process.argv.slice(2);
if (args.includes('--time')) {
timeRangeExamples();
} else {
basicExample().catch(console.error);
}