Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b887ecc3

Přidáno uživatelem Jakub Hlaváč před asi 4 roky(ů)

Re #8792 - Sensor page - tweak

+ adding error catchup on requests
+ toast message upon request

Zobrazit rozdíly:

src/app/app.component.ts
3 3

  
4 4
@Component({
5 5
  selector: 'app-root',
6
  template: '<router-outlet></router-outlet>'
6
  template: `<router-outlet></router-outlet><p-toast position="bottom-center" key="mainToast"></p-toast>`
7 7
})
8 8
export class AppComponent {
9 9
  title = 'SensLog';
src/app/app.module.ts
14 14
import {FontAwesomeModule} from '@fortawesome/angular-fontawesome';
15 15
import {UnitModule} from './unit/unit.module';
16 16
import {ConfirmationService, MessageService} from 'primeng/api';
17
import {ToastModule} from 'primeng/toast';
17 18

  
18 19
@NgModule({
19 20
  declarations: [
......
32 33
    SensorModule,
33 34
    FontAwesomeModule,
34 35
    UnitModule,
36
    ToastModule
35 37
  ],
36 38
  providers: [
37 39
    ConfirmationService,
src/app/auth/interceptors/auth.interceptor.ts
11 11
import {AuthService} from '../services/auth.service';
12 12
import {CookieService} from 'ngx-cookie-service';
13 13
import {GlobalVariable} from '../../globals';
14
import {ToastService} from '../../shared/services/toast.service';
14 15

  
15 16
@Injectable()
16 17
export class AuthInterceptor implements HttpInterceptor {
17 18

  
18 19
  constructor(
19 20
    private authService: AuthService,
20
    private cookieService: CookieService
21
    private cookieService: CookieService,
22
    private toastService: ToastService
21 23
  ) {
22 24
  }
23 25

  
......
36 38
      });
37 39
    }
38 40

  
39
    console.log('Sending request!');
41
    console.log('Sending request!', request);
40 42
    return next.handle(request)
41 43
      .pipe(
42 44
        catchError(err => {
43 45
          if ((err instanceof HttpErrorResponse && err.status === 504 || err.status === 502 || err.status === 401 ||
44 46
            err.status === 0 || err.status === 500)) {
47
            console.log(err);
48
            this.toastService.showError(err.error);
45 49
            return this.handleError(request, next);
46 50
          } else {
47 51
            return throwError(err);
src/app/auth/services/auth.service.ts
76 76
    this.cookieService.deleteAll();
77 77
    localStorage.clear();
78 78
    this.router.navigate(['/login']);
79
    this.cookieService.deleteAll();
79 80
  }
80 81

  
81 82
  setSessionStorage(userCookie: UserCookie, username) {
src/app/sensor/components/sensor.component.ts
1 1
import {Component, OnInit} from '@angular/core';
2 2
import {ActivatedRoute} from '@angular/router';
3
import {Observation} from '../../shared/api/endpoints/models/observation';
4 3
import * as moment from 'moment-timezone';
5 4
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
6 5
import {AggreationModel} from '../../shared/models/aggreation.model';
7 6
import {GraphLoader} from '../../shared/graph-loading/graphloader';
8 7
import {ObservationService} from '../../shared/api/endpoints/services/observation.service';
9

  
10
declare var require: any;
8
import {HttpResponse} from '@angular/common/http';
9
import {map} from 'rxjs/operators';
10
import {ToastService} from '../../shared/services/toast.service';
11 11

  
12 12
@Component({
13 13
  selector: 'app-sensor',
......
18 18

  
19 19
  sensorId: number;
20 20
  unitId: number;
21
  observations: Observation[];
22 21
  data = [];
23 22
  time = [];
24 23
  from: Date;
......
31 30
  constructor(
32 31
    private activatedRoute: ActivatedRoute,
33 32
    private analyticsService: AnalyticsService,
34
    private observationService: ObservationService
33
    private observationService: ObservationService,
34
    private toastService: ToastService
35 35
  ) {
36 36
    this.sensorId = parseInt(this.activatedRoute.snapshot.paramMap.get('sensorId'), 10);
37 37
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
......
52 52
    let agg = 'HOUR';
53 53
    const range: Date[] = [moment().subtract(7, 'days').toDate(), moment().toDate()];
54 54

  
55
    if (this.from && !this.to || !this.from && this.to) {
56
      return;
57
    }
58

  
55 59
    if (this.from && this.to) {
56 60
      range[0] = this.from;
57 61
      range[1] = this.to;
58

  
59
      if (moment(this.to).diff(moment(this.from), 'days') > 7) {
60
        this.showAggregation = true;
61
      }
62 62
    }
63 63

  
64
  /* this.observationService.getObservation({
65
      unit_id: this.unitId,
66
      sensor_id: this.sensorId,
67
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
68
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
69
    }).subscribe(
70
      observations => {
71
        if (observations) {
72
          GraphLoader.getObservationGraph(this.sensorId, observations, "#view");
73
        }
74
      });*/
75

  
76

  
64
    if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
65
      this.showAggregation = true;
66
    }
77 67

  
78 68
    if (this.selectedAggregationFunction) {
79 69
      agg = this.selectedAggregationFunction;
80 70
    }
81 71

  
82
    this.analyticsService.getAnalytics({
72
    if (moment(this.to).diff(moment(this.from), 'days') > 7) {
73
      this.getAnalytics(range, agg);
74
    } else {
75
      this.getObservations(range, agg);
76
    }
77
  }
78

  
79
  getAnalytics(range: Date[], agg: string) {
80
    this.analyticsService.getAnalytics$Response({
83 81
      unit_id: this.unitId, sensor_id: this.sensorId,
84 82
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
85 83
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: agg
86
    }).subscribe(
84
    }).pipe(
85
      map((response: HttpResponse<any>) => {
86
        if (response.status === 200) {
87
          return response.body;
88
        } else if (response.status === 204) {
89
          this.toastService.showWarningNoData();
90
          return response.body;
91
        } else {
92
          return false;
93
        }
94
      })
95
    ).subscribe(
87 96
      observations => {
88 97
        if (observations) {
89 98
          GraphLoader.getGraph(this.sensorId, observations[this.sensorId].data, observations[this.sensorId].interval, '#view');
90 99
        } else {
91 100
          GraphLoader.getGraph(null, null, null, null);
92 101
        }
93
      }
94
    );
102
      }, err => this.toastService.showError(err.error.message));
103
  }
95 104

  
105
  getObservations(range: Date[], agg: string) {
106
    this.observationService.getObservation$Response({
107
      unit_id: this.unitId,
108
      sensor_id: this.sensorId,
109
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
110
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
111
    }).pipe(
112
      map((response: HttpResponse<any>) => {
113
        if (response.status === 200) {
114
          return response.body;
115
        } else if (response.status === 204) {
116
          this.toastService.showWarningNoData();
117
          return response.body;
118
        } else {
119
          return false;
120
        }
121
      })
122
    ).subscribe(
123
      observations => {
124
        if (observations) {
125
          GraphLoader.getObservationGraph(this.sensorId, observations, '#view');
126
        } else {
127
          GraphLoader.getObservationGraph(null, null, null);
128
        }
129
      }, err => this.toastService.showError(err.error.message));
96 130
  }
97 131
}
src/app/shared/api/endpoints/services/management.service.ts
44 44
   */
45 45
  insertUnit$Response(params: {
46 46
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
47
  }): Observable<StrictHttpResponse<void>> {
47
  }): Observable<StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>> {
48 48

  
49 49
    const rb = new RequestBuilder(this.rootUrl, ManagementService.InsertUnitPath, 'post');
50 50
    if (params) {
......
52 52
    }
53 53

  
54 54
    return this.http.request(rb.build({
55
      responseType: 'text',
56
      accept: '*/*'
55
      responseType: 'json',
56
      accept: 'application/json'
57 57
    })).pipe(
58 58
      filter((r: any) => r instanceof HttpResponse),
59 59
      map((r: HttpResponse<any>) => {
60
        return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
60
        return r as StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>;
61 61
      })
62 62
    );
63 63
  }
......
74 74
   */
75 75
  insertUnit(params: {
76 76
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
77
  }): Observable<void> {
77
  }): Observable<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }> {
78 78

  
79 79
    return this.insertUnit$Response(params).pipe(
80
      map((r: StrictHttpResponse<void>) => r.body as void)
80
      map((r: StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>) => r.body as { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> })
81
    );
82
  }
83

  
84
  /**
85
   * Path part for operation insertSensor
86
   */
87
  static readonly InsertSensorPath = '/senslog15/ManagementService?Operation=InsertSensor';
88

  
89
  /**
90
   * Insert Sensor.
91
   *
92
   *
93
   *
94
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
95
   * To access only the response body, use `insertSensor()` instead.
96
   *
97
   * This method sends `application/json` and handles request body of type `application/json`.
98
   */
99
  insertSensor$Response(params: {
100
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
101
  }): Observable<StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>> {
102

  
103
    const rb = new RequestBuilder(this.rootUrl, ManagementService.InsertSensorPath, 'post');
104
    if (params) {
105
      rb.body(params.body, 'application/json');
106
    }
107

  
108
    return this.http.request(rb.build({
109
      responseType: 'json',
110
      accept: 'application/json'
111
    })).pipe(
112
      filter((r: any) => r instanceof HttpResponse),
113
      map((r: HttpResponse<any>) => {
114
        return r as StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>;
115
      })
116
    );
117
  }
118

  
119
  /**
120
   * Insert Sensor.
121
   *
122
   *
123
   *
124
   * This method provides access to only to the response body.
125
   * To access the full response (for headers, for example), `insertSensor$Response()` instead.
126
   *
127
   * This method sends `application/json` and handles request body of type `application/json`.
128
   */
129
  insertSensor(params: {
130
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
131
  }): Observable<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }> {
132

  
133
    return this.insertSensor$Response(params).pipe(
134
      map((r: StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>) => r.body as { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> })
81 135
    );
82 136
  }
83 137

  
......
206 260
   */
207 261
  deleteSensor$Response(params: {
208 262
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
209
  }): Observable<StrictHttpResponse<{ 'message'?: string }>> {
263
  }): Observable<StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>> {
210 264

  
211 265
    const rb = new RequestBuilder(this.rootUrl, ManagementService.DeleteSensorPath, 'delete');
212 266
    if (params) {
......
219 273
    })).pipe(
220 274
      filter((r: any) => r instanceof HttpResponse),
221 275
      map((r: HttpResponse<any>) => {
222
        return r as StrictHttpResponse<{ 'message'?: string }>;
276
        return r as StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>;
223 277
      })
224 278
    );
