/
RecorderApiFacade.cs
254 lines (210 loc) · 10.4 KB
/
RecorderApiFacade.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Configuration;
using Qualtrak.Coach.DataConnector.Core.Recorder;
using Qualtrak.Coach.DataConnector.Core.Recorder.Args;
using Qualtrak.Coach.DataConnector.Core.Shared;
namespace QATestRecorder
{
public class RecorderApiFacade : IRecorderApiFacade
{
private readonly string _defaultFile;
private static List<AgentRecordingLookup> _agentRecordingsLookup = new List<AgentRecordingLookup>();
#region Private classes
private class AgentRecordingLookup
{
public string AgentId { get; set; }
public string RecordingId { get; set; }
public string Filename { get; set; }
public DateTime Created { get; private set; }
public AgentRecordingLookup(string agentId, string recordingId, string filename)
{
AgentId = agentId;
RecordingId = recordingId;
Filename = filename;
Created = DateTime.Now;
}
}
#endregion
public RecorderApiFacade()
{
if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings["coach:service:defaultmediafile"]))
{
this._defaultFile = WebConfigurationManager.AppSettings["coach:service:defaultmediafile"];
}
}
public async Task<IEnumerable<RecorderUser>> GetUsersAsync(DataConnectorProperties properties)
{
var list = new List<RecorderUser>();
for (var i = 0; i < 1000; i++)
{
var user = new RecorderUser
{
UserId = i.ToString(),
AccountId = i.ToString(),
Username = string.Format("agent.{0}", i),
FirstName = "Agent",
LastName = i.ToString(),
Mail = string.Format("agent.{0}@qualtrak.com", i)
};
list.Add(user);
}
return await Task.FromResult(list);
}
/// <summary>
/// For args.Limit see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#limit
/// For args.SearchCriteria see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#date-range
/// </summary>
/// <param name="userId"></param>
/// <param name="args">See http://data-connector-api.readthedocs.org/en/latest/before-you-start.html for guidance</param>
/// <param name="properties"></param>
/// <returns></returns>
public async Task<IEnumerable<Media>> GetMediaForUserAsync(string userId, MediaForUserArgs args, DataConnectorProperties properties)
{
/*
* Note for Integration Engineer:
*
* It is crucial that you incorporate and honour the args.Limit property. Please let Coach dictate how many recordings it needs.
* - see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#limit
* It is also crucial you implement a filter on a date range. Without a date filter you will always return the same set of recordings
* - see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#date-rang
*/
int recordingsProcessedSoFar = 0;
var list = new List<Media>();
PurgeOldRecordings();
for (var i = 0; i < 1000; i++)
{
var media = new Media
{
RecorderUserId = userId,
Id = Guid.NewGuid().ToString(), // i.ToString(),
Date = DateTime.Now.AddDays(-1),
Metadata = "{ \"metadata\" : [{\"label\" : \"Claim No\", \"value\" : \"123456\", \"field\" : \"claim_no\", \"type\" : \"number\"},{\"label\" : \"Caller Id\", \"value\" : \"004401232312311\", \"field\" : \"caller_id\", \"type\" : \"number\" }, {\"label\" : \"Account No\", \"value\" : \"12121231314AB\", \"field\" : \"account_no\", \"type\" : \"string\" }, {\"label\" : \"A really long label description\", \"value\" : \"A really long piece of call metadata information 1234567890 1234567890\", \"field\" : \"notes\", \"type\" : \"string\" }] }",
FileName = ConvertAgentIdToMediaFile(userId)
};
_agentRecordingsLookup.Add(new AgentRecordingLookup(userId, media.Id, media.FileName));
list.Add(media);
recordingsProcessedSoFar++;
if (recordingsProcessedSoFar > args.Limit) break;
}
return await Task.FromResult(list); ;
}
/// <summary>
/// For args.Limit see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#limit
/// For args.SearchCriteria see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#date-range
/// </summary>
/// <param name="args">See http://data-connector-api.readthedocs.org/en/latest/before-you-start.html for guidance</param>
/// <param name="properties"></param>
/// <returns></returns>
public async Task<IEnumerable<MediaUser>> GetMediaForUsersAsync(MediaForUsersArgs args, DataConnectorProperties properties)
{
/*
* Note for Integration Engineer:
*
* It is crucial that you incorporate and honour the args.Limit property. Please let Coach dictate how many recordings it needs.
* - see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#limit
* It is also crucial you implement a filter on a date range. Without a date filter you will always return the same set of recordings
* - see http://data-connector-api.readthedocs.org/en/latest/before-you-start.html#date-rang
*/
var result = new List<MediaUser>();
MediaForUserArgs userArgs = CreateMediaForUserArgs(args);
foreach (var userId in args.UserIds)
{
IEnumerable<Media> media = await this.GetMediaForUserAsync(userId, userArgs, properties);
IEnumerable<MediaUser> mediaUsers = media.Select(x => new MediaUser { MediaId = x.Id, RecorderUserId = x.RecorderUserId }).ToList();
result.AddRange(mediaUsers);
media.ToList().Clear();
mediaUsers.ToList().Clear();
}
return result;
}
public async Task<IEnumerable<Media>> GetMediaByIdsAsync(IEnumerable<string> ids, DataConnectorProperties properties)
{
var list = new List<Media>();
foreach (var recordingId in ids)
{
var media = new Media
{
Id = recordingId,
Date = DateTime.Now.AddDays(-1),
Metadata = "{ \"metadata\" : [{\"label\" : \"Claim No\", \"value\" : \"123456\", \"field\" : \"claim_no\", \"type\" : \"number\"},{\"label\" : \"Caller Id\", \"value\" : \"004401232312311\", \"field\" : \"caller_id\", \"type\" : \"number\" }, {\"label\" : \"Account No\", \"value\" : \"12121231314AB\", \"field\" : \"account_no\", \"type\" : \"string\" }, {\"label\" : \"A really long label description\", \"value\" : \"A really long piece of call metadata information 1234567890 1234567890\", \"field\" : \"notes\", \"type\" : \"string\" }] }",
FileName = GetRecordingById(recordingId)
};
list.Add(media);
}
return await Task.FromResult(list);
}
public async Task<string> GetMediaUrlAsync(string id, string originalUrl, DataConnectorProperties properties)
{
return await Task.FromResult(originalUrl);
}
public async Task SendEvaluationScoreAsync(SendEvaluationScoreArgs args, DataConnectorProperties properties)
{
throw new NotImplementedException();
}
public async Task<Stream> GetStreamAsync(string url)
{
Stream stream = new MemoryStream(1000000 * 10);
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
using (var client = new HttpClient() { BaseAddress = new Uri(url, UriKind.Absolute) })
{
try
{
var response = client.GetByteArrayAsync("").Result;
stream = new MemoryStream(response);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return await Task.FromResult(stream);
}
private static MediaForUserArgs CreateMediaForUserArgs(MediaForUsersArgs args)
{
return new MediaForUserArgs
{
Limit = args.Limit,
SearchCriteria = args.SearchCriteria,
TimeZone = args.TimeZone
};
}
private string ConvertAgentIdToMediaFile(string userId)
{
string mediaName = GetDefaultFilename();
List<string> mediaTypes = new List<string>() { "wmv", "wav", "mp3", "mp4", "avi", "mov" };
string[] mediaFileNameLookups = { "a.wmv", "a.wav", "c.mp3", "a.mp4", "b.avi", "b.mov" };
string userLookup = string.Empty;
userLookup = userId.Contains(".") ? userId.Split('.')[1] : userId;
var index = mediaTypes.FindIndex(x => x.Contains(userLookup));
if (index > -1)
{
mediaName = mediaFileNameLookups[index];
}
return mediaName;
}
private string GetRecordingById(string recordingId)
{
string mediaName = GetDefaultFilename();
var lookup = _agentRecordingsLookup.FirstOrDefault(x => x.RecordingId == recordingId);
if (lookup != null)
{
mediaName = lookup.Filename;
}
return mediaName;
}
private string GetDefaultFilename()
{
return _defaultFile ?? "a.wmv";
}
private void PurgeOldRecordings()
{
_agentRecordingsLookup.RemoveAll(x => x.Created < DateTime.Now.AddDays(-1));
}
}
}