Exemple #1
0
        public List <UserActivityDTO> GetUserActivity(ActivityDTO activity)
        {
            List <UserActivityDTO> user_activities = new List <UserActivityDTO>();

            var activity_records = dc.ExecuteQuery <UserActivityDTO>(activity.settings["Query"].ToString()).ToList();

            foreach (var activity_record in activity_records)
            {
                UserActivityDTO user_activity = user_activities.Where(i => i.user_id == activity_record.user_id && i.date == activity_record.date.Date).SingleOrDefault();
                if (user_activity == null)
                {
                    user_activity = new UserActivityDTO()
                    {
                        user_id         = activity_record.user_id,
                        activity_id     = activity_record.activity_id,
                        count           = 0,
                        created_on_date = DateTime.Now,
                        date            = activity_record.date.Date
                    };
                    user_activities.Add(user_activity);
                }
                user_activity.count++;
            }
            return(user_activities.ToList());
        }
Exemple #2
0
        public UserActivityDTO ConvertItemToDto(CommunityMetrics_UserActivity item)
        {
            UserActivityDTO dto = new UserActivityDTO();

            dto.id              = item.id;
            dto.activity_id     = item.activity_id;
            dto.user_id         = item.user_id;
            dto.date            = item.date;
            dto.count           = item.count;
            dto.notes           = item.notes;
            dto.created_on_date = item.created_on_date;

            if (!string.IsNullOrWhiteSpace(item.User.DisplayName))
            {
                dto.user_name = item.User.DisplayName;
            }
            else
            {
                dto.user_name = item.User.FirstName + " " + item.User.LastName;
            }

            dto.activity_name = item.CommunityMetrics_Activity.name;

            return(dto);
        }
