File: stat.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 126,368 kB
  • ctags: 7,878
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (157 lines) | stat: -rw-r--r-- 4,065 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************/

#include <stdio.h>
#include <string>
#include <WSERVER_debug.h>
#include "stat.h"
#include "clock.h"
#include "config.h"

Stats stats;

VersionInfo::VersionInfo():
	      servers(0), fake_servers(0), clients(0), clients_w_empty_list(0)
{}

ConnectionStats::ConnectionStats(const std::string & fn)
  : LogFile(fn)
{
}

ConnectionStats::~ConnectionStats()
{
  LogFile::CloseFile();
}

void ConnectionStats::AtOpen()
{
  fprintf(fd, "# YYYY-MM-DD hh-mm-ss "
	  "fake_servers, servers, clients, clients_w_empty_list "
	  "nb_version version-1 fake_servers, servers, clients, clients_w_empty_list "
	  "version-2 fake_servers, servers, clients, clients_w_empty_list "
	  "... version-N fake_servers, servers, clients, clients_w_empty_list\n");
}

void ConnectionStats::AtClose()
{
  Write();
}

void ConnectionStats::Write()
{
  if (!fd)
    return;

  struct tm* t;
  time_t now = time(NULL);
  t = localtime(&now);

  fprintf(fd, "%4i-%02i-%02i %02i:%02i:%02i ", 1900 + t->tm_year,t->tm_mon + 1, t->tm_mday,
	  t->tm_hour, t->tm_min, t->tm_sec);

  // Global stats
  unsigned long fake_servers = 0;
  unsigned long servers = 0;
  unsigned long clients = 0;
  unsigned long clients_w_empty_list = 0;
  for (std::map<const std::string, VersionInfo>::const_iterator
	 it = version_stats.begin();
       it != version_stats.end();
       it++) {
    fake_servers += it->second.fake_servers;
    servers += it->second.servers;
    clients += it->second.clients;
    clients_w_empty_list += it->second.clients_w_empty_list;
  }
  fprintf(fd, "%lu %lu %lu %lu", fake_servers, servers, clients, clients_w_empty_list);

  // Stats per version
  fprintf(fd, " %zu ", version_stats.size());

  for (std::map<const std::string, VersionInfo>::const_iterator
	 it = version_stats.begin();
       it != version_stats.end();
       it++) {

    fprintf(fd, "%s %lu %lu %lu %lu ", it->first.c_str(),
	    it->second.fake_servers,
	    it->second.servers,
	    it->second.clients,
	    it->second.clients_w_empty_list);
  }
  fprintf(fd, "\n");

  version_stats.clear();
}

void ConnectionStats::NewServer(const std::string& version)
{
  version_stats[version].servers++;
}

void ConnectionStats::NewFakeServer(const std::string& version)
{
  version_stats[version].fake_servers++;
}

void ConnectionStats::NewClient(const std::string& version)
{
  version_stats[version].clients++;
}

void ConnectionStats::NewClientWoAnswer(const std::string& version)
{
  version_stats[version].clients_w_empty_list++;
}


Stats::Stats() : hourly("hourly"), daily("daily")
{
}

void Stats::Init()
{
  daily.OpenFile();
  hourly.OpenFile();
}

void Stats::NewServer(const std::string& version)
{
  hourly.NewServer(version);
  daily.NewServer(version);
}

void Stats::NewFakeServer(const std::string& version)
{
  hourly.NewFakeServer(version);
  daily.NewFakeServer(version);
}

void Stats::NewClient(const std::string& version)
{
  hourly.NewClient(version);
  daily.NewClient(version);
}

void Stats::NewClientWoAnswer(const std::string& version)
{
  hourly.NewClientWoAnswer(version);
  daily.NewClientWoAnswer(version);
}