AccountService.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using AIPractice.LabellerServer.Managers;
  2. using AIPractice.LabellerServer.Managers.Entities;
  3. using AIPractice.Shared.ImageRois;
  4. using System;
  5. using System.Linq;
  6. using System.ServiceModel;
  7. namespace AIPractice.LabellerServer
  8. {
  9. public enum LoginResult
  10. {
  11. //Login success.
  12. Success = 0,
  13. //The name is wrong.
  14. WrongName,
  15. //The password is wrong.
  16. WrongPassword,
  17. }
  18. [ServiceContract]
  19. public interface IAccountService
  20. {
  21. /// <summary>
  22. /// Gets account by name and password
  23. /// </summary>
  24. /// <param name="name">The name of the account.</param>
  25. /// <returns>The account</returns>
  26. [OperationContract]
  27. Account GetAccout(string name);
  28. /// <summary>
  29. /// Get the virtual case by given id.
  30. /// </summary>
  31. /// <param name="accountId">The account of the virtual case.</param>
  32. /// <param name="caseId">The case id</param>
  33. /// <returns>The found virtual case.</returns>
  34. [OperationContract]
  35. VirtualCase GetVirtualCaseById(string accountId, string caseId);
  36. /// <summary>
  37. /// Gets the virtual case by given index.
  38. /// </summary>
  39. /// <param name="accountId">The account of the virtual case.</param>
  40. /// <param name="caseIndex">The index</param>
  41. /// <returns>The found virtual case.</returns>
  42. [OperationContract]
  43. VirtualCase GetVirtualCaseByIndex(string accountId, int caseIndex);
  44. /// <summary>
  45. /// Get the case image content by the given id.
  46. /// </summary>
  47. /// <param name="accountId">The account id</param>
  48. /// <param name="caseId">The case id</param>
  49. /// <param name="imageId">The image id of the image</param>
  50. /// <returns>The content of the found image.</returns>
  51. [OperationContract]
  52. string GetCaseImageById(string accountId, string caseId, string imageId);
  53. /// <summary>
  54. /// Get the case image content by the given index.
  55. /// </summary>
  56. /// <param name="accountId">The account id</param>
  57. /// <param name="caseId">The case id</param>
  58. /// <param name="index">The case index</param>
  59. /// <returns>The content of the found image.</returns>
  60. [OperationContract]
  61. string GetCaseImageByIndex(string accountId, string caseId, int index);
  62. /// <summary>
  63. /// Get the committed image by image id.
  64. /// </summary>
  65. /// <returns>The content of the image.</returns>
  66. [OperationContract]
  67. CommittedImage GetCommittedImage(string accountId, string imageId);
  68. /// <summary>
  69. /// Get the latest committed image by image id.
  70. /// </summary>
  71. /// <returns>The content of the image.</returns>
  72. [OperationContract]
  73. CommittedImage GetLatestCommittedImage(string accountId);
  74. /// <summary>
  75. /// Get the virtual case image.
  76. /// </summary>
  77. /// <returns>The image info contains the index.</returns>
  78. [OperationContract]
  79. VirtualCaseImage GetVirtualCaseImageById(string accountId, string imageId);
  80. /// <summary>
  81. /// Get the virtual case image.
  82. /// </summary>
  83. /// <returns>The image info contains the index.</returns>
  84. [OperationContract]
  85. VirtualCaseImage GetVirtualCaseImageByIndex(string caseId, int index);
  86. /// <summary>
  87. /// Gets the total case count.
  88. /// </summary>
  89. /// <returns>The case count of this account</returns>
  90. [OperationContract]
  91. int GetCaseCount(string accountId);
  92. /// <summary>
  93. /// Gets the total image count of current case.
  94. /// </summary>
  95. /// <returns>The total image count of current case</returns>
  96. [OperationContract]
  97. int GetCaseImageCount(string caseId);
  98. /// <summary>
  99. /// Gets the total image count.
  100. /// </summary>
  101. /// <returns>The total image count</returns>
  102. [OperationContract]
  103. int GetTotalImageCount(string accountId);
  104. /// <summary>
  105. /// Gets the total commited image count of this account.
  106. /// </summary>
  107. /// <returns>The total committed image count of this account</returns>
  108. [OperationContract]
  109. int GetCommittedImageCount(string accountId);
  110. /// <summary>
  111. /// Commit a labeled image into database.
  112. /// </summary>
  113. /// <param name="content">The image data to be committed.</param>
  114. [OperationContract]
  115. void CommitImage(string accountId, string content);
  116. /// <summary>
  117. /// Set current image to committedImage.
  118. /// </summary>
  119. /// <param name="accountId"></param>
  120. /// <param name="imageId"></param>
  121. [OperationContract]
  122. void SetCurrentCommitedImage(string accountId, string imageId);
  123. }
  124. public partial class LabellerService : IAccountService
  125. {
  126. private const string ReviewrCaseId = "6DEA2E08DC5A45EAAF1311935D6FE02E";
  127. /// <summary>
  128. /// Get account by account name.
  129. /// </summary>
  130. /// <param name="name"></param>
  131. /// <returns></returns>
  132. public Account GetAccout(string name)
  133. {
  134. var dataManager = ManagerContainer.GetManager<IDataManager>();
  135. var account = dataManager.GetAccountByName(name);
  136. if(account != null)
  137. {
  138. LogManager.WriteLog($"客户端获取用户信息:{account.Name}。");
  139. }
  140. else
  141. {
  142. LogManager.WriteLog($"客户端获取用户信息:{name} 失败。");
  143. }
  144. return account;
  145. }
  146. /// <summary>
  147. /// Get the virtual case.
  148. /// </summary>
  149. /// <returns></returns>
  150. public VirtualCase GetVirtualCaseById(string accountId, string caseId)
  151. {
  152. var dataManager = ManagerContainer.GetManager<IDataManager>();
  153. var account = dataManager.GetAccountById(accountId);
  154. var virtualCase = dataManager.GetVirtualCase(accountId, caseId);
  155. if (virtualCase != null)
  156. {
  157. LogManager.WriteLog($"客户端 {account} 通过ID获取虚拟病例信息:{virtualCase.Id}。");
  158. }
  159. else
  160. {
  161. LogManager.WriteLog($"客户端 {account} 通过ID获取虚拟病例信息:{caseId} 失败。");
  162. }
  163. return virtualCase;
  164. }
  165. /// <summary>
  166. /// Get the virtual case.
  167. /// </summary>
  168. /// <returns></returns>
  169. public VirtualCase GetVirtualCaseByIndex(string accountId, int caseIndex)
  170. {
  171. var dataManager = ManagerContainer.GetManager<IDataManager>();
  172. var account = dataManager.GetAccountById(accountId);
  173. var virtualCase = dataManager.GetVirtualCase(accountId, caseIndex);
  174. if (virtualCase != null)
  175. {
  176. LogManager.WriteLog($"客户端 {account} 通过索引获取虚拟病例信息:{virtualCase.Id}。");
  177. }
  178. else
  179. {
  180. LogManager.WriteLog($"客户端 {account} 通过索引获取虚拟病例信息:{caseIndex} 失败。");
  181. }
  182. return virtualCase;
  183. }
  184. /// <summary>
  185. /// Get the image by image id, it could be the orginal image or committed image.
  186. /// </summary>
  187. /// <returns>The content of the image.</returns>
  188. public string GetCaseImageById(string accountId, string caseId, string imageId)
  189. {
  190. var dataManager = ManagerContainer.GetManager<IDataManager>();
  191. var account = dataManager.GetAccountById(accountId);
  192. var committedImage = dataManager.GetCommittedImage(accountId, imageId);
  193. if (committedImage != null && committedImage.CaseId == caseId)
  194. {
  195. LogManager.WriteLog($"客户端 {account} 通过Id获取图像,图像来自提交的图像:{caseId} - {imageId}。");
  196. return committedImage.Content;
  197. }
  198. var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId,imageId);
  199. if (virtualCaseImage != null && virtualCaseImage.CaseId == caseId)
  200. {
  201. var labelImage = dataManager.GetLabelImage(imageId);
  202. if (labelImage != null)
  203. {
  204. LogManager.WriteLog($"客户端 {account} 通过Id获取图像,图像来自原始图像:{caseId} - {imageId}。");
  205. return labelImage.Content;
  206. }
  207. }
  208. LogManager.WriteLog($"客户端 {account} 通过Id获取图像失败:{caseId} - {imageId}。");
  209. return null;
  210. }
  211. /// <summary>
  212. /// Get the image by image index, it could be the orginal image or committed image.
  213. /// </summary>
  214. /// <returns>The content of the image.</returns>
  215. public string GetCaseImageByIndex(string accountId, string caseId, int index)
  216. {
  217. var dataManager = ManagerContainer.GetManager<IDataManager>();
  218. var account = dataManager.GetAccountById(accountId);
  219. var virtualCaseImage = dataManager.GetVirtualCaseImage(caseId, index);
  220. if (virtualCaseImage != null)
  221. {
  222. var committedImage = dataManager.GetCommittedImage(accountId, virtualCaseImage.ImageId);
  223. if (committedImage != null && committedImage.CaseId == caseId)
  224. {
  225. LogManager.WriteLog($"客户端 {account} 通过索引获取图像,图像来自提交的图像:{caseId} - {index}。");
  226. return committedImage.Content;
  227. }
  228. if (virtualCaseImage != null && virtualCaseImage.CaseId == caseId)
  229. {
  230. var labelImage = dataManager.GetLabelImage(virtualCaseImage.ImageId);
  231. if (labelImage != null)
  232. {
  233. LogManager.WriteLog($"客户端 {account} 通过索引获取图像,图像来自原始图像:{caseId} - {index}。");
  234. return labelImage.Content;
  235. }
  236. }
  237. }
  238. LogManager.WriteLog($"客户端 {account} 通过索引获取图像失败:{caseId} - {index}。");
  239. return null;
  240. }
  241. /// <summary>
  242. /// Get committed image by image id.
  243. /// </summary>
  244. /// <returns>The content of the image.</returns>
  245. public CommittedImage GetCommittedImage(string accountId, string imageId)
  246. {
  247. var dataManager = ManagerContainer.GetManager<IDataManager>();
  248. var account = dataManager.GetAccountById(accountId);
  249. var committedImage = dataManager.GetCommittedImage(accountId, imageId);
  250. if(committedImage != null)
  251. {
  252. LogManager.WriteLog($"客户端 {account} 通过Id获取提交的图像:{imageId}。");
  253. }
  254. else
  255. {
  256. LogManager.WriteLog($"客户端 {account} 通过Id获取提交的图像失败:{imageId}。");
  257. }
  258. return committedImage;
  259. }
  260. /// <summary>
  261. /// Get the latest committed image.
  262. /// </summary>
  263. /// <returns>The content of the image.</returns>
  264. public CommittedImage GetLatestCommittedImage(string accountId)
  265. {
  266. var dataManager = ManagerContainer.GetManager<IDataManager>();
  267. var account = dataManager.GetAccountById(accountId);
  268. var committedImage = dataManager.GetLastCommittedImage(accountId);
  269. if (committedImage != null)
  270. {
  271. LogManager.WriteLog($"客户端 {account} 获取最后提交的图像:{committedImage.ImageId}。");
  272. }
  273. else
  274. {
  275. LogManager.WriteLog($"客户端 {account} 获取最后提交的图像失败。");
  276. }
  277. return committedImage;
  278. }
  279. /// <summary>
  280. /// Get the virtual case image.
  281. /// </summary>
  282. /// <returns>The image info contains the index.</returns>
  283. public VirtualCaseImage GetVirtualCaseImageById(string accountId, string imageId)
  284. {
  285. var dataManager = ManagerContainer.GetManager<IDataManager>();
  286. var account = dataManager.GetAccountById(accountId);
  287. var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId, imageId);
  288. if (virtualCaseImage != null)
  289. {
  290. LogManager.WriteLog($"客户端 {account} 通过Id获取虚拟病例图像:{imageId}。");
  291. }
  292. else
  293. {
  294. LogManager.WriteLog($"客户端 {account} 通过Id获取虚拟病例图像失败:{imageId}。");
  295. }
  296. return virtualCaseImage;
  297. }
  298. /// <summary>
  299. /// Get the virtual case image.
  300. /// </summary>
  301. /// <returns>The image info contains the index.</returns>
  302. public VirtualCaseImage GetVirtualCaseImageByIndex(string caseId, int index)
  303. {
  304. var dataManager = ManagerContainer.GetManager<IDataManager>();
  305. var virtualCaseImage = dataManager.GetVirtualCaseImage(caseId, index);
  306. if (virtualCaseImage != null)
  307. {
  308. LogManager.WriteLog($"客户端通过索引获取虚拟病例图像:{caseId} - {index}。");
  309. }
  310. else
  311. {
  312. LogManager.WriteLog($"客户端通过索引获取虚拟病例图像失败:{caseId} - {index}。");
  313. }
  314. return virtualCaseImage;
  315. }
  316. /// <summary>
  317. /// Gets the total case count.
  318. /// </summary>
  319. /// <returns>The case count of this account</returns>
  320. public int GetCaseCount(string accountId)
  321. {
  322. var dataManager = ManagerContainer.GetManager<IDataManager>();
  323. var account = dataManager.GetAccountById(accountId);
  324. var caseCount = dataManager.GetVirtualCaseCount(accountId);
  325. LogManager.WriteLog($"客户端 {account} 获取虚拟病例数量:{caseCount}。");
  326. return caseCount;
  327. }
  328. /// <summary>
  329. /// Gets the total image count of current case.
  330. /// </summary>
  331. /// <returns>The total image count of current case</returns>
  332. public int GetCaseImageCount(string caseId)
  333. {
  334. var dataManager = ManagerContainer.GetManager<IDataManager>();
  335. var imageCount = dataManager.GetVirtualCaseImageCount(caseId);
  336. LogManager.WriteLog($"客户端获取虚拟病例图像数量:{caseId} - {imageCount}。");
  337. return imageCount;
  338. }
  339. /// <summary>
  340. /// Gets the total commited image count of this account.
  341. /// </summary>
  342. /// <returns>The total committed image count of this account</returns>
  343. public int GetCommittedImageCount(string accountId)
  344. {
  345. var dataManager = ManagerContainer.GetManager<IDataManager>();
  346. var account = dataManager.GetAccountById(accountId);
  347. var imageCount = dataManager.GetCommittedImageCount(accountId);
  348. LogManager.WriteLog($"客户端 {account} 获取提交图像数量:{imageCount}。");
  349. return imageCount;
  350. }
  351. /// <summary>
  352. /// Commit one image.
  353. /// </summary>
  354. /// <param name="accountId"></param>
  355. /// <param name="content"></param>
  356. public void CommitImage(string accountId, string content)
  357. {
  358. var start = Environment.TickCount;
  359. var imageData = ImageData.FromXml(content);
  360. var partCount = 0;
  361. if (imageData.Rois.Count == 0)
  362. {
  363. partCount = 1;
  364. }
  365. else
  366. {
  367. foreach (var roi in imageData.Rois)
  368. {
  369. if (roi.Conclusion != null && roi.Conclusion.IsParts)
  370. {
  371. partCount++;
  372. }
  373. }
  374. }
  375. var imageId = imageData.Id;
  376. var dataManager = ManagerContainer.GetManager<IDataManager>();
  377. var account = dataManager.GetAccountById(accountId);
  378. var existCommittedImage = dataManager.GetCommittedImage(accountId, imageId);
  379. if (existCommittedImage != null)
  380. {
  381. existCommittedImage.Content = content;
  382. existCommittedImage.PartCount = partCount;
  383. existCommittedImage.CreatedTime = DateTime.Now;
  384. existCommittedImage.Result = account.IsReviewer ? ReviewResult.Best : existCommittedImage.Result;
  385. dataManager.UpdateCommittedImage(existCommittedImage);
  386. LogManager.WriteLog($"客户端 {account} 提交图像,因为已经存在,实行更新操作:{imageId}。");
  387. }
  388. else
  389. {
  390. if (account.IsReviewer)
  391. {
  392. var labelImage = dataManager.GetLabelImage(imageId);
  393. if (labelImage != null)
  394. {
  395. var committedImage = new CommittedImage
  396. {
  397. Id = Guid.NewGuid().ToString("N").ToUpper(),
  398. AccountId = accountId,
  399. CaseId = ReviewrCaseId,
  400. ImageId = imageId,
  401. CreatedTime = DateTime.Now,
  402. IsShared = labelImage.IsShared,
  403. IsDeleted = false,
  404. Result = ReviewResult.Best,
  405. Content = content,
  406. PartCount = partCount,
  407. Description = labelImage.Description,
  408. };
  409. dataManager.AddCommittedImage(committedImage);
  410. LogManager.WriteLog($"客户端 {account} 提交图像:{imageId}。");
  411. }
  412. }
  413. else
  414. {
  415. var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId, imageId);
  416. if (virtualCaseImage != null)
  417. {
  418. var labelImage = dataManager.GetLabelImage(virtualCaseImage.ImageId);
  419. if (labelImage != null)
  420. {
  421. var virtualCase = dataManager.GetVirtualCase(accountId, virtualCaseImage.CaseId);
  422. if (virtualCase != null)
  423. {
  424. var committedImage = new CommittedImage
  425. {
  426. Id = Guid.NewGuid().ToString("N").ToUpper(),
  427. AccountId = accountId,
  428. CaseId = virtualCase.Id,
  429. ImageId = virtualCaseImage.ImageId,
  430. CreatedTime = DateTime.Now,
  431. IsShared = labelImage.IsShared,
  432. IsDeleted = false,
  433. Result = ReviewResult.No,
  434. Content = content,
  435. PartCount = partCount,
  436. Description = labelImage.Description,
  437. };
  438. dataManager.AddCommittedImage(committedImage);
  439. LogManager.WriteLog($"客户端 {account} 提交图像:{imageId}。");
  440. }
  441. }
  442. }
  443. }
  444. }
  445. var used = Environment.TickCount - start;
  446. LogManager.WriteLog($"提交图像耗时 {used} ms");
  447. }
  448. /// <summary>
  449. /// Get the total image count
  450. /// </summary>
  451. /// <returns></returns>
  452. public int GetTotalImageCount(string accountId)
  453. {
  454. var dataManager = ManagerContainer.GetManager<IDataManager>();
  455. var caseCount = dataManager.GetVirtualCaseCount(accountId);
  456. var imageCount = 0;
  457. for(var i= 0; i< caseCount; i++)
  458. {
  459. var virtualCase = dataManager.GetVirtualCase(accountId, i);
  460. if(virtualCase != null)
  461. {
  462. imageCount += dataManager.GetVirtualCaseImageCount(virtualCase.Id);
  463. }
  464. }
  465. return imageCount;
  466. }
  467. public void SetCurrentCommitedImage(string accountId, string imageId)
  468. {
  469. var dataManager = ManagerContainer.GetManager<IDataManager>();
  470. var account = dataManager.GetAccountById(accountId);
  471. var existCommittedImage = dataManager.GetCommittedImage(accountId, imageId);
  472. if (existCommittedImage != null)
  473. {
  474. existCommittedImage.CreatedTime = DateTime.Now;
  475. dataManager.UpdateCommittedImage(existCommittedImage);
  476. LogManager.WriteLog($"客户端 {account} 将提交图像设置为当前已经处理图像:{imageId}。");
  477. }
  478. }
  479. }
  480. }