Exemplo n.º 1
0
 public async Task <HttpResponseMessage> GetRepos([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ExecutionContext context, ILogger log)
 {
     return(new HttpResponseMessage(HttpStatusCode.OK)
     {
         Content = new StringContent(await blobService.GetRepos(), Encoding.UTF8, "application/json")
     });
 }
Exemplo n.º 2
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");
            }
        }