blob: 3ea7eedb42f43e6b2c9929435a1b18d0a1b89ff3 [file] [log] [blame]
kurtis.heimerl8fd86a52012-12-16 06:23:29 +00001/**@file Module for performance-reporting mechanisms. */
2/*
3* Copyright 2012 Range Networks, Inc.
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26#include "Reporting.h"
27#include "Logger.h"
28#include <stdio.h>
29#include <string.h>
30
31static const char* createReportingTable = {
32 "CREATE TABLE IF NOT EXISTS REPORTING ("
33 "NAME TEXT UNIQUE NOT NULL, "
34 "VALUE INTEGER DEFAULT 0, "
35 "CLEAREDTIME INTEGER NOT NULL, "
36 "UPDATETIME INTEGER DEFAULT 0 "
37 ")"
38};
39
40
41ReportingTable::ReportingTable(const char* filename)
42{
43 gLogEarly(LOG_INFO | mFacility, "opening reporting table from path %s", filename);
44 // Connect to the database.
45 int rc = sqlite3_open(filename,&mDB);
46 if (rc) {
47 gLogEarly(LOG_EMERG | mFacility, "cannot open reporting database at %s, error message: %s", filename, sqlite3_errmsg(mDB));
48 sqlite3_close(mDB);
49 mDB = NULL;
50 return;
51 }
52 // Create the table, if needed.
53 if (!sqlite3_command(mDB,createReportingTable)) {
54 gLogEarly(LOG_EMERG | mFacility, "cannot create reporting table in database at %s, error message: %s", filename, sqlite3_errmsg(mDB));
55 }
56}
57
58
59bool ReportingTable::create(const char* paramName)
60{
61 char cmd[200];
62 sprintf(cmd,"INSERT OR IGNORE INTO REPORTING (NAME,CLEAREDTIME) VALUES (\"%s\",%ld)", paramName, time(NULL));
63 if (!sqlite3_command(mDB,cmd)) {
64 gLogEarly(LOG_CRIT|mFacility, "cannot create reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB));
65 return false;
66 }
67 return true;
68}
69
70
71
72bool ReportingTable::incr(const char* paramName)
73{
74 char cmd[200];
75 sprintf(cmd,"UPDATE REPORTING SET VALUE=VALUE+1, UPDATETIME=%ld WHERE NAME=\"%s\"", time(NULL), paramName);
76 if (!sqlite3_command(mDB,cmd)) {
77 gLogEarly(LOG_CRIT|mFacility, "cannot increment reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB));
78 return false;
79 }
80 return true;
81}
82
83
84
85bool ReportingTable::max(const char* paramName, unsigned newVal)
86{
87 char cmd[200];
88 sprintf(cmd,"UPDATE REPORTING SET VALUE=MAX(VALUE,%u), UPDATETIME=%ld WHERE NAME=\"%s\"", newVal, time(NULL), paramName);
89 if (!sqlite3_command(mDB,cmd)) {
90 gLogEarly(LOG_CRIT|mFacility, "cannot maximize reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB));
91 return false;
92 }
93 return true;
94}
95
96
97bool ReportingTable::clear(const char* paramName)
98{
99 char cmd[200];
100 sprintf(cmd,"UPDATE REPORTING SET VALUE=0, UPDATETIME=0, CLEAREDTIME=%ld WHERE NAME=\"%s\"", time(NULL), paramName);
101 if (!sqlite3_command(mDB,cmd)) {
102 gLogEarly(LOG_CRIT|mFacility, "cannot clear reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB));
103 return false;
104 }
105 return true;
106}
107
108
109bool ReportingTable::create(const char* baseName, unsigned minIndex, unsigned maxIndex)
110{
111 size_t sz = strlen(baseName);
112 for (unsigned i = minIndex; i<=maxIndex; i++) {
113 char name[sz+10];
114 sprintf(name,"%s.%u",baseName,i);
115 if (!create(name)) return false;
116 }
117 return true;
118}
119
120bool ReportingTable::incr(const char* baseName, unsigned index)
121{
122 char name[strlen(baseName)+10];
123 sprintf(name,"%s.%u",baseName,index);
124 return incr(name);
125}
126
127
128bool ReportingTable::max(const char* baseName, unsigned index, unsigned newVal)
129{
130 char name[strlen(baseName)+10];
131 sprintf(name,"%s.%u",baseName,index);
132 return max(name,newVal);
133}
134
135
136bool ReportingTable::clear(const char* baseName, unsigned index)
137{
138 char name[strlen(baseName)+10];
139 sprintf(name,"%s.%u",baseName,index);
140 return clear(name);
141}
142
143
144
145