Program_20Threadsfor25Zoom.cs 20.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 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<Term> terms;

        public BodyRegionImage()
        {
            this.terms = new List<Term>();
        }
    }

    public class Term
    {
        public int TermNumber;
        public List<int> SystemNumbers;
        public List<int> PixelLocations;
        public int ActualTermNumber;

        public Term(int termNumber, int systemNumber, int pixelLocation)
        {
            this.SystemNumbers = new List<int>();
            this.PixelLocations = new List<int>();
            this.TermNumber = termNumber;
            this.SystemNumbers.Add(systemNumber);
            this.PixelLocations.Add(pixelLocation);
        }
    }


    

      public class Program
     {
        //public  JObject data { get; set; }
       // public  Dictionary<int, Term> 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<BodyRegionImage>("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<int,Term> termList = new Dictionary<int, Term>();

                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<BodyRegionImage>("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<int, Term> 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<int> bodyRegions = new List<int>();

        //    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<BodyRegionImage>("DAImagesLogs");
        //    //var col = db.GetCollection<BodyRegionImage>("DAImages");


        //    FilterDefinition<BodyRegionImage>[] filterCondition = {
        //                                                Builders<BodyRegionImage>.Filter.Eq("bodyViewIndex", Convert.ToInt32(bodyviewId))};

        //    dynamic list = col.Find(Builders<BodyRegionImage>.Filter.And(filterCondition)).Project(Builders<BodyRegionImage>.Projection.Include("layerNumber").Include("bodyRegionId")).ToList();
        //    //collection.Find(new BsonDocument()).Project(Builders<BsonDocument>.Projection.Include("Price").Exclude("_id")).ToListAsync();
        //    // return list;
        //    Dictionary<int, List<int>> layersToProcess = new Dictionary<int, List<int>>();

        //    for (int i = 1; i <= totalLayers; i++)
        //    {
        //        List<int> bodyRegionForThisLayer = new List<int>();
        //        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<int, List<int>> filesToProcess, int BodyViewIndex)
        //{
        //    Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "in makeFileNameAndProcess");

        //    foreach (KeyValuePair<int, List<int>> 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);

        //    }
        //}
    }
}