11using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . Threading . Tasks ;
25using LaunchDarkly . Sdk . Server . Ai . Config ;
6+ using LaunchDarkly . Sdk . Server . Ai . Metrics ;
37
48namespace LaunchDarkly . Sdk . Server . Ai ;
59
@@ -16,19 +20,94 @@ public class LdAiConfigTracker : IDisposable
1620 /// <summary>
1721 /// TBD
1822 /// </summary>
19- private ILaunchDarklyClient _client ;
23+ private readonly ILaunchDarklyClient _client ;
24+
25+ private readonly Context _context ;
26+
27+ private readonly string _key ;
28+
29+ private const string Duration = "$ld:ai:duration:total" ;
30+ private const string FeedbackPositive = "$ld:ai:feedback:user:positive" ;
31+ private const string FeedbackNegative = "$ld:ai:feedback:user:negative" ;
32+ private const string Generation = "$ld:ai:generation" ;
2033
2134 /// <summary>
22- /// TBD
35+ ///
2336 /// </summary>
2437 /// <param name="client"></param>
2538 /// <param name="config"></param>
26- public LdAiConfigTracker ( ILaunchDarklyClient client , LdAiConfig config )
39+ /// <param name="context"></param>
40+ /// <param name="key"></param>
41+ /// <exception cref="ArgumentNullException"></exception>
42+ public LdAiConfigTracker ( ILaunchDarklyClient client , LdAiConfig config , Context context , string key )
2743 {
2844 _client = client ?? throw new ArgumentNullException ( nameof ( client ) ) ;
45+ _key = key ?? throw new ArgumentNullException ( nameof ( key ) ) ;
46+ _context = context ;
2947 Config = config ?? throw new ArgumentNullException ( nameof ( config ) ) ;
3048 }
3149
50+ private LdValue GetTrackData ( )
51+ {
52+ return LdValue . ObjectFrom ( new Dictionary < string , LdValue >
53+ {
54+ { "versionKey" , LdValue . Of ( Config . VersionKey ) } ,
55+ { "configKey" , LdValue . Of ( _key ) }
56+ } ) ;
57+ }
58+
59+ /// <summary>
60+ ///
61+ /// </summary>
62+ /// <param name="duration"></param>
63+ public void TrackDuration ( float duration ) =>
64+ _client . Track ( Duration , _context , GetTrackData ( ) , duration ) ;
65+
66+
67+ /// <summary>
68+ ///
69+ /// </summary>
70+ /// <param name="task"></param>
71+ /// <typeparam name="T"></typeparam>
72+ /// <returns></returns>
73+ public async Task < T > TrackDurationOfTask < T > ( Task < T > task )
74+ {
75+ var sw = Stopwatch . StartNew ( ) ;
76+ var result = await task ;
77+ sw . Stop ( ) ;
78+ TrackDuration ( sw . ElapsedMilliseconds ) ;
79+ return result ;
80+ }
81+
82+ /// <summary>
83+ ///
84+ /// </summary>
85+ /// <param name="feedback"></param>
86+ /// <exception cref="ArgumentOutOfRangeException"></exception>
87+ public void TrackFeedback ( Feedback feedback )
88+ {
89+ switch ( feedback )
90+ {
91+ case Feedback . Positive :
92+ _client . Track ( FeedbackPositive , _context , GetTrackData ( ) , 1 ) ;
93+ break ;
94+ case Feedback . Negative :
95+ _client . Track ( FeedbackNegative , _context , GetTrackData ( ) , 1 ) ;
96+ break ;
97+ default :
98+ throw new ArgumentOutOfRangeException ( nameof ( feedback ) , feedback , null ) ;
99+ }
100+ }
101+
102+ /// <summary>
103+ ///
104+ /// </summary>
105+ public void TrackSuccess ( )
106+ {
107+ _client . Track ( Generation , _context , GetTrackData ( ) , 1 ) ;
108+ }
109+
110+
32111 /// <summary>
33112 /// TBD
34113 /// </summary>
0 commit comments