private bool DataEntryWithinFilter(PreAggSpecs spec, StockDataEntry data)
        {
            if (spec.type != StockType.Undefined)
            {
                if (!CheckStockType(spec.type, data.stockType, spec.stockTypeCompare))
                {
                    return(false);
                }
            }

            if (spec.direction != StockDirection.Undefined)
            {
                if (!CheckDirectionType(spec.direction, data.direction, spec.directionCompare))
                {
                    return(false);
                }
            }

            for (int i = 0; i < spec.holderCountry.Count; i++)
            {
                if (CheckHolderCountryType(spec.holderCountry[i], data.holderCountry, spec.countryCompare))
                {
                    return(true);
                }
            }

            return(false);
        }
        public override void Execute(ref bool destroyAfterExecute)
        {
            int            outstadindChunkContexts;
            string         key;
            CriteriaSetObj workingCriteria = Global.criteriaSets[criteriaIndex];
            RawDataChunk   workingData     = Global.parsedRawData[dataIndex];
            PreAggSpecs    myPreSpecs      = workingCriteria.preAggObj;
            AggSpecs       myAggSpecs      = workingCriteria.aggSpecsObj;
            PostAggSpecs   myPostSpecs     = workingCriteria.postAggObj;
            uint           rawDataRowId;

            for (int i = 0; i < workingData.chunkDataParsed.Count; i++)
            {
                StockDataEntry currStockEntry = workingData.chunkDataParsed[i];
                if (DataEntryWithinFilter(myPreSpecs, currStockEntry))
                {
                    rawDataRowId = ((uint)dataIndex << 16) | ((uint)i);
                    key          = myAggSpecs.CreateAggKeyWithAggValues(currStockEntry);
                    workingCriteria.AddValueToDictionary(yesterdayData, key, currStockEntry.precentageSharesHeld, currStockEntry.sharesHeld, currStockEntry.value, rawDataRowId);
                }
            }

            outstadindChunkContexts = Interlocked.Decrement(ref chunkCount);
            if (outstadindChunkContexts == 0)
            {
                Global.mainThreadWait.Set();
            }
        }
        public override void Execute(ref bool destroyAfterExecute)
        {
            int            dataChunksRemaining;
            StockDataEntry entry;
            int            sectionBuffStart = rangeStart;
            int            sectionBuffEnd   = rangeStart;
            int            reQueueRangeCreator;

            // TODO: check that rangeEnd in buffRef is a newline character
            for (int i = rangeStart; i < rangeEnd; i++)
            {
                if (Global.frp.buff[i].Equals('\r'))
                {
                    if (!globalFirstChunk)
                    {
                        // TODO: if string value in split doesn't exist, then value should be 0.
                        string   dataEntrySection = new string(Global.frp.buff, sectionBuffStart, sectionBuffEnd - sectionBuffStart);
                        string[] split            = dataEntrySection.Split(',');
                        entry                      = new StockDataEntry();
                        entry.stockCode            = split[0];
                        entry.stockType            = split[1];
                        entry.holderId             = split[2];
                        entry.holderCountry        = split[3];
                        entry.sharesHeld           = Convert.ToDouble(split[4]);
                        entry.precentageSharesHeld = Convert.ToDouble(split[5]);
                        entry.direction            = split[6];
                        entry.value                = Convert.ToDouble(split[7]);

                        parsedChunk.chunkDataParsed.Add(entry);
                    }
                    else
                    {
                        globalFirstChunk = false;
                    }
                    sectionBuffStart = sectionBuffEnd + 2;
                }
                sectionBuffEnd++;
            }

            reQueueRangeCreator = Interlocked.Decrement(ref Global.frp.m_numOfChunksProcessing);
            if (reQueueRangeCreator == 0)
            {
                Global.threadPool.PutQueue(Global.frp);
            }
            // TODO: if m_numOfChunksProcessing = 0 then re-queue if not endOfFile
            dataChunksRemaining = Interlocked.Decrement(ref outstadingDataChunkParsers);
            if (dataChunksRemaining == 0)
            {
                //Global.mainThreadWait.Set();
            }
            Global.CombineStockDataEntryChunk(parsedChunk);
        }
        internal string CreateAggKeyWithAggValues(StockDataEntry data)
        {
            // TODO: May not be good to create 'new' AggValues, might de-reference.
            StringBuilder sb = new StringBuilder();

            // AggKey
            if (stockCode)
            {
                sb.Append(data.stockCode);
            }
            if (stockType)
            {
                if (sb.Length != 0)
                {
                    sb.Append("~");
                }
                sb.Append(data.stockType);
            }
            if (holderId)
            {
                if (sb.Length != 0)
                {
                    sb.Append("~");
                }
                sb.Append(data.holderId);
            }
            if (direction)
            {
                if (sb.Length != 0)
                {
                    sb.Append("~");
                }
                sb.Append(data.direction);
            }

            return(sb.ToString());
        }
        private void CrossesYesterdayValueComparison(CriteriaSetObj criteriaObj, ThresholdComparison compare)
        {
            int    rawDataChunkIndex;
            int    stockDataIndex;
            double columnValue = 0.0, thresholdCrossed = 0.0;
            ThresholdComparison    comparison = compare;
            CriteriaSetObj         criteria = criteriaObj;
            ThresholdColumn        crossesColumn = criteria.postAggObj.thresholdColumn;
            IReadOnlyList <double> thresholds = criteria.postAggObj.GetRefToThresholdValues();
            IReadOnlyDictionary <string, AggValues> todaysParsedDataDictionary = criteria.GetRefToAggResults();
            IReadOnlyDictionary <string, AggValues> yesterdaysParsedDataDictionary = criteria.GetRefToYesterdayAggResults();
            AggValues todaysAgg;
            AggValues yestedayAgg;
            bool      keyExistsYesterday, todayGreaterYesterday;

            if (crossesColumn == ThresholdColumn.Undefiend)
            {
                // TODO: do something else
                return;
            }

            WriteThresholdHeader(criteria);

            foreach (string key in todaysParsedDataDictionary.Keys)
            {
                todaysParsedDataDictionary.TryGetValue(key, out todaysAgg); // should always find something
                keyExistsYesterday = yesterdaysParsedDataDictionary.TryGetValue(key, out yestedayAgg);
                if (!keyExistsYesterday)
                {
                    yestedayAgg = new AggValues(); // all values are 0 then.
                }

                switch (crossesColumn)
                {
                case ThresholdColumn.PrecentageSharesHeld:
                {
                    columnValue = todaysAgg.percentageSharesHeld;
                    if (columnValue > yestedayAgg.percentageSharesHeld)
                    {
                        todayGreaterYesterday = true;
                    }
                    else
                    {
                        todayGreaterYesterday = false;
                    }
                    thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
                    break;
                }

                case ThresholdColumn.SharesHeld:
                {
                    columnValue = todaysAgg.sharesHeld;
                    if (columnValue > yestedayAgg.sharesHeld)
                    {
                        todayGreaterYesterday = true;
                    }
                    else
                    {
                        todayGreaterYesterday = false;
                    }
                    thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
                    break;
                }

                case ThresholdColumn.Value:
                {
                    columnValue = todaysAgg.value;
                    if (columnValue > yestedayAgg.value)
                    {
                        todayGreaterYesterday = true;
                    }
                    else
                    {
                        todayGreaterYesterday = false;
                    }
                    thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
                    break;
                }

                default:
                {
                    break;
                }
                }

                // TODO: write to files
                criteriaFilterWriter.WriteLine(GetAggValuesCommaSeparatedString(key, todaysAgg));

                IReadOnlyList <uint> unaggregatedDictEntry = todaysAgg.GetRefToRowsInChunk();
                for (int i = 0; i < unaggregatedDictEntry.Count; i++)
                {
                    rawDataChunkIndex = (int)unaggregatedDictEntry[i] >> 16;
                    stockDataIndex    = (int)unaggregatedDictEntry[i] & 0xffff;
                    StockDataEntry sde = Global.parsedRawData[rawDataChunkIndex].chunkDataParsed[stockDataIndex];
                    criteriaFilterDetailsWriter.WriteLine(GetCommaSeparatedStockDataEntryWithKey(sde, key));
                }

                criteriaThresholdWriter.WriteLine(GetCommaSeparatedThresholdEntry(key, columnValue, thresholdCrossed));
            }

            criteriaFilterWriter.Dispose();
            criteriaFilterDetailsWriter.Dispose();
            criteriaThresholdWriter.Dispose();
        }
 private string GetCommaSeparatedStockDataEntryWithKey(StockDataEntry sde, string key)
 {
     return(sde.stockCode + "," + sde.stockType + "," + sde.holderId + "," + sde.holderCountry + "," + Convert.ToString(sde.sharesHeld) + "," + Convert.ToString(sde.precentageSharesHeld) + "," + sde.direction + "," + Convert.ToString(sde.value) + "," + key);
 }
        private void ValueOfColumnComparison(CriteriaSetObj criteriaObj, ThresholdComparison compare)
        {
            int rawDataChunkIndex;
            int stockDataIndex;
            ThresholdComparison comparison = compare;
            CriteriaSetObj      criteria   = criteriaObj;
            ThresholdColumn     column     = criteria.postAggObj.thresholdColumn;
            IReadOnlyDictionary <string, AggValues> readOnlyAggResults = criteria.GetRefToAggResults();
            AggValues aggValue;
            IReadOnlyList <double> thresholdValues = criteria.postAggObj.GetRefToThresholdValues(); // should only contain 1 value.
            double thresholdValue = thresholdValues[0];                                             // should be only 1 value here'
            double columnValue    = 0.0;
            bool   thresholdCheck = false;

            if (column == ThresholdColumn.Undefiend)
            {
                // TODO: Do something if it is undefined, shouldn't be.
                return;
            }

            WriteThresholdHeader(criteria);

            foreach (string key in readOnlyAggResults.Keys)
            {
                readOnlyAggResults.TryGetValue(key, out aggValue);

                switch (column)
                {
                case ThresholdColumn.PrecentageSharesHeld:
                {
                    columnValue    = aggValue.percentageSharesHeld;
                    thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
                    break;
                }

                case ThresholdColumn.SharesHeld:
                {
                    columnValue    = aggValue.sharesHeld;
                    thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
                    break;
                }

                case ThresholdColumn.Value:
                {
                    columnValue    = aggValue.value;
                    thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
                    break;
                }

                default:
                {
                    break;
                }
                }

                if (thresholdCheck)
                {
                    criteriaFilterWriter.WriteLine(GetAggValuesCommaSeparatedString(key, aggValue));

                    IReadOnlyList <uint> unaggregatedDictEntry = aggValue.GetRefToRowsInChunk();
                    for (int i = 0; i < unaggregatedDictEntry.Count; i++)
                    {
                        rawDataChunkIndex = (int)unaggregatedDictEntry[i] >> 16;
                        stockDataIndex    = (int)unaggregatedDictEntry[i] & 0xffff;
                        StockDataEntry sde = Global.parsedRawData[rawDataChunkIndex].chunkDataParsed[stockDataIndex];
                        criteriaFilterDetailsWriter.WriteLine(GetCommaSeparatedStockDataEntryWithKey(sde, key));
                    }
                    criteriaThresholdWriter.WriteLine(GetCommaSeparatedThresholdEntry(key, columnValue, thresholdValue));
                }
            }

            criteriaFilterWriter.Dispose();
            criteriaFilterDetailsWriter.Dispose();
            criteriaThresholdWriter.Dispose();
        }
 internal void AddStockEntry(StockDataEntry sde)
 {
     dividedSummedStockData.Add(sde);
 }