// API.cs // // Copyright (C) 2009 Bartek Jasicki // // This file is part of Grubng. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // /**@file Database.cs * Provide function for server API.*/ using System; namespace grubupload { /**Provide function for server API.*/ internal static class API { /// /// Function return status of server /// /// /// A True is is enough free disk space on server, otherwise false /// /// /// A True if server uploading .arc files to database. /// /// /// A Amount of connected clients to server. /// /// /// A Version of server /// /// /// A Full path to working directory /// /// /// A Status of server /// public static string Status(bool freespace, bool uploading, short connected, ref string version, ref string workpath) { System.Text.StringBuilder returnvalue = new System.Text.StringBuilder(); if (freespace && !uploading) { returnvalue.Append("OK;"); } if (freespace && uploading) { returnvalue.Append("Warning;"); } if (!freespace) { returnvalue.Append("Error;"); } returnvalue.Append(connected); returnvalue.Append(";"); returnvalue.Append(version); returnvalue.Append(";"); Mono.Unix.UnixDriveInfo drive = new Mono.Unix.UnixDriveInfo(workpath); returnvalue.Append(Math.Round((Convert.ToDouble(drive.AvailableFreeSpace) / Convert.ToDouble(drive.TotalSize)) * 100, 1)); drive = null; return returnvalue.ToString(); } /**Function read from database weekly stats for user. * @param username Username in database. * @param db Object to Database class. * @return string with weekly selected user stats.*/ public static string UserWeekly(string username, ref Database db) { string result = db.ReadDB("SELECT urls FROM weekly WHERE user='" + username + "'", 1); string[] urls = result.Split('\n'); int urlsum = 0; int arcs = 0; foreach (string url in urls) { if (String.IsNullOrEmpty(url.Trim())) { break; } urlsum += Convert.ToInt32(url.Trim()); arcs ++; } string startdate = String.Empty; string enddate = String.Empty; if (urlsum > 0) { startdate = db.ReadDB("SELECT MIN(senddate) FROM weekly WHERE user='" + username + "'", 1); startdate = DateTime.FromFileTime(Convert.ToInt64(startdate.Trim())).ToShortDateString(); enddate = db.ReadDB("SELECT MAX(senddate) FROM weekly WHERE user='" + username + "'", 1); enddate = DateTime.FromFileTime(Convert.ToInt64(enddate.Trim())).ToShortDateString(); return arcs.ToString() + "," + urlsum + "," + startdate + "," + enddate; } else { return "No user in database"; } } /**Function read from database stats for user. * @param username Username in database. * @param table Name of table with stats. * @param db Object to Database class. * @return string with selected user stats.*/ public static string UserStats(string username, string year, string month, string table, ref Database db) { string result; if (table == "monthly") { if ((String.IsNullOrEmpty(year)) && (String.IsNullOrEmpty(month))) { result = db.ReadDB("SELECT user, year, month, arcs, urls FROM monthly WHERE user='" + username + "'", 5); } else { result = db.ReadDB("SELECT user, arcs, urls FROM monthly WHERE user='" + username + "'" + " AND year=" + year + " AND month=" + month, 3); } } else { if (String.IsNullOrEmpty(year)) { result = db.ReadDB("SELECT user, year, arcs, urls FROM yearly WHERE user='" + username + "'", 4); } else { result = db.ReadDB("SELECT user, arcs, urls FROM yearly WHERE user='" + username + "'" + " AND year=" + year, 3); } } char[] parameters = new char[2] {'\n', ','}; result = result.TrimEnd(parameters); if (String.IsNullOrEmpty(result)) { result = "No user in database"; } return result; } /**Function read from database stats for users. * @param offset Number of user from which start count stats. * @param year Year from which start count stats. * @param month Month from which start count stats. * @param table Name of table with stats. * @param db Object to Database class. * @return string with users stats.*/ public static string ListUsers(string offset, string year, string month, string table, ref Database db) { System.Text.StringBuilder result = new System.Text.StringBuilder(); string date = String.Empty; switch (table) { case "weekly": string startdate = DateTime.UtcNow.AddDays(-7).ToFileTime().ToString(); result = result.Append(db.ReadDB("SELECT user FROM weekly" + " WHERE senddate > " + startdate + " GROUP BY user LIMIT 20 OFFSET " + offset, 1)); break; case "monthly": date = db.ReadDB("SELECT year, month FROM monthly GROUP BY year, month", 2); date = date.Replace(',', '/'); date = date.TrimEnd('\n'); date = date.Replace('\n', ','); date = date + '\n'; result = result.Append(db.ReadDB("SELECT user FROM monthly WHERE year=" + year + " AND month=" + month + " GROUP BY user LIMIT 20 OFFSET " + offset, 1)); break; case "yearly": date = db.ReadDB("SELECT year FROM yearly GROUP BY year", 1); result = result.Append(db.ReadDB("SELECT user FROM yearly WHERE year=" + year + " GROUP BY user LIMIT 20 OFFSET " + offset, 1)); break; default: break; } if (result.Length == 0) { result = new System.Text.StringBuilder("No users"); } else { string[] users = result.ToString().Split('\n'); result = new System.Text.StringBuilder(); foreach (string user in users) { if (String.IsNullOrEmpty(user.Trim())) { break; } if (table == "weekly") { result.Append(user.Trim()); result.Append(","); result.Append(API.UserWeekly(user.Trim(), ref db)); result.Append("\n"); } else { result.Append(API.UserStats(user.Trim(), year, month, table, ref db)); result.Append("\n"); } } result = new System.Text.StringBuilder(result.ToString().TrimEnd('\n')); } return date + result.ToString(); } /**Function read overall of send arc files and urls to server. * @param username Username in database. * @param db Object to Database class. * @return string with overall stats.*/ public static string OverallStats(string username, ref Database db) { string result; if (String.IsNullOrEmpty(username)) { result = db.ReadDB("SELECT SUM(arcs), SUM(urls), COUNT(user) FROM yearly", 3); } else { result = db.ReadDB("SELECT SUM(arcs), SUM(urls) FROM yearly WHERE user='" + username + "'", 2); result = username + "," + result; } return result; } } }