225 279
  }
......
236 290
   */
237 291
  deleteSensor(params: {
238 292
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
239
  }): Observable<{ 'message'?: string }> {
293
  }): Observable<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }> {
240 294

  
241 295
    return this.deleteSensor$Response(params).pipe(
242
      map((r: StrictHttpResponse<{ 'message'?: string }>) => r.body as { 'message'?: string })
296
      map((r: StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>) => r.body as { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> })
243 297
    );
244 298
  }
245 299

  
......
260 314
   */
261 315
  updateSensor$Response(params: {
262 316
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
263
  }): Observable<StrictHttpResponse<void>> {
317
  }): Observable<StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>> {
264 318

  
265 319
    const rb = new RequestBuilder(this.rootUrl, ManagementService.UpdateSensorPath, 'put');
266 320
    if (params) {
......
268 322
    }
269 323

  
270 324
    return this.http.request(rb.build({
271
      responseType: 'text',
272
      accept: '*/*'
325
      responseType: 'json',
326
      accept: 'application/json'
273 327
    })).pipe(
274 328
      filter((r: any) => r instanceof HttpResponse),
275 329
      map((r: HttpResponse<any>) => {
276
        return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
330
        return r as StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>;
277 331
      })
278 332
    );
279 333
  }
......
290 344
   */
