16
16
using Microsoft . Azure . PowerShell . Tools . AzPredictor . Test . Mocks ;
17
17
using System ;
18
18
using System . Collections . Generic ;
19
- using System . Globalization ;
20
19
using System . Linq ;
21
20
using System . Management . Automation . Subsystem . Prediction ;
22
21
using System . Text . Json ;
23
- using System . Text . Json . Serialization ;
24
22
using System . Threading ;
25
23
using System . Threading . Tasks ;
26
24
using Xunit ;
@@ -922,7 +920,7 @@ public void VerifyAggregationDataSplitAtGetSuggestion()
922
920
923
921
for ( int i = 0 ; i < expectedTelemetryCount ; ++ i )
924
922
{
925
- // Call the methods a few times to make sure the telemetry data is less than 8092 but the next such call will
923
+ // Call the methods a few times to make sure the telemetry data is less than AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer but the next such call will
926
924
// make it larger than it.
927
925
var predictionContext = PredictionContext . Create ( $ "Clear-Content -Path '*' -Filter '{ i } .log'") ;
928
926
var _ = azPredictor . GetSuggestion ( MockObjects . PredictionClient , predictionContext , CancellationToken . None ) ;
@@ -961,7 +959,7 @@ public void VerifyAggregationDataSplitAtGetSuggestion()
961
959
/// Verifies that the Suggestion field is divided into events when SuggestionDisplayedTelemetryData is added.
962
960
/// </summary>
963
961
[ Fact ]
964
- public void VerifyAggregationDataSplitAtAcceptSuggestion ( )
962
+ public void VerifyAggregationDataSplitAtDisplaySuggestion ( )
965
963
{
966
964
var expectedTelemetryCount = 64 ;
967
965
var expectedSuggestionSessionInFirstBatch = expectedTelemetryCount ;
@@ -971,15 +969,14 @@ public void VerifyAggregationDataSplitAtAcceptSuggestion()
971
969
972
970
for ( int i = 0 ; i < expectedTelemetryCount - 1 ; ++ i )
973
971
{
974
- // Call the methods a few times to make sure the telemetry data is less than 8092 but the next such call will
975
- // make it larger than it.
972
+ // Call the methods a few times to make sure the telemetry data is less than AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
976
973
predictionContext = PredictionContext . Create ( $ "Clear-Variable -Name my* -Scop") ;
977
974
var _ = azPredictor . GetSuggestion ( MockObjects . PredictionClient , predictionContext , CancellationToken . None ) ;
978
975
}
979
976
980
- // It's easier to pad the cached telemetry event with the command without parameters. With parameters, it's more likely to exceed the
981
- // buffer size.
977
+ // This call just to make sure that the size is close enough to AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
982
978
predictionContext = PredictionContext . Create ( "Get-ChildIte" ) ;
979
+
983
980
suggestionPackage = azPredictor . GetSuggestion ( MockObjects . PredictionClient , predictionContext , CancellationToken . None ) ;
984
981
985
982
VerifyTelemetryDispatchCount ( expectedTelemetryCount , telemetryClient ) ;
@@ -991,9 +988,8 @@ public void VerifyAggregationDataSplitAtAcceptSuggestion()
991
988
telemetryClient . ExceptedTelemetryDispatchCount = expectedTelemetryCount ;
992
989
993
990
// OnSuggestionDisplayed makes the property value size larger than AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
994
- // But the additional data from SuggestionDisplayedTelemetryData is small so we are still less than the maximum application insight property value size.
995
991
azPredictor . OnSuggestionDisplayed ( MockObjects . PredictionClient , suggestionPackage . Session . Value , 1 ) ;
996
- // We'll send the first batch contains the found suggestions and displayed info when we process SuggestionAcceptedTelemetryData .
992
+ // We'll send the first batch that contains the found suggestions when we process SuggestionDisplayedTelemetryData .
997
993
azPredictor . OnSuggestionAccepted ( MockObjects . PredictionClient , suggestionPackage . Session . Value , "Get-ChildItem" ) ;
998
994
999
995
VerifyTelemetryDispatchCount ( expectedTelemetryCount , telemetryClient ) ;
@@ -1004,10 +1000,8 @@ public void VerifyAggregationDataSplitAtAcceptSuggestion()
1004
1000
Assert . Equal ( expectedSuggestionSessionInFirstBatch , suggestionSessions . Count ( ) ) ;
1005
1001
Assert . True ( suggestionSessions . All ( ( s ) => s . ContainsKey ( GetSuggestionTelemetryData . PropertyNameFound ) && s . ContainsKey ( GetSuggestionTelemetryData . PropertyNameUserInput ) ) ) ;
1006
1002
Assert . True ( suggestionSessions . All ( ( s ) => ! s . ContainsKey ( SuggestionAcceptedTelemetryData . PropertyNameAccepted ) ) ) ;
1007
- Assert . True ( suggestionSessions . SkipLast ( 1 ) . All ( ( s ) => ! s . ContainsKey ( SuggestionDisplayedTelemetryData . PropertyNameDisplayed ) && ! s . ContainsKey ( GetSuggestionTelemetryData . PropertyNameSuggestionSessionId ) ) ) ;
1003
+ Assert . True ( suggestionSessions . All ( ( s ) => ! s . ContainsKey ( SuggestionDisplayedTelemetryData . PropertyNameDisplayed ) ) ) ;
1008
1004
Assert . Equal ( suggestionPackage . Session . Value , ( ( JsonElement ) suggestionSessions . Last ( ) [ GetSuggestionTelemetryData . PropertyNameSuggestionSessionId ] ) . GetUInt32 ( ) ) ;
1009
- Assert . Equal ( 1 , ( ( JsonElement ) suggestionSessions . Last ( ) [ SuggestionDisplayedTelemetryData . PropertyNameDisplayed ] ) [ 0 ] . GetInt32 ( ) ) ;
1010
- Assert . Equal ( 1 , ( ( JsonElement ) suggestionSessions . Last ( ) [ SuggestionDisplayedTelemetryData . PropertyNameDisplayed ] ) [ 1 ] . GetInt32 ( ) ) ;
1011
1005
1012
1006
recordedTelemetry = telemetryClient . RecordedTelemetry [ 1 ] ;
1013
1007
suggestionSessions = JsonSerializer . Deserialize < IList < IDictionary < string , object > > > ( recordedTelemetry . Properties [ GetSuggestionTelemetryData . PropertyNamePrediction ] ) ;
@@ -1016,57 +1010,55 @@ public void VerifyAggregationDataSplitAtAcceptSuggestion()
1016
1010
Assert . False ( suggestionSessions [ 0 ] . ContainsKey ( GetSuggestionTelemetryData . PropertyNameUserInput ) ) ;
1017
1011
Assert . False ( suggestionSessions [ 0 ] . ContainsKey ( GetSuggestionTelemetryData . PropertyNameIsCancelled ) ) ;
1018
1012
Assert . Equal ( suggestionPackage . Session . Value , ( ( JsonElement ) suggestionSessions [ 0 ] [ GetSuggestionTelemetryData . PropertyNameSuggestionSessionId ] ) . GetUInt32 ( ) ) ;
1013
+ Assert . Equal ( 1 , ( ( JsonElement ) suggestionSessions [ 0 ] [ SuggestionDisplayedTelemetryData . PropertyNameDisplayed ] ) [ 0 ] . GetInt32 ( ) ) ;
1014
+ Assert . Equal ( 1 , ( ( JsonElement ) suggestionSessions [ 0 ] [ SuggestionDisplayedTelemetryData . PropertyNameDisplayed ] ) [ 1 ] . GetInt32 ( ) ) ;
1019
1015
Assert . Equal ( "Get-ChildItem" , ( ( JsonElement ) suggestionSessions [ 0 ] [ SuggestionAcceptedTelemetryData . PropertyNameAccepted ] ) . GetString ( ) ) ;
1020
1016
}
1021
1017
1022
1018
/// <summary>
1023
- /// Verifies that the Suggestion field is divided into events when history is added.
1019
+ /// Verifies that the Suggestion field is divided into events when SuggestionAcceptedTelemetryData is added.
1024
1020
/// </summary>
1025
1021
[ Fact ]
1026
- public void VerifyAggregationDataSplitAtCommandHistory ( )
1022
+ public void VerifyAggregationDataSplitAtAcceptSuggestion ( )
1027
1023
{
1028
1024
var expectedTelemetryCount = 64 ;
1029
- var expectedSuggestionSessionInFirstBatch = expectedTelemetryCount ;
1025
+ var expectedSuggestionSessionInFirstBatch = expectedTelemetryCount - 1 ; // The display info is in the same suggstion session as the GetSuggestionTelemetryData
1030
1026
var ( azPredictor , telemetryClient ) = CreateTestObjects ( throwException : false , expectedTelemetryCount , flushTelemetry : false ) ;
1031
1027
PredictionContext predictionContext = default ;
1032
1028
SuggestionPackage suggestionPackage = default ;
1033
1029
1034
1030
for ( int i = 0 ; i < expectedTelemetryCount - 1 ; ++ i )
1035
1031
{
1036
- // Call the methods a few times to make sure the telemetry data is less than 8092 but the CommandAccepted and CommandExecuted events
1037
- // make it larger than it.
1038
- predictionContext = PredictionContext . Create ( $ "Clear-Variable -Name my* -Scop") ;
1039
- var _ = suggestionPackage = azPredictor . GetSuggestion ( MockObjects . PredictionClient , predictionContext , CancellationToken . None ) ;
1032
+ // Call the methods a few times to make sure the telemetry data is less than AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
1033
+ predictionContext = PredictionContext . Create ( $ "Clear-Variable -Name my* -Scope") ;
1034
+ suggestionPackage = azPredictor . GetSuggestion ( MockObjects . PredictionClient , predictionContext , CancellationToken . None ) ;
1040
1035
}
1041
1036
1042
- // It's easier to pad the cached telemetry event with the command without parameters. With parameters, it's more likely to exceed the
1043
- // buffer size.
1044
- predictionContext = PredictionContext . Create ( "Get-ChildIte" ) ;
1045
- suggestionPackage = azPredictor . GetSuggestion ( MockObjects . PredictionClient , predictionContext , CancellationToken . None ) ;
1037
+ // With this call, the size is still less than AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
1038
+ azPredictor . OnSuggestionDisplayed ( MockObjects . PredictionClient , suggestionPackage . Session . Value , 1 ) ;
1046
1039
1047
1040
VerifyTelemetryDispatchCount ( expectedTelemetryCount , telemetryClient ) ;
1048
1041
Assert . True ( telemetryClient . RecordedAggregatedData . EstimateSuggestionSessionSize < AzPredictorTelemetryClient . MaxPropertyValueSizeWithBuffer ) ;
1049
1042
1050
- expectedTelemetryCount = 3 ;
1043
+ expectedTelemetryCount = 1 ;
1051
1044
var expectedSuggestionSessionInSecondBatch = 1 ;
1052
1045
telemetryClient . ResetWaitingTasks ( ) ;
1053
1046
telemetryClient . ExceptedTelemetryDispatchCount = expectedTelemetryCount ;
1054
1047
1055
- // OnSuggestionDisplayed makes the property value size larger than AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
1056
- // But the additional data from SuggestionDisplayedTelemetryData is small so we are still less than the maximum application insight property value size.
1057
- azPredictor . OnSuggestionDisplayed ( MockObjects . PredictionClient , suggestionPackage . Session . Value , 1 ) ;
1048
+ // With this call, the size will exceed AzPredictorTelemetryClient.MaxPropertyValueSizeWithBuffer.
1058
1049
// We'll send the first batch contains the found suggestions and displayed info when we process SuggestionAcceptedTelemetryData.
1059
- azPredictor . OnCommandLineAccepted ( MockObjects . PredictionClient , new string [ ] { "Get-ChildItem" } ) ;
1060
- azPredictor . OnCommandLineExecuted ( MockObjects . PredictionClient , "Get-ChildItem" , success : true ) ;
1050
+ var acceptedSuggestion = "Clear-Variable -Name my* -Scope Global" ;
1051
+ azPredictor . OnSuggestionAccepted ( MockObjects . PredictionClient , suggestionPackage . Session . Value , acceptedSuggestion ) ;
1061
1052
1062
1053
VerifyTelemetryDispatchCount ( expectedTelemetryCount , telemetryClient ) ;
1054
+ telemetryClient . FlushTelemetry ( ) ;
1063
1055
1064
1056
var recordedTelemetry = telemetryClient . RecordedTelemetry [ 0 ] ;
1065
1057
var suggestionSessions = JsonSerializer . Deserialize < IList < IDictionary < string , object > > > ( recordedTelemetry . Properties [ GetSuggestionTelemetryData . PropertyNamePrediction ] ) ;
1066
1058
Assert . Equal ( expectedSuggestionSessionInFirstBatch , suggestionSessions . Count ( ) ) ;
1067
1059
Assert . True ( suggestionSessions . All ( ( s ) => s . ContainsKey ( GetSuggestionTelemetryData . PropertyNameFound ) && s . ContainsKey ( GetSuggestionTelemetryData . PropertyNameUserInput ) ) ) ;
1068
1060
Assert . True ( suggestionSessions . All ( ( s ) => ! s . ContainsKey ( SuggestionAcceptedTelemetryData . PropertyNameAccepted ) ) ) ;
1069
- Assert . True ( suggestionSessions . SkipLast ( 1 ) . All ( ( s ) => ! s . ContainsKey ( SuggestionDisplayedTelemetryData . PropertyNameDisplayed ) ) ) ;
1061
+ Assert . True ( suggestionSessions . SkipLast ( 1 ) . All ( ( s ) => ! s . ContainsKey ( SuggestionDisplayedTelemetryData . PropertyNameDisplayed ) && ! s . ContainsKey ( GetSuggestionTelemetryData . PropertyNameSuggestionSessionId ) ) ) ;
1070
1062
Assert . Equal ( suggestionPackage . Session . Value , ( ( JsonElement ) suggestionSessions . Last ( ) [ GetSuggestionTelemetryData . PropertyNameSuggestionSessionId ] ) . GetUInt32 ( ) ) ;
1071
1063
Assert . Equal ( 1 , ( ( JsonElement ) suggestionSessions . Last ( ) [ SuggestionDisplayedTelemetryData . PropertyNameDisplayed ] ) [ 0 ] . GetInt32 ( ) ) ;
1072
1064
Assert . Equal ( 1 , ( ( JsonElement ) suggestionSessions . Last ( ) [ SuggestionDisplayedTelemetryData . PropertyNameDisplayed ] ) [ 1 ] . GetInt32 ( ) ) ;
@@ -1078,7 +1070,7 @@ public void VerifyAggregationDataSplitAtCommandHistory()
1078
1070
Assert . False ( suggestionSessions [ 0 ] . ContainsKey ( GetSuggestionTelemetryData . PropertyNameUserInput ) ) ;
1079
1071
Assert . False ( suggestionSessions [ 0 ] . ContainsKey ( GetSuggestionTelemetryData . PropertyNameIsCancelled ) ) ;
1080
1072
Assert . Equal ( suggestionPackage . Session . Value , ( ( JsonElement ) suggestionSessions [ 0 ] [ GetSuggestionTelemetryData . PropertyNameSuggestionSessionId ] ) . GetUInt32 ( ) ) ;
1081
- Assert . Equal ( "Get-ChildItem" , recordedTelemetry . Properties [ HistoryTelemetryData . PropertyNameCommand ] ) ;
1073
+ Assert . Equal ( acceptedSuggestion , ( ( JsonElement ) suggestionSessions [ 0 ] [ SuggestionAcceptedTelemetryData . PropertyNameAccepted ] ) . GetString ( ) ) ;
1082
1074
}
1083
1075
1084
1076
private ( AzPredictor , MockAzPredictorTelemetryClient ) CreateTestObjects ( bool throwException , int expectedTelemetryEvent , bool flushTelemetry = true )
0 commit comments