-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
270 lines (209 loc) · 7.08 KB
/
main.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//boost
#include <boost/program_options.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/assign.hpp>
//std
#include <fstream>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>
#include <map>
#include <tr1/functional>
//c99
#include <stdint.h>
#include <cstdlib>
// sql
#include <mysql_connection.h>
#include <cppconn/driver.h>
//lib
#include "BackEnd.hpp"
#include "FrontEnd.hpp"
#include "ConfigParser.hpp"
namespace po = boost::program_options;
// TODO queryresult operator<<
// setup frontEnd and backEnd somewhere in main and pass around
// simplify
//delicious globals
boost::scoped_ptr<IBackEnd> backEnd;
boost::scoped_ptr<IFrontEnd> frontEnd;
//help message
bool help(int argc, char* argv[]) {
(void)argc; (void)argv;
std::cout << "helpful message" << std::endl
<< "modules: visit, reload, matchad, load_click_data" << std::endl
<< "each has its own help message, e.g. $ adworks visit --help" << std::endl;
return true;
}
bool perform_lda(int argc, char* argv[]) {
std::string dirName;
po::options_description desc("Allowed options for perform_lda");
desc.add_options()
("help", "produces help message")
("lda-dir", po::value<std::string>(&dirName));
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if(vm.count("help")) {
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
if(!vm.count("lda-dir")) {
std::cout << "no lda-dir given. See --help" << std::endl;
return EXIT_FAILURE;
}
//iterate the dir and squash the files
std::cout << dirName << std::endl;
// already have a file with that name? Not my problem.
std::ofstream o("tmpfile-lda.txt");
lda(dirName, o);
//here
std::system("lda est 0.01 40 settings.txt tmpfile-lda.txt random foo/");
return true;
}
bool load_click_data(int argc, char* argv[]) {
std::string fileName;
po::options_description desc("Allowed options for load_click_data");
desc.add_options()
("help", "produces help message")
("click-file", po::value<std::string>(&fileName));
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if(vm.count("help")) {
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
if(!vm.count("click-file")) {
std::cout << "no click-file given. See --help" << std::endl;
return EXIT_FAILURE;
}
frontEnd->analyzeClickGraph(fileName);
return EXIT_SUCCESS;
}
//visit
bool visit(int argc, char* argv[]) {
uint32_t id;
po::options_description desc("Allowed options for visit");
desc.add_options()
("help", "produce help message")
("id", po::value<uint32_t>(&id), "visit ad with this id\nanonymous option also possible")
;
po::positional_options_description p;
p.add("id", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
if (!vm.count("id")) {
std::cout << "Missing id. See --help" << std::endl;
return EXIT_SUCCESS;
}
std::cout << " visiting id: " << id << " ";
std::cout << "with landing page: " << frontEnd->getAdURL(id) << std::endl;
return EXIT_SUCCESS;
}
bool matchad(int argc, char* argv[]) {
int age;
char gender;
typedef std::vector< std::string > str_vec;
str_vec queries;
po::options_description desc("Allowed options for matchad");
desc.add_options()
("help", "produce help message")
("q", po::value<str_vec>(&queries)->multitoken(), "match to a list of queries,"
"\nseparate queries in \" or whatever pleases your shell")
("a", po::value<int>(&age), "optional age parameter \n 0: TEEN, 1: YOUNG, 2: OLD, 3: NA")
("g", po::value<char>(&gender), "optional gender parameter \n n: NA, m: MALE, f: FEMALE");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
boost::scoped_ptr<IUser> user;
if(vm.count("a") && vm.count("g"))
user.reset(new User(gender, age));
for(str_vec::const_iterator it = queries.begin(); it != queries.end(); ++it) {
std::vector<std::string> rewrites = frontEnd->matchAd(*it, user.get());
QueryResult query = backEnd->matchAdRewrites(rewrites, user.get());
std::cout << "Query: " << *it << " returns ad with\n"
<< "title: " << query.getTitle() << "\n"
<< "creative: " << query.getCreative() << "\n"
<< "id: " << query.getAdID() << std::endl;
}
return EXIT_SUCCESS;
}
bool reload(int argc, char* argv[]) {
std::string adFile, queryFile;
po::options_description desc("Allowed options for reload");
desc.add_options()
("help", "produce help message")
("adfile", po::value<std::string>(&adFile),
"reload with this adfile \nanonymous option also possible")
("queryfile", po::value<std::string>(&queryFile),
"reload with this queryfile \nanonymous option also possible")
;
po::positional_options_description p;
p.add("adfile", 1);
p.add("queryfile", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
std::cout << "reloading with \n"
<< "adfile " << adFile << "\n"
<< "queryfile " << queryFile << std::endl;
return backEnd->initDatabase(adFile, queryFile);
}
int main(int argc, char* argv[]) {
//map options into functions from arguments to bool
//so we still can read the beautiful whatS of exceptions
//try {
typedef std::tr1::function<bool (int argc, char* argv[])> OptionFunction;
typedef std::map<std::string, OptionFunction> OptionMap;
OptionMap options;
boost::assign::insert(options)
( "help", &help )( "visit", &visit )
( "matchad", &matchad )( "reload", &reload )
( "load_click_data", &load_click_data )("perform_lda", &perform_lda);
if(argc < 2) {
std::cout << "no option specified" << std::endl;
return EXIT_FAILURE;
}
//TODO: bad design!!
std::ifstream in("config.txt");
Config cfg(in);
in.close();
sql::Driver* driver = get_driver_instance();
boost::shared_ptr<sql::Connection> con;
con.reset(driver->connect(cfg["Server"]+":"+cfg["Port"],
cfg["User"], cfg["Password"]));
con->setSchema("AdWorks");
frontEnd.reset(new FrontEnd(con));
backEnd.reset(new BackEnd(con));
frontEnd->setBackend(backEnd.get());
std::string option(argv[1]);
OptionMap::iterator it = options.find(option);
if(it != options.end()) {
//execute the matched function with argv reduced by the program
//name and argc decremmented by one
return (it->second)(--argc, ++argv);
} else {
std::cout << "unknown option: `" << option << "'" << std::endl;
return EXIT_FAILURE;
}
// } catch(...) {
// std::cerr << "aborting..." << std::endl;
// return EXIT_FAILURE;
// }
}