-
Notifications
You must be signed in to change notification settings - Fork 6
/
StripDataSource.c
2186 lines (1879 loc) · 61 KB
/
StripDataSource.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************\
* Copyright (c) 1994-2004 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 1997-2003 Southeastern Universities Research Association,
* as Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 1997-2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/* Notes
*
* This module performs several functions:
*
* (1) acquires real-time data and stores it in a ring
* buffer.
* (2) passes off requests for older data to the archive
* service.
* (3) on a per-curve basis, renders data into XSegments,
* given the appropriate transformation parameters.
*
* So, how does it work? Basically, the user must first
* call init_range() in order to specify what data will
* be rendered. This routine takes a begin time, bin size,
* and number of bins. With this information, it first
* scans through the ring buffer to determine the indexes
* of the samples corresponding to the endpoints lying
* within the requested range, and saves this in the
* DataSource structure. Next, each curve is scanned
* in order to determine whether it contains any data
* on the requested range. If not, and if a request has
* not already been sent to the archive service, one
* such request is issued, and a status variable is toggled
* indicating that a history request for the given time
* is outstanding.
*
* Once the range has been initialized, render() may be
* called to generate XSegment structures describing the
* rasterized data on the time range. If only new data
* is requested, then all data greater than the global
* start time and less than the already-rendered start
* time are built into segments, and connected to the
* end point of the already rendered data. This is
* accomplished by first checking the buffer for older
* data, and then the history result structure. Similarly,
* the rendered endpoint is compared with the global end
* time. If the latter is greater, then the newer points
* are built into segments and connected to the endpoint.
*
* In order to facilitate this building of segments, the
* routine called "segmentify()" takes, among other parameters,
* time, value, and status pointers which indicate the location
* in memory of some contiguous array of sequential samples.
* Depending on the specified direction parameter, these pointers
* are either incremented or decremented until either the maximum
* number of points have been processed or the referenced time
* falls past the specified stop_time (meaning "before" in the case
* of a backward direction, or "after" in the case of a forward
* direction). The caller may also specify connecting endpoints
* to which the generated line segments must be attached.
*/
#define DEBUG1 0
#define DEBUG_SDS_TIMES 0
#define DEBUG_SDDS 0
#ifdef USE_SDDS
#include "SDDS.h"
#endif
#include "StripDataSource.h"
#include "StripDefines.h"
#include "StripMisc.h"
#include "StripGraph.h" /* Albert */
#include <X11/cursorfont.h>
extern Widget history_topShell;
extern int auto_scaleTriger;
extern long radioChange;
static Cursor cursor = 0;
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t)-1)
#endif
#define CURVE_DATA(C) ((CurveData *)((StripCurveInfo *)C)->id)
#define SDS_LTE 0
#define SDS_GTE 1
#define SDS_DUMP_FIELDWIDTH 33 /* Albert -- was 30 */
#define SDS_DUMP_NUMWIDTH 23 /* Albert -- was 20 */
#define SDS_DUMP_BADVALUESTR "BadVal"
#define SDS_BUFFERED_DATA (1 << 0)
#define SDS_HISTORY_DATA (1 << 1)
#define SDS_BOTH_DATA (SDS_BUFFERED_DATA | SDS_HISTORY_DATA)
#define DUMP_SDDS_TIME_COL "Time"
#define DUMP_SDDS_TIME_COL_UNITS "seconds"
#define DUMP_SDDS_CONTENTS "StripTool data"
#define DUMP_SDDS_DESCRIPTION ""
static RenderBuffer render_buffer = {0, 0, 0};
/* These are used as parameter types for segmentify() */
typedef struct _TimeBuffer
{
struct timeval *base;
struct timeval *ptr;
size_t count;
} TimeBuffer;
typedef struct _ValueBuffer
{
double *base;
double *ptr;
size_t count;
} ValueBuffer;
typedef struct _StatusBuffer
{
StatusType *base;
StatusType *ptr;
size_t count;
} StatusBuffer;
typedef enum _SegmentifyDirection
{
SDS_INCREASING, SDS_DECREASING
}
SegmentifyDirection;
static size_t segmentify (StripDataSourceInfo *,
RenderBuffer *,
SegmentifyDirection,
TimeBuffer *,
ValueBuffer *,
StatusBuffer *,
int,
struct timeval *,
DataPoint *,
DataPoint *,
DataPoint *,
DataPoint *,
sdsTransform,
void *,
sdsTransform,
void *);
static int resize (StripDataSourceInfo *sds,
size_t buf_size);
static long find_date_idx (struct timeval *t,
struct timeval *times,
size_t n_times,
size_t max_times,
size_t idx_latest,
int mode);
static int pack_array (void **, size_t,
int, int, int,
int, int *, int *);
static int verify_render_buffer (RenderBuffer *, int);
static int printData(struct timeval *t,CurveData *c,char *v); /*Albert */
static int findNextTime(struct timeval *tv,struct timeval *res,StripDataSourceInfo *s) ; /*Albert */
/*
* StripDataSource_init
*/
StripDataSource
StripDataSource_init (StripHistory history)
{
StripDataSourceInfo *sds;
/* allocate and zero the DataSource structure */
#if DEBUG_SDS_TIMES
printf("StripDataSource_init: malloc StripDataSourceInfo\n");
#endif
if ((sds = (StripDataSourceInfo *)malloc (sizeof(StripDataSourceInfo))))
{
sds->history = history;
sds->buf_size = 0;
sds->cur_idx = 0;
sds->count = 0;
sds->times = 0;
sds->idx_t0 = 0;
sds->idx_t1 = 0;
sds->bin_size = 0;
sds->n_bins = 0;
/* clear the buffers */
memset (sds->buffers, 0, STRIP_MAX_CURVES * sizeof(CurveData));
}
return sds;
}
/*
* StripDataSource_delete
*/
void
StripDataSource_delete (StripDataSource the_sds)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
int i;
for (i = 0; i < STRIP_MAX_CURVES; i++)
{
if (sds->buffers[i].val)
free (sds->buffers[i].val);
if (sds->buffers[i].stat)
free (sds->buffers[i].stat);
}
free (sds);
}
/*
* StripDataSource_setattr
*/
int
StripDataSource_setattr (StripDataSource the_sds, ...)
{
va_list ap;
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
int attrib;
size_t tmp;
int ret_val = 1;
va_start (ap, the_sds);
for (attrib = va_arg (ap, SDSAttribute);
(attrib != 0);
attrib = va_arg (ap, SDSAttribute))
{
if ((ret_val = ((attrib > 0) && (attrib < SDS_LAST_ATTRIBUTE))))
switch (attrib)
{
case SDS_NUMSAMPLES:
tmp = va_arg (ap, size_t);
#if DEBUG_SDS_TIMES
printf("StripDataSource_setattr: tmp=%u buf_size=%u resize=%s\n",
tmp,sds->buf_size,tmp>sds->buf_size?"True":"False");
#endif
if (tmp > sds->buf_size) resize (sds, tmp);
#if DEBUG_SDS_TIMES
printf("StripDataSource_setattr: After resize buf_size=%u\n",
sds->buf_size);
#endif
break;
}
}
va_end (ap);
return ret_val;
}
/*
* StripDataSource_getattr
*/
int
StripDataSource_getattr (StripDataSource the_sds, ...)
{
va_list ap;
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
int attrib;
int ret_val = 1;
size_t index;
va_start (ap, the_sds);
for (attrib = va_arg (ap, SDSAttribute);
(attrib != 0);
attrib = va_arg (ap, SDSAttribute))
{
if ((ret_val = ((attrib > 0) && (attrib < SDS_LAST_ATTRIBUTE))))
switch (attrib)
{
case SDS_NUMSAMPLES:
*(va_arg (ap, size_t *)) = sds->buf_size;
break;
case SDS_BEGIN_TIME:
if (sds->count == sds->buf_size) index = (sds->cur_idx + 1) % sds->buf_size;
else index = 1;
*(va_arg (ap, struct timeval *)) = sds->times[index];
break;
}
}
va_end (ap);
return ret_val;
}
/*
* StripDataSource_addcurve
*/
int
StripDataSource_addcurve (StripDataSource the_sds,
StripCurve the_curve)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
int i;
int ret = 0;
for (i = 0; i < STRIP_MAX_CURVES; i++)
if (!sds->buffers[i].curve) /* it's available */
break;
if (i < STRIP_MAX_CURVES)
{
sds->buffers[i].first = SIZE_MAX;
sds->buffers[i].val = (double *)malloc (sds->buf_size * sizeof (double));
sds->buffers[i].stat = (StatusType *)calloc
(sds->buf_size, sizeof (StatusType));
if (sds->buffers[i].val && sds->buffers[i].stat)
{
sds->buffers[i].curve = (StripCurveInfo *)the_curve;
memset (sds->buffers[i].endpoints, 0, 2*sizeof(DataPoint));
/* use the id field of the strip curve to reference the buffer */
((StripCurveInfo *)the_curve)->id = &sds->buffers[i];
sds->buffers[i].history.fetch_stat = FETCH_IDLE;
ret = 1;
}
else
{
if (sds->buffers[i].val) free (sds->buffers[i].val);
if (sds->buffers[i].stat) free (sds->buffers[i].stat);
}
}
return ret;
}
#if 0
if(sds->cur_idx > 1) {
CurveData *cd = CURVE_DATA(the_curve);
struct timeval h0, h_end;
int n,m,k;
int status;
h_end=sds->times[sds->cur_idx];
for (m=0;m<sds->cur_idx; m++) {
if ( sds->times[m].tv_sec != 0 ) {
h0=sds->times[m];
break;
}
}
printf("m=%d cur_idx=%d\n",m,sds->cur_idx );
if (m < sds->cur_idx)
{
printf("sds->cur_idx=%ld\n",sds->cur_idx);
k=0;
do
{
k++;
status = StripHistory_fetch
(sds->history, cd->curve->details->name, &h0, &h_end,
&cd->history, 0, 0);
printf("%d %d %s:cd->history.n_points=%d\n",
k,status,cd->curve->details->name,cd->history.n_points);
}
while ( (cd->history.n_points == 0 ) && ( k < 2) ) ;
if(cd->history.n_points > 0) {
for(n=0;n<cd->history.n_points;n++)
printf("%s",ctime(&cd->history.times[n].tv_sec));
}
/* History problem */
}
else printf(" only zerro time\n");
}
#endif
void StripDataSource_refresh (StripDataSource the_sds)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
int i;
/* Albert */
for(i=0;i<STRIP_MAX_CURVES;i++)
sds->buffers[i].history.fetch_stat = FETCH_IDLE;
}
/*
* StripDataSource_min_max
*/
int
StripDataSource_min_max (StripDataSourceInfo *sds, struct timeval h0,
struct timeval h_end)
{
StripCurveInfo *c;
int m,i;
int some_data;
int need_refresh=0;
CurveData *cd;
double min=0.0;
double max=0.0;
int first,last;
double width;
double alpha;
int local_precision;
for (m = 0; m < STRIP_MAX_CURVES; m++)
{
if ((c = sds->buffers[m].curve) != NULL)
{
cd = &sds->buffers[m];
some_data = 0;
first=find_date_idx
(&h0, sds->times, sds->count, sds->buf_size, sds->cur_idx, SDS_GTE);
last=find_date_idx
(&h_end, sds->times, sds->count, sds->buf_size, sds->cur_idx, SDS_LTE);
if ((first > -1) && (last > -1) )
{
some_data=1;
min=cd->val[first];
max=cd->val[first];
if (first <= last)
{
for(i=first; i <= last; i++)
{
if(cd->val[i] < min) min=cd->val[i];
if(cd->val[i] > max) max=cd->val[i];
}
}
else
{
#if 0
/* perror Albert problem */
for(i=first; i <= last; i--)
{
printf("\n\n!!!!%f;\n",cd->val[i]);
if(cd->val[i]<min) min=cd->val[i];
if(cd->val[i]>max) max=cd->val[i];
}
#else
/* First part is first to end */
for(i=first; i < (int)sds->buf_size; i++)
{
if(cd->val[i] < min) min=cd->val[i];
if(cd->val[i] > max) max=cd->val[i];
}
/* Second part is 0 to last */
for(i=0; i <= last; i++)
{
if(cd->val[i] < min) min=cd->val[i];
if(cd->val[i] > max) max=cd->val[i];
}
#endif
}
}
#ifdef STRIP_HISTORY
if(!cursor) cursor = XCreateFontCursor(XtDisplay(history_topShell),XC_watch);
XDefineCursor(XtDisplay(history_topShell),
XtWindow(history_topShell), cursor);
XFlush(XtDisplay(history_topShell));
StripHistory_fetch
(sds->history, cd->curve->details->name, &h0, &h_end,
&cd->history, 0, 0);
XUndefineCursor(XtDisplay(history_topShell),
XtWindow(history_topShell));
if((cd->history.n_points>0)&&(cd->history.fetch_stat==FETCH_DONE))
{
first = find_date_idx
(&h0, cd->history.times, cd->history.n_points,
cd->history.n_points, cd->history.n_points - 1, SDS_GTE);
last = find_date_idx
(&h_end, cd->history.times, cd->history.n_points,
cd->history.n_points, cd->history.n_points - 1, SDS_LTE);
if ((first > -1) && (last > -1) && (first<=last ) )
{
if (some_data == 0)
{
min=cd->history.data[first];
max=cd->history.data[first];
some_data =1;
}
for(i=first;i<= last; i++)
{
if (cd->history.data[i]< min) min=cd->history.data[i];
if (cd->history.data[i]> max) max=cd->history.data[i];
}
}
}
#endif /* STRIP_HISTORY */
if ((min < max) && (some_data) )
{
if((c->details->min != min)||(c->details->max != max))
{
need_refresh=1;
width = max-min;
c->details->max= max + (double) (width/100.0);
c->details->min= min - (double) (width/100.0);
}
}
else if ((min == max) && (some_data) )
{
if( !((c->details->min < min)&& (c->details->max > min)) &&
(c->details->max - c->details->min > 0 ) )
{
/* constant-curve is unvisible - put it in visible area
using psevdo-random algorithm:*/
need_refresh=1;
width= c->details->max - c->details->min;
alpha = (double) ( (double) (time(NULL)%25) +25.0) /100.0;
c->details->min = min - width*( alpha);
c->details->max = min + width*(1-alpha);
}
}
else
{
printf ("min>max:%g>%g or isData=%d\n",min,max,some_data);
continue;
}
/****Reasonable shift of lim i.e. min=0.123098712345 -> min=0.123 *******/
local_precision=c->details->precision;
if (local_precision < 2) local_precision =1;
for(i=0;i<local_precision;i++)
{
c->details->min= c->details->min*10.0;
c->details->max= c->details->max*10.0;
}
c->details->min=floor(c->details->min);
c->details->max=ceil (c->details->max);
for(i=0;i<local_precision;i++)
{
c->details->min= c->details->min/10.0;
c->details->max= c->details->max/10.0;
}
/* End of Reasonable shift of limits ********************* */
}
}
return (need_refresh);
}
/*
* StripDataSource_removecurve
*/
int
StripDataSource_removecurve (StripDataSource the_sds, StripCurve the_curve)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
CurveData *cd;
int ret_val = 1;
if ((cd = CURVE_DATA(the_curve)) != NULL)
{
StripHistoryResult_release (sds->history, &cd->history);
cd->curve = NULL;
free (cd->val);
free (cd->stat);
cd->val = NULL;
cd->stat = NULL;
((StripCurveInfo *)the_curve)->id = NULL;
}
return ret_val;
}
#if 0
/* KE: This is not used */
/*
* StripDataSource_removecurveAll
* for final clean all garbage at data-buffer Albert
* This very much like StripDataSource_init except not everything
* is zeroed
*/
int
StripDataSource_removecurveAll(StripDataSource the_sds)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
int ret_val = 1;
#if DEBUG_SDS_TIMES
/* KE: Expect you should free things before setting the pointers to
zero, e.g. sds->times */
printf("StripDataSource_removecurveAll: times=%x\n",sds->times);
#endif
/*sds->history = NULL; Albert*/
sds->buf_size = 0;
sds->cur_idx = 0;
sds->count = 0;
sds->times = 0;
sds->idx_t0 = 0;
sds->idx_t1 = 0;
sds->bin_size = 0;
sds->n_bins = 0;
/* clear the buffers */
memset (sds->buffers, 0, STRIP_MAX_CURVES * sizeof(CurveData));
return ret_val;
}
#endif
/*
* StripDataSource_sample
*/
void
StripDataSource_sample (StripDataSource the_sds, char *sgP)
{
StripGraph sg = (StripGraph) sgP;
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
StripCurveInfo *c;
int i;
int need_time = 1;
double a; /*Albert*/
for (i = 0; i < STRIP_MAX_CURVES; i++)
{
if ((c = sds->buffers[i].curve) != NULL)
{
a=c->get_value (c->func_data);
if (need_time)
{
if (a != sds->buffers[i].val[sds->cur_idx])
{
/*printf("name=%s;old=%f,new=%f\n",
c->details->name,a,
sds->buffers[i].val[sds->cur_idx]);*/
CurveLegendRefresh(c,sg,a);
}
sds->cur_idx = (sds->cur_idx + 1) % sds->buf_size;
get_current_time (&sds->times[sds->cur_idx]);
sds->count = min ((sds->count+1), sds->buf_size);
need_time = 0;
}
else {
if (a != sds->buffers[i].val[sds->cur_idx -1 ])
{
CurveLegendRefresh(c,sg,a);
}
}
if ((c->status & STRIPCURVE_CONNECTED) &&
!(c->status & STRIPCURVE_WAITING))
{
sds->buffers[i].val[sds->cur_idx] = a;
/*c->get_value (c->func_data); */
sds->buffers[i].stat[sds->cur_idx] = DATASTAT_PLOTABLE;
/* first sample for this curve? */
if (sds->buffers[i].first == SIZE_MAX)
sds->buffers[i].first = sds->cur_idx;
/* otherwise, have we just overwritten what was previously
* the first data point? If so, then increment the first data
* point index */
else if (sds->cur_idx == sds->buffers[i].first)
sds->buffers[i].first = (sds->buffers[i].first + 1) % sds->buf_size;
}
else sds->buffers[i].stat[sds->cur_idx] &= ~DATASTAT_PLOTABLE;
}
}
}
/*
Line 844
some HACK here if delta time for History request < 2% from range interval
do not send History request! Albert Kagarmanov 23.11.2000
*/
/*
* StripDataSource_init_range
*/
int
StripDataSource_init_range (StripDataSource the_sds,
struct timeval *t0,
double bin_size,
int n_bins,
sdsRenderTechnique method)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
CurveData *cd;
struct timeval t1, t_tmp;
struct timeval h0, h1, *h_end;
long r0, r1 = 0;
int have_data = 0;
int i;
long deltaHistoryTime;
/* make t1 */
dbl2time (&t_tmp, n_bins * bin_size);
add_times (&t1, t0, &t_tmp);
/* initial history request range */
h0 = *t0;
h1 = t1;
/* find earliest timestamp in ring buffer which is greater than
* or equal to the desired begin time */
r0 = ((sds->count > 0)?
find_date_idx
(t0, sds->times, sds->count, sds->buf_size, sds->cur_idx, SDS_GTE)
: -1);
/* look for last date only if the first one was ok,
* set up history request range */
if (r0 >= 0)
{
r1 = find_date_idx
(&t1, sds->times, sds->count, sds->buf_size, sds->cur_idx, SDS_LTE);
if (compare_times (&sds->times[r0], &t1) <= 0)
h1 = sds->times[r0];
}
/* set the ring buffer date pointers */
if ((r0 >= 0) && (r1 >= 0))
{
sds->idx_t0 = (size_t)r0;
sds->idx_t1 = (size_t)r1;
if (sds->idx_t0 <= sds->idx_t1)
r0 = sds->idx_t1 - sds->idx_t0 + 1;
else r0 = sds->buf_size - sds->idx_t0 + sds->idx_t1 + 1;
have_data = 1;
}
else sds->idx_t0 = sds->idx_t1;
/* check each curve for fast-update plausibility, and send off
* any requisite history fetches */
for (i = 0; i < STRIP_MAX_CURVES; i++)
if (sds->buffers[i].curve)
{
cd = &sds->buffers[i];
/* verify endpoints
*
* If there is already some data rendered (in which case
* the endpoints for the curve will have been set to valid
* time values), then check whether either endpoint falls
* into the current range. If so, then we'll be able to
* do a fast update by connecting to the already rendered
* data. We also can avoid calling out to the history
* service if the timespan for which we don't have live
* data lies entirely within the interval which the
* already-rendered data spans (described by extents).
*/
cd->connectable = False;
if (compare_times (&cd->endpoints[0].t, &cd->endpoints[1].t) < 0)
if ((compare_times (t0, &cd->endpoints[0].t) < 0) ||
(compare_times (&t1, &cd->endpoints[1].t) > 0))
cd->connectable = (method == SDS_JOIN_NEW);
/* history request range
*
* case 1: no live data (first = ??)
* request entire range
*
* case 2: live data starts on or before t0 (first <= t0)
* don't need history data
*
* case 3: live data starts on or after t1 (first >= t1)
* request entire range
*
* case 4: live data starts within the interval (t0 < first < t1)
* case a: no rendered data
* request range ends at first
* case b: have connectable rendered data
* case 1: live data starts before or after rendered extent
* (first < extents[0] OR first > extents[1])
* request range ends at first
* case 2: live data starts within rendered extent
* (extents[0] <= first <= extents[1])
* request range ends at extents[0]
*/
/* case 1 */
if (cd->first == SIZE_MAX)
h_end = &h1;
/* case 2 */
else if (compare_times (&sds->times[cd->first], t0) <= 0)
h_end = &h0;
/* case 3 */
else if (compare_times (&sds->times[cd->first], &t1) >= 0)
h_end = &h1;
/* case 4-a */
else if (!cd->connectable)
h_end = &sds->times[cd->first];
/* case 4-b-1 */
else if ((compare_times (&sds->times[cd->first], &cd->extents[0]) < 0) ||
(compare_times (&sds->times[cd->first], &cd->extents[1]) > 0))
h_end = &sds->times[cd->first];
/* case 4-b-2 */
else h_end = &cd->extents[0];
deltaHistoryTime = h_end->tv_sec - h0.tv_sec;
/* printf("deltaHistoryTime=%ld n_bins=%d bin_size=%g \n",deltaHistoryTime,n_bins,bin_size); */
/* get the history data? */
if ( (compare_times (&h0, h_end) < 0) &&
((cd->history.fetch_stat == FETCH_IDLE) ||
(compare_times (&cd->history.t0, &h0) > 0) ||
(compare_times (&cd->history.t1, h_end) < 0)) &&
((auto_scaleTriger!=1)||((auto_scaleTriger==1)&&(radioChange)))
&& (n_bins*bin_size > 0) && (deltaHistoryTime > 1) &&
(deltaHistoryTime > ((5.0*n_bins*bin_size)/100.0) )
)
{
/* cancel preceding request if necessary */
if (cd->history.fetch_stat == FETCH_PENDING)
StripHistory_cancel (sds->history, &cd->history);
/* send off new request */
if(!cursor) cursor=XCreateFontCursor(XtDisplay(history_topShell),XC_watch);
XDefineCursor(XtDisplay(history_topShell),
XtWindow(history_topShell), cursor);
XFlush(XtDisplay(history_topShell));
StripHistory_fetch
(sds->history, cd->curve->details->name, &h0, h_end,
&cd->history, 0, 0);
XUndefineCursor(XtDisplay(history_topShell),
XtWindow(history_topShell));
}
/* if we have history data, we now need to find the
* begin and end locations for the current history range */
if ((compare_times (&h0, h_end) < 0) &&
(cd->history.fetch_stat == FETCH_DONE))
{
cd->hidx_t0 = find_date_idx
(&h0, cd->history.times, cd->history.n_points,
cd->history.n_points, cd->history.n_points - 1, SDS_GTE);
cd->hidx_t1 = find_date_idx
(h_end, cd->history.times, cd->history.n_points,
cd->history.n_points, cd->history.n_points - 1, SDS_LTE);
have_data |= ((cd->hidx_t0 >= 0) && (cd->hidx_t1 >= cd->hidx_t0));
}
}
if (radioChange) radioChange=0;
sds->req_t0 = *t0;
sds->req_t1 = t1;
sds->bin_size = bin_size;
sds->n_bins = n_bins;
return have_data;
}
/* StripDataSource_render
*/
size_t
StripDataSource_render (StripDataSource the_sds,
StripCurve curve,
sdsTransform x_transform,
void *x_data,
sdsTransform y_transform,
void *y_data,
XSegment **segs)
{
StripDataSourceInfo *sds = (StripDataSourceInfo *)the_sds;
CurveData *cd = CURVE_DATA(curve);
int max_points = 0;
DataPoint ring_first, hist_first, ring_last, hist_last;
TimeBuffer ring_times, hist_times;
ValueBuffer ring_values, hist_values;
StatusBuffer ring_status, hist_status;
int data_state = 0;
render_buffer.n_segs = 0;
/* ring buffer pointers & initializations */
if (sds->idx_t0 != sds->idx_t1)
{
data_state |= SDS_BUFFERED_DATA;
if (sds->idx_t0 > sds->idx_t1)
max_points = sds->buf_size - sds->idx_t0 + sds->idx_t1 + 1;
else max_points = sds->idx_t1 - sds->idx_t0 + 1;
ring_times.base = sds->times;
ring_times.count = sds->buf_size;
ring_values.base = cd->val;
ring_values.count = sds->buf_size;
ring_status.base = cd->stat;
ring_status.count = sds->buf_size;
}
/* history buffer pointers & initializations */
if (cd->history.fetch_stat == FETCH_DONE)
{
data_state |= SDS_HISTORY_DATA;
hist_times.base = cd->history.times;
hist_times.count = cd->history.n_points;
hist_values.base = cd->history.data;
hist_values.count = cd->history.n_points;
hist_status.base = cd->history.status;
hist_status.count = cd->history.n_points;
}
if (!(data_state & SDS_BOTH_DATA)) /* no data at all? */
{
cd->endpoints[0].t.tv_sec = 1;
cd->endpoints[1].t.tv_sec = 0;
return 0;
}
/* fast update? */
if (cd->connectable)
{
/* any data in the ring buffer ahead of currently rendered? */
if (data_state & SDS_BUFFERED_DATA)
{
if (compare_times
(&ring_times.base[sds->idx_t0], &cd->endpoints[0].t) < 0)
{
ring_times.ptr = ring_times.base + sds->idx_t0;
ring_values.ptr = ring_values.base + sds->idx_t0;
ring_status.ptr = ring_status.base + sds->idx_t0;
segmentify
(sds, &render_buffer, SDS_INCREASING,
&ring_times, &ring_values, &ring_status,
max_points, &cd->endpoints[0].t,
0, &cd->endpoints[0],
&ring_first, 0, x_transform, x_data, y_transform, y_data);
/* remember new beginning endpoint */
cd->endpoints[0] = ring_first;
}
else
{
ring_first.t = ring_times.base[sds->idx_t0];
ring_first.v = ring_values.base[sds->idx_t0];
ring_first.s = ring_status.base[sds->idx_t0];
}
}
/* any history data before currently rendered? */
if (data_state & SDS_HISTORY_DATA)
{
if (compare_times
(&hist_times.base[cd->hidx_t0], &cd->endpoints[0].t) < 0)
{
hist_times.ptr = hist_times.base + cd->hidx_t0;
hist_values.ptr = hist_values.base + cd->hidx_t0;
hist_status.ptr = hist_status.base + cd->hidx_t0;