Skip to content

Commit 03dc7ab

Browse files
committed
Updated with a sentiment trending arrow
1 parent 70e008a commit 03dc7ab

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

Fritz.Chatbot/FritzBot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ private async Task ProcessChatMessage(object sender, ChatMessageEventArgs e)
139139
await chatService.SendWhisperAsync(e.UserName, "Unknown command. Try !help for a list of available commands");
140140
}
141141
} else {
142+
143+
// TODO: Eliminate some of the common bots
144+
// TODO: Capture and transmit the user who sent the message
145+
142146
SentimentSink.RecentChatMessages.Enqueue(e.Message);
143147
}
144148

Fritz.StreamTools/Pages/Sentiment.cshtml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
<head>
99
<meta name="viewport" content="width=device-width" />
1010
<title>Sentiment</title>
11+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/solid.css" integrity="sha384-TbilV5Lbhlwdyc4RuIV/JhD8NR+BfMrvz4BL5QFa2we1hQu6wvREr3v6XSRfCTRp" crossorigin="anonymous">
12+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/fontawesome.css" integrity="sha384-ozJwkrqb90Oa3ZNb+yKFW2lToAWYdTiF1vt8JiH5ptTGHTGcN7qdoR1F95e0kYyG" crossorigin="anonymous">
1113
</head>
1214
<body>
13-
<div id="currentSentiment"></div>
15+
<div>
16+
<span id="currentSentiment"></span><i id="trend"></i>
17+
</div>
18+
<div id="allSentiment"></div>
1419

1520
<script src="~/lib/signalr/signalr-client.js"></script>
1621
<script src="~/js/streamhub.js"></script>
@@ -19,12 +24,21 @@
1924
2025
var hub = new StreamHub();
2126
var sentimentEl = document.getElementById("currentSentiment");
27+
var allEl = document.getElementById("allSentiment");
28+
var trend = document.getElementById("trend");
2229
23-
hub.onSentiment = (newValue) => {
30+
hub.onSentiment = (instant, oneMinute, fiveMinute, all) => {
2431
25-
console.log(newValue);
32+
console.log({
33+
instant: instant,
34+
oneMinute: oneMinute,
35+
fiveMinute: fiveMinute,
36+
all: all
37+
});
2638
27-
sentimentEl.textContent = `Current Sentiment: ${(newValue * 100).toFixed(1)}`;
39+
sentimentEl.textContent = `Current Sentiment: ${(oneMinute * 100).toFixed(1)}`;
40+
allEl.textContent = `Since start: ${(all * 100).toFixed(1)}`;
41+
trend.className = (fiveMinute < oneMinute) ? "fas fa-arrow-up" : (fiveMinute != oneMinute) ? "fas fa-arrow-down" : "fas arrows-alt-h";
2842
2943
}
3044

Fritz.StreamTools/Services/FollowerClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ internal void UpdateGitHub(IEnumerable<GitHubInformation> contributors)
3737
FollowerContext.Clients.Group("github").SendAsync("OnGitHubUpdated", contributors);
3838
}
3939

40-
internal void UpdateSentiment(double newSentiment) {
40+
internal void UpdateSentiment(double newSentiment, double oneMinuteSentiment, double fiveMinuteSentiment, double allSentiment ) {
4141

42-
FollowerContext.Clients.Group("sentiment").SendAsync("OnSentimentUpdated", newSentiment);
42+
FollowerContext.Clients.Group("sentiment").SendAsync("OnSentimentUpdated", newSentiment, oneMinuteSentiment, fiveMinuteSentiment, allSentiment);
4343

4444
}
4545

Fritz.StreamTools/Services/SentimentService.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class SentimentService : IHostedService
2121
private TextAnalyticsClient _client;
2222
private static string _SubscriptionKey;
2323

24+
private Dictionary<DateTime, (int count, double average)> _Observations = new Dictionary<DateTime, (int count, double average)>();
25+
2426
private class ApiKeyServiceClientCredentials : ServiceClientCredentials
2527
{
2628
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
@@ -75,7 +77,14 @@ public async Task Run()
7577
var avgScore = results.Documents
7678
.Where(d => d.Score.HasValue)
7779
.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+
7988

8089
}
8190

@@ -85,5 +94,16 @@ public async Task Run()
8594

8695
}
8796

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+
88108
}
89109
}

Fritz.StreamTools/wwwroot/js/streamhub.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class StreamHub {
3737
if (this.debug) console.debug("OnViewersCountUpdated", { serviceName, viewerCount });
3838
if (this.onViewers) this.onViewers(serviceName, viewerCount);
3939
});
40-
this._hub.on('OnSentimentUpdated', (newSentiment) => {
41-
if (this.debug) console.debug("OnSentimentUpdated", { newSentiment });
42-
if (this.onSentiment) this.onSentiment(newSentiment);
40+
this._hub.on('OnSentimentUpdated', (newSentiment, oneMinute, fiveMinute, all) => {
41+
if (this.debug) console.debug("OnSentimentUpdated", { newSentiment, oneMinute, fiveMinute, all });
42+
if (this.onSentiment) this.onSentiment(newSentiment, oneMinute, fiveMinute, all);
4343
});
4444

4545
return this._hub.start();

0 commit comments

Comments
 (0)