@@ -21,6 +21,8 @@ public class SentimentService : IHostedService
21
21
private TextAnalyticsClient _client ;
22
22
private static string _SubscriptionKey ;
23
23
24
+ private Dictionary < DateTime , ( int count , double average ) > _Observations = new Dictionary < DateTime , ( int count , double average ) > ( ) ;
25
+
24
26
private class ApiKeyServiceClientCredentials : ServiceClientCredentials
25
27
{
26
28
public override Task ProcessHttpRequestAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
@@ -75,7 +77,14 @@ public async Task Run()
75
77
var avgScore = results . Documents
76
78
. Where ( d => d . Score . HasValue )
77
79
. Average ( d => d . Score ) . Value ;
78
- _followerClient . UpdateSentiment ( avgScore ) ;
80
+
81
+ var now = DateTime . Now ;
82
+ _Observations . Add ( now , ( results . Documents . Count , avgScore ) ) ;
83
+ _followerClient . UpdateSentiment ( avgScore ,
84
+ CalculateSentimentOverLastMinutes ( 1 ) ,
85
+ CalculateSentimentOverLastMinutes ( 5 ) ,
86
+ CalculateSentimentOverLastMinutes ( ) ) ;
87
+
79
88
80
89
}
81
90
@@ -85,5 +94,16 @@ public async Task Run()
85
94
86
95
}
87
96
97
+ private double CalculateSentimentOverLastMinutes ( int numMinutes = 0 ) {
98
+
99
+ if ( numMinutes > 0 ) {
100
+ return _Observations . Where ( o => o . Key > DateTime . Now . AddMinutes ( - 1 * numMinutes ) )
101
+ . Average ( v => v . Value . average ) ;
102
+ }
103
+
104
+ return _Observations . Average ( v => v . Value . average ) ;
105
+
106
+ }
107
+
88
108
}
89
109
}
0 commit comments