291 345
  updateSensor(params: {
292 346
    body: { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }
293
  }): Observable<void> {
347
  }): Observable<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }> {
294 348

  
295 349
    return this.updateSensor$Response(params).pipe(
296
      map((r: StrictHttpResponse<void>) => r.body as void)
350
      map((r: StrictHttpResponse<{ 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> }>) => r.body as { 'unit'?: InsertUnit, 'sensors'?: Array<InsertSensor> })
297 351
    );
298 352
  }
299 353

  
src/app/shared/api/endpoints/services/sensors.service.ts
9 9
import { Observable } from 'rxjs';
10 10
import { map, filter } from 'rxjs/operators';
11 11

  
12
import { Phenomenon } from '../models/phenomenon';
12 13

  
13 14

  
14 15
/**
......
41 42
   * This method doesn't expect any request body.
42 43
   */
43 44
  getPhenomenons$Response(params?: {
44
  }): Observable<StrictHttpResponse<Array<any>>> {
45
  }): Observable<StrictHttpResponse<Array<Phenomenon>>> {
45 46

  
46 47
    const rb = new RequestBuilder(this.rootUrl, SensorsService.GetPhenomenonsPath, 'get');
47 48
    if (params) {
......
53 54
    })).pipe(
54 55
      filter((r: any) => r instanceof HttpResponse),
55 56
      map((r: HttpResponse<any>) => {
56
        return r as StrictHttpResponse<Array<any>>;
57
        return r as StrictHttpResponse<Array<Phenomenon>>;
57 58
      })
58 59
    );
