aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/shared/services/procOnSrvSide.service.ts
blob: 17666d34932739f62d175582f961a48faacb1939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//.. processing document on the server side
import { Injectable } from '@angular/core';
//import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import { NotificationsService } from 'angular2-notifications';

import { UtilityService } from '../../shared/services/utilityService/utility.service';

//const httpOptionsT = {
 // headers: new HttpHeaders({ 'Content-Type': 'text/plain' })
//};

@Injectable(
// { providedIn: 'root' }
)
export class ProcOnSrvSideSvc
{
  clName: string = "ProcOnSrvSideSvc";
  public theUrl: string = "/api/proc_cont";
  public resUrlPfx: string = "/api/get_result";
  public parmsUrlPfx: string = "/api/get_params";
  public taskId: string = '';
  public stringBuf: string;
  public responBuf: string;
  public procResult: string;
  public parmsBuf: string;
  public responObj: any;
 // private respObs: Observable<string>;
  private respObs: Observable<Response>;
  private respObsObj: Observable<Object>;
  public ppartLen: number = 102400; //.. 102912 is too large payload
 // public ppartLen: number = 10240;
  public ppartCnt: number = 0;
  public p_offset: number = 0;
  public interval: any;
  cycleCnt: number;
  cycleMAX: number = 40;
  editorHolder: any;
  templSyncer: any;
  fHeaders: Headers;
  rOptions: RequestOptions;
  noptions = {
    timeOut: 4000,
    showProgressBar: true,
    pauseOnHover: true,
    clickToClose: true,
    maxLength: 250
  };
  prevTstampInt: number = 0;
  currTstampInt: number = 0;
  notifDelayMsec: number = 1200;

  constructor(
   // private http: HttpClient,
    private http: Http,
    private utilSvc: UtilityService,
    private nService: NotificationsService )
  {
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(this.clName+": new: start");
    this.fHeaders= new Headers({'Content-Type': 'text/plain'});
    this.rOptions= new RequestOptions({'responseType':0});
  }

  sendToSrv( content: string, editorHolder: any, templSyncer: any ) {
    var methName= "sendToSrv";
    this.stringBuf= content;
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(this.clName+": "+methName+": start: content length="+
        this.stringBuf.length );
    this.editorHolder= editorHolder;
    this.templSyncer= templSyncer;
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(this.clName+": "+methName+": emptying editor...");
    this.editorHolder.editor.session.setValue("temp empty");
    if( this.utilSvc.getTracelvl() > 0 )
      console.log( this.clName+": "+methName+": theUrl:["+this.theUrl+"]");
    this.nService.info( "Start processing",
      "sending: content length="+this.stringBuf.length, this.noptions );
    this.taskId= '';
    let contLen= this.stringBuf.length;
    if( this.utilSvc.getTracelvl() > 0 ) 
      console.log(this.clName+": "+methName+": content length="+contLen+
        " ppartLen="+this.ppartLen );
    this.ppartCnt= 1+ Math.floor(contLen / this.ppartLen);
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(this.clName+": "+methName+": ppartCnt="+ this.ppartCnt );
    if( this.ppartCnt > 1 ) {
      if( this.utilSvc.getTracelvl() > 0 )
        console.log(this.clName+": "+methName+": will send multiple parts...");
      this.nService.info( "Start processing", "will send multiple parts...");
      this.prevTstampInt= Date.now();
      this.p_offset= 0;
      let ppart= this.stringBuf.substr( this.p_offset, this.ppartLen );
      if( this.utilSvc.getTracelvl() > 0 )
        console.log(this.clName+": "+methName+": First part:["+ppart+"]");
      //.. first
      this.sendPart( this.theUrl, ppart, 1 );
    }
    else { //.. ppartCnt == 1
      if( this.utilSvc.getTracelvl() > 0 )
        console.log(this.clName+": "+methName+": will send all-in-1");
      this.nService.info( "Start processing",
        "will send all-in-1 part", this.noptions);
      this.prevTstampInt= Date.now();
      //.. single
      var sUrl= this.theUrl+"?part=1of1";
      this.sendPart( sUrl, this.stringBuf, 1 );
    };
  }

