@@ -60,60 +60,95 @@ public void AppendPoints()
60
60
? $ "Appending { Points . Count } { pointExtents } within TimeRange={ GetTimeRange ( ) } to { timeSeries . Identifier } ({ timeSeries . TimeSeriesType } ) ..."
61
61
: $ "Appending { Points . Count } { pointExtents } to { timeSeries . Identifier } ({ timeSeries . TimeSeriesType } ) ...") ;
62
62
63
+ var numberOfPointsAppended = 0 ;
64
+ var numberOfPointsDeleted = 0 ;
65
+ var batchCount = 0 ;
63
66
var stopwatch = Stopwatch . StartNew ( ) ;
64
67
65
- AppendResponse appendResponse ;
68
+ foreach ( var batch in GetPointBatches ( ) )
69
+ {
70
+ var result = AppendPointBatch ( client , timeSeries , batch . Item1 , batch . Item2 , isReflected , hasTimeRange ) ;
71
+ numberOfPointsAppended += result . NumberOfPointsAppended ;
72
+ numberOfPointsDeleted += result . NumberOfPointsDeleted ;
73
+ batchCount ++ ;
74
+
75
+ if ( result . AppendStatus != AppendStatusCode . Completed )
76
+ throw new ExpectedException ( $ "Unexpected append status={ result . AppendStatus } ") ;
77
+ }
78
+
79
+ var batchText = batchCount > 1 ? $ " using { batchCount } appends" : "" ;
80
+ Log . Info ( $ "Appended { numberOfPointsAppended } points (deleting { numberOfPointsDeleted } points) in { stopwatch . ElapsedMilliseconds / 1000.0 : F1} seconds{ batchText } .") ;
81
+ }
82
+ }
83
+
84
+ private TimeSeriesAppendStatus AppendPointBatch ( IAquariusClient client , TimeSeries timeSeries , List < ReflectedTimeSeriesPoint > points , Interval timeRange , bool isReflected , bool hasTimeRange )
85
+ {
86
+ AppendResponse appendResponse ;
87
+
88
+ if ( isReflected )
89
+ {
90
+ appendResponse = client . Acquisition . Post ( new PostReflectedTimeSeries
91
+ {
92
+ UniqueId = timeSeries . UniqueId ,
93
+ TimeRange = timeRange ,
94
+ Points = points
95
+ } ) ;
96
+ }
97
+ else
98
+ {
99
+ var basicPoints = points
100
+ . Select ( p => new TimeSeriesPoint
101
+ {
102
+ Time = p . Time ,
103
+ Value = p . Value
104
+ } )
105
+ . ToList ( ) ;
66
106
67
- if ( isReflected )
107
+ if ( hasTimeRange )
68
108
{
69
- appendResponse = client . Acquisition . Post ( new PostReflectedTimeSeries
109
+ appendResponse = client . Acquisition . Post ( new PostTimeSeriesOverwriteAppend
70
110
{
71
111
UniqueId = timeSeries . UniqueId ,
72
- TimeRange = GetTimeRange ( ) ,
73
- Points = Points
112
+ TimeRange = timeRange ,
113
+ Points = basicPoints
74
114
} ) ;
75
115
}
76
116
else
77
117
{
78
- var basicPoints = Points
79
- . Select ( p => new TimeSeriesPoint
80
- {
81
- Time = p . Time ,
82
- Value = p . Value
83
- } )
84
- . ToList ( ) ;
85
-
86
- if ( hasTimeRange )
87
- {
88
- appendResponse = client . Acquisition . Post ( new PostTimeSeriesOverwriteAppend
89
- {
90
- UniqueId = timeSeries . UniqueId ,
91
- TimeRange = GetTimeRange ( ) ,
92
- Points = basicPoints
93
- } ) ;
94
- }
95
- else
118
+ appendResponse = client . Acquisition . Post ( new PostTimeSeriesAppend
96
119
{
97
- appendResponse = client . Acquisition . Post ( new PostTimeSeriesAppend
98
- {
99
- UniqueId = timeSeries . UniqueId ,
100
- Points = basicPoints
101
- } ) ;
102
- }
120
+ UniqueId = timeSeries . UniqueId ,
121
+ Points = basicPoints
122
+ } ) ;
103
123
}
124
+ }
104
125
105
- var result = client . Acquisition . RequestAndPollUntilComplete (
106
- acquisition => appendResponse ,
107
- ( acquisition , response ) => acquisition . Get ( new GetTimeSeriesAppendStatus { AppendRequestIdentifier = response . AppendRequestIdentifier } ) ,
108
- polledStatus => polledStatus . AppendStatus != AppendStatusCode . Pending ,
109
- null ,
110
- Context . AppendTimeout ) ;
126
+ return client . Acquisition . RequestAndPollUntilComplete (
127
+ acquisition => appendResponse ,
128
+ ( acquisition , response ) => acquisition . Get ( new GetTimeSeriesAppendStatus { AppendRequestIdentifier = response . AppendRequestIdentifier } ) ,
129
+ polledStatus => polledStatus . AppendStatus != AppendStatusCode . Pending ,
130
+ null ,
131
+ Context . AppendTimeout ) ;
132
+ }
133
+
134
+ private IEnumerable < Tuple < List < ReflectedTimeSeriesPoint > , Interval > > GetPointBatches ( )
135
+ {
136
+ var points = GetPoints ( ) ;
137
+ var remainingTimeRange = GetTimeRange ( ) ;
111
138
112
- if ( result . AppendStatus != AppendStatusCode . Completed )
113
- throw new ExpectedException ( $ "Unexpected append status={ result . AppendStatus } ") ;
139
+ var index = 0 ;
140
+ while ( points . Count - index > Context . BatchSize )
141
+ {
142
+ var batchPoints = points . Skip ( index ) . Take ( Context . BatchSize ) . ToList ( ) ;
143
+ var batchTimeRange = new Interval ( remainingTimeRange . Start , batchPoints . Last ( ) . Time . GetValueOrDefault ( ) . PlusTicks ( 1 ) ) ;
144
+ remainingTimeRange = new Interval ( batchTimeRange . End , remainingTimeRange . End ) ;
145
+
146
+ yield return new Tuple < List < ReflectedTimeSeriesPoint > , Interval > ( batchPoints , batchTimeRange ) ;
114
147
115
- Log . Info ( $ "Appended { result . NumberOfPointsAppended } points (deleting { result . NumberOfPointsDeleted } points) in { stopwatch . ElapsedMilliseconds / 1000.0 : F1 } seconds." ) ;
148
+ index += Context . BatchSize ;
116
149
}
150
+
151
+ yield return new Tuple < List < ReflectedTimeSeriesPoint > , Interval > ( points . Skip ( index ) . ToList ( ) , remainingTimeRange ) ;
117
152
}
118
153
119
154
private List < ReflectedTimeSeriesPoint > GetPoints ( )
0 commit comments