<commit_before>﻿#r "..\Common\PppPool.Common.dll"
#r "..\Common\Microsoft.WindowsAzure.Storage.dll"
#r "System.Xml.Linq"
#r "Newtonsoft.Json"

using System;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using PppPool.Common;
using System.Dynamic;

public static async Task Run(TimerInfo timer, TraceWriter log)
{
    var start = DateTime.UtcNow;
    var connectionString = "StatsStorage".GetEnvVar();
    var blobService = new BlobService(connectionString);

    var tour = "PGA TOUR";
    var tournamentsUrl = "TournamentsUrl".GetEnvVar();
    var seasonData = await RestService.AuthorizedPostAsync($"{tournamentsUrl}/api/Season", null, "ServiceToken".GetEnvVar());
    var season = (int)seasonData["Season"];
    var week = (int)seasonData["CurrentWeek"];

    // We only need previous or current tournaments. Not picking either because nothing has happened.
    var tournaments = await RestService.AuthorizedPostAsync($"{tournamentsUrl}/api/GetTournaments", new Dictionary<string, string>
    {
        ["season"] = "current",
        ["tour"] = tour,
        ["key"] = "state",
        ["value"] = "dequeued,completed",
    }, "ServiceToken".GetEnvVar());

    dynamic profiles = await GetProfiles();
    JArray pooliesStatsFile = new JArray();
    JArray monthsStatsFile = new JArray();

    foreach (var profile in profiles)
    {
        var nameSplit = ((string)profile["Name"]).Split(new[] { ' ' });
        var lastFirst = nameSplit.Last() + ", " + nameSplit.First();
        dynamic profileStats = JObject.FromObject(new
        {
            UserId = profile.UserId,
            Name = profile.Name,
            Email = profile.Email,
            LastFirst = lastFirst,
            Points = new JArray(),
        });
        pooliesStatsFile.Add(profileStats);
    }

    // get Months that will be calculated (current and previous)
    for (int month = 1; month <= DateTime.UtcNow.Month; month++)
    {
        var monthTournaments = tournaments.Where(x => ((DateTime)x["End"]).Month == month);

        List<JObject> statsFiles = new List<JObject>();
        foreach (var tournament in monthTournaments)
        {
            var stats = await blobService.DownloadBlobAsync("tournament", $"{season}/{tour}/{tournament["Index"]}.json");
            statsFiles.Add(JObject.Parse(stats));
        }

        var monthArray = new JArray();

        foreach (var poolieStats in pooliesStatsFile)
        {
            double monthPoints = 0.0d;
            foreach (var statFile in statsFiles)
            {
                var poolie = ((JArray)statFile["Poolies"]).FirstOrDefault(x => (string)x["UserId"] == (string)poolieStats["UserId"]);
                if(poolie != null)
                    monthPoints += (double)poolie["Points"];
            }
            ((JArray)poolieStats["Points"]).Add(monthPoints);

            var nameSplit = ((string)poolieStats["Name"]).Split(new[] { ' ' });
            var lastFirst = nameSplit.Last() + ", " + nameSplit.First();
            monthArray.Add(JObject.FromObject(new
            {
                UserId = (string)poolieStats["UserId"],
                Name = (string)poolieStats["Name"],
                Email = (string)poolieStats["Email"],<commit_msg>Monthly stats now includes "projectedPoints" for the current month. Also removed test accounts.<commit_after>﻿#r "..\Common\PppPool.Common.dll"
#r "..\Common\Microsoft.WindowsAzure.Storage.dll"
#r "System.Xml.Linq"
#r "Newtonsoft.Json"

using System;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using PppPool.Common;
using System.Dynamic;

public static async Task Run(TimerInfo timer, TraceWriter log)
{
    var start = DateTime.UtcNow;
    var connectionString = "StatsStorage".GetEnvVar();
    var blobService = new BlobService(connectionString);

    var tour = "PGA TOUR";
    var tournamentsUrl = "TournamentsUrl".GetEnvVar();
    var seasonData = await RestService.AuthorizedPostAsync($"{tournamentsUrl}/api/Season", null, "ServiceToken".GetEnvVar());
    var season = (int)seasonData["Season"];
    var week = (int)seasonData["CurrentWeek"];

    // We only need previous or current tournaments. Not picking either because nothing has happened.
    var tournaments = await RestService.AuthorizedPostAsync($"{tournamentsUrl}/api/GetTournaments", new Dictionary<string, string>
    {
        ["season"] = "current",
        ["tour"] = tour,
        ["key"] = "state",
        ["value"] = "dequeued,completed,progressing",
    }, "ServiceToken".GetEnvVar());

    dynamic profiles = await GetProfiles();
    JArray pooliesStatsFile = new JArray();
    JArray monthsStatsFile = new JArray();

    foreach (var profile in profiles)
    {
        if (profile.isTest != null)
            continue;
        var nameSplit = ((string)profile["Name"]).Split(new[] { ' ' });
        var lastFirst = nameSplit.Last() + ", " + nameSplit.First();
        dynamic profileStats = JObject.FromObject(new
        {
            UserId = profile.UserId,
            Name = profile.Name,
            Email = profile.Email,
            LastFirst = lastFirst,
            Points = new JArray(),
        });
        pooliesStatsFile.Add(profileStats);
    }

    // get Months that will be calculated (current and previous)
    for (int month = 1; month <= DateTime.UtcNow.Month; month++)
    {
        var monthTournaments = tournaments.Where(x => ((DateTime)x["End"]).Month == month);

        List<JObject> statsFiles = new List<JObject>();
        foreach (var tournament in monthTournaments)
        {
            var stats = await blobService.DownloadBlobAsync("tournament", $"{season}/{tour}/{tournament["Index"]}.json");
            statsFiles.Add(JObject.Parse(stats));
        }

        var monthArray = new JArray();
        foreach (var poolieStats in pooliesStatsFile)
        {
            double monthPoints = 0.0d;
            double ytdPoints = 0.0d;
            foreach (var statFile in statsFiles)
            {
                var poolie = ((JArray)statFile["Poolies"]).FirstOrDefault(x => (string)x["UserId"] == (string)poolieStats["UserId"]);
                if (poolie != null)
                {
                    ytdPoints = (double)poolie["YtdPoints"];
                    if (((string)statFile["State"]) == "progressing")
                        poolieStats["projectedPoints"] = ytdPoints + (double)poolie["Points"];
                }
            }
            ((JArray)poolieStats["Points"]).Add(ytdPoints);


            var nameSplit = ((string)poolieStats["Name"]).Split(new[] { ' ' });
            var lastFirst = nameSplit.Last() + ", " + nameSplit.First();
            monthArray.Add(JObject.FromObject(new
            {
                UserId = (string)poolieStats["UserId"],
                Name = (string)poolieStats["Name"],
                Email = (string)poolieStats["Email"],
{'fraction_non_alphanumeric': 0.1336573511543135, 'fraction_numerical': 0.0010800594032671796, 'mean_word_length': 2.7718940936863543, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3721492', 'n_tokens_mistral': 2121, 'n_tokens_neox': 2055, 'n_words': 467}