  sendPart( postUrl: string, contPart: string, partNum: number ) {
    var methName= "sendPart";
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(this.clName+": "+methName+": start: Url:["+postUrl+"]");
    this.currTstampInt= Date.now();
      let ntDiff= this.currTstampInt - this.prevTstampInt;
      if( this.utilSvc.getTracelvl() > 1 )
        console.log( this.clName+": "+methName+
          ":  prevTstampInt="+this.prevTstampInt+
          " currTstampInt="+this.currTstampInt+" the diff="+ntDiff );
    if( ntDiff > this.notifDelayMsec ) {
      if( this.utilSvc.getTracelvl() > 1 )
        console.log(this.clName+": "+methName+": notif.delay's long enough.");
      this.prevTstampInt= this.currTstampInt;
      this.nService.info( "Transferring file",
        " part Number="+partNum, this.noptions );
    };
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(this.clName+": "+methName+": part length="+ contPart.length );
    this.respObs=
      this.http.post( postUrl, contPart, this.rOptions );
     // this.http.post<string>( postUrl, contPart, httpOptionsT );
   // this.respObs.subscribe( (respo: string) => {
    this.respObs.subscribe( (respo: Response) => {
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+": got response:["+respo+"]");
      if( this.utilSvc.getTracelvl() > 1 )
        console.log( this.clName+": "+methName+": json:["+
          JSON.stringify(respo)+"]");
      this.responBuf= respo.text();
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+": responBuf:["+this.responBuf+"]");
      if( this.taskId.length < 1 ) {
        if( this.utilSvc.getTracelvl() > 0 )
          console.log( this.clName+": "+methName+
            ": taskId is empty -get it from response");
        let respObj= JSON.parse(this.responBuf);
        if( this.utilSvc.getTracelvl() > 0 )
          console.log( this.clName+": "+methName+": respObj.taskId:["+
            respObj.taskId+"]");
        if( respObj.taskId == null || respObj.taskId.length == 0 ) {
          let errMsg= this.clName+": "+methName+
            ": Error: failed to get taskId from the server response !";
          console.log( errMsg );
          this.nService.error( "Transferring file", errMsg, this.noptions );
          return;
        }
        else { //.. extracted respObj.taskId
          this.taskId= respObj.taskId;
          if( this.utilSvc.getTracelvl() > 0 )
            console.log( this.clName+": "+methName+": obtained new taskId:["+
              this.taskId+"]");
          this.nService.info( "Transferring file",
            "current taskId:["+this.taskId+"]", this.noptions);
        };
      };
      let tpercent= (100.0*partNum/this.ppartCnt).toFixed();
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+
          ": part#="+partNum+" transfer percent="+tpercent );
      this.currTstampInt= Date.now();
      let ntDiff= this.currTstampInt - this.prevTstampInt;
      if( this.utilSvc.getTracelvl() > 1 )
        console.log( this.clName+": "+methName+
          ":  prevTstampInt="+this.prevTstampInt+
          " currTstampInt="+this.currTstampInt+" the diff="+ntDiff );
      if( ntDiff > this.notifDelayMsec ) {
        if( this.utilSvc.getTracelvl() > 1 )
          console.log(this.clName+": "+methName+": notif.delay long enough.");
        this.prevTstampInt= this.currTstampInt;
        this.nService.info( "Transferring file",
          " progress: "+tpercent+" %", this.noptions );
       //" part Number="+partNum+" vs part Count="+this.ppartCnt, this.noptions );
      };
      if( partNum < this.ppartCnt ) {
       // this.nService.info( methName,"need to send more parts...");
        let partN= partNum + 1;
        this.p_offset= this.p_offset + this.ppartLen;
        var ppart= '';
        if( partN < this.ppartCnt ) {
          if( this.utilSvc.getTracelvl() > 0 )
            console.log(this.clName+": "+methName+
              ":  next part is not the last: partN="+partN );
          ppart= this.stringBuf.substr( this.p_offset, this.ppartLen );
        }
        else {
          if( this.utilSvc.getTracelvl() > 0 )
            console.log(this.clName+": "+methName+ ": next part is the last.");
          ppart= this.stringBuf.substr( this.p_offset );
        };
        if( this.utilSvc.getTracelvl() > 0 )
          console.log(this.clName+": "+methName+": next part:["+ppart+"]");
        let nUrl=
          this.theUrl+"?taskId="+this.taskId+"&part="+partN+"of"+this.ppartCnt;
        if( this.utilSvc.getTracelvl() > 0 )
          console.log(this.clName+": "+methName+": next Url:["+nUrl+"]");
        this.sendPart( nUrl, ppart, partN );
      }
      else { //.. partNum == this.ppartCnt
        this.nService.info( "Transferring file",
          "all "+this.ppartCnt+ " parts are sent - check processing...",
          this.noptions);
        var progrUrl= "/api/get_progress?taskId="+this.taskId;
        if( this.utilSvc.getTracelvl() > 0 )
          console.log(this.clName+": "+methName+": progrUrl:["+progrUrl+"]");
        this.showProcProgr( progrUrl );
      };
    },
    error => {
      console.log( this.clName+": "+methName+
        ": got Error:["+JSON.stringify(error)+']');
      this.responBuf= JSON.stringify(error);
      this.nService.error( "Transferring file",
        " Error:["+this.responBuf+"]", this.noptions);
    });
  }

  showProcProgr( proUrl: string ) {
    var methName= "showProcProgr";
    if( this.utilSvc.getTracelvl() > 0 )
      console.log(methName+": start: proUrl:["+proUrl+"]");
    this.cycleCnt= 0;
    this.interval = setInterval( () => {
      if( this.utilSvc.getTracelvl() > 1 )
        console.log(methName+": call getProcProgr");
      this.getProcProgr( proUrl );
    }, 2500 );
  }

  getProcProgr( proUrl: string ) {
    var methName= "getProcProgr";
    if( this.utilSvc.getTracelvl() > 0 )
      console.log( methName+": getProcProgr: start: proUrl:["+proUrl+"]");
    this.cycleCnt++;
    if( this.utilSvc.getTracelvl() > 0 )
      console.log( methName+": cycleCnt="+this.cycleCnt );
    this.nService.info( "Processing",
      "Requesting server status...", this.noptions);
   // this.respObsObj=
   //   this.http.get( proUrl );
    this.respObs=
      this.http.get( proUrl );
   // this.respObs.subscribe( (respo: string) => {
    //this.respObsObj.subscribe( (respo)  => {
    this.respObs.subscribe( (respo: Response) => {
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( methName+": response:["+JSON.stringify(respo)+"]");
      this.responBuf= respo.text();
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( methName+": responBuf:["+this.responBuf+"]");
      let respObj= JSON.parse(this.responBuf);
     // this.responObj= respo; //.. Object
      if( respObj.percentage != undefined &&
          respObj.percentage != null )
      {
        if( this.utilSvc.getTracelvl() > 0 )
          console.log(methName+": got percentage:["+respObj.percentage+"]");
        if( respObj.percentage >= 100.0 ) {
          if( this.utilSvc.getTracelvl() > 0 )
            console.log(methName+": percentage == 100 !");
          this.nService.info( "Processing completed",
            "The server finished: 100% !", this.noptions);
          clearInterval( this.interval );
          if( this.utilSvc.getTracelvl() > 0 )
            console.log(methName+": getting the processing result...");
          this.getProcResult();
        }
      };
      if( this.utilSvc.getTracelvl() > 0 )
        console.log(methName+": cycleCnt="+this.cycleCnt+
          " vs MAX="+this.cycleMAX );
      if( this.cycleCnt > this.cycleMAX ) {
        this.nService.error( "Processing",
          "Too many status requests - stop !",this.noptions );
        clearInterval( this.interval );
      }
    },
    error => {
      console.log( this.clName+": "+methName+":  got Error:["+
        JSON.stringify(error)+']');
      this.responObj= error; //.. as Object
      this.nService.error( "Processing"," Error:["+
         JSON.stringify(error)+']', this.noptions );
      clearInterval( this.interval );
    });
  }

  getProcResult() {
    var methName= "getProcResult";
    let resUrl= this.resUrlPfx+"?taskId="+this.taskId;
    if( this.utilSvc.getTracelvl() > 0 )
      console.log( this.clName+": "+methName+": start: resUrl:["+resUrl+"]");
    this.respObs=
      this.http.get( resUrl, this.rOptions );
     // this.http.get( resUrl, {responseType: 'text'} );
   // this.respObs.subscribe( (respo: string) => {
    this.respObs.subscribe( (respo: Response) => {
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+": got response:["+respo+"]");
      if( this.utilSvc.getTracelvl() > 1 )
        console.log( this.clName+": "+methName+": json:["+
          JSON.stringify(respo)+"]");
      this.responBuf= respo.text();
      if( this.utilSvc.getTracelvl() > 1 )
        console.log( this.clName+": "+methName+": responBuf:["+this.responBuf+"]");
      if( this.utilSvc.getTracelvl() == 0 ) {
        let respoBg= this.responBuf.substr(0, 300);
        console.log(this.clName+": "+methName+": response Begin:["+respoBg+"...]");
      };
      this.procResult= this.responBuf;
      this.nService.info( "Processing completed",
        "the result length="+this.procResult.length, this.noptions );
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+
          ": setting response to the editor...");
      this.editorHolder.editor.session.setValue( this.procResult );
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+": calling syncTemplate ...");
      this.templSyncer.syncTemplate('1');
      if( this.utilSvc.getTracelvl() > 0 )
        console.log( this.clName+": "+methName+": finished.");
    },
    error => {
      console.log( this.clName+": "+methName+": subscribe Error:["+
        JSON.stringify(error)+']');
      this.procResult= JSON.stringify(error);
      this.nService.error( "Getting Processing result",
        " Error:["+JSON.stringify(error)+']', this.noptions);
    });
  }
}