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