From 27cceb14e1465c5b1cdbbf28d16ffb175167ad62 Mon Sep 17 00:00:00 2001 From: amrita.vishnoi Date: Mon, 14 Aug 2017 11:06:08 +0530 Subject: [PATCH] Optimized query for getting data on the basis of body sytem --- 400-SOURCECODE/AIAHTML5.API/Models/PixelLocator.cs | 44 +++++++++++++++++--------------------------- 600-POC/fileSave.rar | Bin 0 -> 23905 bytes 600-POC/fileSave/DBSAVe/Program.cs | 551 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/DBSAVe/Program_20Threadsfor25Zoom.cs | 537 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/DBSAVe/Program_NOMultiThreading.cs | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/DBSAVe/Program_UserInput_DifferentDB.cs | 438 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/DBSAVe/Program_WithoutMultithreading_withDBSearchBeforeExeutiom.cs | 458 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/jsonFile_generate/AJs/app.component.css | 0 600-POC/fileSave/jsonFile_generate/AJs/app.component.html | 5 +++++ 600-POC/fileSave/jsonFile_generate/AJs/app.component.spec.ts | 32 ++++++++++++++++++++++++++++++++ 600-POC/fileSave/jsonFile_generate/AJs/app.component.ts | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/jsonFile_generate/AJs/app.module.ts | 20 ++++++++++++++++++++ 600-POC/fileSave/jsonFile_generate/AJs/services/image.service.ts | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 600-POC/fileSave/jsonFile_generate/ProcessImageController.cs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 2907 insertions(+), 27 deletions(-) create mode 100644 600-POC/fileSave.rar create mode 100644 600-POC/fileSave/DBSAVe/Program.cs create mode 100644 600-POC/fileSave/DBSAVe/Program_20Threadsfor25Zoom.cs create mode 100644 600-POC/fileSave/DBSAVe/Program_NOMultiThreading.cs create mode 100644 600-POC/fileSave/DBSAVe/Program_UserInput_DifferentDB.cs create mode 100644 600-POC/fileSave/DBSAVe/Program_WithoutMultithreading_withDBSearchBeforeExeutiom.cs create mode 100644 600-POC/fileSave/jsonFile_generate/AJs/app.component.css create mode 100644 600-POC/fileSave/jsonFile_generate/AJs/app.component.html create mode 100644 600-POC/fileSave/jsonFile_generate/AJs/app.component.spec.ts create mode 100644 600-POC/fileSave/jsonFile_generate/AJs/app.component.ts create mode 100644 600-POC/fileSave/jsonFile_generate/AJs/app.module.ts create mode 100644 600-POC/fileSave/jsonFile_generate/AJs/services/image.service.ts create mode 100644 600-POC/fileSave/jsonFile_generate/ProcessImageController.cs diff --git a/400-SOURCECODE/AIAHTML5.API/Models/PixelLocator.cs b/400-SOURCECODE/AIAHTML5.API/Models/PixelLocator.cs index c50d4b9..9d881e1 100644 --- a/400-SOURCECODE/AIAHTML5.API/Models/PixelLocator.cs +++ b/400-SOURCECODE/AIAHTML5.API/Models/PixelLocator.cs @@ -9,31 +9,26 @@ namespace AIAHTML5.API.Models { public class PixelLocator { - - internal static dynamic GetBodyRegionsPixelData(string layerNumber, string bodyViewIndex, string systemNumber, string zoom) - { - try + + internal static dynamic GetBodyRegionsPixelData(string layerNumber, string bodyViewIndex, string systemNumber, string zoom) { - var client = new MongoClient("localhost:27017"); - var db = client.GetDatabase("AIAHTML5"); + var client = new MongoClient("mongodb://192.168.90.43:27017"); + var db = client.GetDatabase("aia"); var col = db.GetCollection("DAImages"); - var pipeline = new BsonDocument[] - { - new BsonDocument{ {"$match", new BsonDocument("terms.SystemNumbers", Convert.ToInt32(systemNumber)) }}, - new BsonDocument{{"$unwind", "$terms"}}, - new BsonDocument{ { "$match", new BsonDocument("terms.SystemNumbers", Convert.ToInt32(systemNumber)) }}, - new BsonDocument{ { "$match", new BsonDocument("bodyViewIndex", Convert.ToInt32(bodyViewIndex)) }}, - new BsonDocument{ { "$match", new BsonDocument("layerNumber", Convert.ToInt32(layerNumber)) }}, - new BsonDocument{ { "$match", new BsonDocument("zoom", Convert.ToInt32(zoom)) }}, - - new BsonDocument { {"$project", new BsonDocument() - .Add("_id", 0) - .Add("terms", 1) - .Add("bodyRegionId", 1) - .Add("layerNumber", 1) - .Add("bodyViewIndex", 1) + var pipeline = new BsonDocument[] + { + new BsonDocument{{"$unwind", "$terms"}}, + new BsonDocument{ { "$match", new BsonDocument("terms.SystemNumbers", + Convert.ToInt32(systemNumber)).Add("bodyViewIndex", + Convert.ToInt32(bodyViewIndex)).Add("layerNumber", + Convert.ToInt32(layerNumber)).Add("zoom", + Convert.ToInt32(zoom)) }}, + new BsonDocument { {"$project", new BsonDocument() + .Add("_id", 0) + .Add("terms", 1) + .Add("bodyRegionId", 1) } } }; @@ -42,10 +37,5 @@ namespace AIAHTML5.API.Models return pixelData.ToJson(); } - catch (Exception e) - { - return "error"; - } - } -} +} } diff --git a/600-POC/fileSave.rar b/600-POC/fileSave.rar new file mode 100644 index 0000000..d4302ab Binary files /dev/null and b/600-POC/fileSave.rar differ diff --git a/600-POC/fileSave/DBSAVe/Program.cs b/600-POC/fileSave/DBSAVe/Program.cs new file mode 100644 index 0000000..d1b7b19 --- /dev/null +++ b/600-POC/fileSave/DBSAVe/Program.cs @@ -0,0 +1,551 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +//using log4net; +using MongoDB.Driver; +using System.IO; +using Newtonsoft.Json.Linq; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace AIAHighlight.DBSave +{ + [BsonIgnoreExtraElements] + public class BodyRegionImage + { + public ObjectId _id { get; set; } + public string imageName; + public int bodyViewIndex; + public int layerNumber; + public int bodyRegionId; + public int zoom; + public List terms; + + public BodyRegionImage() + { + this.terms = new List(); + } + } + + public class Term + { + public int TermNumber; + public List SystemNumbers; + public List PixelLocations; + public int ActualTermNumber; + + public Term(int termNumber, int systemNumber, int pixelLocation) + { + this.SystemNumbers = new List(); + this.PixelLocations = new List(); + this.TermNumber = termNumber; + this.SystemNumbers.Add(systemNumber); + this.PixelLocations.Add(pixelLocation); + } + } + + + + + public class Program + { + //public JObject data { get; set; } + // public Dictionary termList; + + internal void saveImageData(JObject data, int bodyviewId) + { + // DateTime datetime = new DateTime(); + Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+ + data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]); + try + { + BodyRegionImage bodyRegionImage = new BodyRegionImage(); + + var client = new MongoClient(); + + //var db = client.GetDatabase("AIADB_bodyviewId"+bodyviewId.ToString()); + var db = client.GetDatabase("aia"); + + var col = db.GetCollection("DAImages"); + + JObject imageData = data; + + + + string bodyViewIndex = data["bodyViewIndex"].ToString(); + + string termFilePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\bodyViewsData\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + //string termFilePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\dist\assets\data\json\da\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + + string systemTermMapping; + using (StreamReader reader = new StreamReader(termFilePath)) + + // using (StreamReader reader = new StreamReader(path+fileName)) + { + systemTermMapping = reader.ReadToEnd(); + } + + JObject systemTermMappingObject = JObject.Parse(systemTermMapping); + + //Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject created."); + + dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; + + dynamic imageDataArray = data["data"]; + + Dictionary termList = new Dictionary(); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + for (int i = 0; i < imageDataArray.Count; i += 4) + { + if(imageDataArray[i] == 0 && imageDataArray[i+1] == 0 && imageDataArray[i+2] == 0) + { + continue; + } + var RED = Convert.ToInt32(imageDataArray[i]) - 9; + var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9; + var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9; + + if (RED == -9 && GREEN == -9 && BLUE == -9) + continue; + + var Red = ""; + var Green = ""; + var Blue = ""; + string zero = "0"; + + if ((RED).ToString().Length != 2) + { + Red = zero + RED.ToString(); + } + else + { + Red = RED.ToString(); + } + if ((GREEN).ToString().Length != 2) + { + Green = zero + GREEN.ToString(); + } + else + { + Green = GREEN.ToString(); + } + if ((BLUE).ToString().Length != 2) + { + Blue = zero + BLUE.ToString(); + } + else + { + Blue = BLUE.ToString(); + } + + string Icolor = (Red + Green + Blue); + + + + foreach (dynamic systemTerm in systemTerms) + { + + int iColor = Convert.ToInt32(Icolor); + int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString()); + int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString()); + + if (systemTerm._TermNumber.ToString() == Icolor) + { + this.AddToTermList(termNumber, systemNumber, i, termList); + //this.AddToTermList(termNumber, systemNumber, i, termList); + this.AddToTermList(termNumber, systemNumber, i + 1, termList); + this.AddToTermList(termNumber, systemNumber, i + 2, termList); + break; + } + } + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList.. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + bodyRegionImage.imageName = data["imageName"].ToString(); + bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]); + + foreach (int key in termList.Keys) + { + bodyRegionImage.terms.Add(termList[key]); + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + col.InsertOne(bodyRegionImage); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + var logCol = db.GetCollection("DAImageLogs"); + + BodyRegionImage bodyRegionImageTolog = new BodyRegionImage(); + bodyRegionImageTolog.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImageTolog.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImageTolog.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImageTolog.zoom = Convert.ToInt32(data["zoom"]); + + logCol.InsertOne(bodyRegionImageTolog); + + bodyRegionImageTolog = null; + + data = null; + termList = null; + systemTermMappingObject = null; + systemTerms = null; + } + catch(Exception ex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace); + + } + + + } + + public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary termList) + { + if (!termList.ContainsKey(termNumber)) + { + termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation)); + } + else + { + bool isSystemNumberExisting = false; + foreach (int sn in termList[termNumber].SystemNumbers) + { + if (systemNumber == sn) + { + isSystemNumberExisting = true; + break; + } + + } + if (!isSystemNumberExisting) + termList[termNumber].SystemNumbers.Add(systemNumber); + + bool isPixelLocationExisting = false; + foreach (int pl in termList[termNumber].PixelLocations) + { + if (pixelLocation == pl) + { + isPixelLocationExisting = true; + break; + } + + } + if (!isPixelLocationExisting) + termList[termNumber].PixelLocations.Add(pixelLocation); + + } + } + + + + + static void Main(string[] args) + { + + Console.WriteLine("Enter BodyViewId"); + int BodyViewIndex = Convert.ToInt32(Console.ReadLine()); + + Program program = new Program(); + program.getAlreadyInsertedLayer(BodyViewIndex.ToString()); + program = null; + + } + + internal void getAlreadyInsertedLayer(string bodyviewId) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "inside getAlreadyInsertedLayer"); + int totalLayers = 0; + int [] totalBodyReigons =null; + List bodyRegions = new List(); + + switch (Convert.ToInt32(bodyviewId)) + { + case 1: + totalLayers = 330; + totalBodyReigons = new int[6] {1,2,3,4,5,6}; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + bodyRegions.Add(6); + break; + case 2: + totalLayers = 288; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + break; + case 3: + totalLayers = 127; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + break; + case 4: + totalLayers = 186; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + bodyRegions.Add(6); + break; + case 5: + totalLayers = 229; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + bodyRegions.Add(6); + break; + case 6: + totalLayers = 289; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + + break; + case 7: + totalLayers = 130; + + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + + break; + case 8: + totalLayers = 186; + + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + bodyRegions.Add(6); + break; + case 9: + totalLayers = 64; + bodyRegions.Add(6); + break; + case 10: + totalLayers = 72; + bodyRegions.Add(6); + break; + case 11: + totalLayers = 64; + bodyRegions.Add(6); + break; + case 12: + totalLayers = 72; + bodyRegions.Add(6); + break; + } + + var client = new MongoClient(); + // var db = client.GetDatabase("AIADB_bodyviewId" + bodyviewId.ToString()); + var db = client.GetDatabase("aia"); + + var col = db.GetCollection("DAImages"); + //var col = db.GetCollection("DAImages"); + + + FilterDefinition[] filterCondition = { + Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(bodyviewId))}; + + dynamic list = col.Find(Builders.Filter.And(filterCondition)).Project(Builders.Projection.Include("layerNumber").Include("bodyRegionId")).ToList(); + Console.WriteLine("already inserted record count: " + list.Count); + + Dictionary> layersToProcess = new Dictionary>(); + + for (int i = 1; i <= totalLayers; i++) + { + List bodyRegionForThisLayer = new List(); + switch (Convert.ToInt32(bodyviewId)) + { + case 1: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + bodyRegionForThisLayer.Add(6); + break; + case 2: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + break; + case 3: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + break; + case 4: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + bodyRegionForThisLayer.Add(6); + break; + case 5: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + bodyRegionForThisLayer.Add(6); + break; + case 6: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + + break; + case 7: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + + break; + case 8: + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + bodyRegionForThisLayer.Add(6); + break; + } + + int previousLayerNumber = 0; + int existingLayerNumber = 0; + bool isLayerMatched = false; + + foreach (dynamic lNumber in list) + { + existingLayerNumber = Convert.ToInt32((lNumber.GetValue("layerNumber")).ToString()); + + if (existingLayerNumber == i) + { + isLayerMatched = true; + int existingBodyRegionId = Convert.ToInt32((lNumber.GetValue("bodyRegionId")).ToString()); + + + if (bodyRegionForThisLayer.Contains(existingBodyRegionId)) { + //remove from bodyRegions list as this is already present + bodyRegionForThisLayer.Remove(existingBodyRegionId); + } + previousLayerNumber = existingLayerNumber; + + } + + else + { + + } + + } + if(isLayerMatched) + layersToProcess.Add(previousLayerNumber, bodyRegionForThisLayer); + else + layersToProcess.Add(i, bodyRegionForThisLayer); + } + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "after preparing list to process files To Process count: "+ layersToProcess.Count); + + makeFileNameAndProcess(layersToProcess,Convert.ToInt32(bodyviewId)); + } + + + internal void makeFileNameAndProcess(Dictionary> filesToProcess, int BodyViewIndex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in makeFileNameAndProcess"); + + foreach (KeyValuePair> pair in filesToProcess) + { + + + foreach (int item in pair.Value) + { + string fileName = "BodyView" + BodyViewIndex + "_layer" + pair.Key + "_bodyRegionId" + item+".json"; + + //string filePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\AIAHighlight\AIAHighlight\data\" + fileName; + string filePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\" + fileName; + getDataFromFileAndSaveInDB(filePath, BodyViewIndex); + } + + } + } + + internal void processFiles(string[] files,int bodyviewId) + { + + //without multithreading + foreach(string filename in files) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in processFiles for filename"); + getDataFromFileAndSaveInDB(filename, bodyviewId); + } + + } + + + + internal void getDataFromFileAndSaveInDB(string filePath,int bodyViewId) + { + if (File.Exists(filePath)) + { + string fileData; + using (StreamReader reader = new StreamReader(filePath)) + { + fileData = reader.ReadToEnd(); + + JObject data = JObject.Parse(fileData); + this.saveImageData(data, bodyViewId); + } + + + } + else + { + Console.WriteLine("FILE.NOT.FOUND AT" + + ": " + filePath); + + } + } + } +} diff --git a/600-POC/fileSave/DBSAVe/Program_20Threadsfor25Zoom.cs b/600-POC/fileSave/DBSAVe/Program_20Threadsfor25Zoom.cs new file mode 100644 index 0000000..3c530c9 --- /dev/null +++ b/600-POC/fileSave/DBSAVe/Program_20Threadsfor25Zoom.cs @@ -0,0 +1,537 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +//using log4net; +using MongoDB.Driver; +using System.IO; +using Newtonsoft.Json.Linq; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace AIAHighlight.DBSave +{ + [BsonIgnoreExtraElements] + public class BodyRegionImage + { + public ObjectId _id { get; set; } + public string imageName; + public int bodyViewIndex; + public int layerNumber; + public int bodyRegionId; + public int zoom; + public List terms; + + public BodyRegionImage() + { + this.terms = new List(); + } + } + + public class Term + { + public int TermNumber; + public List SystemNumbers; + public List PixelLocations; + public int ActualTermNumber; + + public Term(int termNumber, int systemNumber, int pixelLocation) + { + this.SystemNumbers = new List(); + this.PixelLocations = new List(); + this.TermNumber = termNumber; + this.SystemNumbers.Add(systemNumber); + this.PixelLocations.Add(pixelLocation); + } + } + + + + + public class Program + { + //public JObject data { get; set; } + // public Dictionary termList; + + internal void saveImageData(JObject data) + { + // DateTime datetime = new DateTime(); + Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+ + data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]); + try + { + BodyRegionImage bodyRegionImage = new BodyRegionImage(); + + var client = new MongoClient(); + + var db = client.GetDatabase("AIAImageData_25"); + + var col = db.GetCollection("DAImages"); + + JObject imageData = data; + + + + string bodyViewIndex = data["bodyViewIndex"].ToString(); + + string termFilePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\bodyViewsData\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + //string termFilePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\dist\assets\data\json\da\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + + string systemTermMapping; + using (StreamReader reader = new StreamReader(termFilePath)) + + // using (StreamReader reader = new StreamReader(path+fileName)) + { + systemTermMapping = reader.ReadToEnd(); + } + + JObject systemTermMappingObject = JObject.Parse(systemTermMapping); + + //Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject created."); + + dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; + + dynamic imageDataArray = data["data"]; + + Dictionary termList = new Dictionary(); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + for (int i = 0; i < imageDataArray.Count; i += 4) + { + if(imageDataArray[i] == 0 && imageDataArray[i+1] == 0 && imageDataArray[i+2] == 0) + { + continue; + } + var RED = Convert.ToInt32(imageDataArray[i]) - 9; + var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9; + var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9; + + if (RED == -9 && GREEN == -9 && BLUE == -9) + continue; + + var Red = ""; + var Green = ""; + var Blue = ""; + string zero = "0"; + + if ((RED).ToString().Length != 2) + { + Red = zero + RED.ToString(); + } + else + { + Red = RED.ToString(); + } + if ((GREEN).ToString().Length != 2) + { + Green = zero + GREEN.ToString(); + } + else + { + Green = GREEN.ToString(); + } + if ((BLUE).ToString().Length != 2) + { + Blue = zero + BLUE.ToString(); + } + else + { + Blue = BLUE.ToString(); + } + + string Icolor = (Red + Green + Blue); + + + + foreach (dynamic systemTerm in systemTerms) + { + + int iColor = Convert.ToInt32(Icolor); + int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString()); + int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString()); + + if (systemTerm._TermNumber.ToString() == Icolor) + { + this.AddToTermList(termNumber, systemNumber, i, termList); + //this.AddToTermList(termNumber, systemNumber, i, termList); + this.AddToTermList(termNumber, systemNumber, i + 1, termList); + this.AddToTermList(termNumber, systemNumber, i + 2, termList); + break; + } + } + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList.. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + bodyRegionImage.imageName = data["imageName"].ToString(); + bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]); + + foreach (int key in termList.Keys) + { + bodyRegionImage.terms.Add(termList[key]); + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + col.InsertOne(bodyRegionImage); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + var logCol = db.GetCollection("DAImageLogs"); + + BodyRegionImage bodyRegionImageTolog = new BodyRegionImage(); + bodyRegionImageTolog.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImageTolog.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImageTolog.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImageTolog.zoom = Convert.ToInt32(data["zoom"]); + + logCol.InsertOne(bodyRegionImageTolog); + + bodyRegionImageTolog = null; + + data = null; + termList = null; + systemTermMappingObject = null; + systemTerms = null; + } + catch(Exception ex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace); + + } + + + } + + public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary termList) + { + if (!termList.ContainsKey(termNumber)) + { + termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation)); + } + else + { + bool isSystemNumberExisting = false; + foreach (int sn in termList[termNumber].SystemNumbers) + { + if (systemNumber == sn) + { + isSystemNumberExisting = true; + break; + } + + } + if (!isSystemNumberExisting) + termList[termNumber].SystemNumbers.Add(systemNumber); + + bool isPixelLocationExisting = false; + foreach (int pl in termList[termNumber].PixelLocations) + { + if (pixelLocation == pl) + { + isPixelLocationExisting = true; + break; + } + + } + if (!isPixelLocationExisting) + termList[termNumber].PixelLocations.Add(pixelLocation); + + } + } + + + + + static void Main(string[] args) + { + + + //string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data", "*.json"); + + string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\25", "*.json"); + //string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\BodyView4", "*.json"); + Program program = new Program(); + program.processFiles(files, 0, 19); + program = null; + + } + + internal void processFiles(string[] files, int startIndex, int endIndex) + { + + prepareArrayOfFiles(files, startIndex, endIndex); + + } + + internal void prepareArrayOfFiles(string[] files, int startIndex, int endIndex) + { + string[] filesToProcess = new string[20]; + int processedFiles = 0; + int j = 0; + for (int i = startIndex; i <= endIndex; i++) + { + + filesToProcess[j] = files[i]; + j++; + + } + + Parallel.ForEach(filesToProcess, (currentFile) => + { + if (currentFile != null) + { + Console.WriteLine("currentFile = " + currentFile); + using (StreamReader reader = new StreamReader(currentFile)) + { + getDataFromFileAndSaveInDB(currentFile); + processedFiles++; + + } + + if (processedFiles == 20) + { + startIndex = endIndex + 1; + endIndex = startIndex + 19; + filesToProcess = null; + + //in case after adding 20, the endIndex reaches more than length of files array(total files) + if (endIndex > files.Length - 1) + { + // int remainingFiles = endIndex - files.Length; + endIndex = files.Length - 1; + } + if (endIndex < files.Length) + { + processFiles(files, startIndex, endIndex); + } + //} + } + } + } + ); + + } + + + internal void getDataFromFileAndSaveInDB(string filePath) + { + if (File.Exists(filePath)) + { + string fileData; + using (StreamReader reader = new StreamReader(filePath)) + { + fileData = reader.ReadToEnd(); + + JObject data = JObject.Parse(fileData); + this.saveImageData(data); + } + + + } + else + { + Console.WriteLine("FILE.NOT.FOUND AT" + + ": " + filePath); + + } + } + //internal void getAlreadyInsertedLayer(string bodyviewId) + //{ + // Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "inside getAlreadyInsertedLayer"); + // int totalLayers = 0; + // int [] totalBodyReigons =null; + // List bodyRegions = new List(); + + // switch (Convert.ToInt32(bodyviewId)) + // { + // case 1: + // totalLayers = 330; + // totalBodyReigons = new int[6] {1,2,3,4,5,6}; + // bodyRegions.Add(1); + // bodyRegions.Add(2); + // bodyRegions.Add(3); + // bodyRegions.Add(4); + // bodyRegions.Add(5); + // bodyRegions.Add(6); + // break; + // case 2: + // totalLayers = 288; + // // totalBodyReigons = 6; + // break; + // case 3: + // totalLayers = 127; + // // totalBodyReigons = 6; + // break; + // case 4: + // totalLayers = 186; + // // totalBodyReigons = 6; + // break; + // case 5: + // totalLayers = 229; + // // totalBodyReigons = 6; + // break; + // case 6: + // totalLayers = 289; + // // totalBodyReigons = 6; + // break; + // case 7: + // totalLayers = 130; + // // totalBodyReigons = 6; + // break; + // case 8: + // totalLayers = 186; + // // totalBodyReigons = 6; + // break; + // case 9: + // totalLayers = 64; + // // totalBodyReigons = 6; + // break; + // case 10: + // totalLayers = 72; + // // totalBodyReigons = 6; + // break; + // case 11: + // totalLayers = 64; + // // totalBodyReigons = 6; + // break; + // case 12: + // totalLayers = 72; + // // totalBodyReigons = 6; + // break; + // } + + // var client = new MongoClient(); + // var db = client.GetDatabase("AIADB_bodyviewId" + bodyviewId.ToString()); + // //var db = client.GetDatabase("AIAImageDB"); + + // var col = db.GetCollection("DAImagesLogs"); + // //var col = db.GetCollection("DAImages"); + + + // FilterDefinition[] filterCondition = { + // Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(bodyviewId))}; + + // dynamic list = col.Find(Builders.Filter.And(filterCondition)).Project(Builders.Projection.Include("layerNumber").Include("bodyRegionId")).ToList(); + // //collection.Find(new BsonDocument()).Project(Builders.Projection.Include("Price").Exclude("_id")).ToListAsync(); + // // return list; + // Dictionary> layersToProcess = new Dictionary>(); + + // for (int i = 1; i <= totalLayers; i++) + // { + // List bodyRegionForThisLayer = new List(); + // bodyRegionForThisLayer.Add(1); + // bodyRegionForThisLayer.Add(2); + // bodyRegionForThisLayer.Add(3); + // bodyRegionForThisLayer.Add(4); + // bodyRegionForThisLayer.Add(5); + // bodyRegionForThisLayer.Add(6); + + // int previousLayerNumber = 0; + // int existingLayerNumber = 0; + // bool isLayerMatched = false; + + // foreach (dynamic lNumber in list) + // { + // existingLayerNumber = Convert.ToInt32((lNumber.GetValue("layerNumber")).ToString()); + + // if (existingLayerNumber == i) + // { + // isLayerMatched = true; + // int existingBodyRegionId = Convert.ToInt32((lNumber.GetValue("bodyRegionId")).ToString()); + + + // if (bodyRegionForThisLayer.Contains(existingBodyRegionId)) { + // //remove from bodyRegions list as this is already present + // bodyRegionForThisLayer.Remove(existingBodyRegionId); + // } + // previousLayerNumber = existingLayerNumber; + + // } + + // else + // { + + // } + + // } + // if(isLayerMatched) + // layersToProcess.Add(previousLayerNumber, bodyRegionForThisLayer); + // else + // layersToProcess.Add(i, bodyRegionForThisLayer); + // } + // Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "after preparing list to process"); + + // makeFileNameAndProcess(layersToProcess,Convert.ToInt32(bodyviewId)); + //} + + + //internal void makeFileNameAndProcess(Dictionary> filesToProcess, int BodyViewIndex) + //{ + // Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in makeFileNameAndProcess"); + + // foreach (KeyValuePair> pair in filesToProcess) + // { + + + // foreach (int item in pair.Value) + // { + // string fileName = "BodyView" + BodyViewIndex + "_layer" + pair.Key + "_bodyRegionId" + item+".json"; + + // //string filePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\AIAHighlight\AIAHighlight\data\" + fileName; + // string filePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\" + fileName; + // getDataFromFileAndSaveInDB(filePath, BodyViewIndex); + // } + + // } + //} + + //internal void processFiles(string[] files,int bodyviewId) + //{ + + // //without multithreading + // foreach(string filename in files) + // { + // Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in processFiles for filename"); + // getDataFromFileAndSaveInDB(filename, bodyviewId); + // } + + //} + + + + //internal void getDataFromFileAndSaveInDB(string filePath,int bodyViewId) + //{ + // if (File.Exists(filePath)) + // { + // string fileData; + // using (StreamReader reader = new StreamReader(filePath)) + // { + // fileData = reader.ReadToEnd(); + + // JObject data = JObject.Parse(fileData); + // this.saveImageData(data, bodyViewId); + // } + + + // } + // else + // { + // Console.WriteLine("FILE.NOT.FOUND AT" + + // ": " + filePath); + + // } + //} + } +} diff --git a/600-POC/fileSave/DBSAVe/Program_NOMultiThreading.cs b/600-POC/fileSave/DBSAVe/Program_NOMultiThreading.cs new file mode 100644 index 0000000..8d6e2a1 --- /dev/null +++ b/600-POC/fileSave/DBSAVe/Program_NOMultiThreading.cs @@ -0,0 +1,299 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +//using log4net; +using MongoDB.Driver; +using System.IO; +using Newtonsoft.Json.Linq; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace AIAHighlight.DBSave +{ + [BsonIgnoreExtraElements] + public class BodyRegionImage + { + public ObjectId _id { get; set; } + public string imageName; + public int bodyViewIndex; + public int layerNumber; + public int bodyRegionId; + public int zoom; + public List terms; + + public BodyRegionImage() + { + this.terms = new List(); + } + } + + public class Term + { + public int TermNumber; + public List SystemNumbers; + public List PixelLocations; + public int ActualTermNumber; + + public Term(int termNumber, int systemNumber, int pixelLocation) + { + this.SystemNumbers = new List(); + this.PixelLocations = new List(); + this.TermNumber = termNumber; + this.SystemNumbers.Add(systemNumber); + this.PixelLocations.Add(pixelLocation); + } + } + + + + + public class Program + { + //public JObject data { get; set; } + public Dictionary termList; + + internal void saveImageData(JObject data) + { + // DateTime datetime = new DateTime(); + Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+ + data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]); + try + { + BodyRegionImage bodyRegionImage = new BodyRegionImage(); + // var client = new MongoClient("mongodb://192.168.90.43:27017"); + var client = new MongoClient("mongodb://192.168.90.43:27017"); + + var db = client.GetDatabase("aia"); + + var col = db.GetCollection("DAImages"); + + // var list = col.Find(Builders.Filter.Eq("imageName", image["imageName"].ToString())).ToList(); + JObject imageData = data; + + + var projection = Builders.Projection.Include("_id"); + + FilterDefinition[] filterCondition = { Builders.Filter.Eq("imageName", data["imageName"].ToString()), + Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(data["bodyViewIndex"])), + Builders.Filter.Eq("layerNumber", Convert.ToInt32(data["layerNumber"])), + Builders.Filter.Eq("bodyRegionId", Convert.ToInt32(data["bodyRegion"])), + Builders.Filter.Eq("zoom", Convert.ToInt32(data["zoom"]))}; + var list = col.Find(Builders.Filter.And(filterCondition)).Project(projection).ToList(); + + //FilterDefinition[] filterCondition = { Builders.Filter.Eq("imageName", data["imageName"].ToString()), + // Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(data["bodyViewIndex"])), + // Builders.Filter.Eq("layerNumber", Convert.ToInt32(data["layerNumber"])), + // Builders.Filter.Eq("bodyRegionId", Convert.ToInt32(data["bodyRegion"])), + // Builders.Filter.Eq("zoom", Convert.ToInt32(data["zoom"]))}; + //var list = col.Find(Builders.Filter.And(filterCondition)).ToList(); + + if (list.Count > 0) + return; + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..record not found."); + + string bodyViewIndex = data["bodyViewIndex"].ToString(); + // string termFilePath = "D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\dist\\assets\\data\\json\\da\\body-views\\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + // string termFilePath = " D:\\ADAM\\100-Projects\\001-AIAHighlight.FileSave\\AIAHighlight\\dist\\assets\\data\\json\\da\\body-views\\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + string path = @"\\ADAMSRVR\dist\assets\data\json\da\body-views\" + bodyViewIndex+"\\"; + string fileName = "da_dat_body_system_term_" + bodyViewIndex + ".json"; + string termFilePath = " D:\\ADAM\\100-Projects\\001-AIAHighlight.FileSave\\AIAHighlight\\dist\\assets\\data\\json\\da\\body-views\\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + string systemTermMapping; + // using (StreamReader reader = new StreamReader(termFilePath)) + + using (StreamReader reader = new StreamReader(path+fileName)) + { + systemTermMapping = reader.ReadToEnd(); + } + + JObject systemTermMappingObject = JObject.Parse(systemTermMapping); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject craeted."); + + dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; + + dynamic imageDataArray = data["data"]; + + Dictionary termList = new Dictionary(); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray"); + + for (int i = 0; i < imageDataArray.Count; i += 4) + { + var RED = Convert.ToInt32(imageDataArray[i]) - 9; + var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9; + var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9; + + if (RED == -9 && GREEN == -9 && BLUE == -9) + continue; + + var Red = ""; + var Green = ""; + var Blue = ""; + string zero = "0"; + + if ((RED).ToString().Length != 2) + { + Red = zero + RED.ToString(); + } + else + { + Red = RED.ToString(); + } + if ((GREEN).ToString().Length != 2) + { + Green = zero + GREEN.ToString(); + } + else + { + Green = GREEN.ToString(); + } + if ((BLUE).ToString().Length != 2) + { + Blue = zero + BLUE.ToString(); + } + else + { + Blue = BLUE.ToString(); + } + + string Icolor = (Red + Green + Blue); + // alert('Icolor: ' + Icolor) + + foreach (dynamic systemTerm in systemTerms) + { + int iColor = Convert.ToInt32(Icolor); + int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString()); + int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString()); + + if (systemTerm._TermNumber.ToString() == Icolor) + { + this.AddToTermList(termNumber, systemNumber, i, termList); + //new Program().AddToTermList(termNumber, systemNumber, i,termList); + //new Program().AddToTermList(termNumber, systemNumber, i + 1, termList); + //new Program().AddToTermList(termNumber, systemNumber, i + 2, termList); + this.AddToTermList(termNumber, systemNumber, i, termList); + this.AddToTermList(termNumber, systemNumber, i + 1, termList); + this.AddToTermList(termNumber, systemNumber, i + 2, termList); + } + } + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList."); + + bodyRegionImage.imageName = data["imageName"].ToString(); + bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]); + + foreach (int key in termList.Keys) + { + bodyRegionImage.terms.Add(termList[key]); + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + col.InsertOne(bodyRegionImage); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + data = null; + termList = null; + systemTermMappingObject = null; + systemTerms = null; + } + catch(Exception ex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace); + + } + + + } + + public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary termList) + { + if (!termList.ContainsKey(termNumber)) + { + termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation)); + } + else + { + bool isSystemNumberExisting = false; + foreach (int sn in termList[termNumber].SystemNumbers) + { + if (systemNumber == sn) + { + isSystemNumberExisting = true; + break; + } + + } + if (!isSystemNumberExisting) + termList[termNumber].SystemNumbers.Add(systemNumber); + + bool isPixelLocationExisting = false; + foreach (int pl in termList[termNumber].PixelLocations) + { + if (pixelLocation == pl) + { + isPixelLocationExisting = true; + break; + } + + } + if (!isPixelLocationExisting) + termList[termNumber].PixelLocations.Add(pixelLocation); + + } + } + //static void saveImageData() + //{ + + //} + + static void Main(string[] args) + { + Console.WriteLine("Enter BodyViewIndex"); + string bodyViewId = Console.ReadLine(); + string searchPattern = "BodyView" + bodyViewId+"_layer*"; + // string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data"); + // string[] files = Directory.GetFiles("D:\\ADAM\\100-Projects\\001-AIAHighlight.FileSave\\AIAHighlight.Serevr\\AIAHighlight\\data", "*.json"); + // string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data",searchPattern); + + string[] files = Directory.GetFiles(@"\\ADAMSRVR\data", searchPattern); + Program program; + foreach (string filePath in files) + { + program = new Program(); + program.getDataFromFileAndSaveInDB(filePath); + program = null; + } + + } + + internal void getDataFromFileAndSaveInDB(string filePath) + { + if (File.Exists(filePath)) + { + string fileData; + using (StreamReader reader = new StreamReader(filePath)) + { + fileData = reader.ReadToEnd(); + + JObject data = JObject.Parse(fileData); + this.saveImageData(data); + } + + + } + } + } +} diff --git a/600-POC/fileSave/DBSAVe/Program_UserInput_DifferentDB.cs b/600-POC/fileSave/DBSAVe/Program_UserInput_DifferentDB.cs new file mode 100644 index 0000000..97ddc19 --- /dev/null +++ b/600-POC/fileSave/DBSAVe/Program_UserInput_DifferentDB.cs @@ -0,0 +1,438 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +//using log4net; +using MongoDB.Driver; +using System.IO; +using Newtonsoft.Json.Linq; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace AIAHighlight.DBSave +{ + [BsonIgnoreExtraElements] + public class BodyRegionImage + { + public ObjectId _id { get; set; } + public string imageName; + public int bodyViewIndex; + public int layerNumber; + public int bodyRegionId; + public int zoom; + public List terms; + + public BodyRegionImage() + { + this.terms = new List(); + } + } + + public class Term + { + public int TermNumber; + public List SystemNumbers; + public List PixelLocations; + public int ActualTermNumber; + + public Term(int termNumber, int systemNumber, int pixelLocation) + { + this.SystemNumbers = new List(); + this.PixelLocations = new List(); + this.TermNumber = termNumber; + this.SystemNumbers.Add(systemNumber); + this.PixelLocations.Add(pixelLocation); + } + } + + + + + public class Program + { + //public JObject data { get; set; } + // public Dictionary termList; + + internal void saveImageData(JObject data, int bodyviewId) + { + // DateTime datetime = new DateTime(); + Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+ + data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]); + try + { + BodyRegionImage bodyRegionImage = new BodyRegionImage(); + // var client = new MongoClient("mongodb://192.168.90.43:27017"); + var client = new MongoClient(); + + var db = client.GetDatabase("AIADB_bodyviewId"+bodyviewId.ToString()); + + var col = db.GetCollection("DAImages"); + + // var list = col.Find(Builders.Filter.Eq("imageName", image["imageName"].ToString())).ToList(); + JObject imageData = data; + + + //var projection = Builders.Projection.Include("_id"); + + //FilterDefinition[] filterCondition = { Builders.Filter.Eq("imageName", data["imageName"].ToString()), + // Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(data["bodyViewIndex"])), + // Builders.Filter.Eq("layerNumber", Convert.ToInt32(data["layerNumber"])), + // Builders.Filter.Eq("bodyRegionId", Convert.ToInt32(data["bodyRegion"])), + // Builders.Filter.Eq("zoom", Convert.ToInt32(data["zoom"]))}; + //var list = col.Find(Builders.Filter.And(filterCondition)).Project(projection).ToList(); + + + //if (list.Count > 0) + // return; + + //Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..record not found."); + + string bodyViewIndex = data["bodyViewIndex"].ToString(); + + //string termFilePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\bodyViewsData\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + string termFilePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\dist\assets\data\json\da\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + //string path = @"\\ADAMSRVR\dist\assets\data\json\da\body-views\" + bodyViewIndex+"\\"; + //string fileName = "da_dat_body_system_term_" + bodyViewIndex + ".json"; + + string systemTermMapping; + using (StreamReader reader = new StreamReader(termFilePath)) + + // using (StreamReader reader = new StreamReader(path+fileName)) + { + systemTermMapping = reader.ReadToEnd(); + } + + JObject systemTermMappingObject = JObject.Parse(systemTermMapping); + + //Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject created."); + + dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; + + dynamic imageDataArray = data["data"]; + //int[] imageDataIntArray = (int[])imageDataArray; + //int numToRemove = 0; + //dynamic imageDataWithoutZero = imageDataIntArray.Where(val => val != numToRemove).ToArray(); + + Dictionary termList = new Dictionary(); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + for (int i = 0; i < imageDataArray.Count; i += 4) + { + var RED = Convert.ToInt32(imageDataArray[i]) - 9; + var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9; + var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9; + + if (RED == -9 && GREEN == -9 && BLUE == -9) + continue; + + var Red = ""; + var Green = ""; + var Blue = ""; + string zero = "0"; + + if ((RED).ToString().Length != 2) + { + Red = zero + RED.ToString(); + } + else + { + Red = RED.ToString(); + } + if ((GREEN).ToString().Length != 2) + { + Green = zero + GREEN.ToString(); + } + else + { + Green = GREEN.ToString(); + } + if ((BLUE).ToString().Length != 2) + { + Blue = zero + BLUE.ToString(); + } + else + { + Blue = BLUE.ToString(); + } + + string Icolor = (Red + Green + Blue); + // alert('Icolor: ' + Icolor) + if(Icolor=="000000") + foreach (dynamic systemTerm in systemTerms) + { + int iColor = Convert.ToInt32(Icolor); + int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString()); + int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString()); + + if (systemTerm._TermNumber.ToString() == Icolor) + { + this.AddToTermList(termNumber, systemNumber, i, termList); + this.AddToTermList(termNumber, systemNumber, i, termList); + this.AddToTermList(termNumber, systemNumber, i + 1, termList); + this.AddToTermList(termNumber, systemNumber, i + 2, termList); + break; + } + } + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList.. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + bodyRegionImage.imageName = data["imageName"].ToString(); + bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]); + + foreach (int key in termList.Keys) + { + bodyRegionImage.terms.Add(termList[key]); + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + col.InsertOne(bodyRegionImage); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + var logCol = db.GetCollection("DAImageLogs"); + + BodyRegionImage bodyRegionImageTolog = new BodyRegionImage(); + bodyRegionImageTolog.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImageTolog.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImageTolog.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImageTolog.zoom = Convert.ToInt32(data["zoom"]); + + logCol.InsertOne(bodyRegionImageTolog); + + bodyRegionImageTolog = null; + + data = null; + termList = null; + systemTermMappingObject = null; + systemTerms = null; + } + catch(Exception ex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace); + + } + + + } + + public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary termList) + { + if (!termList.ContainsKey(termNumber)) + { + termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation)); + } + else + { + bool isSystemNumberExisting = false; + foreach (int sn in termList[termNumber].SystemNumbers) + { + if (systemNumber == sn) + { + isSystemNumberExisting = true; + break; + } + + } + if (!isSystemNumberExisting) + termList[termNumber].SystemNumbers.Add(systemNumber); + + bool isPixelLocationExisting = false; + foreach (int pl in termList[termNumber].PixelLocations) + { + if (pixelLocation == pl) + { + isPixelLocationExisting = true; + break; + } + + } + if (!isPixelLocationExisting) + termList[termNumber].PixelLocations.Add(pixelLocation); + + } + } + + + //static void Main(string[] args) + //{ + // Console.WriteLine("Enter BodyViewIndex"); + // string bodyViewId = Console.ReadLine(); + // string searchPattern = "BodyView" + bodyViewId+"_layer*"; + + // string[] files = Directory.GetFiles(@"\\ADAMSRVR\data", searchPattern); + // Program program; + // foreach (string filePath in files) + // { + // program = new Program(); + // program.getDataFromFileAndSaveInDB(filePath); + // program = null; + // } + + //} + + static void Main(string[] args) + { + + + //string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data", "*.json"); + + //string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data", "*.json"); + Console.WriteLine("Enter BodyViewId"); + int BodyViewIndex = Convert.ToInt32(Console.ReadLine()); + + Console.WriteLine("Enter LayerNumber"); + string layerNumber = Console.ReadLine(); + int totalLayers = 0; + + switch (BodyViewIndex) + { + case 1: + totalLayers = 330; + break; + case 2: + totalLayers = 288; + break; + case 3: + totalLayers = 127; + break; + case 4: + totalLayers = 186; + break; + case 5: + totalLayers = 229; + break; + case 6: + totalLayers = 289; + break; + case 7: + totalLayers = 130; + break; + case 8: + totalLayers = 186; + break; + case 9: + totalLayers = 64; + break; + case 10: + totalLayers = 72; + break; + case 11: + totalLayers = 64; + break; + case 12: + totalLayers = 72; + break; + } + + for (int i = Convert.ToInt32(layerNumber); i <= totalLayers; i++) + { + string searchPattern = "BodyView" + BodyViewIndex + "_layer" + i + "*"; + // string searchPattern = "BodyView" + bodyViewId + "_layer*"; + + string[] files = Directory.GetFiles(@"D:\100-PROJECTS\008-AIAHighlight.FileSave\data", searchPattern); + // string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\", searchPattern); + Program program = new Program(); + program.processFiles(files, BodyViewIndex); + program = null; + } + } + + internal void processFiles(string[] files,int bodyviewId) + { + //with multithreading + // prepareArrayOfFiles(files, startIndex, endIndex); + + //without multithreading + foreach(string filename in files) + { + getDataFromFileAndSaveInDB(filename, bodyviewId); + } + + } + + //internal void prepareArrayOfFiles(string[] files,int startIndex, int endIndex) + //{ + // string[] filesToProcess = new string[20]; + // int processedFiles = 1; + // int j = 0; + // for (int i = startIndex; i <= endIndex; i++) + // { + + // filesToProcess[j] = files[i]; + // j++; + + // } + + // Parallel.ForEach(filesToProcess, (currentFile) => + // { + // if (currentFile != null) + // { + // using (StreamReader reader = new StreamReader(currentFile)) + // { + // getDataFromFileAndSaveInDB(currentFile); + // processedFiles++; + + // } + + // if (processedFiles == 20) + // { + // startIndex = endIndex + 1; + // endIndex = startIndex + 19; + // filesToProcess = null; + + // //in case after adding 20, the endIndex reaches more than length of files array(total files) + // if (endIndex > files.Length - 1) + // { + // // int remainingFiles = endIndex - files.Length; + // endIndex = files.Length - 1; + // } + // if (endIndex < files.Length) + // { + // processFiles(files, startIndex, endIndex); + // } + // //} + // } + // } + // } + // ); + + //} + + + internal void getDataFromFileAndSaveInDB(string filePath,int bodyViewId) + { + if (File.Exists(filePath)) + { + string fileData; + using (StreamReader reader = new StreamReader(filePath)) + { + fileData = reader.ReadToEnd(); + + JObject data = JObject.Parse(fileData); + this.saveImageData(data, bodyViewId); + } + + + } + else + { + Console.WriteLine("FILE.NOT.FOUND AT" + + ": " + filePath); + + } + } + } +} diff --git a/600-POC/fileSave/DBSAVe/Program_WithoutMultithreading_withDBSearchBeforeExeutiom.cs b/600-POC/fileSave/DBSAVe/Program_WithoutMultithreading_withDBSearchBeforeExeutiom.cs new file mode 100644 index 0000000..93f96b3 --- /dev/null +++ b/600-POC/fileSave/DBSAVe/Program_WithoutMultithreading_withDBSearchBeforeExeutiom.cs @@ -0,0 +1,458 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +//using log4net; +using MongoDB.Driver; +using System.IO; +using Newtonsoft.Json.Linq; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace AIAHighlight.DBSave +{ + [BsonIgnoreExtraElements] + public class BodyRegionImage + { + public ObjectId _id { get; set; } + public string imageName; + public int bodyViewIndex; + public int layerNumber; + public int bodyRegionId; + public int zoom; + public List terms; + + public BodyRegionImage() + { + this.terms = new List(); + } + } + + public class Term + { + public int TermNumber; + public List SystemNumbers; + public List PixelLocations; + public int ActualTermNumber; + + public Term(int termNumber, int systemNumber, int pixelLocation) + { + this.SystemNumbers = new List(); + this.PixelLocations = new List(); + this.TermNumber = termNumber; + this.SystemNumbers.Add(systemNumber); + this.PixelLocations.Add(pixelLocation); + } + } + + + + + public class Program + { + //public JObject data { get; set; } + // public Dictionary termList; + + internal void saveImageData(JObject data, int bodyviewId) + { + // DateTime datetime = new DateTime(); + Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+ + data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]); + try + { + BodyRegionImage bodyRegionImage = new BodyRegionImage(); + + var client = new MongoClient(); + + var db = client.GetDatabase("AIADB_bodyviewId"+bodyviewId.ToString()); + + var col = db.GetCollection("DAImages"); + + JObject imageData = data; + + + + string bodyViewIndex = data["bodyViewIndex"].ToString(); + + string termFilePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\bodyViewsData\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + //string termFilePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\dist\assets\data\json\da\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; + + string systemTermMapping; + using (StreamReader reader = new StreamReader(termFilePath)) + + // using (StreamReader reader = new StreamReader(path+fileName)) + { + systemTermMapping = reader.ReadToEnd(); + } + + JObject systemTermMappingObject = JObject.Parse(systemTermMapping); + + //Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject created."); + + dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; + + JObject sysT = JObject.Parse(systemTerms); + + dynamic imageDataArray = data["data"]; + + Dictionary termList = new Dictionary(); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + for (int i = 0; i < imageDataArray.Count; i += 4) + { + if(imageDataArray[i] == 0 && imageDataArray[i+1] == 0 && imageDataArray[i+2] == 0) + { + continue; + } + var RED = Convert.ToInt32(imageDataArray[i]) - 9; + var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9; + var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9; + + if (RED == -9 && GREEN == -9 && BLUE == -9) + continue; + + var Red = ""; + var Green = ""; + var Blue = ""; + string zero = "0"; + + if ((RED).ToString().Length != 2) + { + Red = zero + RED.ToString(); + } + else + { + Red = RED.ToString(); + } + if ((GREEN).ToString().Length != 2) + { + Green = zero + GREEN.ToString(); + } + else + { + Green = GREEN.ToString(); + } + if ((BLUE).ToString().Length != 2) + { + Blue = zero + BLUE.ToString(); + } + else + { + Blue = BLUE.ToString(); + } + + string Icolor = (Red + Green + Blue); + + + + foreach (dynamic systemTerm in systemTerms) + { + + int iColor = Convert.ToInt32(Icolor); + int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString()); + int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString()); + + if (systemTerm._TermNumber.ToString() == Icolor) + { + this.AddToTermList(termNumber, systemNumber, i, termList); + //this.AddToTermList(termNumber, systemNumber, i, termList); + this.AddToTermList(termNumber, systemNumber, i + 1, termList); + this.AddToTermList(termNumber, systemNumber, i + 2, termList); + break; + } + } + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList.. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + bodyRegionImage.imageName = data["imageName"].ToString(); + bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]); + + foreach (int key in termList.Keys) + { + bodyRegionImage.terms.Add(termList[key]); + } + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + col.InsertOne(bodyRegionImage); + + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); + + var logCol = db.GetCollection("DAImageLogs"); + + BodyRegionImage bodyRegionImageTolog = new BodyRegionImage(); + bodyRegionImageTolog.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); + bodyRegionImageTolog.layerNumber = Convert.ToInt32(data["layerNumber"]); + bodyRegionImageTolog.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); + bodyRegionImageTolog.zoom = Convert.ToInt32(data["zoom"]); + + logCol.InsertOne(bodyRegionImageTolog); + + bodyRegionImageTolog = null; + + data = null; + termList = null; + systemTermMappingObject = null; + systemTerms = null; + } + catch(Exception ex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace); + + } + + + } + + public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary termList) + { + if (!termList.ContainsKey(termNumber)) + { + termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation)); + } + else + { + bool isSystemNumberExisting = false; + foreach (int sn in termList[termNumber].SystemNumbers) + { + if (systemNumber == sn) + { + isSystemNumberExisting = true; + break; + } + + } + if (!isSystemNumberExisting) + termList[termNumber].SystemNumbers.Add(systemNumber); + + bool isPixelLocationExisting = false; + foreach (int pl in termList[termNumber].PixelLocations) + { + if (pixelLocation == pl) + { + isPixelLocationExisting = true; + break; + } + + } + if (!isPixelLocationExisting) + termList[termNumber].PixelLocations.Add(pixelLocation); + + } + } + + + + + static void Main(string[] args) + { + + Console.WriteLine("Enter BodyViewId"); + int BodyViewIndex = Convert.ToInt32(Console.ReadLine()); + + Program program = new Program(); + program.getAlreadyInsertedLayer(BodyViewIndex.ToString()); + program = null; + + } + + internal void getAlreadyInsertedLayer(string bodyviewId) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "inside getAlreadyInsertedLayer"); + int totalLayers = 0; + int [] totalBodyReigons =null; + List bodyRegions = new List(); + + switch (Convert.ToInt32(bodyviewId)) + { + case 1: + totalLayers = 330; + totalBodyReigons = new int[6] {1,2,3,4,5,6}; + bodyRegions.Add(1); + bodyRegions.Add(2); + bodyRegions.Add(3); + bodyRegions.Add(4); + bodyRegions.Add(5); + bodyRegions.Add(6); + break; + case 2: + totalLayers = 288; + // totalBodyReigons = 6; + break; + case 3: + totalLayers = 127; + // totalBodyReigons = 6; + break; + case 4: + totalLayers = 186; + // totalBodyReigons = 6; + break; + case 5: + totalLayers = 229; + // totalBodyReigons = 6; + break; + case 6: + totalLayers = 289; + // totalBodyReigons = 6; + break; + case 7: + totalLayers = 130; + // totalBodyReigons = 6; + break; + case 8: + totalLayers = 186; + // totalBodyReigons = 6; + break; + case 9: + totalLayers = 64; + // totalBodyReigons = 6; + break; + case 10: + totalLayers = 72; + // totalBodyReigons = 6; + break; + case 11: + totalLayers = 64; + // totalBodyReigons = 6; + break; + case 12: + totalLayers = 72; + // totalBodyReigons = 6; + break; + } + + var client = new MongoClient(); + var db = client.GetDatabase("AIADB_bodyviewId" + bodyviewId.ToString()); + //var db = client.GetDatabase("AIAImageDB"); + + var col = db.GetCollection("DAImagesLogs"); + //var col = db.GetCollection("DAImages"); + + + FilterDefinition[] filterCondition = { + Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(bodyviewId))}; + + dynamic list = col.Find(Builders.Filter.And(filterCondition)).Project(Builders.Projection.Include("layerNumber").Include("bodyRegionId")).ToList(); + //collection.Find(new BsonDocument()).Project(Builders.Projection.Include("Price").Exclude("_id")).ToListAsync(); + // return list; + Dictionary> layersToProcess = new Dictionary>(); + + for (int i = 1; i <= totalLayers; i++) + { + List bodyRegionForThisLayer = new List(); + bodyRegionForThisLayer.Add(1); + bodyRegionForThisLayer.Add(2); + bodyRegionForThisLayer.Add(3); + bodyRegionForThisLayer.Add(4); + bodyRegionForThisLayer.Add(5); + bodyRegionForThisLayer.Add(6); + + int previousLayerNumber = 0; + int existingLayerNumber = 0; + bool isLayerMatched = false; + + foreach (dynamic lNumber in list) + { + existingLayerNumber = Convert.ToInt32((lNumber.GetValue("layerNumber")).ToString()); + + if (existingLayerNumber == i) + { + isLayerMatched = true; + int existingBodyRegionId = Convert.ToInt32((lNumber.GetValue("bodyRegionId")).ToString()); + + + if (bodyRegionForThisLayer.Contains(existingBodyRegionId)) { + //remove from bodyRegions list as this is already present + bodyRegionForThisLayer.Remove(existingBodyRegionId); + } + previousLayerNumber = existingLayerNumber; + + } + + else + { + + } + + } + if(isLayerMatched) + layersToProcess.Add(previousLayerNumber, bodyRegionForThisLayer); + else + layersToProcess.Add(i, bodyRegionForThisLayer); + } + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "after preparing list to process"); + + makeFileNameAndProcess(layersToProcess,Convert.ToInt32(bodyviewId)); + } + + + internal void makeFileNameAndProcess(Dictionary> filesToProcess, int BodyViewIndex) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in makeFileNameAndProcess"); + + foreach (KeyValuePair> pair in filesToProcess) + { + + + foreach (int item in pair.Value) + { + string fileName = "BodyView" + BodyViewIndex + "_layer" + pair.Key + "_bodyRegionId" + item+".json"; + + //string filePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\AIAHighlight\AIAHighlight\data\" + fileName; + string filePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\" + fileName; + getDataFromFileAndSaveInDB(filePath, BodyViewIndex); + } + + } + } + + internal void processFiles(string[] files,int bodyviewId) + { + + //without multithreading + foreach(string filename in files) + { + Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in processFiles for filename"); + getDataFromFileAndSaveInDB(filename, bodyviewId); + } + + } + + + + internal void getDataFromFileAndSaveInDB(string filePath,int bodyViewId) + { + if (File.Exists(filePath)) + { + string fileData; + using (StreamReader reader = new StreamReader(filePath)) + { + fileData = reader.ReadToEnd(); + + JObject data = JObject.Parse(fileData); + this.saveImageData(data, bodyViewId); + } + + + } + else + { + Console.WriteLine("FILE.NOT.FOUND AT" + + ": " + filePath); + + } + } + } +} diff --git a/600-POC/fileSave/jsonFile_generate/AJs/app.component.css b/600-POC/fileSave/jsonFile_generate/AJs/app.component.css new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/AJs/app.component.css diff --git a/600-POC/fileSave/jsonFile_generate/AJs/app.component.html b/600-POC/fileSave/jsonFile_generate/AJs/app.component.html new file mode 100644 index 0000000..0739427 --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/AJs/app.component.html @@ -0,0 +1,5 @@ +
+ bodyViewId:
{{bodyViewId}}
+ layerNo: + +
\ No newline at end of file diff --git a/600-POC/fileSave/jsonFile_generate/AJs/app.component.spec.ts b/600-POC/fileSave/jsonFile_generate/AJs/app.component.spec.ts new file mode 100644 index 0000000..c740bcd --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/AJs/app.component.spec.ts @@ -0,0 +1,32 @@ +import { TestBed, async } from '@angular/core/testing'; + +import { AppComponent } from './app.component'; + +describe('AppComponent', () => { + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ + AppComponent + ], + }).compileComponents(); + })); + + it('should create the app', async(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + expect(app).toBeTruthy(); + })); + + it(`should have as title 'app works!'`, async(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + expect(app.title).toEqual('app works!'); + })); + + it('should render title in a h1 tag', async(() => { + const fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + const compiled = fixture.debugElement.nativeElement; + expect(compiled.querySelector('h1').textContent).toContain('app works!'); + })); +}); diff --git a/600-POC/fileSave/jsonFile_generate/AJs/app.component.ts b/600-POC/fileSave/jsonFile_generate/AJs/app.component.ts new file mode 100644 index 0000000..d66c7e9 --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/AJs/app.component.ts @@ -0,0 +1,367 @@ + +import { Component, AfterViewInit, ViewChild } from '@angular/core'; +import { ImageService } from './services/image.service'; + +declare var jQuery: any; +declare var jinqJs: any; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'], + providers: [ImageService] +}) +export class AppComponent implements AfterViewInit { + title = 'app works!'; + context: CanvasRenderingContext2D; + bodyViewData: any; + imageData: any; + imageDataCollection: Array; + imageCount: number; + isLayerProcessCompleted: boolean; + totalBodyViews: number; + bodyViewIndex: number; + currentLayer: number; + datalayerLength: number; + bodyRegionInLayer: Array;; + BRCounter: number; + layerNo: string; + bodyViewId: string; + remainingFiles75: any; + remainingFiles25: any; + bodyRegionId: number; + previousBodyViewId: number; + counter: number; + constructor(private imageService: ImageService) { + this.imageData = new Object(); + this.imageDataCollection = new Array(); + this.isLayerProcessCompleted = true; + this.totalBodyViews = 12; + this.bodyViewIndex = 2; + this.currentLayer = 95; + this.previousBodyViewId = 0; + this.counter = 0; + } + + ngAfterViewInit(): void { + // this.getBodyViewJson(); + //for (let i = 1; i <= 12; i++) { + // this.imageService.getBodyViewData(i).subscribe((data) => { + // this.bodyViewData = data.json(); + // this.saveBodyViewImageData(); + // }); + //} + } + + + getBodyViewJson() { + console.log("inside getBodyViewJson for this.bodyViewIndex= " + this.bodyViewIndex); + this.previousBodyViewId = this.bodyViewIndex; + this.imageService.getBodyViewData(this.bodyViewIndex).subscribe((data) => { + console.log("Fetched body view json for bodyViewIndex: " + this.bodyViewIndex); + this.bodyViewData = data.json(); + this.getImageName(); + }); + } + + getImageName(): void { + + //var layerData = this.bodyViewData.filter(lNo => lNo._LayerNumber == this.layerNo); + //var bodyRegiondata = layerData.filter(br => br._BodyRegionId == this.bodyRegionId); + + //var layerData = this.bodyViewData.find(findData); + //var bodyRegiondata = layerData.filter(findData);\ + console.log("getImageName:" + this.currentLayer); + var layerNumber = this.currentLayer + 1; + console.log('layerNumber= ' + layerNumber); + var layerData = new jinqJs() + .from(this.bodyViewData.Layers.DataLayer) + .where('_LayerNumber == ' + layerNumber) + .select(); + alert("layerData= " + layerData[0]); + + + var bodyRegiondata = new jinqJs() + .from(layerData[0].BodyRegion) + .where('_BodyRegionId == ' + this.bodyRegionId) + .select(); + + alert("bodyRegiondata= " + bodyRegiondata); + + const imgName = bodyRegiondata[0].Image._ImageName.split('.')[0] + '_mci.png'; + const skintone = bodyRegiondata[0].Image._SkintTone; + const bodyRegionId = bodyRegiondata[0]._BodyRegionId; + + console.log("inside getImageName. imgName= " + imgName + ",skintone= " + skintone); + + this.drawImage(imgName, this.bodyViewIndex, this.currentLayer, bodyRegionId, 75, skintone); + + } + + findData(layer):any { + return layer._LayerNumber === this.layerNo; + } + saveBodyViewImageData(): void { + this.datalayerLength = this.bodyViewData.Layers.DataLayer.length; + console.log('this.datalayerLength= ' + this.datalayerLength); + console.log('this.bodyViewData.Layers.DataLayer = ' + this.bodyViewData.Layers.DataLayer); + this.processLayer(); + //for (let i = 0; i <= datalayerLength-1; i++) { + // const bodyRegions = this.bodyViewData.Layers.DataLayer[datalayerLength - i - 1].BodyRegion; + // let k = 1; + // this.imageCount = bodyRegions.length; + // for (let j = bodyRegions.length - 1; j >= 0; j--) { + // const imgName = bodyRegions[j].Image._ImageName.split('.')[0] + '_mci.png'; + // this.drawImage(imgName, 1, i, k, 75); + + // if (j === 0) { + // k = 1; + // } else { + // k = k + 1; + // } + + // } + //} + } + + processLayer(): void { + console.log("Processing started for layer: " + this.currentLayer + ' of bodyViewIndex: ' + this.bodyViewIndex + ', time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1")); + this.imageDataCollection = null; + this.imageDataCollection = new Array(); + this.BRCounter = 1; + this.bodyRegionInLayer = new Array(); + console.log('inside processLayer. this.bodyViewData.Layers.DataLayer = ' + this.bodyViewData.Layers.DataLayer); + //console.log('this.bodyViewData.Layers.DataLayer[63].BodyRegion = ' + this.bodyViewData.Layers.DataLayer[63].BodyRegion); + //console.log('this.datalayerLength - this.currentLayer - 1= ' + (this.datalayerLength - this.currentLayer - 1)); + const bodyRegions = this.bodyViewData.Layers.DataLayer[this.datalayerLength - this.currentLayer - 1].BodyRegion; + //console.log('bodyRegions= ' + bodyRegions + ' this.bodyViewData.Layers.DataLayer[this.datalayerLength - this.currentLayer - 1].BodyRegion.length= ' + (this.bodyViewData.Layers.DataLayer[this.datalayerLength - this.currentLayer - 1].BodyRegion).length); + //console.log("Process layer: " + this.currentLayer + ", bodyRegions length= " + bodyRegions.length); + //console.log('abc= ' + bodyRegions._BodyRegionId + ', xyze= ' + bodyRegions.Image._ImageName); + //let k = 1; + if (this.bodyViewIndex == 9 || this.bodyViewIndex == 10 || this.bodyViewIndex == 11 || this.bodyViewIndex == 12) { + + this.imageCount = 1; + const imgName = bodyRegions.Image._ImageName.split('.')[0] + '_mci.png'; + const skintone = bodyRegions.Image._SkintTone; + const bodyRegionId = bodyRegions._BodyRegionId; + this.drawImage(imgName, this.bodyViewIndex, this.currentLayer, bodyRegionId, 25, skintone); + + } + else{ + this.imageCount = bodyRegions.length; + for (let j = bodyRegions.length - 1; j >= 0; j--) { + const imgName = bodyRegions[j].Image._ImageName.split('.')[0] + '_mci.png'; + const skintone = bodyRegions[j].Image._SkintTone; + const bodyRegionId = bodyRegions[j]._BodyRegionId; + //for missing BRs + // if (bodyRegionId == 2 || bodyRegionId == 3 || bodyRegionId == 6) { + + this.drawImage(imgName, this.bodyViewIndex, this.currentLayer, bodyRegionId, 25, skintone); + // } + + } //if (j === 0) { + // k = 1; + //} else { + // k = k + 1; + //} + + } + } + + drawImage(imageName: string, bodyViewIndex: number, layerNumber: number, bodyRegionIndex: number, zoom: number, skintone: string): void { + console.log('drawImage for bodyViewIndex= ' + bodyViewIndex + ',layerNumber= ' + layerNumber + ',bodyRegionIndex= ' + bodyRegionIndex); + //if (layerNumber == 0) { + // console.log('drawImage.in if block. for bodyViewIndex= ' + bodyViewIndex + ',layerNumber= ' + layerNumber + ',bodyRegionIndex= ' + bodyRegionIndex); + + // const img = new Image(); + // img.src = '../assets/images/da/' + zoom + '/body-views/' + bodyViewIndex + '/layers/' + layerNumber + '/' + bodyRegionIndex + '/' + skintone + '/' + imageName; + // img.onload = (() => { + // this.imageData.imageName = imageName; + // this.imageData.bodyViewIndex = bodyViewIndex; + // this.imageData.layerNumber = layerNumber; + // this.imageData.bodyRegion = bodyRegionIndex; + // const canvas: HTMLCanvasElement = document.createElement('canvas'); + // canvas.width = this.imageData.width = img.width; + // canvas.height = this.imageData.height = img.height; + // this.context = canvas.getContext('2d'); + // this.context.drawImage(img, 0, 0); + // this.imageData.data = Array.from(this.context.getImageData(0, 0, img.width, img.height).data); + // console.log(this.imageData); + // this.writeImageDataToJson(this.imageData); + // this.imageData = new Object(); + // }); + + + // } + + // else { + console.log('drawImage.in else block. for bodyViewIndex= ' + bodyViewIndex + ',layerNumber= ' + layerNumber + ',bodyRegionIndex= ' + bodyRegionIndex); + + var img = new Image(); + img.id = 'canvasImg'; + + img.src = '../assets/images/da/' + zoom + '/body-views/' + bodyViewIndex + '/layers/' + layerNumber + '/' + bodyRegionIndex + '/' + imageName; + img.onload = (() => { + console.log("onload. img.src= " + img.src); + this.imageData.imageName = imageName; + this.imageData.bodyViewIndex = bodyViewIndex; + this.imageData.layerNumber = layerNumber; + this.imageData.bodyRegion = bodyRegionIndex; + this.imageData.zoom = zoom; + var elem = document.getElementById("imgCanvas"); + if (elem != null) { + elem.parentNode.removeChild(elem); + elem = null; + } + var canvas: HTMLCanvasElement = document.createElement('canvas'); + canvas.id = 'imgCanvas'; + canvas.width = this.imageData.width = img.width; + canvas.height = this.imageData.height = img.height; + this.context = canvas.getContext('2d'); + this.context.drawImage(img, 0, 0); + this.imageData.data = Array.from(this.context.getImageData(0, 0, img.width, img.height).data); + console.log("imageData of of bodyViewIndex: " + this.bodyViewIndex + ", layerNumber: " + layerNumber + ", bodyRegionIndex: " + bodyRegionIndex + ", imgData= " + this.imageData); + this.writeImageDataToJson(this.imageData); + var elem1 = document.getElementById("canvasImg"); + if (elem1 != null) { + elem1.parentNode.removeChild(elem1); + elem1 = null; + } + this.imageData = new Object(); + }); + //} + // canvas.width = canvas.width; // to clear the canvas + + // console.log(img.src); + } + + writeImageDataToJson(imageData: any): void { + this.imageService.updateImageData(imageData).subscribe((resp) => { + console.log('inside subscribe of addToImageDataCollection: result= ' + 'bodyViewIndex:' + resp.bodyViewIndex + ',layerNumber:' + resp.layerNumber + ',bodyRegionId:' + resp.bodyRegionId + ', time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1")); + if (this.counter < this.remainingFiles75.length) { + this.counter++; + this.getDataFromFile(this.remainingFiles75, this.counter); + } + }); + } + + addToImageDataCollection(imageData: any): void { + for (let i = 0; i < this.imageDataCollection.length; i++) { + if (this.imageDataCollection[i].imageName === imageData.imageName) { + return; + } + } + this.imageDataCollection.push(imageData); + console.log("this.imageDataCollection.length= " + this.imageDataCollection.length); + + if (this.imageDataCollection.length === this.imageCount) { + console.log("starting pushing to server for layer: " + this.currentLayer + " of bodyViewIndex: " + this.bodyViewIndex + ', time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1")); + for (let i = 0; i < this.imageDataCollection.length; i++) { + this.imageService.updateImageData(this.imageDataCollection[i]).subscribe((resp) => { + + // alert("resp:" + resp.isSuccess); + // this.BRCounter++; + + this.bodyRegionInLayer.push({ 'bodyViewIndex': resp.bodyViewIndex, 'layerNumber': resp.layerNumber, 'bodyRegionId': resp.bodyRegionId }); + + console.log('inside subscribe of addToImageDataCollection: result= ' + 'bodyViewIndex:' + resp.bodyViewIndex + ',layerNumber:' + resp.layerNumber + ',bodyRegionId:' + resp.bodyRegionId + ', time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1")); + //if (i === this.imageDataCollection.length - 1 && this.BRCounter == this.imageCount) + //{ + + if (this.bodyRegionInLayer.length > 0 && this.bodyRegionInLayer.length == this.imageCount) { + //comparign with this.datalayerLength - 2(-2 due to we have not processed from layer 0) + if ((this.currentLayer <= this.datalayerLength - 2)) { + this.currentLayer++; + this.processLayer(); + } + else { + if (this.bodyViewIndex < this.totalBodyViews) { + this.bodyViewIndex++; + //if (this.bodyViewIndex == 3) { + + //} + //else { + this.currentLayer = 1; + this.getBodyViewJson(); + //} + } + } + } + }); + } + } + } + + generateFile(): void { + + //this.imageService.getRemianingFilesData('../remainingFies/RemainingFiles25'); + + //this.imageService.getBodyViewData(this.bodyViewIndex).subscribe((data) => { + // // console.log("Fetched body view json for bodyViewIndex: " + this.bodyViewIndex); + // this.remainingFiles25 = data; + // //this.saveBodyViewImageData(); + //}); + + + this.imageService.getRemianingFilesData('../assets/remainingFies/RemainingFiles75.json').subscribe((data) => { + console.log("Fetched RemainingFiles75.data= ====" + data); + + this.remainingFiles75 = data; + console.log("this.remainingFiles75.length= " + this.remainingFiles75.length) + + this.getDataFromFile(this.remainingFiles75, this.counter); + //for (let i = 0; i < this.remainingFiles75.length; i++) { + + + //} + }); + } + + getDataFromFile(remainingFiles75,i): void { + + var filename = this.remainingFiles75[i]; + var spiltedFileName = filename.split('_'); + + var bodyViewString = spiltedFileName[0]; + // alert("bodyViewString: " + bodyViewString + "bodyViewString.indexOf('w'):" + bodyViewString.indexOf('w') + ".bodyViewString.Length: " + bodyViewString.length); + + var indexofW = bodyViewString.indexOf('w'); + var bodyViewId = bodyViewString.substring(indexofW + 1); + + + + var layerString = spiltedFileName[1]; + // alert("layerString: " + layerString + "layerString.indexOf('r'):" + layerString.indexOf('r') + ".layerString.Length: " + layerString.length); + + var indexofR = layerString.indexOf('r'); + var layerNo = layerString.substring(indexofR + 1); + + var brString = spiltedFileName[2]; + // alert("brString: " + brString + "brString.indexOf('id'):" + brString.indexOf("id") + ".brString.Length: " + brString.length); + + var indexofId = brString.indexOf("Id"); + var BRId = (brString.substring(indexofId + 2)).replace('.json', ''); + + this.currentLayer = parseInt(layerNo); + this.bodyViewIndex = parseInt(bodyViewId); + this.bodyRegionId = parseInt(BRId); + + console.log("bodyViewId= " + bodyViewId + ",layerNo= " + layerNo + ",BRId= " + BRId); + + + if (parseInt(layerNo) != 0) { + console.log("this.bodyViewIndex: " + this.bodyViewIndex + ", this.currentLayer: " + this.currentLayer + ", this.bodyRegionId:" + this.bodyRegionId); + this.getBodyViewJson(); + + } + //else { + // console.log("this.bodyViewIndex: " + this.bodyViewIndex + ", this.currentLayer: " + this.currentLayer + ", this.bodyRegionId:" + this.bodyRegionId); + // this.getBodyViewJson(); + //} + + } + + } + + + diff --git a/600-POC/fileSave/jsonFile_generate/AJs/app.module.ts b/600-POC/fileSave/jsonFile_generate/AJs/app.module.ts new file mode 100644 index 0000000..67ae491 --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/AJs/app.module.ts @@ -0,0 +1,20 @@ +import { BrowserModule } from '@angular/platform-browser'; +import { NgModule } from '@angular/core'; +import { FormsModule } from '@angular/forms'; +import { HttpModule } from '@angular/http'; + +import { AppComponent } from './app.component'; + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + FormsModule, + HttpModule + ], + providers: [], + bootstrap: [AppComponent] +}) +export class AppModule { } diff --git a/600-POC/fileSave/jsonFile_generate/AJs/services/image.service.ts b/600-POC/fileSave/jsonFile_generate/AJs/services/image.service.ts new file mode 100644 index 0000000..4ea8a4e --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/AJs/services/image.service.ts @@ -0,0 +1,86 @@ +import { Injectable } from '@angular/core'; +import { Http, Response, Headers, URLSearchParams } from '@angular/http'; +import { Router } from '@angular/router'; + +import { Observable } from 'rxjs/Observable'; +import 'rxjs/add/operator/map'; +import 'rxjs/add/operator/catch'; + +declare var jQuery: any; + +@Injectable() +export class ImageService { + + constructor(private http: Http) { + + } + + public getRemianingFilesData(filePath: string): Observable { + return this.http.get(filePath).map(this.getData).catch(this.handleError); + + } + public getBodyViewData(index: number): Observable { + console.log("getBodyViewData.index :" + index); + return this.http.get('../assets/data/json/da/body-views/' + index + '/da_dat_layer_' + index + '.json'); + } + + public updateImageData(imageData: any): Observable { + const headers = new Headers(); + headers.append('Content-Type', 'application/json'); + headers.append('bodyViewIndex', imageData.bodyViewIndex); + headers.append('layerNumber', imageData.layerNumber); + headers.append('bodyRegion', imageData.bodyRegion); + + let payload: string = JSON.stringify(imageData); + console.log('inside updateImageData.JSON.stringify(imageData)= ' + payload); + return this.http.post('/API/api/ProcessImage', payload, { headers: headers }) + .map(this.extractData) + .catch(this.handleError); + } + + //public updateImageData(imageData: any): any { + // const headers = new Headers(); + // headers.append('Content-Type', 'application/json'); + // let payload: string = JSON.stringify(imageData); + // console.log('inside updateImageData.JSON.stringify(imageData)= ' + payload); + // //return this.http.post('/API/api/ProcessImage', payload, { headers: headers }) + // // .map(this.extractData) + // // .catch(this.handleError); + // jQuery.ajax({ + // headers: { + // 'Accept': 'application/json', + // 'Content-Type': 'application/json' + // }, + // url: '/API/api/ProcessImage/Post', + // type: 'POST', + // data: { "value": payload }, + // success: function (result) { + // return result; + // } + // }); + //} +private getData(res:Response){ + const body = res.json(); + console.log("inside getData.body: "+body) + return body || {}; + } + + private extractData(res: Response) { + const body = res.json(); + return body || {}; + } + + private handleError(error: Response | any) { + // In a real world app, we might use a remote logging infrastructure + let errMsg: string; + if (error instanceof Response) { + const body = error.json() || ''; + const err = body.error || JSON.stringify(body); + errMsg = `${error.status} - ${error.statusText || ''} ${err}`; + } else { + errMsg = error.message ? error.message : error.toString(); + } + console.error(errMsg); + return Observable.throw(errMsg); + } +} diff --git a/600-POC/fileSave/jsonFile_generate/ProcessImageController.cs b/600-POC/fileSave/jsonFile_generate/ProcessImageController.cs new file mode 100644 index 0000000..177c65b --- /dev/null +++ b/600-POC/fileSave/jsonFile_generate/ProcessImageController.cs @@ -0,0 +1,97 @@ +using AIAHighlight.Helpers; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +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; +using System.Web.Http; + +namespace AIAHighlight.Controllers +{ + public class ProcessImageController : ApiController + { + // GET: api/ProcessImage + public IEnumerable Get() + { + return new string[] { "value1", "value2" }; + } + + // GET: api/ProcessImage/5 + public string Get(int id) + { + return "value"; + } + + // POST: api/ProcessImage + [HttpPost] + public async Task Post(HttpRequestMessage request) + { + var httpRequest = HttpContext.Current.Request; + Stream requestStream = await request.Content.ReadAsStreamAsync(); + HttpResponseMessage result = null; + + IEnumerable layerNumberHeaderValue = request.Headers.GetValues("layerNumber"); + var layerNumber = layerNumberHeaderValue.FirstOrDefault(); + + IEnumerable bodyViewIndexHeaderValue = request.Headers.GetValues("bodyViewIndex"); + var bodyViewIndex = bodyViewIndexHeaderValue.FirstOrDefault(); + + IEnumerable bodyRegionHeaderValue = request.Headers.GetValues("bodyRegion"); + var bodyRegionId = bodyRegionHeaderValue.FirstOrDefault(); + + if (requestStream != null) + { + + string fileName = "BodyView" + bodyViewIndex + "_layer" + layerNumber + "_bodyRegionId" + bodyRegionId; + // string filePath = "D:\\ADAM\\100-PROJECTS\\001-AIAHighlight.FileSave\\AIAHighlight.Serevr\\AIAHighlight\\data1\\75\\" + fileName + ".json"; + string filePath = "D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data1\\75\\" + fileName + ".json"; + + + try + { + using (FileStream fileStream = System.IO.File.Create(filePath, (int)requestStream.Length)) + { + // Fill the bytes[] array with the stream data + byte[] bytesInStream = new byte[requestStream.Length]; + // string response1 = (JsonConvert.SerializeObject(bytesInStream)); + requestStream.Read(bytesInStream, 0, (int)bytesInStream.Length); + // Use FileStream object to write to the specified file + fileStream.Write(bytesInStream, 0, bytesInStream.Length); + result = Request.CreateResponse(HttpStatusCode.Created, "TEMP.txt"); + } + return result; + } + catch (HttpException ex) + { + return result = Request.CreateResponse(HttpStatusCode.BadGateway, "Http Exception Come" + ex.Message); + } + catch (Exception ex) + { + return result = Request.CreateResponse(HttpStatusCode.BadGateway, "Http Exception Come" + ex.Message); + } + } + + return result; ; + } + //public HttpResponseMessage Post([FromBody]JObject image) + //{ + // dynamic response = new ProcessImageHelper().writeFile(image); + // return new HttpResponseMessage { StatusCode = HttpStatusCode.Created, Content = new StringContent(JsonConvert.SerializeObject(response)) }; + //} + + // PUT: api/ProcessImage/5 + //public void Put(int id, [FromBody]string value) + //{ + //} + + //// DELETE: api/ProcessImage/5 + //public void Delete(int id) + //{ + //} + } +} -- libgit2 0.21.4