Exemple #3
0
        public HttpResponseMessage Get(Nullable <int> user_id, Nullable <int> activity_id, Nullable <DateTime> period_start, Nullable <DateTime> period_end, Nullable <int> skip = null, Nullable <int> take = null)
        {
            try
            {
                var query = dc.CommunityMetrics_UserActivities.Where(i => i.CommunityMetrics_Activity.active == true);

                // user_id?
                if (user_id.HasValue)
                {
                    query = query.Where(i => i.user_id == user_id.GetValueOrDefault());
                }

                // activity_id?
                if (activity_id.HasValue)
                {
                    query = query.Where(i => i.activity_id == activity_id.GetValueOrDefault());
                }

                // period_start?
                if (period_start.HasValue)
                {
                    query = query.Where(i => i.date >= period_start.GetValueOrDefault());
                }

                // period_end?
                if (period_end.HasValue)
                {
                    query = query.Where(i => i.date <= period_end.GetValueOrDefault());
                }

                // skip?
                if (skip.HasValue)
                {
                    query = query.Skip(skip.GetValueOrDefault());
                }

                // take?
                if (take.HasValue)
                {
                    query = query.Take(take.GetValueOrDefault());
                }

                List <UserActivityDTO> dtos = new List <UserActivityDTO>();
                foreach (CommunityMetrics_UserActivity item in query)
                {
                    UserActivityDTO dto = ConvertItemToDto(item);
                    dtos.Add(dto);
                }

                return(Request.CreateResponse(HttpStatusCode.OK, dtos));
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        public List <UserActivityDTO> GetUserActivity(ActivityDTO activity)
        {
            List <UserActivityDTO> user_activities = new List <UserActivityDTO>();

            var user_profiles = dc.UserProfiles.Where(i => i.ProfilePropertyDefinition.PropertyName == activity.settings["Profile"].ToString()).Select(i => new { user_id = i.UserID, gitHub_login = i.PropertyValue }).ToList();

            GitHubClient gitHubClient = new GitHubClient(new ProductHeaderValue("Dnn.CommunityActivity"));

            gitHubClient.Credentials = new Credentials(activity.settings["Credentials"].ToString());

            List <Repository> repositories = new List <Repository>();

            var totalCount = int.MaxValue;
            var page       = 1;

            // get a list of all the repos matching the search criteria
            while (repositories.Count() < totalCount)
            {
                var request = new SearchRepositoriesRequest(activity.settings["Query"].ToString())
                {
                    Page = page,
                };
                var result = gitHubClient.Search.SearchRepo(request).Result;
                totalCount = result.TotalCount;
                repositories.AddRange(result.Items);
                page++;
            }

            foreach (Repository repository in repositories)
            {
                IReadOnlyList <Octokit.User> users = gitHubClient.Activity.Starring.GetAllStargazers(repository.Id).Result;
                foreach (Octokit.User user in users)
                {
                    var user_profile = dc.UserProfiles.Where(i => i.ProfilePropertyDefinition.PropertyName == activity.settings["Profile"].ToString() && i.PropertyValue == user.Login).SingleOrDefault();

                    if (user_profile != null)
                    {
                        var user_activity = user_activities.Where(i => i.user_id == user_profile.UserID && i.activity_id == activity.id).Single();
                        if (user_activity == null)
                        {
                            user_activity = new UserActivityDTO()
                            {
                                user_id         = user_profile.UserID,
                                activity_id     = activity.id,
                                count           = 0,
                                created_on_date = DateTime.Now,
                                date            = DateTime.Now
                            };
                            user_activities.Add(user_activity);
                        }
                        user_activity.count++;
                    }
                }
            }
            return(user_activities);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="objActivity"></param>
        /// <param name="ExecutionDate"></param>
        /// <returns></returns>
        public List <UserActivityDTO> GetUserActivity(ActivityDTO activity)
        {
            List <UserActivityDTO> user_activities = new List <UserActivityDTO>();

            TwitterAPI api = new TwitterAPI(
                activity.settings["Access Token"].ToString(),
                activity.settings["Access Secret"].ToString(),
                activity.settings["Consumer Key"].ToString(),
                activity.settings["Consumer Secret"].ToString());

            foreach (JSONObject json in api.Get("search/tweets.json", new Parameters {
                { "q", activity.settings["Query"].ToString() }
            }))
            {
                foreach (JSONObject status in json.GetList <JSONObject>("statuses"))
                {
                    string ScreenName   = status.Get("user.screen_name").ToString();
                    var    user_profile = dc.UserProfiles.Where(i => i.ProfilePropertyDefinition.PropertyName == activity.settings["Profile"].ToString() && i.PropertyValue == ScreenName).SingleOrDefault();

                    if (user_profile != null) // tweet was made by a community member
                    {
                        Nullable <DateTime> last_activity_date = dc.CommunityMetrics_UserActivities.Where(i => i.user_id == user_profile.UserID && i.activity_id == activity.id).OrderByDescending(i => i.date).Select(i => i.date).FirstOrDefault();

                        DateTime CreatedDate = DateTime.ParseExact(status.Get("created_at").ToString(), "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture).Date;

                        if (CreatedDate > last_activity_date.GetValueOrDefault() && CreatedDate < DateTime.Now.Date)
                        {
                            // record the points

                            var user_activity = user_activities.Where(i => i.user_id == user_profile.UserID && i.date == CreatedDate).SingleOrDefault();

                            if (user_activity == null)
                            {
                                user_activity = new UserActivityDTO()
                                {
                                    user_id     = user_profile.UserID,
                                    activity_id = activity.id,
                                    count       = 0,
                                    date        = CreatedDate
                                };
                                user_activities.Add(user_activity);
                            }
                            user_activity.count++;
                        }
                    }
                }
            }
            return(user_activities);
        }
Exemple #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        public List <UserActivityDTO> GetUserActivity(ActivityDTO activity)
        {
            Dictionary <int, int> arrUsers = new Dictionary <int, int>();

            // returns the top 20 users in the past month who have used a specified tag in their answers on StackOverflow
            var apiUrl = ("http://api.stackexchange.com/2.2/tags/" + activity.settings["Tag"].ToString() + "/top-answerers/month?site=stackoverflow");

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(apiUrl);

            httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            httpWebRequest.Method = "GET";
            var    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string responseText;

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                responseText = streamReader.ReadToEnd();
            }
            var result = (Result) new JavaScriptSerializer().Deserialize(responseText, typeof(Result));

            foreach (TagScore item in result.items)
            {
                var user_profile = dc.UserProfiles.Where(i => i.ProfilePropertyDefinition.PropertyName == activity.settings["Profile"].ToString() && i.PropertyValue == item.user.user_id.ToString()).SingleOrDefault();

                if (user_profile != null)
                {
                    arrUsers.Add(user_profile.UserID, item.post_count);
                }
            }

            List <UserActivityDTO> user_activities = new List <UserActivityDTO>();

            foreach (KeyValuePair <int, int> kvp in arrUsers)
            {
                UserActivityDTO user_activity = new UserActivityDTO()
                {
                    user_id     = kvp.Key,
                    count       = kvp.Value,
                    activity_id = activity.id,
                    date        = DateTime.Now
                };
                user_activities.Add(user_activity);
            }
            return(user_activities);
        }
Exemple #7
0
        public HttpResponseMessage Post(UserActivityDTO dto)
        {
            try
            {
                CommunityMetrics_UserActivity item = ConvertDtoToItem(null, dto);

                item.created_on_date = DateTime.Now;

                dc.CommunityMetrics_UserActivities.InsertOnSubmit(item);
                dc.SubmitChanges();

                return(Request.CreateResponse(HttpStatusCode.OK, ConvertItemToDto(item)));
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Exemple #8
0
        public void SaveUserActivity(UserActivityDTO dto)
        {
            // prevents saving of duplicate activity
            CommunityMetrics_UserActivity user_activity = dc.CommunityMetrics_UserActivities.Where(i => i.activity_id == dto.activity_id && i.user_id == dto.user_id && i.date == dto.date).SingleOrDefault();

            if (user_activity == null)
            {
                user_activity = new CommunityMetrics_UserActivity()
                {
                    activity_id = dto.activity_id,
                    user_id     = dto.user_id
                };
                dc.CommunityMetrics_UserActivities.InsertOnSubmit(user_activity);
            }

            user_activity.count           = dto.count;
            user_activity.notes           = dto.notes;
            user_activity.date            = dto.date;
            user_activity.created_on_date = DateTime.Now;

            dc.SubmitChanges();
        }
Exemple #9
0
        public HttpResponseMessage Put(UserActivityDTO dto)
        {
            try
            {
                CommunityMetrics_UserActivity item = dc.CommunityMetrics_UserActivities.Where(i => i.id == dto.id).SingleOrDefault();

                if (item == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                item = ConvertDtoToItem(item, dto);

                dc.SubmitChanges();

                return(Request.CreateResponse(HttpStatusCode.OK, ConvertItemToDto(item))); // send back the updated record
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        public List <UserActivityDTO> GetUserActivity(ActivityDTO activity)
        {
            List <UserActivityDTO> user_activities = new List <UserActivityDTO>();

            var user_profiles = dc.UserProfiles.Where(i => i.ProfilePropertyDefinition.PropertyName == activity.settings["Profile"].ToString()).Select(i => new { user_id = i.UserID, gitHub_login = i.PropertyValue }).ToList();

            GitHubClient gitHubClient = new GitHubClient(new ProductHeaderValue("Dnn.CommunityActivity"));

            gitHubClient.Credentials = new Credentials(activity.settings["Credentials"].ToString());

            RepositoryIssueRequest objOptions = new RepositoryIssueRequest();

            objOptions.Filter = IssueFilter.All;
            objOptions.State  = ItemStateFilter.All;

            List <Repository> repositories = new List <Repository>();

            var totalCount = int.MaxValue;
            var page       = 1;

            // get a list of all the repos matching the search criteria
            while (repositories.Count() < totalCount)
            {
                var request = new SearchRepositoriesRequest(activity.settings["Query"].ToString())
                {
                    Page = page,
                };
                var result = gitHubClient.Search.SearchRepo(request).Result;
                totalCount = result.TotalCount;
                repositories.AddRange(result.Items);
                page++;
            }

            foreach (Repository repository in repositories)
            {
                IReadOnlyList <Issue> issues = gitHubClient.Issue.GetAllForRepository(repository.Id, objOptions).Result;
                if (issues.Any())
                {
                    foreach (var user_profile in user_profiles)
                    {
                        Nullable <DateTime> last_activity_date = dc.CommunityMetrics_UserActivities.Where(i => i.user_id == user_profile.user_id && i.activity_id == activity.id).OrderByDescending(i => i.date).Select(i => i.date).FirstOrDefault();

                        var recent_issues = issues
                                            .Where(i =>
                                                   i.User != null &&
                                                   i.User.Login == user_profile.gitHub_login &&
                                                   i.CreatedAt.Date > last_activity_date.GetValueOrDefault() &&
                                                   i.CreatedAt.Date < DateTime.Now.Date
                                                   );

                        foreach (Issue issue in recent_issues)
                        {
                            if (issue.PullRequest == null) // exclude issues which are automatically generated for pull requests
                            {
                                var user_activity = user_activities.Where(i => i.user_id == user_profile.user_id && i.date == issue.CreatedAt.Date).SingleOrDefault();

                                if (user_activity == null)
                                {
                                    user_activity = new UserActivityDTO()
                                    {
                                        user_id     = user_profile.user_id,
                                        activity_id = activity.id,
                                        count       = 0,
                                        date        = issue.CreatedAt.Date
                                    };
                                    user_activities.Add(user_activity);
                                }
                                user_activity.count++;
                            }
                        }
                    }
                }
            }
            return(user_activities);
        }
Exemple #11
0
        public List <UserActivityDTO> GetUserActivity(ActivityDTO activity)
        {
            List <UserActivityDTO> user_activities = new List <UserActivityDTO>();

            var user_profiles = dc.UserProfiles.Where(i => i.ProfilePropertyDefinition.PropertyName == activity.settings["Profile"].ToString()).Select(i => new { user_id = i.UserID, gitHub_login = i.PropertyValue }).ToList();

            GitHubClient gitHubClient = new GitHubClient(new ProductHeaderValue("Dnn.CommunityActivity"));

            gitHubClient.Credentials = new Credentials(activity.settings["Credentials"].ToString());

            List <Repository> repositories = new List <Repository>();

            var totalCount = int.MaxValue;
            var page       = 1;

            // get a list of all the repos matching the search criteria
            while (repositories.Count() < totalCount)
            {
                var request = new SearchRepositoriesRequest(activity.settings["Query"].ToString())
                {
                    Page = page,
                };
                var result = gitHubClient.Search.SearchRepo(request).Result;
                totalCount = result.TotalCount;
                repositories.AddRange(result.Items);
                page++;
            }

            foreach (Repository repository in repositories)
            {
                IReadOnlyList <GitHubCommit> commits = new List <GitHubCommit>();
                try
                {
                    var commitRequest = new CommitRequest()
                    {
                        Since = DateTime.Now.AddYears(-1) // only scan the last year of activity, otherwise too much data
                    };
                    commits = gitHubClient.Repository.Commit.GetAll(repository.Id, commitRequest).Result;
                }
                catch { };

                foreach (var user_profile in user_profiles)
                {
                    Nullable <DateTime> last_activity_date = dc.CommunityMetrics_UserActivities.Where(i => i.user_id == user_profile.user_id && i.activity_id == activity.id).OrderByDescending(i => i.date).Select(i => i.date).FirstOrDefault();

                    if (commits != null && commits.Count > 0)
                    {
                        //Debug.WriteLine("\n Commits: " + commits.Count.ToString());

                        var recent_commits = commits
                                             .Where(i =>
                                                    i.Author != null &&
                                                    i.Commit != null &&
                                                    i.Commit.Author != null &&
                                                    i.Author.Login == user_profile.gitHub_login &&
                                                    i.Commit.Author.Date > last_activity_date.GetValueOrDefault().Date&&
                                                    i.Commit.Author.Date < DateTime.Now.Date
                                                    )
                                             .ToList();

                        foreach (var commit in recent_commits)
                        {
                            var user_activity = user_activities.Where(i => i.user_id == user_profile.user_id && i.date == commit.Commit.Author.Date.Date).SingleOrDefault();

                            if (user_activity == null)
                            {
                                user_activity = new UserActivityDTO()
                                {
                                    user_id     = user_profile.user_id,
                                    activity_id = activity.id,
                                    count       = 0,
                                    date        = commit.Commit.Author.Date.DateTime
                                };
                                user_activities.Add(user_activity);
                            }

                            user_activity.count++;
                        }
                    }
                }
            }
            return(user_activities);
        }
Exemple #12
0
        public CommunityMetrics_UserActivity ConvertDtoToItem(CommunityMetrics_UserActivity item, UserActivityDTO dto)
        {
            if (item == null)
            {
                item = new CommunityMetrics_UserActivity();
            }

            if (dto == null)
            {
                return(item);
            }

            item.id              = dto.id;
            item.activity_id     = dto.activity_id;
            item.user_id         = dto.user_id;
            item.date            = dto.date;
            item.count           = dto.count;
            item.notes           = dto.notes;
            item.created_on_date = dto.created_on_date;

            return(item);
        }