winix/plugins/stats/init.cpp

222 lines
4.0 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "core/log.h"
#include "core/request.h"
#include "core/config.h"
#include "db/db.h"
#include "bot.h"
#include "stats.h"
#include "templates.h"
#include "statssession.h"
extern "C" void Init(PluginInfo &);
static const wchar_t plugin_name[] = L"stats";
// if there is a winix function parameter nostat_param used
// such as: http://mysite.com/uptime/nostat
// then the statistics will not be calculated
// and winix will not log about creating and destroying plugins date
// (useful when making performance tests so it not change the real statistics)
static const wchar_t nostat_param[] = L"nostat";
namespace Stats
{
Stats stats;
Bot bot;
long current_item_id;
void CalcItemId(Request * request)
{
if( request->is_item )
current_item_id = request->item.id;
else
if( !request->dir_tab.empty() )
current_item_id = request->dir_tab.back()->id;
else
current_item_id = -1;
}
void UpdateStats(PluginInfo & info, Stats::ItemStats & item_stats)
{
bot.SetBrowserName(info.request->env_http_user_agent);
stats.global_all += 1;
item_stats.all += 1;
if( bot.IsGoogle() )
{
stats.global_google += 1;
item_stats.google += 1;
}
if( bot.IsYahoo() )
{
stats.global_yahoo += 1;
item_stats.yahoo += 1;
}
if( bot.IsBing() )
{
stats.global_bing += 1;
item_stats.bing += 1;
}
}
void ContentMake(PluginInfo & info)
{
StatsSession * stats_session = 0;
CalcItemId(info.request);
if( current_item_id == - 1 || !info.plugin_data_base )
return;
stats_session = reinterpret_cast<StatsSession*>(info.plugin_data_base);
stats_session->nostat = info.request->IsParam(nostat_param);
if( stats_session->nostat )
return;
// this simply prevents F5 (refresh) from a webbrowser
if( stats_session->last_visited != -1 &&
stats_session->last_visited == current_item_id )
return;
stats_session->last_visited = current_item_id;
UpdateStats(info, stats.stats_tab[current_item_id]);
if( !stats_session->calculated )
{
stats.global_unique += 1;
stats_session->calculated = true;
}
stats.PeriodicSave();
}
void SessionCreated(PluginInfo & info)
{
StatsSession * d = new StatsSession();
info.request->session->plugin_data.Assign(d);
if( !info.request->IsParam(nostat_param) )
{
log << log4 << "Stats: created stats plugin data"
<< ", plugin id: " << info.plugin_id
<< ", pointer: " << d << logend;
}
}
void RemoveSession(PluginInfo & info)
{
if( !info.plugin_data_base )
{
// temporarily for debug
// sometimes the pointer is null here
log << log1 << "Stats: why the info.plugin_data_base is zero? !!!!" << logend;
return;
}
StatsSession * d = reinterpret_cast<StatsSession*>(info.plugin_data_base);
if( !d->nostat )
{
log << log4 << "Stats: deleting stats plugin data"
<< ", plugin id: " << info.plugin_id
<< ", pointer: " << info.plugin_data_base << logend;
}
delete info.plugin_data_base;
}
void Close(PluginInfo & info)
{
stats.SaveStats();
}
void RemoveFile(PluginInfo & info)
{
stats.RemoveItem(info.l1);
}
void RemoveDir(PluginInfo & info)
{
DbItemQuery query;
std::vector<long> items;
size_t i;
// !! may only files can be retrieved here?
query.SetAll(false, false);
query.WhereParentId(info.l1);
info.db->GetItems(items, query);
// removing childs
for(i=0 ; i<items.size() ; ++i)
stats.RemoveItem(items[i]);
// removing the directory
stats.RemoveItem(info.l1);
}
} // namespace
void Init(PluginInfo & info)
{
using namespace Stats;
plugin.Assign(WINIX_TEMPLATES_CREATEFUNCTIONS, CreateFunctions);
plugin.Assign(WINIX_CONTENT_MAKE, ContentMake);
plugin.Assign(WINIX_SESSION_CREATED, SessionCreated);
plugin.Assign(WINIX_SESSION_REMOVE, RemoveSession);
plugin.Assign(WINIX_CLOSE, Close);
plugin.Assign(WINIX_FILE_REMOVED, RemoveFile);
plugin.Assign(WINIX_DIR_PREPARE_TO_REMOVE, RemoveDir);
stats.ReadConfig(info.config);
stats.ReadStats();
info.p1 = (void*)plugin_name;
}