// This demo shows how you can consume business data.
        private static async Task Main(string[] args)
        {
            Console.Title = "Business Data User";

            IDistributedSearchConfiguration demoCredential = new DemoCredential();

            var cts = new CancellationTokenSource();

            var businessDataPump = new BusinessDataPump <FashionBusinessData, FashionBusinessDataUpdate>(
                demoCredential: demoCredential,
                createEmptyBusinessData: newFashionBusinessData,
                applyUpdate: FashionBusinessDataExtensions.ApplyFashionUpdate,
                snapshotContainerClient: new BlobContainerClient(
                    blobContainerUri: new Uri($"https://{demoCredential.BusinessDataSnapshotAccountName}.blob.core.windows.net/{demoCredential.BusinessDataSnapshotContainerName}/"),
                    credential: demoCredential.AADServicePrincipal));

            Func <BusinessData <FashionBusinessData>, string> bdToStr = bd => string.Join(" ",
                                                                                          bd.Data.Markup.Select(kvp => $"{kvp.Key}={kvp.Value}").ToArray());

            bool demoObservable = true;

            if (demoObservable)
            {
                // This code observes business data changes and only prints when there have been changes.
                // One line oer change, only when something changes.
                var businessDataObservable = await businessDataPump.CreateObservable(cts.Token);

                businessDataObservable.Subscribe(onNext: async bd =>
                {
                    await Console.Out.WriteLineAsync(bdToStr(bd));
                }, token: cts.Token);

                _ = await Console.In.ReadLineAsync();

                cts.Cancel();
            }
            else
            {
                // This code runs in an endless loop, and prints the business data (regardless whether it changed or not)
                // Prints out the same message, if there are no changes.
                await businessDataPump.StartUpdateProcess(cts.Token);

                while (true)
                {
                    await Console.Out.WriteLineAsync(bdToStr(businessDataPump.BusinessData));
                }
            }
        }