59 60
  }
......
69 70
   * This method doesn't expect any request body.
70 71
   */
71 72
  getPhenomenons(params?: {
72
  }): Observable<Array<any>> {
73
  }): Observable<Array<Phenomenon>> {
73 74

  
74 75
    return this.getPhenomenons$Response(params).pipe(
75
      map((r: StrictHttpResponse<Array<any>>) => r.body as Array<any>)
76
      map((r: StrictHttpResponse<Array<Phenomenon>>) => r.body as Array<Phenomenon>)
76 77
    );
77 78
  }
78 79

  
src/app/shared/api/openapi.yaml
53 53
            application/json:
54 54
              schema:
55 55
                $ref: '#/components/schemas/UserCookie'
56
        401:
57
          description: Wrong username or password
58
          content:
59
            text/plain:
60
              schema:
61
                type: string
56 62

  
57 63
  /senslog15/rest/user:
58 64
    get:
......
116 122
                type: array
117 123
                items:
118 124
                  $ref: '#/components/schemas/OldObservation'
125
        500:
126
          description: Internal server error
119 127

  
120 128
  /senslog1/GroupService:
121 129
    get:
......
220 228
                properties:
221 229
                  sensor:
222 230
                    $ref: '#/components/schemas/Sensors'
231
        400:
232
          description: wrong request
233
          content:
234
            application/json:
235
              schema:
236
                type: object
237
                properties:
238
                  message:
239
                    type: string
240
                  path:
241
                    type: string
242
                  timestamp:
243
                    type: number
223 244

  
224 245
  /senslog15/ManagementService?Operation=InsertUnit:
225 246
    post:
......
230 251
      responses:
231 252
        200:
232 253
          description: inserted
254
          content:
255
            application/json:
256
              schema:
257
                type: object
258
                properties:
259
                  unit:
260
                    $ref: '#/components/schemas/InsertUnit'
261
                  sensors:
262
                    type: array
263
                    items:
264
                      $ref: '#/components/schemas/InsertSensor'
265
        400:
266
          description: wrong request
267
          content:
268
            application/json:
269
              schema:
270
                type: object
271
                properties:
272
                  message:
273
                    type: string
274
      requestBody:
275
        required: true
276
        content:
277
          application/json:
278
            schema:
279
              type: object
280
              properties:
281
                unit:
282
                  $ref: '#/components/schemas/InsertUnit'
283
                sensors:
284
                  type: array
285
                  items:
286
                    $ref: '#/components/schemas/InsertSensor'
287

  
288
  /senslog15/ManagementService?Operation=InsertSensor:
289
    post:
290
      tags:
291
        - management
292
      summary: Insert Sensor
293
      operationId: insertSensor
294
      responses:
295
        200:
296
          description: inserted
297
          content:
298
            application/json:
299
              schema:
300
                type: object
301
                properties:
302
                  unit:
303
                    $ref: '#/components/schemas/InsertUnit'
304
                  sensors:
305
                    type: array
306
                    items:
307
                      $ref: '#/components/schemas/InsertSensor'
308
        400:
309
          description: wrong request
310
          content:
311
            application/json:
312
              schema:
313
                type: object
314
                properties:
315
                  message:
316
                    type: string
