Projekt

Obecné

Profil

« Předchozí | Další » 

Revize cd2a65f3

Přidáno uživatelem Jakub Hlaváč před téměř 4 roky(ů)

Re #8793 - Unit page - implementace

+ implementation of unit page graphs
+ multiple graphs on one page from one request

Zobrazit rozdíly:

src/app/auth/interceptors/auth.interceptor.ts
27 27

  
28 28
    if (!this.cookieService.get(GlobalVariable.SESSION_ID) && !request.url.includes('ControllerServlet')) {
29 29
      console.log('No sessionid!');
30
      this.cookieService.deleteAll();
31
      this.authService.doLogout();
30 32
      return of(null);
31 33
    }
32 34

  
src/app/auth/services/auth.service.ts
82 82
  setSessionStorage(userCookie: UserCookie, username) {
83 83
    localStorage.setItem(GlobalVariable.USER_NAME, username)
84 84
    localStorage.setItem(GlobalVariable.RIGHTS, userCookie.rightsID.toString());
85
    this.cookieService.set('user', username);
85 86
  }
86 87
}
src/app/sensor/components/sensor.component.html
10 10
    <div class="input-group-prepend">
11 11
      <span class="input-group-text">From</span>
12 12
    </div>
13
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="from" [showTime]="true" (onSelect)="showGraph()" [maxDate]="today"></p-calendar>
13
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="from" [showTime]="true" (onSelect)="showGraph()" [maxDate]="today" showButtonBar="true"></p-calendar>
14 14
  </div>
15 15
</div>
16 16
<div class="col">
......
18 18
    <div class="input-group-prepend">
19 19
      <span class="input-group-text">To</span>
20 20
    </div>
21
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="to" [showTime]="true" (onSelect)="showGraph()" [maxDate]="today"></p-calendar>
21
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="to" [showTime]="true" (onSelect)="showGraph()" [maxDate]="today" showButtonBar="true"></p-calendar>
22 22
  </div>
23 23
</div>
24 24
<div *ngIf="showAggregation" class="col">
src/app/sensor/components/sensor.component.ts
49 49
  }