Exemple #2
0
        private static async Task Main()
        {
            Console.Title = "Update Configuration";

            IDistributedSearchConfiguration demoCredential = new DemoCredential();

            var businessDataUpdates = new BusinessDataPump <FashionBusinessData, FashionBusinessDataUpdate>(
                demoCredential: demoCredential,
                createEmptyBusinessData: newFashionBusinessData,
                applyUpdate: FashionBusinessDataExtensions.ApplyFashionUpdate,
                snapshotContainerClient: new BlobContainerClient(
                    blobContainerUri: new Uri($"https://{demoCredential.BusinessDataSnapshotAccountName}.blob.core.windows.net/{demoCredential.BusinessDataSnapshotContainerName}/"),
                    credential: demoCredential.AADServicePrincipal));

            while (true)
            {
                await Console.Out.WriteAsync($"Please enter an item and a price, separated by a space: ");

                var input = await Console.In.ReadLineAsync();

                var values = input.Split(" ");
                if (values.Length != 2)
                {
                    continue;
                }
                var item = values[0];

                if (!decimal.TryParse(values[1], out var newMarkup))
                {
                    continue;
                }

                var update = FashionBusinessDataUpdate.NewMarkupUpdate(
                    fashionType: item,
                    markupPrice: newMarkup);

                // Here, we're directly dropping the update in Kafka.
                // This must be replaced with a secured HTTP request.
                var watermark = await businessDataUpdates.SendUpdate(update);

                await Console.Out.WriteLineAsync($"Update sent for {newMarkup} (#{watermark.Item})");
            }
        }
        private static async Task Main()
        {
            Console.Title = "Snapshot Generator Service";

            IDistributedSearchConfiguration demoCredential = new DemoCredential();

            var businessDataPump = new BusinessDataPump <FashionBusinessData, FashionBusinessDataUpdate>(
                demoCredential: demoCredential,
                createEmptyBusinessData: newFashionBusinessData,
                applyUpdate: FashionBusinessDataExtensions.ApplyFashionUpdate,
                snapshotContainerClient: new BlobContainerClient(
                    blobContainerUri: new Uri($"https://{demoCredential.BusinessDataSnapshotAccountName}.blob.core.windows.net/{demoCredential.BusinessDataSnapshotContainerName}/"),
                    credential: demoCredential.AADServicePrincipal));

            var cts = new CancellationTokenSource();
            IObservable <BusinessData <FashionBusinessData> > businessDataObservable = await businessDataPump.CreateObservable(cts.Token);

            businessDataObservable
            .Sample(interval: TimeSpan.FromSeconds(5))
            .Subscribe(
                onNext: async bd =>
            {
                await Console.Out.WriteLineAsync($"{bd.AsJSON()}");
                var snapshotName = await businessDataPump.WriteBusinessDataSnapshot(bd);
                await Console.Out.WriteLineAsync($"wrote snapshot {snapshotName}");
            },
                onError: ex => Console.Error.WriteLine($"ERROR: {ex.Message}"),
                onCompleted: () => Console.Out.WriteLine($"Completed"),
                token: cts.Token);

            await Console.Out.WriteAsync("Press <return> to stop snapshot generation");

            _ = await Console.In.ReadLineAsync();

            cts.Cancel();
        }
        private static async Task Main()
        {
            Console.Title = "Sample Provider";

            IDistributedSearchConfiguration demoCredential = new DemoCredential();

            var requestsClient = MessagingClients.Requests <ProviderSearchRequest <FashionSearchRequest> >(demoCredential);

            var cts = new CancellationTokenSource();

            var clients = new Dictionary <TopicAndPartition, IRequestResponseMessageClient <ProviderSearchResponse <FashionItem> > >();

            IRequestResponseMessageClient <ProviderSearchResponse <FashionItem> > getMessageClient(TopicAndPartition tpid)
            {
                lock (clients)
                {
                    if (!clients.ContainsKey(tpid))
                    {
                        var client = MessagingClients.Responses <ProviderSearchResponse <FashionItem> >(
                            demoCredential: demoCredential, topicAndPartition: tpid);

                        clients.Add(tpid, client);
                    }
                }
                return(clients[tpid]);
            }

            requestsClient
            .CreateObervable(cts.Token)
            .Subscribe(
                onNext: async providerSearchRequestMessage =>
            {
                var search    = providerSearchRequestMessage.Payload;
                var requestId = providerSearchRequestMessage.RequestID;

                var responseProducer = getMessageClient(search.ResponseTopic);

                // await Console.Out.WriteLineAsync($"{requestId}: Somebody's looking for {search.SearchRequest.FashionType}");

                var tcs = new TaskCompletionSource <bool>();

                GetResponses()
                .Select(foundFashionItem => new ProviderSearchResponse <FashionItem>(
                            requestID: requestId,
                            response: ListModule.OfArray(new[] { foundFashionItem })))
                .Subscribe(
                    onNext: async(responsePayload) =>
                {
                    await responseProducer.SendRequestResponseMessage(
                        messagePayload: responsePayload,
                        requestId: requestId,
                        cancellationToken: cts.Token);

                    // await Console.Out.WriteLineAsync($"{requestId}: Sending {responsePayload.Response.Head.Description}");
                },
                    onError: ex =>
                {
                    Console.Error.WriteLine($"Error with request {requestId}: {ex.Message}");
                },
                    onCompleted: async() =>
                {
                    await Console.Out.WriteLineAsync($"Finished with request {requestId}");
                    tcs.SetResult(true);
                },
                    token: cts.Token);

                _ = await tcs.Task;         // only leave on onCompleted
            },
                onError: ex => Console.Error.WriteLine($"Error with EventHub: {ex.Message}"),
                onCompleted: () => Console.WriteLine($"Finished with EventHub"),
                token: cts.Token);

            await Console.In.ReadLineAsync();

            cts.Cancel();
        }