77using System . Net . Http ;
88using System . Security . Principal ;
99using System . Web ;
10+ using System . Web . Helpers ;
1011using Newtonsoft . Json ;
1112using NuGet . Services . Entities ;
1213using NuGet . Services . FeatureFlags ;
@@ -39,6 +40,7 @@ internal class Events
3940 public const string PackageUnlisted = "PackageUnlisted" ;
4041 public const string PackageListed = "PackageListed" ;
4142 public const string PackageDelete = "PackageDelete" ;
43+ public const string PackageDeprecate = "PackageDeprecate" ;
4244 public const string PackageReupload = "PackageReupload" ;
4345 public const string PackageHardDeleteReflow = "PackageHardDeleteReflow" ;
4446 public const string PackageRevalidate = "PackageRevalidate" ;
@@ -96,7 +98,7 @@ internal class Events
9698 // ODataCustomQuery properties
9799 public const string IsCustomQuery = "IsCustomQuery" ;
98100
99- // Package push properties
101+ // Package event properties
100102 public const string AuthenticationMethod = "AuthenticationMethod" ;
101103 public const string ClientVersion = "ClientVersion" ;
102104 public const string ProtocolVersion = "ProtocolVersion" ;
@@ -106,6 +108,13 @@ internal class Events
106108 public const string KeyCreationDate = "KeyCreationDate" ;
107109 public const string PackageId = "PackageId" ;
108110 public const string PackageVersion = "PackageVersion" ;
111+ public const string PackageVersions = "PackageVersions" ;
112+
113+ // Package deprecate properties
114+ public const string DeprecationReason = "PackageDeprecationReason" ;
115+ public const string DeprecationAlternatePackageId = "PackageDeprecationAlternatePackageId" ;
116+ public const string DeprecationAlternatePackageVersion = "PackageDeprecationAlternatePackageVersion" ;
117+ public const string DeprecationCustomMessage = "PackageDeprecationCustomMessage" ;
109118
110119 // User properties
111120 public const string RegistrationMethod = "RegistrationMethod" ;
@@ -392,6 +401,25 @@ public void TrackPackageRevalidate(Package package)
392401 TrackMetricForPackage ( Events . PackageRevalidate , package ) ;
393402 }
394403
404+ public void TrackPackageDeprecate (
405+ IReadOnlyList < Package > packages ,
406+ PackageDeprecationStatus status ,
407+ PackageRegistration alternateRegistration ,
408+ Package alternatePackage ,
409+ bool hasCustomMessage )
410+ {
411+ TrackMetricForPackageVersions (
412+ Events . PackageDeprecate ,
413+ packages ,
414+ properties =>
415+ {
416+ properties . Add ( DeprecationReason , ( ( int ) status ) . ToString ( ) ) ;
417+ properties . Add ( DeprecationAlternatePackageId , alternateRegistration ? . Id ?? alternatePackage ? . Id ) ;
418+ properties . Add ( DeprecationAlternatePackageVersion , alternatePackage ? . NormalizedVersion ) ;
419+ properties . Add ( DeprecationCustomMessage , hasCustomMessage . ToString ( ) ) ;
420+ } ) ;
421+ }
422+
395423 public void TrackPackageMetadataComplianceError ( string packageId , string packageVersion , IEnumerable < string > complianceFailures )
396424 {
397425 TrackMetricForPackage (
@@ -648,6 +676,39 @@ private void TrackMetricForPackage(
648676 } ) ;
649677 }
650678
679+ private void TrackMetricForPackageVersions (
680+ string metricName ,
681+ IReadOnlyList < Package > packages ,
682+ Action < Dictionary < string , string > > addProperties = null )
683+ {
684+ if ( packages == null || ! packages . Any ( ) || packages . Select ( p => p . PackageRegistrationKey ) . Distinct ( ) . Count ( ) > 1 )
685+ {
686+ throw new ArgumentException ( nameof ( packages ) ) ;
687+ }
688+
689+ TrackMetricForPackageVersions (
690+ metricName ,
691+ packages . First ( ) . PackageRegistration . Id ,
692+ packages . Select ( p => p . NormalizedVersion ) . ToList ( ) ,
693+ addProperties ) ;
694+ }
695+
696+ private void TrackMetricForPackageVersions (
697+ string metricName ,
698+ string packageId ,
699+ IReadOnlyList < string > packageVersions ,
700+ Action < Dictionary < string , string > > addProperties = null )
701+ {
702+ TrackMetric ( metricName , packageVersions . Count ( ) , properties => {
703+ properties . Add ( ClientVersion , GetClientVersion ( ) ) ;
704+ properties . Add ( ProtocolVersion , GetProtocolVersion ( ) ) ;
705+ properties . Add ( ClientInformation , GetClientInformation ( ) ) ;
706+ properties . Add ( PackageId , packageId ) ;
707+ properties . Add ( PackageVersion , BuildArrayProperty ( packageVersions ) ) ;
708+ addProperties ? . Invoke ( properties ) ;
709+ } ) ;
710+ }
711+
651712 public void TrackUserPackageDeleteChecked ( UserPackageDeleteEvent details , UserPackageDeleteOutcome outcome )
652713 {
653714 if ( details == null )
@@ -727,7 +788,7 @@ public void TrackAccountDeletionCompleted(User deletedUser, User deletedBy, bool
727788 }
728789
729790 TrackMetric ( Events . AccountDeleteCompleted , 1 , properties => {
730- properties . Add ( AccountDeletedByRole , string . Join ( "," , deletedBy . Roles ? . Select ( role => role . Name ) ?? new List < string > ( ) ) ) ;
791+ properties . Add ( AccountDeletedByRole , BuildArrayProperty ( deletedBy . Roles ? . Select ( role => role . Name ) ?? new string [ 0 ] ) ) ;
731792 properties . Add ( AccountIsSelfDeleted , $ "{ deletedUser . Key == deletedBy . Key } ") ;
732793 properties . Add ( AccountDeletedIsOrganization , $ "{ deletedUser is Organization } ") ;
733794 properties . Add ( AccountDeleteSucceeded , $ "{ success } ") ;
@@ -766,7 +827,7 @@ public void TrackMetricForTyposquattingCheckResultAndTotalTime(
766827 TrackMetric ( Events . TyposquattingCheckResultAndTotalTimeInMs , totalTime . TotalMilliseconds , properties => {
767828 properties . Add ( PackageId , packageId ) ;
768829 properties . Add ( WasUploadBlocked , wasUploadBlocked . ToString ( ) ) ;
769- properties . Add ( CollisionPackageIds , string . Join ( "," , collisionPackageIds . Take ( TyposquattingCollisionIdsMaxPropertyValue ) ) ) ;
830+ properties . Add ( CollisionPackageIds , BuildArrayProperty ( collisionPackageIds . Take ( TyposquattingCollisionIdsMaxPropertyValue ) ) ) ;
770831 properties . Add ( CollisionPackageIdsCount , collisionPackageIds . Count . ToString ( ) ) ;
771832 properties . Add ( CheckListLength , checkListLength . ToString ( ) ) ;
772833 properties . Add ( HasExtraCollisionPackageIds , ( collisionPackageIds . Count > TyposquattingCollisionIdsMaxPropertyValue ) . ToString ( ) ) ;
@@ -900,5 +961,10 @@ protected virtual void TrackMetric(string metricName, double value, Action<Dicti
900961
901962 _telemetryClient . TrackMetric ( metricName , value , telemetryProperties ) ;
902963 }
964+
965+ private string BuildArrayProperty ( IEnumerable < string > list )
966+ {
967+ return JsonConvert . SerializeObject ( list ) ;
968+ }
903969 }
904970}
0 commit comments