123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- using AIPractice.LabellerServer.Managers;
- using AIPractice.LabellerServer.Managers.Entities;
- using AIPractice.Shared.ImageRois;
- using System;
- using System.Linq;
- using System.ServiceModel;
- namespace AIPractice.LabellerServer
- {
- public enum LoginResult
- {
- //Login success.
- Success = 0,
- //The name is wrong.
- WrongName,
- //The password is wrong.
- WrongPassword,
- }
- [ServiceContract]
- public interface IAccountService
- {
- /// <summary>
- /// Gets account by name and password
- /// </summary>
- /// <param name="name">The name of the account.</param>
- /// <returns>The account</returns>
- [OperationContract]
- Account GetAccout(string name);
- /// <summary>
- /// Get the virtual case by given id.
- /// </summary>
- /// <param name="accountId">The account of the virtual case.</param>
- /// <param name="caseId">The case id</param>
- /// <returns>The found virtual case.</returns>
- [OperationContract]
- VirtualCase GetVirtualCaseById(string accountId, string caseId);
- /// <summary>
- /// Gets the virtual case by given index.
- /// </summary>
- /// <param name="accountId">The account of the virtual case.</param>
- /// <param name="caseIndex">The index</param>
- /// <returns>The found virtual case.</returns>
- [OperationContract]
- VirtualCase GetVirtualCaseByIndex(string accountId, int caseIndex);
- /// <summary>
- /// Get the case image content by the given id.
- /// </summary>
- /// <param name="accountId">The account id</param>
- /// <param name="caseId">The case id</param>
- /// <param name="imageId">The image id of the image</param>
- /// <returns>The content of the found image.</returns>
- [OperationContract]
- string GetCaseImageById(string accountId, string caseId, string imageId);
- /// <summary>
- /// Get the case image content by the given index.
- /// </summary>
- /// <param name="accountId">The account id</param>
- /// <param name="caseId">The case id</param>
- /// <param name="index">The case index</param>
- /// <returns>The content of the found image.</returns>
- [OperationContract]
- string GetCaseImageByIndex(string accountId, string caseId, int index);
- /// <summary>
- /// Get the committed image by image id.
- /// </summary>
- /// <returns>The content of the image.</returns>
- [OperationContract]
- CommittedImage GetCommittedImage(string accountId, string imageId);
- /// <summary>
- /// Get the latest committed image by image id.
- /// </summary>
- /// <returns>The content of the image.</returns>
- [OperationContract]
- CommittedImage GetLatestCommittedImage(string accountId);
- /// <summary>
- /// Get the virtual case image.
- /// </summary>
- /// <returns>The image info contains the index.</returns>
- [OperationContract]
- VirtualCaseImage GetVirtualCaseImageById(string accountId, string imageId);
- /// <summary>
- /// Get the virtual case image.
- /// </summary>
- /// <returns>The image info contains the index.</returns>
- [OperationContract]
- VirtualCaseImage GetVirtualCaseImageByIndex(string caseId, int index);
- /// <summary>
- /// Gets the total case count.
- /// </summary>
- /// <returns>The case count of this account</returns>
- [OperationContract]
- int GetCaseCount(string accountId);
- /// <summary>
- /// Gets the total image count of current case.
- /// </summary>
- /// <returns>The total image count of current case</returns>
- [OperationContract]
- int GetCaseImageCount(string caseId);
- /// <summary>
- /// Gets the total image count.
- /// </summary>
- /// <returns>The total image count</returns>
- [OperationContract]
- int GetTotalImageCount(string accountId);
- /// <summary>
- /// Gets the total commited image count of this account.
- /// </summary>
- /// <returns>The total committed image count of this account</returns>
- [OperationContract]
- int GetCommittedImageCount(string accountId);
- /// <summary>
- /// Commit a labeled image into database.
- /// </summary>
- /// <param name="content">The image data to be committed.</param>
- [OperationContract]
- void CommitImage(string accountId, string content);
- /// <summary>
- /// Set current image to committedImage.
- /// </summary>
- /// <param name="accountId"></param>
- /// <param name="imageId"></param>
- [OperationContract]
- void SetCurrentCommitedImage(string accountId, string imageId);
- }
- public partial class LabellerService : IAccountService
- {
- private const string ReviewrCaseId = "6DEA2E08DC5A45EAAF1311935D6FE02E";
- /// <summary>
- /// Get account by account name.
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public Account GetAccout(string name)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountByName(name);
- if(account != null)
- {
- LogManager.WriteLog($"客户端获取用户信息:{account.Name}。");
- }
- else
- {
- LogManager.WriteLog($"客户端获取用户信息:{name} 失败。");
- }
- return account;
- }
- /// <summary>
- /// Get the virtual case.
- /// </summary>
- /// <returns></returns>
- public VirtualCase GetVirtualCaseById(string accountId, string caseId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var virtualCase = dataManager.GetVirtualCase(accountId, caseId);
- if (virtualCase != null)
- {
- LogManager.WriteLog($"客户端 {account} 通过ID获取虚拟病例信息:{virtualCase.Id}。");
- }
- else
- {
- LogManager.WriteLog($"客户端 {account} 通过ID获取虚拟病例信息:{caseId} 失败。");
- }
- return virtualCase;
- }
- /// <summary>
- /// Get the virtual case.
- /// </summary>
- /// <returns></returns>
- public VirtualCase GetVirtualCaseByIndex(string accountId, int caseIndex)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var virtualCase = dataManager.GetVirtualCase(accountId, caseIndex);
- if (virtualCase != null)
- {
- LogManager.WriteLog($"客户端 {account} 通过索引获取虚拟病例信息:{virtualCase.Id}。");
- }
- else
- {
- LogManager.WriteLog($"客户端 {account} 通过索引获取虚拟病例信息:{caseIndex} 失败。");
- }
- return virtualCase;
- }
- /// <summary>
- /// Get the image by image id, it could be the orginal image or committed image.
- /// </summary>
- /// <returns>The content of the image.</returns>
- public string GetCaseImageById(string accountId, string caseId, string imageId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var committedImage = dataManager.GetCommittedImage(accountId, imageId);
- if (committedImage != null && committedImage.CaseId == caseId)
- {
- LogManager.WriteLog($"客户端 {account} 通过Id获取图像,图像来自提交的图像:{caseId} - {imageId}。");
- return committedImage.Content;
- }
- var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId,imageId);
- if (virtualCaseImage != null && virtualCaseImage.CaseId == caseId)
- {
- var labelImage = dataManager.GetLabelImage(imageId);
- if (labelImage != null)
- {
- LogManager.WriteLog($"客户端 {account} 通过Id获取图像,图像来自原始图像:{caseId} - {imageId}。");
- return labelImage.Content;
- }
- }
- LogManager.WriteLog($"客户端 {account} 通过Id获取图像失败:{caseId} - {imageId}。");
- return null;
- }
- /// <summary>
- /// Get the image by image index, it could be the orginal image or committed image.
- /// </summary>
- /// <returns>The content of the image.</returns>
- public string GetCaseImageByIndex(string accountId, string caseId, int index)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var virtualCaseImage = dataManager.GetVirtualCaseImage(caseId, index);
- if (virtualCaseImage != null)
- {
- var committedImage = dataManager.GetCommittedImage(accountId, virtualCaseImage.ImageId);
- if (committedImage != null && committedImage.CaseId == caseId)
- {
- LogManager.WriteLog($"客户端 {account} 通过索引获取图像,图像来自提交的图像:{caseId} - {index}。");
- return committedImage.Content;
- }
- if (virtualCaseImage != null && virtualCaseImage.CaseId == caseId)
- {
- var labelImage = dataManager.GetLabelImage(virtualCaseImage.ImageId);
- if (labelImage != null)
- {
- LogManager.WriteLog($"客户端 {account} 通过索引获取图像,图像来自原始图像:{caseId} - {index}。");
- return labelImage.Content;
- }
- }
- }
- LogManager.WriteLog($"客户端 {account} 通过索引获取图像失败:{caseId} - {index}。");
- return null;
- }
- /// <summary>
- /// Get committed image by image id.
- /// </summary>
- /// <returns>The content of the image.</returns>
- public CommittedImage GetCommittedImage(string accountId, string imageId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var committedImage = dataManager.GetCommittedImage(accountId, imageId);
- if(committedImage != null)
- {
- LogManager.WriteLog($"客户端 {account} 通过Id获取提交的图像:{imageId}。");
- }
- else
- {
- LogManager.WriteLog($"客户端 {account} 通过Id获取提交的图像失败:{imageId}。");
- }
- return committedImage;
- }
- /// <summary>
- /// Get the latest committed image.
- /// </summary>
- /// <returns>The content of the image.</returns>
- public CommittedImage GetLatestCommittedImage(string accountId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var committedImage = dataManager.GetLastCommittedImage(accountId);
- if (committedImage != null)
- {
- LogManager.WriteLog($"客户端 {account} 获取最后提交的图像:{committedImage.ImageId}。");
- }
- else
- {
- LogManager.WriteLog($"客户端 {account} 获取最后提交的图像失败。");
- }
- return committedImage;
- }
- /// <summary>
- /// Get the virtual case image.
- /// </summary>
- /// <returns>The image info contains the index.</returns>
- public VirtualCaseImage GetVirtualCaseImageById(string accountId, string imageId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId, imageId);
- if (virtualCaseImage != null)
- {
- LogManager.WriteLog($"客户端 {account} 通过Id获取虚拟病例图像:{imageId}。");
- }
- else
- {
- LogManager.WriteLog($"客户端 {account} 通过Id获取虚拟病例图像失败:{imageId}。");
- }
- return virtualCaseImage;
- }
- /// <summary>
- /// Get the virtual case image.
- /// </summary>
- /// <returns>The image info contains the index.</returns>
- public VirtualCaseImage GetVirtualCaseImageByIndex(string caseId, int index)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var virtualCaseImage = dataManager.GetVirtualCaseImage(caseId, index);
- if (virtualCaseImage != null)
- {
- LogManager.WriteLog($"客户端通过索引获取虚拟病例图像:{caseId} - {index}。");
- }
- else
- {
- LogManager.WriteLog($"客户端通过索引获取虚拟病例图像失败:{caseId} - {index}。");
- }
- return virtualCaseImage;
- }
- /// <summary>
- /// Gets the total case count.
- /// </summary>
- /// <returns>The case count of this account</returns>
- public int GetCaseCount(string accountId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var caseCount = dataManager.GetVirtualCaseCount(accountId);
- LogManager.WriteLog($"客户端 {account} 获取虚拟病例数量:{caseCount}。");
- return caseCount;
- }
- /// <summary>
- /// Gets the total image count of current case.
- /// </summary>
- /// <returns>The total image count of current case</returns>
- public int GetCaseImageCount(string caseId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var imageCount = dataManager.GetVirtualCaseImageCount(caseId);
- LogManager.WriteLog($"客户端获取虚拟病例图像数量:{caseId} - {imageCount}。");
- return imageCount;
- }
- /// <summary>
- /// Gets the total commited image count of this account.
- /// </summary>
- /// <returns>The total committed image count of this account</returns>
- public int GetCommittedImageCount(string accountId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var imageCount = dataManager.GetCommittedImageCount(accountId);
- LogManager.WriteLog($"客户端 {account} 获取提交图像数量:{imageCount}。");
- return imageCount;
- }
- /// <summary>
- /// Commit one image.
- /// </summary>
- /// <param name="accountId"></param>
- /// <param name="content"></param>
- public void CommitImage(string accountId, string content)
- {
- var start = Environment.TickCount;
- var imageData = ImageData.FromXml(content);
- var partCount = 0;
- if (imageData.Rois.Count == 0)
- {
- partCount = 1;
- }
- else
- {
- foreach (var roi in imageData.Rois)
- {
- if (roi.Conclusion != null && roi.Conclusion.IsParts)
- {
- partCount++;
- }
- }
- }
- var imageId = imageData.Id;
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var existCommittedImage = dataManager.GetCommittedImage(accountId, imageId);
- if (existCommittedImage != null)
- {
- existCommittedImage.Content = content;
- existCommittedImage.PartCount = partCount;
- existCommittedImage.CreatedTime = DateTime.Now;
- existCommittedImage.Result = account.IsReviewer ? ReviewResult.Best : existCommittedImage.Result;
- dataManager.UpdateCommittedImage(existCommittedImage);
- LogManager.WriteLog($"客户端 {account} 提交图像,因为已经存在,实行更新操作:{imageId}。");
- }
- else
- {
- if (account.IsReviewer)
- {
- var labelImage = dataManager.GetLabelImage(imageId);
- if (labelImage != null)
- {
- var committedImage = new CommittedImage
- {
- Id = Guid.NewGuid().ToString("N").ToUpper(),
- AccountId = accountId,
- CaseId = ReviewrCaseId,
- ImageId = imageId,
- CreatedTime = DateTime.Now,
- IsShared = labelImage.IsShared,
- IsDeleted = false,
- Result = ReviewResult.Best,
- Content = content,
- PartCount = partCount,
- Description = labelImage.Description,
- };
- dataManager.AddCommittedImage(committedImage);
- LogManager.WriteLog($"客户端 {account} 提交图像:{imageId}。");
- }
- }
- else
- {
- var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId, imageId);
- if (virtualCaseImage != null)
- {
- var labelImage = dataManager.GetLabelImage(virtualCaseImage.ImageId);
- if (labelImage != null)
- {
- var virtualCase = dataManager.GetVirtualCase(accountId, virtualCaseImage.CaseId);
- if (virtualCase != null)
- {
- var committedImage = new CommittedImage
- {
- Id = Guid.NewGuid().ToString("N").ToUpper(),
- AccountId = accountId,
- CaseId = virtualCase.Id,
- ImageId = virtualCaseImage.ImageId,
- CreatedTime = DateTime.Now,
- IsShared = labelImage.IsShared,
- IsDeleted = false,
- Result = ReviewResult.No,
- Content = content,
- PartCount = partCount,
- Description = labelImage.Description,
- };
- dataManager.AddCommittedImage(committedImage);
- LogManager.WriteLog($"客户端 {account} 提交图像:{imageId}。");
- }
- }
- }
- }
- }
- var used = Environment.TickCount - start;
- LogManager.WriteLog($"提交图像耗时 {used} ms");
- }
- /// <summary>
- /// Get the total image count
- /// </summary>
- /// <returns></returns>
- public int GetTotalImageCount(string accountId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var caseCount = dataManager.GetVirtualCaseCount(accountId);
- var imageCount = 0;
- for(var i= 0; i< caseCount; i++)
- {
- var virtualCase = dataManager.GetVirtualCase(accountId, i);
- if(virtualCase != null)
- {
- imageCount += dataManager.GetVirtualCaseImageCount(virtualCase.Id);
- }
- }
- return imageCount;
- }
-
- public void SetCurrentCommitedImage(string accountId, string imageId)
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var account = dataManager.GetAccountById(accountId);
- var existCommittedImage = dataManager.GetCommittedImage(accountId, imageId);
- if (existCommittedImage != null)
- {
- existCommittedImage.CreatedTime = DateTime.Now;
- dataManager.UpdateCommittedImage(existCommittedImage);
- LogManager.WriteLog($"客户端 {account} 将提交图像设置为当前已经处理图像:{imageId}。");
- }
- }
- }
- }
|