233 317
      requestBody:
234 318
        required: true
235 319
        content:
......
258 342
              schema:
259 343
                type: array
260 344
                items:
261
                  $ref: ''#/components/schemas/Phenomenon''
345
                  $ref: '#/components/schemas/Phenomenon'
262 346

  
263 347
  /senslog15/ManagementService?Operation=UpdateUnit:
264 348
    put:
......
295 379
                properties:
296 380
                  message:
297 381
                    type: string
382
        400:
383
          description: wrong request
384
          content:
385
            application/json:
386
              schema:
387
                type: object
388
                properties:
389
                  message:
390
                    type: string
298 391
      requestBody:
299 392
        required: true
300 393
        content:
......
314 407
      responses:
315 408
        200:
316 409
          description: deleted
410
          content:
411
            application/json:
412
              schema:
413
                type: object
414
                properties:
415
                  unit:
416
                    $ref: '#/components/schemas/InsertUnit'
417
                  sensors:
418
                    type: array
419
                    items:
420
                      $ref: '#/components/schemas/InsertSensor'
421
        400:
422
          description: wrong request
317 423
          content:
318 424
            application/json:
319 425
              schema:
......
344 450
      responses:
345 451
        200:
346 452
          description: updated
453
          content:
454
            application/json:
455
              schema:
456
                type: object
457
                properties:
458
                  unit:
459
                    $ref: '#/components/schemas/InsertUnit'
460
                  sensors:
461
                    type: array
462
                    items:
463
                      $ref: '#/components/schemas/InsertSensor'
464
        400:
465
          description: wrong request
466
          content:
467
            application/json:
468
              schema:
469
                type: object
470
                properties:
471
                  message:
472
                    type: string
347 473
      requestBody:
348 474
        required: true
349 475
        content:
src/app/shared/nav-bar/components/user-insert-popup/user-insert-popup.component.ts
6 6
import {Right} from '../../../api/endpoints/models/right';
7 7
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
8 8
import {HttpResponse} from '@angular/common/http';
9
import {ToastService} from '../../../services/toast.service';
9 10

  
10 11
@Component({
11 12
  selector: 'app-user-insert-popup',
......
23 24
  constructor(
24 25
    private formBuilder: FormBuilder,
25 26
    private groupService: GroupService,
26
    private administrationService: AdministrationService
27
    private administrationService: AdministrationService,
28
    private toastService: ToastService
27 29
  ) {
28 30
    this.initForm();
29 31
    // TODO this.getGroups();
......
56 58
        userName: this.insertForm.controls.userName.value,
57 59
        rightsId: this.insertForm.controls.rights.value,
58 60
        userRealName: this.insertForm.controls.userRealName.value,
59
          // TODO
60
        groupId: 2
61
        groupId: 2 // TODO
61 62
        }
62 63
      }).pipe(
63 64
        map((response: HttpResponse<any>) => {
64
          if (response.status === 200) { // Accepts only 200 status response, due to API Definition
65
          if (response.status === 200) {
65 66
            this.close();
66 67
          } else {
67
            console.log('ERROR');
68
            this.toastService.showError(null);
68 69
          }
69 70
        })
70
      ).toPromise();
71
      ).toPromise().then().catch(err => this.toastService.showError(err.error));
71 72
    }
72 73
  }
73 74

  
src/app/shared/services/toast.service.ts
1
import {Injectable} from '@angular/core';
2
import {MessageService} from 'primeng/api';
3

  
4
@Injectable({
5
  providedIn: 'root'
6
})
7
export class ToastService {
8

  
9
  constructor(
10
    private messageService: MessageService
11
  ) {
12
  }
13

  
14
  showError(err: any) {
15
    this.messageService.add({key: 'mainToast', severity:'error', summary: 'Unsuccessful!', detail: err});
16
  }
17

  
18
  showWarningNoData() {
19
    this.messageService.add({key: 'mainToast', severity:'warn', summary: 'No data!', detail: 'Can not display any data for given interval and aggregation!'});
20
  }
21

  
22
  showSuccess() {
23
    this.messageService.add({key: 'mainToast', severity:'success', summary: 'No data!', detail: 'Successful request!'});
24
  }
25
}

Také k dispozici: Unified diff