Exemplo n.º 1
0
        public async static Task BatchRequestExample(GraphServiceClient graphServiceClient, int limit)
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            var batchRequestContent = new BatchRequestContent();
            var teams = DbOperations.GetTeams(limit);

            // 1. construct a Batch request
            for (int i = 0; i < teams.Count; i++)
            {
                var requestUrl1 = graphServiceClient.Groups[teams[i].TeamId].Drive.Root
                                  .Delta()
                                  .Request().RequestUrl;
                var request1     = new HttpRequestMessage(HttpMethod.Get, requestUrl1);
                var requestStep1 = new BatchRequestStep($"{i}", request1, null);
                batchRequestContent.AddBatchRequestStep(requestStep1);
            }

            //3. Submit request
            var batchRequest = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/$batch");

            batchRequest.Content = batchRequestContent;
            await graphServiceClient.AuthenticationProvider.AuthenticateRequestAsync(batchRequest);

            var httpClient    = new HttpClient();
            var batchResponse = await httpClient.SendAsync(batchRequest);

            // 3. Process response
            var batchResponseContent = new BatchResponseContent(batchResponse);
            var responses            = await batchResponseContent.GetResponsesAsync();

            foreach (var response in responses)
            {
                if (response.Value.IsSuccessStatusCode)
                {
                    //Console.WriteLine();
                    //Console.WriteLine($"response {response.Key} - {await response.Value.Content.ReadAsStringAsync()}");
                    //Console.WriteLine();
                    Console.WriteLine($"response {response.Key}");
                }
            }
            watch.Stop();
            Console.WriteLine($"Checking Teams completed on {watch.ElapsedMilliseconds / 1000} seconds");
        }
