Esempio n. 1
0
        public static List <KeyValuePair <Script, int> > getpopular(int amount)
        {
            List <KeyValuePair <Script, int> > popular = new List <KeyValuePair <Script, int> >();
            DateTime timestamp = DateTime.Now.ToUniversalTime();
            string   fileID    = GetFileIDForTimestamp(timestamp);


            if (timestamp.Subtract(StartTime).TotalMinutes < 60)
            {
                YAMLConfiguration log = getlog(fileID);
                foreach (Script script in ScriptTable)
                {
                    int servers = log.GetKeys(timestamp.AddHours(-1).Hour + "." + script.ID).Count;
                    popular.Add(new KeyValuePair <Script, int>(script, servers));
                }
            }
            else
            {
                foreach (Script script in ScriptTable)
                {
                    int servers = GetRTservers(script);
                    popular.Add(new KeyValuePair <Script, int>(script, servers));
                }
            }

            popular.Sort((one, two) => two.Value.CompareTo(one.Value));
            sortedscripts = popular;
            return(popular.GetRange(0, amount > popular.Count ? popular.Count : amount));
        }
Esempio n. 2
0
        public static void HandleTrackerInput(HttpListenerContext request)
        {
            if (string.IsNullOrWhiteSpace(request.Request.QueryString["script"]))
            {
                byte[] data = Encoding.UTF8.GetBytes("FAILURE! No script specified!");
                request.Response.OutputStream.Write(data, 0, data.Length);
                Console.WriteLine("REFUSED data for script: unknown, reason: no script argument.");
                return;
            }
            int    ID      = int.Parse(request.Request.QueryString["script"]);
            string address = request.Request.Headers["X-Forwarded-For"].ToString().Replace(".", "-");
            Script script  = GetScript(ID);

            if (script == null)
            {
                byte[] data = Encoding.UTF8.GetBytes("FAILURE! This script ID does not match our database.");
                request.Response.OutputStream.Write(data, 0, data.Length);
                Console.WriteLine("REFUSED data for script: unknown, reason: inexisting script ID.");
                return;
            }
            if (!script.FloodControl.ContainsKey(address))
            {
                script.FloodControl[address] = new KeyValuePair <int, DateTime>(0, DateTime.Now);
            }
            else if ((script.FloodControl[address].Key > 5) && (DateTime.Now.Subtract(script.FloodControl[address].Value).TotalMinutes < 10))
            {
                byte[] data = Encoding.UTF8.GetBytes("FAILURE! don't force feed me!");
                request.Response.OutputStream.Write(data, 0, data.Length);
                Console.WriteLine("REFUSED data for script: " + script.ID + ", reason: spam prevention.");
                return;
            }
            else if (DateTime.Now.Subtract(script.FloodControl[address].Value).TotalMinutes > 10)
            {
                script.FloodControl[address] = new KeyValuePair <int, DateTime>(0, DateTime.Now);
            }
            script.FloodControl[address] = new KeyValuePair <int, DateTime>(script.FloodControl[address].Key + 1, DateTime.Now);
            DateTime          timestamp = DateTime.Now.ToUniversalTime().AddMinutes(30);
            string            fileID    = GetFileIDForTimestamp(timestamp);
            YAMLConfiguration log       = getlog(fileID);

            Console.WriteLine("Recieved data for script: " + script.ID);
            foreach (string queryKey in request.Request.QueryString.Keys)
            {
                log.Set(timestamp.Hour + "." + script.ID + "." + address + "-" + timestamp.Ticks.ToString() + "." + queryKey.ToLowerFast().Replace('.', '_'), request.Request.QueryString[queryKey].ToLowerFast());
            }
            Directory.CreateDirectory("logs/");
            File.WriteAllText("logs/" + fileID + ".yml", log.SaveToString());
            byte[] data2 = Encoding.UTF8.GetBytes("SUCCESS! We successfully recieved your data. Thank your for your contribution.");
            request.Response.OutputStream.Write(data2, 0, data2.Length);
            AddRTTrackingInput(script);
        }
Esempio n. 3
0
        public static YAMLConfiguration getlog(string fileID)
        {
            YAMLConfiguration log;

            if (!LoadedLogs.ContainsKey(fileID))
            {
                if (File.Exists("logs/" + fileID + ".yml"))
                {
                    log = new YAMLConfiguration(File.ReadAllText("logs/" + fileID + ".yml"));
                }
                else
                {
                    LoadedLogs[fileID] = log = new YAMLConfiguration("");
                }
            }
            else
            {
                log = LoadedLogs[fileID];
            }
            return(log);
        }
Esempio n. 4
0
        public static void getPopularGraph(HttpListenerContext request)
        {
            DateTime timestamp = DateTime.Now.ToUniversalTime();
            Dictionary <string, List <int> > graphvalues = new Dictionary <string, List <int> >();
            List <string> labels  = new List <string>();
            int           highest = 10;
            int           days    = 10;
            List <Script> scripts = new List <Script>();

            foreach (KeyValuePair <Script, int> scriptvalue in Program.getpopular(10))
            {
                scripts.Add(scriptvalue.Key);
            }
            for (int i = days - 1; i >= 0; i--)
            {
                YAMLConfiguration file = Program.getlog(Program.GetFileIDForTimestamp(timestamp.AddDays(i * -1)));
                labels.Add(timestamp.AddDays(i * -1).Day + "/" + timestamp.AddDays(i * -1).Month);
                for (int y = 0; y < 24; y++)
                {
                    foreach (Script script in scripts)
                    {
                        int amount = file.GetKeys(y + "." + script.ID).Count;
                        if (!graphvalues.ContainsKey(script.Name))
                        {
                            graphvalues.Add(script.Name, new List <int>());
                        }
                        graphvalues[script.Name].Add(amount);
                        if (highest < amount)
                        {
                            highest = amount;
                        }
                    }
                }
            }
            byte[] output = GraphRenderer.MultiLineGraph("popular", graphvalues, highest + 10, Math.Ceiling(highest / 10.0), days, 24, labels);
            request.Response.OutputStream.Write(output, 0, output.Length);
        }