50 50

  
51 51
  showGraph() {
52
    let agg = 'HOUR';
53 52
    const range: Date[] = [moment().subtract(7, 'days').toDate(), moment().toDate()];
54 53

  
55 54
    if (this.from && !this.to || !this.from && this.to) {
......
63 62

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

  
68
    if (this.selectedAggregationFunction) {
69
      agg = this.selectedAggregationFunction;
70
    }
71

  
72
    if (moment(this.to).diff(moment(this.from), 'days') > 7) {
73
      this.getAnalytics(range, agg);
65
      this.getAnalytics(range);
74 66
    } else {
75
      this.getObservations(range, agg);
67
      this.showAggregation = false;
68
      this.getObservations(range);
76 69
    }
77 70
  }
78 71

  
79
  getAnalytics(range: Date[], agg: string) {
72
  getAnalytics(range: Date[]) {
80 73
    this.analyticsService.getAnalytics$Response({
81 74
      unit_id: this.unitId, sensor_id: this.sensorId,
82 75
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
83
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: agg
76
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction
84 77
    }).pipe(
85 78
      map((response: HttpResponse<any>) => {
86 79
        if (response.status === 200) {
......
102 95
      }, err => this.toastService.showError(err.error.message));
103 96
  }
104 97

  
105
  getObservations(range: Date[], agg: string) {
98
  getObservations(range: Date[]) {
106 99
    this.observationService.getObservation$Response({
107 100
      unit_id: this.unitId,
108 101
      sensor_id: this.sensorId,
src/app/shared/api/endpoints/api.module.ts
6 6

  
7 7
import { LoginService } from './services/login.service';
8 8
import { AdministrationService } from './services/administration.service';
9
import { SensorsService } from './services/sensors.service';
9 10
import { ObservationService } from './services/observation.service';
10 11
import { GroupService } from './services/group.service';
11 12
import { DataService } from './services/data.service';
12 13
import { AnalyticsService } from './services/analytics.service';
13 14
import { ManagementService } from './services/management.service';
14
import { SensorsService } from './services/sensors.service';
15 15

  
16 16
/**
17 17
 * Module that provides all services and configuration.
......
23 23
  providers: [
24 24
    LoginService,
25 25
    AdministrationService,
26
    SensorsService,
26 27
    ObservationService,
27 28
    GroupService,
28 29
    DataService,
29 30
    AnalyticsService,
30 31
    ManagementService,
31
    SensorsService,
32 32
    ApiConfiguration
33 33
  ],
34 34
})
src/app/shared/api/endpoints/services.ts
1 1
export { LoginService } from './services/login.service';
2 2
export { AdministrationService } from './services/administration.service';
3
export { SensorsService } from './services/sensors.service';
3 4
export { ObservationService } from './services/observation.service';
4 5
export { GroupService } from './services/group.service';
5 6
export { DataService } from './services/data.service';
6 7
export { AnalyticsService } from './services/analytics.service';
7 8
export { ManagementService } from './services/management.service';
8
export { SensorsService } from './services/sensors.service';
src/app/shared/api/endpoints/services/sensors.service.ts
10 10
import { map, filter } from 'rxjs/operators';
11 11

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

  
14 15

  
15 16
/**
......
26 27
    super(config, http);
27 28
  }
28 29

  
30
  /**
31
   * Path part for operation getUnitSensors
32
   */
33
  static readonly GetUnitSensorsPath = '/senslog1/SensorService?Operation=GetSensors';
34

  
35
  /**
36
   * Get unit sensors.
37
   *
38
   *
39
   *
40
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
41
   * To access only the response body, use `getUnitSensors()` instead.
42
   *
43
   * This method doesn't expect any request body.
44
   */
45
  getUnitSensors$Response(params: {
46
    unit_id: number;
47
  }): Observable<StrictHttpResponse<Array<Sensor>>> {
48

  
49
    const rb = new RequestBuilder(this.rootUrl, SensorsService.GetUnitSensorsPath, 'get');
50
    if (params) {
51
      rb.query('unit_id', params.unit_id, {});
52
    }
53

  
54
    return this.http.request(rb.build({
55
      responseType: 'json',
56
      accept: 'application/json'
57
    })).pipe(
58
      filter((r: any) => r instanceof HttpResponse),
59
      map((r: HttpResponse<any>) => {
60
        return r as StrictHttpResponse<Array<Sensor>>;
61
      })
62
    );
63
  }
64

  
65
  /**
66
   * Get unit sensors.
67
   *
68
   *
69
   *
70
   * This method provides access to only to the response body.
71
   * To access the full response (for headers, for example), `getUnitSensors$Response()` instead.
72
   *
73
   * This method doesn't expect any request body.
74
   */
75
  getUnitSensors(params: {
76
    unit_id: number;
77
  }): Observable<Array<Sensor>> {
78

  
79
    return this.getUnitSensors$Response(params).pipe(
80
      map((r: StrictHttpResponse<Array<Sensor>>) => r.body as Array<Sensor>)
81
    );
82
  }
83

  
29 84
  /**
30 85
   * Path part for operation getPhenomenons
31 86
   */
src/app/shared/api/openapi.yaml
88 88
            schema:
89 89
              $ref: '#/components/schemas/UserInfo'
90 90

  
91
  /senslog1/SensorService?Operation=GetSensors:
92
    get:
93
      tags:
94
        - sensors
95
      summary: Get unit sensors
96
      operationId: getUnitSensors
97
      parameters:
98
        - in: query
99
          name: unit_id
100
          required: true
101
          schema:
102
            type: integer
103
      responses:
104
        200:
105
          description: successful
106
          content:
107
            application/json:
108
              schema:
109
                type: array
110
                items:
111
                  $ref: '#/components/schemas/Sensor'
112

  
113

  
91 114
  /senslog1/SensorService?Operation=GetObservations:
92 115
    get:
93 116
      tags:
src/app/unit/components/unit.component.html
5 5
------
6 6
<br><br>
7 7

  
8
<div class="row">
9
  <div class="col">
10
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="rangeDates" selectionMode="range" [showTime]="true" [numberOfMonths]="3" (onSelect)="showGraph()"></p-calendar>
8
<div class="col">
9
  <div class="input-group form-group">
10
    <div class="input-group-prepend">
11
      <span class="input-group-text">From</span>
12
    </div>
13
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="from" [showTime]="true" (onSelect)="showGraph()" [maxDate]="today" showButtonBar="true"></p-calendar>
11 14
  </div>
12
  <div class="col">
13
    <p-listbox [options]="aggregationFunction" [(ngModel)]="selectedAggregationFunction" optionLabel="name" optionValue="code" (onClick)="showGraph()"></p-listbox>
15
</div>
16
<div class="col">
17
  <div class="input-group form-group">
18
    <div class="input-group-prepend">
19
      <span class="input-group-text">To</span>
20
    </div>
21
    <p-calendar [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" [(ngModel)]="to" [showTime]="true" (onSelect)="showGraph()" [maxDate]="today" showButtonBar="true"></p-calendar>
14 22
  </div>
15 23
</div>
16

  
17
<div *ngFor="let group of sensorGroups">
18
  <div id="vega_container_{{group}}"></div>
24
<div *ngIf="showAggregation" class="col">
25
  <div class="input-group form-group">
26
    <div class="input-group-prepend">
27
      <span class="input-group-text">Aggregation</span>
28
    </div>
29
    <p-listbox [options]="aggregationFunction" [(ngModel)]="selectedAggregationFunction" optionLabel="name" optionValue="code" (onClick)="showGraph()" ></p-listbox>
30
  </div>
19 31
</div>
20 32

  
21
  {{ data | json}}
33
<ng-container *ngFor="let group of sensorGroups">
34
  Group of sensors:
35
  <ng-container *ngFor="let sensor of sensors">
36
    <ng-container *ngIf="sensor.sensorId.toString().slice(0, 2) === group">
37
      {{ sensor.sensorId }}
38
    </ng-container>
39
  </ng-container>
40
  <div id="vega_container_{{group}}"></div>
41
</ng-container>
src/app/unit/components/unit.component.ts
1 1
import {Component, OnInit} from '@angular/core';
2 2
import {ActivatedRoute} from '@angular/router';
3 3
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
4
import {tap} from 'rxjs/operators';
5
import {Sensors} from '../../shared/api/endpoints/models/sensors';
4
import {map, tap} from 'rxjs/operators';
6 5
import {AggreationModel} from '../../shared/models/aggreation.model';
7 6
import * as moment from 'moment-timezone';
8 7
import {GraphLoader} from '../../shared/graph-loading/graphloader';
8
import {SensorsService} from '../../shared/api/endpoints/services/sensors.service';
9
import {HttpResponse} from '@angular/common/http';
10
import {ToastService} from '../../shared/services/toast.service';
11
import {Sensor} from '../../shared/api/endpoints/models/sensor';
9 12

  
10 13
declare var require: any
11 14

  
......
19 22
  preselectedSensors: string;
20 23
  unitId: number;
21 24
  viewCount = 0;
22
  data;
25
  data = [];
26
  time = [];
27
  from: Date;
28
  to: Date;
29
  today: Date;
23 30
  sensorGroups = [];
24
  rangeDates: Date[];
31
  selectedSensors = [];
32
  sensors: Sensor[];
33
  showAggregation = false;
25 34
  aggregationFunction: AggreationModel[];
26
  selectedAggregationFunction: string;
35
  selectedAggregationFunction = 'HOUR';
27 36

  
28 37
  constructor(
29 38
    private activatedRoute: ActivatedRoute,
30
    private analyticsService: AnalyticsService
39
    private analyticsService: AnalyticsService,
40
    private sensorService: SensorsService,
41
    private toastService: ToastService
31 42
  ) {
43
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
32 44
    this.aggregationFunction = [
33 45
      {name: 'Hour', code: 'HOUR'},
34 46
      {name: 'Day', code: 'DAY'},
35 47
      {name: 'Month', code: 'MONTH'},
36 48
      {name: 'Year', code: 'YEAR'}
37 49
    ];
50
    this.today = moment().toDate();
51
    this.sensorService.getUnitSensors({unit_id: this.unitId}).subscribe(
52
      sensors => {
53
        this.sensors = sensors;
54
        if (sensors) {
55
          sensors.forEach(sensor => {
56
            const sensorType = sensor.sensorId.toString().slice(0, 2);
57
            if (!this.sensorGroups.some(group => group === sensorType)) {
58
              this.sensorGroups.push(sensorType);
59
            }
60
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 2) === sensorType)) {
61
              this.selectedSensors.push(sensor.sensorId);
62
            }
63
          })
64
        }
65
      }
66
    )
38 67
  }
39 68

  
40 69
  ngOnInit(): void {
41
/*    this.route.queryParams.subscribe(params => {
42
      if(params.sensors)  {
43
        this.preselectedSensors = params.sensors;
44
      }
45
    });*/
46

  
47
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
48

  
49
    this.showGraph()
70
    setTimeout(() => {
71
      this.showGraph();
72
    }, 0);
50 73
  }
51 74

  
52 75
  showGraph() {
53
    let agg = 'HOUR';
54
    let range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
55
    if (this.selectedAggregationFunction) {
56
      agg = this.selectedAggregationFunction;
76
    const range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
77

  
78
    if (this.from && !this.to || !this.from && this.to) {
79
      return;
80
    }
81

  
82
    if (this.from && this.to) {
83
      range[0] = this.from;
84
      range[1] = this.to;
57 85
    }
58 86

  
59
    if (this.rangeDates) {
60
      range = this.rangeDates;
87
    if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
88
      this.showAggregation = true;
89
      this.getAnalytics(range);
90
    } else {
91
      this.showAggregation = false;
92
      // this.getObservations(range);
61 93
    }
94
  }
62 95

  
63
    this.analyticsService.getAnalytics({unit_id: this.unitId, sensor_id: null,
96
  getAnalytics(range: Date[]) {
97
    this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: null,
64 98
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
65
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: agg}).pipe(
66
      tap(data => {
67
        var objectKeys = Object.keys(data);
68
        for(const key of objectKeys ) {
69
          let element = this.createView();
70
          GraphLoader.getGraph(key, data[key]["data"], data[key]["interval"], element);
99
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
100
      map((response: HttpResponse<any>) => {
101
        if (response.status === 200) {
102
          return response.body;
103
        } else if (response.status === 204) {
104
          this.toastService.showWarningNoData();
105
          return response.body;
106
        } else {
107
          return false;
71 108
        }
72 109
      })
73
    ).subscribe();
74
  }
75

  
76
  createView() {
77
    let div = document.createElement("div");
78
    let name = "view" + this.viewCount;
79
    div.id = name;
80
    this.viewCount++;
81

  
82
    let container = document.getElementById("vega_container");
83
    container.appendChild(div);
84

  
85
    return '#' + name;
110
    ).subscribe(data => {
111
        const objectKeys = Object.keys(data);
112
        for(const key of objectKeys ) {
113
          if (data[key].data) {
114
            const view = '#vega_container_' + key.slice(0, 2);
115
            if (this.selectedSensors.some(sens => sens.toString() === key)) {
116
              GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
117
            }
118
          }
119
        }
120
      });
86 121
  }
87 122
}

Také k dispozici: Unified diff