Exemplo n.º 2
0
        private static async Task RunBatchAsync()
        {
            var teams = DbOperations.GetTeams(5);
            var batch = new BatchRequestContent();
            //foreach (var team in teams)
            {
                var deltaRequest01 = graphClient.Groups[teams[0].TeamId].Drive.Root
                            .Delta()
                            .Request()
                            .GetAsync().Result;

                var deltaRequest0 = graphClient.Groups[teams[0].TeamId].Drive.Root
                            .Delta()
                            .Request()
                            //.Select("CreatedDateTime,Deleted,File,Folder,LastModifiedDateTime,Root,SharepointIds,Size,WebUrl")
                            ;
                var deltaRequest1 = graphClient.Groups[teams[0].TeamId].Drive.Root
                            .Delta()
                            .Request()
                            //.Select("CreatedDateTime,Deleted,File,Folder,LastModifiedDateTime,Root,SharepointIds,Size,WebUrl")
                            ;
                var deltaResp0 = batch.AddBatchRequestStep(deltaRequest0);
                var deltaResp1 = batch.AddBatchRequestStep(deltaRequest1);
                var returnedResponse = await graphClient.Batch.Request().PostAsync(batch);

                try
                {
                    var deltaResponse0 = await returnedResponse.GetResponseByIdAsync<IDriveItemDeltaCollectionPage>(deltaResp0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                try
                {
                    var deltaResponse1 = await returnedResponse.GetResponseByIdAsync<IDriveItemDeltaCollectionPage>(deltaResp1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


            }
        }
Exemplo n.º 3
0
        private static async Task WatchTeamsAsync(int limit)
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            //await GetTeams();
            //await DbOperations.AddTeamsToTable(allTeams);

            var teams = DbOperations.GetTeams(limit);//.Skip(limit*2);//allTeams.OrderBy(o => o.CreatedDateTime).Take(limit);

            foreach (var team in teams)
            {
                teamSitesDeltaLinks.Add(team.TeamId, null);
            }

            watch.Stop();
            Console.WriteLine($"Checking Teams completed on {watch.ElapsedMilliseconds / 1000} seconds");

            while (!noChanges || iterationCounter != MaxIteration)
            {
                try
                {
                    await WatchTeamsSitesAsync();

                    processTime     = (int)DateTime.UtcNow.Subtract(lastProcessTime).TotalSeconds;
                    lastProcessTime = DateTime.UtcNow;
                    var wait = interval - processTime;
                    if (wait < 0)
                    {
                        wait = 0;
                    }
                    await Task.Delay(wait * 1000);

                    await DbOperations.UpdateResourcesAsync(resources);

                    resources.Clear();
                    firstCall = false;

                    if (teamSitesDeltaLinks.Any(w => w.Value == null))
                    {
                        noChanges        = false;
                        iterationCounter = 0;
                    }
                    else if (teamSitesDeltaLinks.All(w => w.Value.NoChanges))
                    {
                        noChanges = true;
                        ++iterationCounter;
                    }
                    else
                    {
                        noChanges        = false;
                        iterationCounter = 0;
                    }
                    perf.ActivitiesCalls     = activitiesCalls;
                    perf.DeltaCalls          = libraryDeltaCalls;
                    perf.AverageSyncDuration = DbOperations.GetAverageSync();
                    await DbOperations.UpdatePerformanceAsync(perf);
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                }
            }
        }
Exemplo n.º 4
0
        private static async Task WatchTeamsAsync(int limit)
        {
            //var watch = new System.Diagnostics.Stopwatch();
            //watch.Start();
            //await DbOperations.ClearTeamsTable();

            var teams = DbOperations.GetTeams(limit);

            if (teams.Count == 0)
            {
                Console.WriteLine("Fill Teams Table");
                await GetTeams();

                await DbOperations.AddTeamsToTable(allTeams);

                teams = DbOperations.GetTeams(limit);
            }
            foreach (var team in teams)
            {
                teamSitesDeltaLinks.Add(team.TeamId, null);
            }

            //watch.Stop();
            //Console.WriteLine($"Checking Teams completed on {watch.ElapsedMilliseconds / 1000} seconds");

            while (true)//(!noChanges || iterationCounter != MaxIteration)
            {
                try
                {
                    var watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    Console.WriteLine("Start Checking Changes on Team Sites...");
                    await WatchTeamsSitesAsync();

                    watch.Stop();
                    processTime = (int)watch.ElapsedMilliseconds / 1000;
                    Console.WriteLine($"{DateTime.UtcNow} - Checking Changes on Team Sites completed on {processTime} seconds");
                    //AddTeamSiteDeltaLinksToResources();
                    //await DbOperations.UpdateResourcesAsync(resources);
                    resources = new List <Resource>();
                    await DbOperations.AddExceptionsAsync(exceptions);

                    exceptions = new List <Exceptions>();
                    //processTime = (int)DateTime.UtcNow.Subtract(lastProcessTime).TotalSeconds;
                    lastProcessTime = DateTime.UtcNow;
                    var wait = interval - processTime;
                    if (wait < 0)
                    {
                        wait = 0;
                    }
                    await Task.Delay(wait * 1000);

                    firstCall = false;

                    if (teamSitesDeltaLinks.Any(w => w.Value == null))
                    {
                        noChanges        = false;
                        iterationCounter = 0;
                    }
                    else if (teamSitesDeltaLinks.All(w => w.Value.NoChanges))
                    {
                        noChanges = true;
                        ++iterationCounter;
                    }
                    else
                    {
                        noChanges        = false;
                        iterationCounter = 0;
                    }
                    perf.ActivitiesCalls     = activitiesCalls;
                    perf.DeltaCalls          = libraryDeltaCalls;
                    perf.Duration            = (int)DateTime.UtcNow.Subtract(perf.StartOn).TotalSeconds;
                    perf.CompletedOn         = DateTime.UtcNow;
                    perf.AverageSyncDuration = DbOperations.GetAverageSync();
                    totalDuration           += processTime;
                    perf.TotalDuration       = totalDuration;
                    noOfRuns++;
                    perf.NoOfRuns    = noOfRuns;
                    perf.AvgDuration = totalDuration / noOfRuns;
                    await DbOperations.UpdatePerformanceAsync(perf);
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                    AddException(exc, "WatchTeamsAsync");
                }
            }
        }