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';
8 import { UtilityService } from '../../shared/services/utilityService/utility.service';
10 //const httpOptionsT = {
11 // headers: new HttpHeaders({ 'Content-Type': 'text/plain' })
15 // { providedIn: 'root' }
17 export class ProcOnSrvSideSvc
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;
38 cycleMAX: number = 40;
42 rOptions: RequestOptions;
45 showProgressBar: true,
50 prevTstampInt: number = 0;
51 currTstampInt: number = 0;
52 notifDelayMsec: number = 1200;
55 // private http: HttpClient,
57 private utilSvc: UtilityService,
58 private nService: NotificationsService )
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});
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 );
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();
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+"]");
99 this.sendPart( this.theUrl, ppart, 1 );
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();
108 var sUrl= this.theUrl+"?part=1of1";
109 this.sendPart( sUrl, this.stringBuf, 1 );
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 );
130 if( this.utilSvc.getTracelvl() > 0 )
131 console.log(this.clName+": "+methName+": part length="+ contPart.length );
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:["+
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 );
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:["+
165 this.nService.info( "Transferring file",
166 "current taskId:["+this.taskId+"]", this.noptions);
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 );
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;
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 );
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 );
203 if( this.utilSvc.getTracelvl() > 0 )
204 console.log(this.clName+": "+methName+": next part:["+ppart+"]");
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 );
211 else { //.. partNum == this.ppartCnt
212 this.nService.info( "Transferring file",
213 "all "+this.ppartCnt+ " parts are sent - check processing...",
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 );
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);
230 showProcProgr( proUrl: string ) {
231 var methName= "showProcProgr";
232 if( this.utilSvc.getTracelvl() > 0 )
233 console.log(methName+": start: proUrl:["+proUrl+"]");
235 this.interval = setInterval( () => {
236 if( this.utilSvc.getTracelvl() > 1 )
237 console.log(methName+": call getProcProgr");
238 this.getProcProgr( proUrl );
242 getProcProgr( proUrl: string ) {
243 var methName= "getProcProgr";
244 if( this.utilSvc.getTracelvl() > 0 )
245 console.log( methName+": getProcProgr: start: proUrl:["+proUrl+"]");
247 if( this.utilSvc.getTracelvl() > 0 )
248 console.log( methName+": cycleCnt="+this.cycleCnt );
249 this.nService.info( "Processing",
250 "Requesting server status...", this.noptions);
252 // this.http.get( proUrl );
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 )
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();
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 );
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 );
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+"]");
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+"...]");
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.");
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);