Beispiel #1
0
        private async Task <List <string> > DownloadUpdatedProducts()
        {
            List <string> errors = new List <string>();

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = false,
                Encoder       = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            int      total       = 0;
            int      pageNum     = 0;
            int      perPage     = 1000;
            int      counter     = 0;
            DateTime lastUpdated = DateTime.MinValue;
            DateTime dateUtc     = SettingsProvider.GetSetting <DateTime>(LAST_UPDATED_SETTING_NAME);

            FileLogger.Log($"GraphQL: current timestamp = {dateUtc:yyyy-MM-dd HH:mm:ss}");

            do
            {
                pageNum++;
                ProductSearch result = await client.GetUpdatedProducts(dateUtc, pageNum, perPage);

                if (total == 0)
                {
                    total = result.pageInfo.total;

                    FileLogger.Log($"GraphQL: loading {total} products ...");
                }

                foreach (Product product in result.items)
                {
                    DatabaseIncomingMessage message = new DatabaseIncomingMessage()
                    {
                        Sender        = "AC",
                        OperationType = "UPDATE",
                        MessageType   = "Accord.Product",
                        MessageBody   = JsonSerializer.Serialize(product, options)
                    };

                    producer.InsertMessage(message);
                    counter++;

                    if (product.updatedAt > lastUpdated)
                    {
                        lastUpdated = product.updatedAt;
                    }
                }

                FileLogger.Log($"GraphQL: {counter} products loaded.");

                total -= result.pageInfo.perPage;
            } while (total > 0);

            if (lastUpdated != DateTime.MinValue)
            {
                SettingsProvider.SetSetting(LAST_UPDATED_SETTING_NAME, lastUpdated);
                FileLogger.Log($"GraphQL: new timestamp = {lastUpdated:yyyy-MM-dd HH:mm:ss}");
            }
            else
            {
                FileLogger.Log($"GraphQL: timestamp has not been changed");
            }

            FileLogger.Log($"GraphQL: loaded {counter} products.");

            return(errors);
        }
        [TestMethod] public async Task TestDatabaseProducer()
        {
            metadata
            .UseConnectionString(CONNECTION_STRING)
            .UseDatabaseProvider(DatabaseProvider.SQLServer);

            InfoBase infoBase = metadata.OpenInfoBase();

            producer
            .UseConnectionString(CONNECTION_STRING)
            .UseDatabaseProvider(DatabaseProvider.SQLServer)
            .Initialize(infoBase, "РегистрСведений.DaJetExchangeВходящаяОчередь");

            Console.WriteLine(producer.InsertMessageScript);

            //int pageNum = 1;
            //int perPage = 1000;
            //DateTime dateUtc = new DateTime(2021, 1, 1);
            //ProductSearch result = await client.GetUpdatedProducts(dateUtc, pageNum, perPage);

            //if (result == null)
            //{
            //    Console.WriteLine($"Failed to get products list.");
            //    return;
            //}

            //Console.WriteLine();
            //Console.WriteLine($"Start date: {dateUtc:yyyy-MM-dd}");
            //Console.WriteLine($"Page number: {result.pageInfo.page}");
            //Console.WriteLine($"Items per page: {result.pageInfo.perPage}");
            //Console.WriteLine($"Total items: {result.pageInfo.total}");

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = false,
                Encoder       = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            int      total   = 0;
            int      pageNum = 0;
            int      perPage = 100;
            DateTime dateUtc = new DateTime(2021, 10, 15);

            do
            {
                pageNum++;
                ProductSearch result = await client.GetUpdatedProducts(dateUtc, pageNum, perPage);

                if (total == 0)
                {
                    Console.WriteLine();
                    Console.WriteLine($"Start date: {dateUtc:yyyy-MM-dd}");
                    Console.WriteLine($"Page number: {result.pageInfo.page}");
                    Console.WriteLine($"Items per page: {result.pageInfo.perPage}");
                    Console.WriteLine($"Total items: {result.pageInfo.total}");

                    total = result.pageInfo.total;
                }

                foreach (Product product in result.items)
                {
                    DatabaseIncomingMessage message = new DatabaseIncomingMessage()
                    {
                        Sender        = "AC",
                        OperationType = "UPDATE",
                        MessageType   = "Accord.Product",
                        MessageBody   = JsonSerializer.Serialize(product, options)
                    };

                    producer.InsertMessage(message);
                }

                //total -= result.pageInfo.perPage;
                total = 0;
            } while (total > 0);

            Console.WriteLine();
            Console.WriteLine("The end =)");
        }