]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/vandelay/recent-imports.component.ts
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / recent-imports.component.ts
1 import {Component, OnInit} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {tap, concatMap} from 'rxjs/operators';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {VandelayService} from './vandelay.service';
8
9 @Component({
10     templateUrl: 'recent-imports.component.html'
11 })
12
13 export class RecentImportsComponent implements OnInit {
14
15     trackers: IdlObject[];
16     // eslint-disable-next-line no-magic-numbers
17     refreshInterval = 2000; // ms
18     sinceDate: string;
19     pollTimeout: any;
20
21     constructor(
22         private idl: IdlService,
23         private auth: AuthService,
24         private pcrud: PcrudService,
25         private vandelay: VandelayService
26     ) {
27         this.trackers = [];
28     }
29
30     ngOnInit() {
31         // Default to showing all trackers created today.
32         const d = new Date();
33         d.setHours(0);
34         d.setMinutes(0);
35         d.setSeconds(0);
36         this.sinceDate = d.toISOString();
37
38         this.pollTrackers();
39     }
40
41     dateFilterChange(iso: string) {
42         if (iso) {
43             this.sinceDate = iso;
44             if (this.pollTimeout) {
45                 clearTimeout(this.pollTimeout);
46                 this.pollTimeout = null;
47             }
48             this.trackers = [];
49             this.pollTrackers();
50         }
51     }
52
53     pollTrackers() {
54
55         // Report on recent trackers for this workstation and for the
56         // logged in user.  Always show active trackers regardless
57         // of sinceDate.
58         const query: any = {
59             '-and': [
60                 {
61                     '-or': [
62                         {workstation: this.auth.user().wsid()},
63                         {usr: this.auth.user().id()}
64                     ],
65                 }, {
66                     '-or': [
67                         {create_time: {'>=': this.sinceDate}},
68                         {state: 'active'}
69                     ]
70                 }
71             ]
72         };
73
74         this.pcrud.search('vst', query, {order_by: {vst: 'create_time'}})
75             .pipe(tap(tracker => this.trackTheTracker(tracker)))
76             .pipe(concatMap(tracker => this.fleshTrackerQueue(tracker)))
77             .toPromise().then(_ => {
78                 const active =
79                 this.trackers.filter(t => t.state() === 'active');
80
81                 // Continue updating the display with updated tracker
82                 // data as long as we have any active trackers.
83                 if (active.length > 0) {
84                     this.pollTimeout = setTimeout(
85                         () => this.pollTrackers(), this.refreshInterval);
86                 } else {
87                     this.pollTimeout = null;
88                 }
89             });
90     }
91
92     trackTheTracker(tracker: IdlObject) {
93         const existing =
94             this.trackers.filter(t => t.id() === tracker.id())[0];
95
96         if (existing) {
97             existing.update_time(tracker.update_time());
98             existing.state(tracker.state());
99             existing.total_actions(tracker.total_actions());
100             existing.actions_performed(tracker.actions_performed());
101         } else {
102
103             // Only show the import tracker when both an enqueue
104             // and import tracker exist for a given session.
105             const sameSes = this.trackers.filter(
106                 t => t.session_key() === tracker.session_key())[0];
107
108             if (sameSes) {
109                 if (sameSes.action_type() === 'enqueue') {
110                     // Remove the enqueueu tracker
111
112                     for (let idx = 0; idx < this.trackers.length; idx++) {
113                         const trkr = this.trackers[idx];
114                         if (trkr.id() === sameSes.id()) {
115                             this.trackers.splice(idx, 1);
116                             break;
117                         }
118                     }
119                 } else if (sameSes.action_type() === 'import') {
120                     // Avoid adding the new enqueue tracker
121                     return;
122                 }
123             }
124
125             this.trackers.unshift(tracker);
126         }
127     }
128
129     fleshTrackerQueue(tracker: IdlObject): Observable<any> {
130         const qClass = tracker.record_type() === 'bib' ? 'vbq' : 'vaq';
131         return this.pcrud.retrieve(qClass, tracker.queue())
132             .pipe(tap(queue => tracker.queue(queue)));
133     }
134 }