fix(graph); change time range start and end

This commit is contained in:
Desy Ayurianti 2024-11-06 13:38:20 +07:00
parent 50c3ce7497
commit 2c6123c54c
2 changed files with 140 additions and 45 deletions

View File

@ -27,9 +27,28 @@ export class SensorService extends ApiService {
}); });
} }
getSensorData(sensor: string, metric: string, startEnd: string, timeRange: string): Observable<ApiResponse> { getSensorDataHourly(sensor: string, metric: string, start: string, end: string, timeRange: string): Observable<ApiResponse> {
const params = new HttpParams() const params = new HttpParams()
.set('range[end]', startEnd) .set('range[start]', start)
.set('range[end]', end)
.set('range[time_range]', timeRange)
.set('sensor', sensor)
.set('metric', metric);
const headers= this.createAuthHeaders();
return this.http.get<ApiResponse>(this.getDataUrl, { params, headers }).pipe(
catchError(error => {
// this.toast.error('Failed to get sensor data for graphic, please try again');
return throwError(error);
})
);
}
getSensorDataDaily(sensor: string, metric: string, start: string, end: string, timeRange: string): Observable<ApiResponse> {
const params = new HttpParams()
.set('range[start]', start)
.set('range[end]', end)
.set('range[time_range]', timeRange) .set('range[time_range]', timeRange)
.set('sensor', sensor) .set('sensor', sensor)
.set('metric', metric); .set('metric', metric);

View File

@ -114,9 +114,41 @@ export class GraphComponent implements OnInit, AfterViewInit, OnDestroy, OnChang
return `${year}-${month}-${day}`; return `${year}-${month}-${day}`;
} }
getDateAgo():string{
const today = new Date();
today.setDate(today.getDate() - 7);
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
fetchDHTData(timeRange: string): void { fetchDHTData(timeRange: string): void {
const startEnd = this.getDate(); const hEnd = this.getDate();
this.sensorService.getSensorData('dht', 'npk', startEnd, timeRange).subscribe({ const hStart = this.getDate();
const dEnd = this.getDate();
const dStart = this.getDateAgo();
if(timeRange === 'HOURLY'){
this.sensorService.getSensorDataHourly('dht', 'npk', hStart, hEnd, timeRange).subscribe({
next: (response) => {
this.isLoadingDHT = false;
if (response.statusCode === 200 && response.data.dht?.length > 0) {
this.createChart(this.dhtChartElement.nativeElement, response, 'dht', 'npk');
this.isNoDataDHT = false;
} else {
this.isNoDataDHT = true;
}
},
error: () => {
this.isLoadingDHT = false;
this.isNoDataDHT = true;
}
});
} else if(timeRange === 'DAILY'){
this.sensorService.getSensorDataDaily('dht', 'npk', dStart, dEnd, timeRange).subscribe({
next: (response) => { next: (response) => {
this.isLoadingDHT = false; this.isLoadingDHT = false;
if (response.statusCode === 200 && response.data.dht?.length > 0) { if (response.statusCode === 200 && response.data.dht?.length > 0) {
@ -132,10 +164,32 @@ export class GraphComponent implements OnInit, AfterViewInit, OnDestroy, OnChang
} }
}); });
} }
}
fetchNPK1Data(timeRange: string): void { fetchNPK1Data(timeRange: string): void {
const startEnd = this.getDate(); const hEnd = this.getDate();
this.sensorService.getSensorData('npk1', this.selectedNPK1, startEnd, timeRange).subscribe({ const hStart = this.getDate();
const dEnd = this.getDate();
const dStart = this.getDateAgo();
if(timeRange === 'HOURLY'){
this.sensorService.getSensorDataHourly('npk1', this.selectedNPK1, hEnd, hStart, timeRange).subscribe({
next: (response) => {
this.isLoadingNPK1 = false;
if (response.statusCode === 200 && response.data.npk1?.length > 0) {
this.createChart(this.npk1ChartElement.nativeElement, response, 'npk1', this.selectedNPK1);
this.isNoDataNPK1 = false;
} else {
this.isNoDataNPK1 = true;
}
},
error: () => {
this.isLoadingNPK1 = false;
this.isNoDataNPK1 = true;
}
})
}else if(timeRange === 'DAILY'){
this.sensorService.getSensorDataDaily('npk1', this.selectedNPK1, dStart, dEnd, timeRange).subscribe({
next: (response) => { next: (response) => {
this.isLoadingNPK1 = false; this.isLoadingNPK1 = false;
if (response.statusCode === 200 && response.data.npk1?.length > 0) { if (response.statusCode === 200 && response.data.npk1?.length > 0) {
@ -151,12 +205,17 @@ export class GraphComponent implements OnInit, AfterViewInit, OnDestroy, OnChang
} }
}); });
} }
}
fetchNPK2Data(savedTimeRange: string): void { fetchNPK2Data(savedTimeRange: string): void {
const startEnd = this.getDate(); const hEnd = this.getDate();
this.sensorService.getSensorData('npk2', this.selectedNPK2, startEnd, savedTimeRange).subscribe({ const hStart = this.getDate();
const dEnd = this.getDate();
const dStart = this.getDateAgo();
if(savedTimeRange === 'HOURLY'){
this.sensorService.getSensorDataHourly('npk2', this.selectedNPK2, hStart, hEnd, savedTimeRange).subscribe({
next: (response) => { next: (response) => {
console.log(savedTimeRange);
this.isLoadingNPK2 = false; this.isLoadingNPK2 = false;
if (response.statusCode === 200 && response.data.npk2?.length > 0) { if (response.statusCode === 200 && response.data.npk2?.length > 0) {
this.createChart(this.npk2ChartElement.nativeElement, response, 'npk2', this.selectedNPK2); this.createChart(this.npk2ChartElement.nativeElement, response, 'npk2', this.selectedNPK2);
@ -170,6 +229,23 @@ export class GraphComponent implements OnInit, AfterViewInit, OnDestroy, OnChang
this.isNoDataNPK2 = true; this.isNoDataNPK2 = true;
} }
}); });
} else if(savedTimeRange === 'DAILY'){
this.sensorService.getSensorDataDaily('npk2', this.selectedNPK2, dStart, dEnd, savedTimeRange).subscribe({
next: (response) => {
this.isLoadingNPK2 = false;
if (response.statusCode === 200 && response.data.npk2?.length > 0) {
this.createChart(this.npk2ChartElement.nativeElement, response, 'npk2', this.selectedNPK2);
this.isNoDataNPK2 = false;
} else {
this.isNoDataNPK2 = true;
}
},
error: () => {
this.isLoadingNPK2 = false;
this.isNoDataNPK2 = true;
}
});
}
} }
updateCharts(): void { updateCharts(): void {