Esempio n. 1
0
        //public static BsonDocument GetASXListedCompaniesDates()
        //{

        //    string databaseName = "stocks";
        //    string collectionName = "ASXListedCompanies";
        //    MongoClient client = new MongoClient("mongodb://localhost:27017");
        //    var database = client.GetDatabase(databaseName);
        //    var collection = database.GetCollection<BsonDocument>(collectionName);
        //    var query_where2 = from a in collection.AsQueryable<BsonDocument>()
        //                        where a..Name.Contains("Contoso")
        //                        where a.Address1_City == "Redmond"
        //                        select a;
        //    foreach (var doc in docs)
        //    {
        //        System.Diagnostics.Debug.WriteLine(doc["watchlist"]);
        //        if (doc["watchlist"].AsString.ToUpper() == name.ToUpper())
        //        {
        //            return doc;
        //        }
        //    }
        //    return null;
        //}


        public static void UpdateASXListedCompaniesBatchDates()
        {
            StocksDB stocksDB           = new StocksDB();
            ASXListedCompanyBatch batch = GetASXListedCompaniesBatchRecord(stocksDB);

            if (batch == null)
            {
                batch = new ASXListedCompanyBatch();
                //batch.Id = ObjectId.GenerateNewId();
            }

            DateTime?updateStartedDateCurrent = batch.Dates.UpdateStartedDate;

            batch.Dates.UpdateStartedDate = DateTime.Now;


            string      databaseName   = "stocks";
            string      collectionName = "ASXListedCompanies";
            MongoClient client         = new MongoClient("mongodb://localhost:27017");
            var         database       = client.GetDatabase(databaseName);
            var         collection     = database.GetCollection <ASXListedCompanyBatch>(collectionName);
            //var filter = Builders<ASXListedCompanyBatch>.Filter.And(Builders<ASXListedCompanyBatch>.Filter.Eq("Id", batch.Id),
            //                                                        Builders<ASXListedCompanyBatch>.Filter.Eq("Id", batch.Id));
            //ReplaceResult replaceResult = collection.ReplaceOne(filter, batch, new UpdateOptions { IsUpsert = true });
            //var insert = new UpdateO
            //using(stocksDB.Client..)
            //collection.BulkWrite
        }
Esempio n. 2
0
        public static ASXListedCompanyBatch GetASXListedCompaniesBatchRecord(StocksDB stocksDB)
        {
            ASXListedCompanyBatch batch = null;

            if (stocksDB is null)
            {
                stocksDB = new StocksDB();
            }
            string collectionName = "ASXListedCompanies";
            var    collection     = stocksDB.GetCollection <ASXListedCompanyBatch>(collectionName);
            var    filter         = Builders <ASXListedCompanyBatch> .Filter.Eq("BatchName", ASXListedCompanyBatch.cBatchName);

            try
            {
                batch = collection.Find <ASXListedCompanyBatch>(filter).First();
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            return(batch);
        }
Esempio n. 3
0
        public static bool MarkBatchCompleted(StocksDB stocksDB, ASXListedCompanyBatch batch)
        {
            if (stocksDB is null)
            {
                stocksDB = new StocksDB();
            }
            string collectionName = "ASXListedCompanies";
            var    collection     = stocksDB.GetCollection <ASXListedCompanyBatchRecord>(collectionName);
            var    filter         = Builders <ASXListedCompanyBatchRecord> .Filter.Eq("Dates.UpdateStartedDate", batch.Dates.UpdateStartedDate);

            var update = Builders <ASXListedCompanyBatchRecord> .Update.CurrentDate("Dates.LastUpdatedDate");

            try
            {
                UpdateResult replaceResult = collection.UpdateMany(filter, update);
            }
            catch (Exception e)
            {
                Console.Write(e);
                return(false);
            }
            return(true);
        }
Esempio n. 4
0
        public static bool StartUpdateASXListedCompaniesBatch(StocksDB stocksDB, out ASXListedCompanyBatch batch)
        {
            batch = null;
            string collectionName = "ASXListedCompanies";

            if (stocksDB is null)
            {
                stocksDB = new StocksDB();
            }
            batch = GetASXListedCompaniesBatchRecord(stocksDB);
            var collection = stocksDB.GetCollection <ASXListedCompanyBatch>(collectionName);

            if (batch == null)
            {
                batch                         = new ASXListedCompanyBatch();
                batch.BatchName               = ASXListedCompanyBatch.cBatchName;
                batch.Dates                   = new Dates();
                batch.Dates.CreatedDate       = DateTime.Now;
                batch.Dates.UpdateStartedDate = batch.Dates.CreatedDate;
                var filter = Builders <ASXListedCompanyBatch> .Filter.Eq("BatchName", ASXListedCompanyBatch.cBatchName);

                try
                {
                    collection.InsertOne(batch);
                }
                catch (Exception e)
                {
                    Console.Write(e);
                    return(false);
                }
            }
            else
            {
                DateTime updateStartedDate = DateTime.Now;

                //Check for in progress or run away updates
                if (!batch.Dates.LastUpdatedDate.HasValue ||
                    (batch.Dates.UpdateStartedDate > batch.Dates.LastUpdatedDate &&
                     batch.Dates.UpdateStartedDate.Value.AddHours(1) > updateStartedDate)
                    )
                {
                    return(false);
                }

                var filter = Builders <ASXListedCompanyBatch> .Filter.And(
                    Builders <ASXListedCompanyBatch> .Filter.Eq("BatchName", ASXListedCompanyBatch.cBatchName),
                    Builders <ASXListedCompanyBatch> .Filter.Eq("Dates.LastUpdatedDate", batch.Dates.LastUpdatedDate),
                    Builders <ASXListedCompanyBatch> .Filter.Eq("Dates.UpdateStartedDate", batch.Dates.UpdateStartedDate));

                try
                {
                    batch.Dates.UpdateStartedDate = updateStartedDate;
                    ReplaceOneResult replaceResult = collection.ReplaceOne(filter, batch);
                    return(replaceResult.ModifiedCount == 1);
                }
                catch (Exception e)
                {
                    Console.Write(e);
                    return(false);
                }
            }

            return(true);
        }