added ansible server functionality
[appc/cdt.git] / src / app / shared / services / procOnSrvSide.service.ts
1 //.. processing document on the server side
2 import { Injectable } from '@angular/core';
3 //import { HttpClient, HttpHeaders } from '@angular/common/http';
4 import { Http, Response, Headers, RequestOptions } from '@angular/http';
5 import { Observable } from 'rxjs';
6 import { NotificationsService } from 'angular2-notifications';
7
8 import { UtilityService } from '../../shared/services/utilityService/utility.service';
9
10 //const httpOptionsT = {
11  // headers: new HttpHeaders({ 'Content-Type': 'text/plain' })
12 //};
13
14 @Injectable(
15 // { providedIn: 'root' }
16 )
17 export class ProcOnSrvSideSvc
18 {
19   clName: string = "ProcOnSrvSideSvc";
20   public theUrl: string = "/api/proc_cont";
21   public resUrlPfx: string = "/api/get_result";
22   public parmsUrlPfx: string = "/api/get_params";
23   public taskId: string = '';
24   public stringBuf: string;
25   public responBuf: string;
26   public procResult: string;
27   public parmsBuf: string;
28   public responObj: any;
29  // private respObs: Observable<string>;
30   private respObs: Observable<Response>;
31   private respObsObj: Observable<Object>;
32   public ppartLen: number = 102400; //.. 102912 is too large payload
33  // public ppartLen: number = 10240;
34   public ppartCnt: number = 0;
35   public p_offset: number = 0;
36   public interval: any;
37   cycleCnt: number;
38   cycleMAX: number = 40;
39   editorHolder: any;
40   templSyncer: any;
41   fHeaders: Headers;
42   rOptions: RequestOptions;
43   noptions = {
44     timeOut: 4000,
45     showProgressBar: true,
46     pauseOnHover: true,
47     clickToClose: true,
48     maxLength: 250
49   };
50   prevTstampInt: number = 0;
51   currTstampInt: number = 0;
52   notifDelayMsec: number = 1200;
53
54   constructor(
55    // private http: HttpClient,
56     private http: Http,
57     private utilSvc: UtilityService,
58     private nService: NotificationsService )
59   {
60     if( this.utilSvc.getTracelvl() > 0 )
61       console.log(this.clName+": new: start");
62     this.fHeaders= new Headers({'Content-Type': 'text/plain'});
63     this.rOptions= new RequestOptions({'responseType':0});
64   }
65
66   sendToSrv( content: string, editorHolder: any, templSyncer: any ) {
67     var methName= "sendToSrv";
68     this.stringBuf= content;
69     if( this.utilSvc.getTracelvl() > 0 )
70       console.log(this.clName+": "+methName+": start: content length="+
71         this.stringBuf.length );
72     this.editorHolder= editorHolder;
73     this.templSyncer= templSyncer;
74     if( this.utilSvc.getTracelvl() > 0 )
75       console.log(this.clName+": "+methName+": emptying editor...");
76     this.editorHolder.editor.session.setValue("temp empty");
77     if( this.utilSvc.getTracelvl() > 0 )
78       console.log( this.clName+": "+methName+": theUrl:["+this.theUrl+"]");
79     this.nService.info( "Start processing",
80       "sending: content length="+this.stringBuf.length, this.noptions );
81     this.taskId= '';
82     let contLen= this.stringBuf.length;
83     if( this.utilSvc.getTracelvl() > 0 ) 
84       console.log(this.clName+": "+methName+": content length="+contLen+
85         " ppartLen="+this.ppartLen );
86     this.ppartCnt= 1+ Math.floor(contLen / this.ppartLen);
87     if( this.utilSvc.getTracelvl() > 0 )
88       console.log(this.clName+": "+methName+": ppartCnt="+ this.ppartCnt );
89     if( this.ppartCnt > 1 ) {
90       if( this.utilSvc.getTracelvl() > 0 )
91         console.log(this.clName+": "+methName+": will send multiple parts...");
92       this.nService.info( "Start processing", "will send multiple parts...");
93       this.prevTstampInt= Date.now();
94       this.p_offset= 0;
95       let ppart= this.stringBuf.substr( this.p_offset, this.ppartLen );
96       if( this.utilSvc.getTracelvl() > 0 )
97         console.log(this.clName+": "+methName+": First part:["+ppart+"]");
98       //.. first
99       this.sendPart( this.theUrl, ppart, 1 );
100     }
101     else { //.. ppartCnt == 1
102       if( this.utilSvc.getTracelvl() > 0 )
103         console.log(this.clName+": "+methName+": will send all-in-1");
104       this.nService.info( "Start processing",
105         "will send all-in-1 part", this.noptions);
106       this.prevTstampInt= Date.now();
107       //.. single
108       var sUrl= this.theUrl+"?part=1of1";
109       this.sendPart( sUrl, this.stringBuf, 1 );
110     };
111   }
112
113   sendPart( postUrl: string, contPart: string, partNum: number ) {
114     var methName= "sendPart";
115     if( this.utilSvc.getTracelvl() > 0 )
116       console.log(this.clName+": "+methName+": start: Url:["+postUrl+"]");
117     this.currTstampInt= Date.now();
118       let ntDiff= this.currTstampInt - this.prevTstampInt;
119       if( this.utilSvc.getTracelvl() > 1 )
120         console.log( this.clName+": "+methName+
121           ":  prevTstampInt="+this.prevTstampInt+
122           " currTstampInt="+this.currTstampInt+" the diff="+ntDiff );
123     if( ntDiff > this.notifDelayMsec ) {
124       if( this.utilSvc.getTracelvl() > 1 )
125         console.log(this.clName+": "+methName+": notif.delay's long enough.");
126       this.prevTstampInt= this.currTstampInt;
127       this.nService.info( "Transferring file",
128         " part Number="+partNum, this.noptions );
129     };
130     if( this.utilSvc.getTracelvl() > 0 )
131       console.log(this.clName+": "+methName+": part length="+ contPart.length );
132     this.respObs=
133       this.http.post( postUrl, contPart, this.rOptions );
134      // this.http.post<string>( postUrl, contPart, httpOptionsT );
135    // this.respObs.subscribe( (respo: string) => {
136     this.respObs.subscribe( (respo: Response) => {
137       if( this.utilSvc.getTracelvl() > 0 )
138         console.log( this.clName+": "+methName+": got response:["+respo+"]");
139       if( this.utilSvc.getTracelvl() > 1 )
140         console.log( this.clName+": "+methName+": json:["+
141           JSON.stringify(respo)+"]");
142       this.responBuf= respo.text();
143       if( this.utilSvc.getTracelvl() > 0 )
144         console.log( this.clName+": "+methName+": responBuf:["+this.responBuf+"]");
145       if( this.taskId.length < 1 ) {
146         if( this.utilSvc.getTracelvl() > 0 )
147           console.log( this.clName+": "+methName+
148             ": taskId is empty -get it from response");
149         let respObj= JSON.parse(this.responBuf);
150         if( this.utilSvc.getTracelvl() > 0 )
151           console.log( this.clName+": "+methName+": respObj.taskId:["+
152             respObj.taskId+"]");
153         if( respObj.taskId == null || respObj.taskId.length == 0 ) {
154           let errMsg= this.clName+": "+methName+
155             ": Error: failed to get taskId from the server response !";
156           console.log( errMsg );
157           this.nService.error( "Transferring file", errMsg, this.noptions );
158           return;
159         }
160         else { //.. extracted respObj.taskId
161           this.taskId= respObj.taskId;
162           if( this.utilSvc.getTracelvl() > 0 )
163             console.log( this.clName+": "+methName+": obtained new taskId:["+
164               this.taskId+"]");
165           this.nService.info( "Transferring file",
166             "current taskId:["+this.taskId+"]", this.noptions);
167         };
168       };
169       let tpercent= (100.0*partNum/this.ppartCnt).toFixed();
170       if( this.utilSvc.getTracelvl() > 0 )
171         console.log( this.clName+": "+methName+
172           ": part#="+partNum+" transfer percent="+tpercent );
173       this.currTstampInt= Date.now();
174       let ntDiff= this.currTstampInt - this.prevTstampInt;
175       if( this.utilSvc.getTracelvl() > 1 )
176         console.log( this.clName+": "+methName+
177           ":  prevTstampInt="+this.prevTstampInt+
178           " currTstampInt="+this.currTstampInt+" the diff="+ntDiff );
179       if( ntDiff > this.notifDelayMsec ) {
180         if( this.utilSvc.getTracelvl() > 1 )
181           console.log(this.clName+": "+methName+": notif.delay long enough.");
182         this.prevTstampInt= this.currTstampInt;
183         this.nService.info( "Transferring file",
184           " progress: "+tpercent+" %", this.noptions );
185        //" part Number="+partNum+" vs part Count="+this.ppartCnt, this.noptions );
186       };
187       if( partNum < this.ppartCnt ) {
188        // this.nService.info( methName,"need to send more parts...");
189         let partN= partNum + 1;
190         this.p_offset= this.p_offset + this.ppartLen;
191         var ppart= '';
192         if( partN < this.ppartCnt ) {
193           if( this.utilSvc.getTracelvl() > 0 )
194             console.log(this.clName+": "+methName+
195               ":  next part is not the last: partN="+partN );
196           ppart= this.stringBuf.substr( this.p_offset, this.ppartLen );
197         }
198         else {
199           if( this.utilSvc.getTracelvl() > 0 )
200             console.log(this.clName+": "+methName+ ": next part is the last.");
201           ppart= this.stringBuf.substr( this.p_offset );
202         };
203         if( this.utilSvc.getTracelvl() > 0 )
204           console.log(this.clName+": "+methName+": next part:["+ppart+"]");
205         let nUrl=
206           this.theUrl+"?taskId="+this.taskId+"&part="+partN+"of"+this.ppartCnt;
207         if( this.utilSvc.getTracelvl() > 0 )
208           console.log(this.clName+": "+methName+": next Url:["+nUrl+"]");
209         this.sendPart( nUrl, ppart, partN );
210       }
211       else { //.. partNum == this.ppartCnt
212         this.nService.info( "Transferring file",
213           "all "+this.ppartCnt+ " parts are sent - check processing...",
214           this.noptions);
215         var progrUrl= "/api/get_progress?taskId="+this.taskId;
216         if( this.utilSvc.getTracelvl() > 0 )
217           console.log(this.clName+": "+methName+": progrUrl:["+progrUrl+"]");
218         this.showProcProgr( progrUrl );
219       };
220     },
221     error => {
222       console.log( this.clName+": "+methName+
223         ": got Error:["+JSON.stringify(error)+']');
224       this.responBuf= JSON.stringify(error);
225       this.nService.error( "Transferring file",
226         " Error:["+this.responBuf+"]", this.noptions);
227     });
228   }
229
230   showProcProgr( proUrl: string ) {
231     var methName= "showProcProgr";
232     if( this.utilSvc.getTracelvl() > 0 )
233       console.log(methName+": start: proUrl:["+proUrl+"]");
234     this.cycleCnt= 0;
235     this.interval = setInterval( () => {
236       if( this.utilSvc.getTracelvl() > 1 )
237         console.log(methName+": call getProcProgr");
238       this.getProcProgr( proUrl );
239     }, 2500 );
240   }
241
242   getProcProgr( proUrl: string ) {
243     var methName= "getProcProgr";
244     if( this.utilSvc.getTracelvl() > 0 )
245       console.log( methName+": getProcProgr: start: proUrl:["+proUrl+"]");
246     this.cycleCnt++;
247     if( this.utilSvc.getTracelvl() > 0 )
248       console.log( methName+": cycleCnt="+this.cycleCnt );
249     this.nService.info( "Processing",
250       "Requesting server status...", this.noptions);
251    // this.respObsObj=
252    //   this.http.get( proUrl );
253     this.respObs=
254       this.http.get( proUrl );
255    // this.respObs.subscribe( (respo: string) => {
256     //this.respObsObj.subscribe( (respo)  => {
257     this.respObs.subscribe( (respo: Response) => {
258       if( this.utilSvc.getTracelvl() > 0 )
259         console.log( methName+": response:["+JSON.stringify(respo)+"]");
260       this.responBuf= respo.text();
261       if( this.utilSvc.getTracelvl() > 0 )
262         console.log( methName+": responBuf:["+this.responBuf+"]");
263       let respObj= JSON.parse(this.responBuf);
264      // this.responObj= respo; //.. Object
265       if( respObj.percentage != undefined &&
266           respObj.percentage != null )
267       {
268         if( this.utilSvc.getTracelvl() > 0 )
269           console.log(methName+": got percentage:["+respObj.percentage+"]");
270         if( respObj.percentage >= 100.0 ) {
271           if( this.utilSvc.getTracelvl() > 0 )
272             console.log(methName+": percentage == 100 !");
273           this.nService.info( "Processing completed",
274             "The server finished: 100% !", this.noptions);
275           clearInterval( this.interval );
276           if( this.utilSvc.getTracelvl() > 0 )
277             console.log(methName+": getting the processing result...");
278           this.getProcResult();
279         }
280       };
281       if( this.utilSvc.getTracelvl() > 0 )
282         console.log(methName+": cycleCnt="+this.cycleCnt+
283           " vs MAX="+this.cycleMAX );
284       if( this.cycleCnt > this.cycleMAX ) {
285         this.nService.error( "Processing",
286           "Too many status requests - stop !",this.noptions );
287         clearInterval( this.interval );
288       }
289     },
290     error => {
291       console.log( this.clName+": "+methName+":  got Error:["+
292         JSON.stringify(error)+']');
293       this.responObj= error; //.. as Object
294       this.nService.error( "Processing"," Error:["+
295          JSON.stringify(error)+']', this.noptions );
296       clearInterval( this.interval );
297     });
298   }
299
300   getProcResult() {
301     var methName= "getProcResult";
302     let resUrl= this.resUrlPfx+"?taskId="+this.taskId;
303     if( this.utilSvc.getTracelvl() > 0 )
304       console.log( this.clName+": "+methName+": start: resUrl:["+resUrl+"]");
305     this.respObs=
306       this.http.get( resUrl, this.rOptions );
307      // this.http.get( resUrl, {responseType: 'text'} );
308    // this.respObs.subscribe( (respo: string) => {
309     this.respObs.subscribe( (respo: Response) => {
310       if( this.utilSvc.getTracelvl() > 0 )
311         console.log( this.clName+": "+methName+": got response:["+respo+"]");
312       if( this.utilSvc.getTracelvl() > 1 )
313         console.log( this.clName+": "+methName+": json:["+
314           JSON.stringify(respo)+"]");
315       this.responBuf= respo.text();
316       if( this.utilSvc.getTracelvl() > 1 )
317         console.log( this.clName+": "+methName+": responBuf:["+this.responBuf+"]");
318       if( this.utilSvc.getTracelvl() == 0 ) {
319         let respoBg= this.responBuf.substr(0, 300);
320         console.log(this.clName+": "+methName+": response Begin:["+respoBg+"...]");
321       };
322       this.procResult= this.responBuf;
323       this.nService.info( "Processing completed",
324         "the result length="+this.procResult.length, this.noptions );
325       if( this.utilSvc.getTracelvl() > 0 )
326         console.log( this.clName+": "+methName+
327           ": setting response to the editor...");
328       this.editorHolder.editor.session.setValue( this.procResult );
329       if( this.utilSvc.getTracelvl() > 0 )
330         console.log( this.clName+": "+methName+": calling syncTemplate ...");
331       this.templSyncer.syncTemplate('1');
332       if( this.utilSvc.getTracelvl() > 0 )
333         console.log( this.clName+": "+methName+": finished.");
334     },
335     error => {
336       console.log( this.clName+": "+methName+": subscribe Error:["+
337         JSON.stringify(error)+']');
338       this.procResult= JSON.stringify(error);
339       this.nService.error( "Getting Processing result",
340         " Error:["+JSON.stringify(error)+']', this.noptions);
341     });
342   }
343 }