Esempio n. 5
0
        public static byte[] GetGraph(Script script, int days, string data, string datavalue, ModeEnum mode)
        {
            data      = data.ToLowerFast();
            datavalue = datavalue?.ToLowerFast();
            DateTime timestamp = DateTime.Now.ToUniversalTime();

            switch (mode)
            {
            case ModeEnum.ADD:
            case ModeEnum.COUNT:
            case ModeEnum.AVERAGE:

            {
                List <double> graphvalues = new List <double>();
                List <string> labels      = new List <string>();
                int           highest     = 10;
                for (int i = days - 1; i >= 0; i--)
                {
                    YAMLConfiguration file = Program.getlog(Program.GetFileIDForTimestamp(timestamp.AddDays(i * -1)));
                    labels.Add(timestamp.AddDays(i * -1).Day + "/" + timestamp.AddDays(i * -1).Month);
                    for (int y = 0; y < 24; y++)
                    {
                        if (data == "servers")
                        {
                            int amount = file.GetKeys(y + "." + script.ID).Count;
                            graphvalues.Add(amount);
                            if (highest < amount)
                            {
                                highest = amount;
                            }
                        }
                        else
                        {
                            switch (mode)
                            {
                            case ModeEnum.ADD:
                                double count = 0;
                                foreach (string server in file.GetKeys(y + "." + script.ID))
                                {
                                    count += file.ReadDouble(y + "." + script.ID + "." + server + "." + data, 0);
                                }
                                if (highest < count)
                                {
                                    highest = Convert.ToInt32(Math.Ceiling(count));
                                }
                                graphvalues.Add(count);
                                break;

                            case ModeEnum.COUNT:
                                int count2 = 0;
                                foreach (string server in file.GetKeys(y + "." + script.ID))
                                {
                                    if (file.ReadString(y + "." + script.ID + "." + server + "." + data, "").ToLowerFast() == datavalue)
                                    {
                                        count2++;
                                    }
                                }
                                if (highest < count2)
                                {
                                    highest = count2;
                                }
                                graphvalues.Add(count2);
                                break;

                            case ModeEnum.AVERAGE:
                                double        count3 = 0;
                                List <string> keys   = file.GetKeys(y + "." + script.ID);
                                foreach (string server in keys)
                                {
                                    count3 += file.ReadDouble(y + "." + script.ID + "." + server + "." + data, 0);
                                }
                                int keycount = keys.Count;
                                if (keycount < 1)
                                {
                                    keycount = 1;
                                }
                                count3 /= keycount;
                                if (highest < count3)
                                {
                                    highest = Convert.ToInt32(Math.Ceiling(count3));
                                }
                                graphvalues.Add(count3);
                                break;
                            }
                        }
                    }
                }
                return(GraphRenderer.BasicLineGraph(script.Name, graphvalues, highest, Math.Ceiling(highest / 10.0), days, 24, labels));
            }

            case (ModeEnum.MULTICOUNT):
            {
                Dictionary <string, List <int> > graphvalues = new Dictionary <string, List <int> >();
                List <string> labels    = new List <string>();
                int           highest   = 10;
                int           countsize = 0;
                for (int i = days - 1; i >= 0; i--)
                {
                    YAMLConfiguration file = Program.getlog(Program.GetFileIDForTimestamp(timestamp.AddDays(i * -1)));
                    labels.Add(timestamp.AddDays(i * -1).Day + "/" + timestamp.AddDays(i * -1).Month);
                    for (int y = 0; y < 24; y++)
                    {
                        Dictionary <string, int> loopvaluecount = new Dictionary <string, int>();
                        foreach (string server in file.GetKeys(y + "." + script.ID))
                        {
                            string currentvalue = file.ReadString(y + "." + script.ID + "." + server + "." + data, "").ToLowerFast();
                            int    count;
                            if (!string.IsNullOrWhiteSpace(currentvalue))
                            {
                                count = 1;
                            }
                            else
                            {
                                count = 0;
                            }
                            if (!loopvaluecount.ContainsKey(currentvalue))
                            {
                                loopvaluecount.Add(currentvalue, 0);
                            }
                            loopvaluecount[currentvalue] = loopvaluecount[currentvalue] + count;
                            if (!graphvalues.ContainsKey(currentvalue) && graphvalues.Keys.Count < 15 && !string.IsNullOrWhiteSpace(currentvalue))
                            {
                                graphvalues.Add(currentvalue, new List <int>());
                                for (int z = 0; z < countsize; z++)
                                {
                                    graphvalues[currentvalue].Add(0);
                                }
                            }
                        }
                        foreach (KeyValuePair <string, List <int> > currentvalue in graphvalues)
                        {
                            if (loopvaluecount.ContainsKey(currentvalue.Key))
                            {
                                graphvalues[currentvalue.Key].Add(loopvaluecount[currentvalue.Key]);
                                if (loopvaluecount[currentvalue.Key] > highest)
                                {
                                    highest = loopvaluecount[currentvalue.Key];
                                }
                            }
                            else
                            {
                                graphvalues[currentvalue.Key].Add(0);
                            }
                        }
                        countsize++;
                    }
                }
                return(GraphRenderer.MultiLineGraph(script.Name, graphvalues, highest, Math.Ceiling(highest / 10.0), days, 24, labels));
            }
            }
            return(null);
        }