11using System ;
2+ using System . Collections . Generic ;
23using Elasticsearch . Net ;
34using FluentAssertions ;
45using Nest ;
@@ -11,22 +12,63 @@ namespace Tests.XPack.MachineLearning.ForecastJob
1112 [ SkipVersion ( "<6.1.0" , "Only exists in Elasticsearch 6.1.0+" ) ]
1213 public class ForecastJobApiTests : MachineLearningIntegrationTestBase < IForecastJobResponse , IForecastJobRequest , ForecastJobDescriptor , ForecastJobRequest >
1314 {
15+ private const int BucketSpanSeconds = 3600 ;
16+
1417 public ForecastJobApiTests ( XPackMachineLearningCluster cluster , EndpointUsage usage ) : base ( cluster , usage ) { }
1518
1619 protected override void IntegrationSetup ( IElasticClient client , CallUniqueValues values )
1720 {
1821 foreach ( var callUniqueValue in values )
1922 {
20- PutJob ( client , callUniqueValue . Value ) ;
21- IndexAnomalyRecord ( client , callUniqueValue . Value , new DateTimeOffset ( 2016 , 6 , 2 , 00 , 00 , 00 , TimeSpan . Zero ) ) ;
23+ var putJobResponse = client . PutJob < object > ( callUniqueValue . Value , f => f
24+ . Description ( "ForecastJobApiTests" )
25+ . AnalysisConfig ( a => a
26+ . BucketSpan ( $ "{ BucketSpanSeconds } s")
27+ . Detectors ( d => d
28+ . Mean ( m => m
29+ . FieldName ( "value" )
30+ )
31+ )
32+ )
33+ . DataDescription ( d => d
34+ . TimeFormat ( "epoch" )
35+ )
36+ ) ;
37+
38+ if ( ! putJobResponse . IsValid )
39+ throw new Exception ( $ "Problem putting job { callUniqueValue . Value } for integration test: { putJobResponse . DebugInformation } ") ;
40+
2241 OpenJob ( client , callUniqueValue . Value ) ;
42+
43+ var epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
44+ var now = ( DateTime . UtcNow - epoch ) . TotalSeconds ;
45+ var timestamp = now - 50 * BucketSpanSeconds ;
46+ var data = new List < object > ( 50 ) ;
47+ while ( timestamp < now )
48+ {
49+ data . AddRange ( new [ ]
50+ {
51+ new { time = timestamp , value = 10d } ,
52+ new { time = timestamp , value = 30d }
53+ } ) ;
54+ timestamp += BucketSpanSeconds ;
55+ }
56+
57+ var postJobDataResponse = client . PostJobData ( callUniqueValue . Value , d => d . Data ( data ) ) ;
58+ if ( ! postJobDataResponse . IsValid )
59+ throw new Exception ( $ "Problem posting data for integration test: { postJobDataResponse . DebugInformation } ") ;
60+
61+ FlushJob ( client , callUniqueValue . Value , false ) ;
2362 }
2463 }
2564
2665 protected override void IntegrationTeardown ( IElasticClient client , CallUniqueValues values )
2766 {
2867 foreach ( var callUniqueValue in values )
68+ {
2969 CloseJob ( client , callUniqueValue . Value ) ;
70+ DeleteJob ( client , callUniqueValue . Value ) ;
71+ }
3072 }
3173
3274 protected override LazyResponses ClientUsage ( ) => Calls (
0 commit comments