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';
10 templateUrl: 'recent-imports.component.html'
13 export class RecentImportsComponent implements OnInit {
15 trackers: IdlObject[];
16 // eslint-disable-next-line no-magic-numbers
17 refreshInterval = 2000; // ms
22 private idl: IdlService,
23 private auth: AuthService,
24 private pcrud: PcrudService,
25 private vandelay: VandelayService
31 // Default to showing all trackers created today.
36 this.sinceDate = d.toISOString();
41 dateFilterChange(iso: string) {
44 if (this.pollTimeout) {
45 clearTimeout(this.pollTimeout);
46 this.pollTimeout = null;
55 // Report on recent trackers for this workstation and for the
56 // logged in user. Always show active trackers regardless
62 {workstation: this.auth.user().wsid()},
63 {usr: this.auth.user().id()}
67 {create_time: {'>=': this.sinceDate}},
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(_ => {
79 this.trackers.filter(t => t.state() === 'active');
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);
87 this.pollTimeout = null;
92 trackTheTracker(tracker: IdlObject) {
94 this.trackers.filter(t => t.id() === tracker.id())[0];
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());
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];
109 if (sameSes.action_type() === 'enqueue') {
110 // Remove the enqueueu tracker
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);
119 } else if (sameSes.action_type() === 'import') {
120 // Avoid adding the new enqueue tracker
125 this.trackers.unshift(tracker);
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)));