public async Task <IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
        {
            try {
                NormalizeUserBeforeInsertOrUpdate(user);
                await userTable.InsertOrMergeEntityAsync(user);

                return(IdentityResult.Success);
            }
            catch (Exception ex) {
                return(IdentityResult.Failed(new IdentityError {
                    Code = ex.GetType().Name,
                    Description = ex.Message
                }));
            }
        }
        public async Task Run()
        {
            List <RepoStatEntity> stats = new List <RepoStatEntity>();
            var repos = await _tableService.GetActiveRepos("GitHubRepoStats");


            List <string> dateArray = new List <string>();

            for (DateTime date = DateTime.Now.AddDays(-10); date <= DateTime.Now; date = date.AddDays(1))
            {
                dateArray.Add(date.ToShortDateString());
            }


            //foreach (var repo in repos.Where(a => dateArray.Contains(a.Date)))
            foreach (var repo in repos)
            {
                try
                {
                    _gitHubService.AuthClient(repo.AccessToken);

                    var todayRepo = await _gitHubService.GetRepository(repo.RepoId);

                    var views = await _gitHubService.GetRepoViews(repo.RepoId);

                    var clones = await _gitHubService.GetRepoClones(repo.RepoId);

                    foreach (var view in views.Views)
                    {
                        var stat = new RepoStatEntity($"{repo.UserName}{repo.RepoName}", view.Timestamp.DateTime.ToShortDateString().Replace("/", ""))
                        {
                            AccessToken = repo.AccessToken,
                            RepoName    = repo.RepoName,
                            Date        = view.Timestamp.DateTime.ToShortDateString(),
                            RepoId      = repo.RepoId,
                            UserName    = repo.UserName,
                            SyncEnabled = repo.SyncEnabled,

                            Views       = view.Count,
                            UniqueUsers = view.Uniques
                        };

                        if (stat.Date == DateTime.Now.ToShortDateString())
                        {
                            stat.StarCount  = todayRepo.StargazersCount;
                            stat.ForksCount = todayRepo.ForksCount;
                        }
                        else
                        {
                            stat.ForksCount = repo.ForksCount;
                            stat.StarCount  = repo.StarCount;
                        }


                        var clone = clones.Clones.Where(a => a.Timestamp.DateTime.ToShortDateString() == view.Timestamp.DateTime.ToShortDateString()).FirstOrDefault();

                        if (clone != null)
                        {
                            stat.Clones       = clone.Count;
                            stat.UniqueClones = clone.Uniques;
                        }
                        stats.Add(stat);
                    }
                }
                catch (Exception e)
                {
                    var foo = e.Message;
                }
            }

            foreach (var view in stats)
            {
                await _tableService.InsertOrMergeEntityAsync(view, "GitHubRepoStats");
            }
        }
Ejemplo n.º 3
0
        public async Task Run()
        {
            var client = new GitHubClient(new ProductHeaderValue("TrackRepos"));

            var basicAuth = new Credentials(Environment.GetEnvironmentVariable("GitHubToken"));

            client.Credentials = basicAuth;

            string storageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");

            List <Campaign> campaigns = JsonConvert.DeserializeObject <List <Campaign> >(await _blobService.GetRepos());

            List <RepoStats> stats = new List <RepoStats>();

            foreach (Campaign campaign in campaigns)
            {
                try
                {
                    if (!string.IsNullOrEmpty(campaign.CampaignName) && !string.IsNullOrEmpty(campaign.OrgName))
                    {
                        foreach (Repo repo in campaign.Repos)
                        {
                            if (!string.IsNullOrEmpty(repo.RepoName))
                            {
                                var views = await client.Repository.Traffic.GetViews(campaign.OrgName, repo.RepoName, new RepositoryTrafficRequest(TrafficDayOrWeek.Day));

                                var clones = await client.Repository.Traffic.GetClones(campaign.OrgName, repo.RepoName, new RepositoryTrafficRequest(TrafficDayOrWeek.Day));

                                foreach (var item in views.Views)
                                {
                                    var stat = new RepoStats($"{campaign.CampaignName}{repo.RepoName}", item.Timestamp.UtcDateTime.ToShortDateString().Replace("/", ""))
                                    {
                                        OrgName      = campaign.OrgName,
                                        CampaignName = campaign.CampaignName,
                                        RepoName     = repo.RepoName,
                                        Date         = item.Timestamp.UtcDateTime.ToShortDateString(),
                                        Views        = item.Count,
                                        UniqueUsers  = item.Uniques
                                    };

                                    var clone = clones.Clones.Where(a => a.Timestamp.UtcDateTime.ToShortDateString() == item.Timestamp.UtcDateTime.ToShortDateString()).FirstOrDefault();

                                    if (clone != null)
                                    {
                                        stat.Clones       = clone.Count;
                                        stat.UniqueClones = clone.Uniques;
                                    }
                                    stats.Add(stat);
                                }
                                Thread.Sleep(5000);
                            }
                        }
                    }
                }
                catch (Exception e)
                { }
            }

            string     tableName = "RepoStats";
            CloudTable table     = await _tableService.CreateTableAsync(tableName);

            foreach (var view in stats)
            {
                Console.WriteLine("Insert an Entity.");
                await _tableService.InsertOrMergeEntityAsync(view, "RepoStats");
            }
        }