public static void AnalyzePostTimes(string storiesFilePath, string postTimeStatsFilePath)
        {
            //Get posts and convert to date
            var posts = JsonNetUtils.DeserializeSequenceFromJson <JObject>(storiesFilePath)
                        .SelectMany(responseJson => responseJson["hits"])
                        .Select(hitJson => new StoryRating
            {
                CreatedOn = Utils.FromUnixTime(hitJson["created_at_i"].Value <long>()).ToLocalTime(),
                Points    = hitJson["points"].Value <int>(),
                Comments  = hitJson["num_comments"].Value <int>()
            }).ToList();

            var postsPerMinutes = posts
                                  .GroupBy(post => post.CreatedOn.ToString("ddd hh:mm tt"))
                                  .OrderBy(g => g.Key);

            using (var outputFile = File.CreateText(postTimeStatsFilePath))
            {
                outputFile.WriteLine("Time\tPoints\tComments");
                foreach (var postsPerMinute in postsPerMinutes)
                {
                    outputFile.WriteLine("{0}\t{1}\t{2}".FormatEx(postsPerMinute.Key,
                                                                  postsPerMinute.Select(p => p.Points.ToStringInvariant()).ToDelimitedString(","),
                                                                  postsPerMinute.Select(p => p.Comments.ToStringInvariant()).ToDelimitedString(",")
                                                                  ));
                }
            }
        }
Esempio n. 2
0
        private void buttongetStats_Click(object sender, EventArgs e)
        {
            foreach (var filePath in new[] { textBoxStoriesFilePath.Text, textBoxCommentsFilePath.Text })
            {
                var createDatesUnix = JsonNetUtils.DeserializeSequenceFromJson <JObject>(filePath)
                                      .SelectMany(responseJson => responseJson["hits"])
                                      .Select(hitJson => hitJson["created_at_i"].Value <long>());

                long minDateUnix = long.MaxValue, maxDateUnix = long.MinValue, count = 0;
                foreach (var createDateUnix in createDatesUnix)
                {
                    minDateUnix = Math.Min(minDateUnix, createDateUnix);
                    maxDateUnix = Math.Max(maxDateUnix, createDateUnix);
                    count++;
                }

                MessageBox.Show("{3}\n{0} items from {1} to {2}".FormatEx(count, Utils.FromUnixTime(minDateUnix).ToString("r"), Utils.FromUnixTime(maxDateUnix).ToString("r"), filePath));
            }
        }
Esempio n. 3
0
        private void buttonAnalyzeUniqueUrls_Click(object sender, EventArgs e)
        {
            var storyUrlsStats = new Dictionary <string, StoryUrlStats>();
            int storyCount = 0, blankUrls = 0, badUrls = 0;

            //Consider stories upto 3 years old for this analysis
            var minUnixDate = DateTime.UtcNow.Subtract(TimeSpan.FromDays(365 * 3)).ToUnixTime();

            foreach (var responseJson in JsonNetUtils.DeserializeSequenceFromJson <JObject>(textBoxStoriesFilePath.Text))
            {
                try
                {
                    foreach (var hitJson in responseJson["hits"])
                    {
                        storyCount++;

                        if (hitJson["created_at_i"].Value <long>() < minUnixDate)
                        {
                            goto endFileReading;
                        }

                        var urlString = hitJson["url"].ToString();
                        if (string.IsNullOrWhiteSpace(urlString))
                        {
                            blankUrls++;
                            continue;
                        }
                        try
                        {
                            var uri   = new Uri(urlString);
                            var host  = uri.Host;
                            var stats = storyUrlsStats.AddOrGetValue(host, () => StoryUrlStats.Create(urlString));
                            stats.AddStory(hitJson);
                        }
                        catch (UriFormatException)
                        {
                            badUrls++;
                        }
                    }
                }
                catch {}
            }
endFileReading:

            Task.WaitAll(storyUrlsStats.Values.Select(s => s.AnalyzeWpTask).ToArray());

            using (var urlStatsFile = File.CreateText(textBoxUrlAnalysisFilePath.Text))
            {
                foreach (var urlStatsKvp in storyUrlsStats)
                {
                    if (string.IsNullOrWhiteSpace(urlStatsKvp.Value.ThemeNames) && string.IsNullOrWhiteSpace(urlStatsKvp.Value.PluginNames))
                    {
                        continue;
                    }

                    //Tab seperated Column Header
                    //Host	URLSample	PointsSum	ThemeNames	PluginNames	StoryCount	CommentsSum	MaxDate	MinDate
                    var values = new string[] { urlStatsKvp.Key, urlStatsKvp.Value.Url, urlStatsKvp.Value.PointsSum.ToStringInvariant(),
                                                urlStatsKvp.Value.ThemeNames, urlStatsKvp.Value.PluginNames,
                                                urlStatsKvp.Value.StoryCount.ToStringInvariant(), urlStatsKvp.Value.CommentsSum.ToStringInvariant(),
                                                urlStatsKvp.Value.CreatedUnixTimeMax.ToString(), urlStatsKvp.Value.CreatedUnixTimeMin.ToString() };

                    urlStatsFile.WriteLine(values.ToDelimitedString("\t"));
                }
            }

            MessageBox.Show("Total: {0}, Blank: {1}, Bad: {2}".FormatEx(storyCount, blankUrls, badUrls));
        }