Commit 06be1bc7 authored by Liang Ding's avatar Liang Ding

修复浏览计数问题

parent 512b0adc
...@@ -66,7 +66,7 @@ import org.jsoup.Jsoup; ...@@ -66,7 +66,7 @@ import org.jsoup.Jsoup;
* Article processor. * Article processor.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.2.13, Oct 26, 2013 * @version 1.1.2.14, Nov 17, 2013
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -130,6 +130,12 @@ public class ArticleProcessor { ...@@ -130,6 +130,12 @@ public class ArticleProcessor {
*/ */
@Inject @Inject
private ArticleMgmtService articleMgmtService; private ArticleMgmtService articleMgmtService;
/**
* Statistic management service.
*/
@Inject
private StatisticMgmtService statisticMgmtService;
/** /**
* Shows the article view password form. * Shows the article view password form.
...@@ -724,6 +730,8 @@ public class ArticleProcessor { ...@@ -724,6 +730,8 @@ public class ArticleProcessor {
filler.fillBlogFooter(request, dataModel, preference); filler.fillBlogFooter(request, dataModel, preference);
filler.fillSide(request, dataModel, preference); filler.fillSide(request, dataModel, preference);
Skins.fillLangs(preference.optString(Preference.LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel); Skins.fillLangs(preference.optString(Preference.LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final ServiceException e) { } catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
...@@ -817,6 +825,8 @@ public class ArticleProcessor { ...@@ -817,6 +825,8 @@ public class ArticleProcessor {
filler.fillBlogHeader(request, response, dataModel, preference); filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference); filler.fillBlogFooter(request, dataModel, preference);
filler.fillSide(request, dataModel, preference); filler.fillSide(request, dataModel, preference);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
...@@ -927,6 +937,8 @@ public class ArticleProcessor { ...@@ -927,6 +937,8 @@ public class ArticleProcessor {
if (!Requests.hasBeenServed(request, response)) { if (!Requests.hasBeenServed(request, response)) {
articleMgmtService.incViewCount(articleId); articleMgmtService.incViewCount(articleId);
} }
statisticMgmtService.incBlogViewCount(request, response);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
......
...@@ -45,6 +45,7 @@ import org.b3log.solo.model.Preference; ...@@ -45,6 +45,7 @@ import org.b3log.solo.model.Preference;
import org.b3log.solo.processor.renderer.ConsoleRenderer; import org.b3log.solo.processor.renderer.ConsoleRenderer;
import org.b3log.solo.processor.util.Filler; import org.b3log.solo.processor.util.Filler;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.StatisticMgmtService;
import org.b3log.solo.util.Skins; import org.b3log.solo.util.Skins;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -54,7 +55,7 @@ import org.json.JSONObject; ...@@ -54,7 +55,7 @@ import org.json.JSONObject;
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="mailto:385321165@qq.com">DASHU</a> * @author <a href="mailto:385321165@qq.com">DASHU</a>
* @version 1.1.1.4, Sep 11, 2013 * @version 1.1.1.5, Nov 17, 2013
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -82,6 +83,12 @@ public class IndexProcessor { ...@@ -82,6 +83,12 @@ public class IndexProcessor {
*/ */
@Inject @Inject
private LangPropsService langPropsService; private LangPropsService langPropsService;
/**
* Statistic management service.
*/
@Inject
private StatisticMgmtService statisticMgmtService;
/** /**
* Shows index with the specified context. * Shows index with the specified context.
...@@ -126,6 +133,8 @@ public class IndexProcessor { ...@@ -126,6 +133,8 @@ public class IndexProcessor {
} }
dataModel.put(Common.PATH, ""); dataModel.put(Common.PATH, "");
statisticMgmtService.incBlogViewCount(request, response);
} catch (final ServiceException e) { } catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
......
...@@ -40,6 +40,7 @@ import org.b3log.solo.model.Preference; ...@@ -40,6 +40,7 @@ import org.b3log.solo.model.Preference;
import org.b3log.solo.processor.util.Filler; import org.b3log.solo.processor.util.Filler;
import org.b3log.solo.service.CommentQueryService; import org.b3log.solo.service.CommentQueryService;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.StatisticMgmtService;
import org.b3log.solo.util.Markdowns; import org.b3log.solo.util.Markdowns;
import org.b3log.solo.util.Skins; import org.b3log.solo.util.Skins;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -49,7 +50,7 @@ import org.json.JSONObject; ...@@ -49,7 +50,7 @@ import org.json.JSONObject;
* Page processor. * Page processor.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.3, Oct 14, 2013 * @version 1.1.0.4, Nov 17, 2013
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -84,9 +85,15 @@ public class PageProcessor { ...@@ -84,9 +85,15 @@ public class PageProcessor {
@Inject @Inject
private CommentQueryService commentQueryService; private CommentQueryService commentQueryService;
/**
* Statistic management service.
*/
@Inject
private StatisticMgmtService statisticMgmtService;
/** /**
* Shows page with the specified context. * Shows page with the specified context.
* *
* @param context the specified context * @param context the specified context
*/ */
@RequestProcessing(value = "/page", method = HTTPRequestMethod.GET) @RequestProcessing(value = "/page", method = HTTPRequestMethod.GET)
...@@ -144,6 +151,8 @@ public class PageProcessor { ...@@ -144,6 +151,8 @@ public class PageProcessor {
filler.fillSide(request, dataModel, preference); filler.fillSide(request, dataModel, preference);
filler.fillBlogHeader(request, response, dataModel, preference); filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference); filler.fillBlogFooter(request, dataModel, preference);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
......
...@@ -48,6 +48,7 @@ import org.b3log.solo.model.Tag; ...@@ -48,6 +48,7 @@ import org.b3log.solo.model.Tag;
import org.b3log.solo.processor.util.Filler; import org.b3log.solo.processor.util.Filler;
import org.b3log.solo.service.ArticleQueryService; import org.b3log.solo.service.ArticleQueryService;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.StatisticMgmtService;
import org.b3log.solo.service.TagQueryService; import org.b3log.solo.service.TagQueryService;
import org.b3log.solo.service.UserQueryService; import org.b3log.solo.service.UserQueryService;
import org.b3log.solo.util.Skins; import org.b3log.solo.util.Skins;
...@@ -60,7 +61,7 @@ import org.json.JSONObject; ...@@ -60,7 +61,7 @@ import org.json.JSONObject;
* Tag processor. * Tag processor.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.1.2, Oct 29, 2013 * @version 1.1.1.3, Nov 17, 2013
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -107,6 +108,12 @@ public class TagProcessor { ...@@ -107,6 +108,12 @@ public class TagProcessor {
@Inject @Inject
private TagQueryService tagQueryService; private TagQueryService tagQueryService;
/**
* Statistic management service.
*/
@Inject
private StatisticMgmtService statisticMgmtService;
/** /**
* Shows articles related with a tag with the specified context. * Shows articles related with a tag with the specified context.
* *
...@@ -201,6 +208,8 @@ public class TagProcessor { ...@@ -201,6 +208,8 @@ public class TagProcessor {
filler.fillSide(request, dataModel, preference); filler.fillSide(request, dataModel, preference);
filler.fillBlogHeader(request, response, dataModel, preference); filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference); filler.fillBlogFooter(request, dataModel, preference);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final ServiceException e) { } catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
......
...@@ -38,23 +38,24 @@ import org.b3log.latke.util.freemarker.Templates; ...@@ -38,23 +38,24 @@ import org.b3log.latke.util.freemarker.Templates;
import org.b3log.solo.model.Preference; import org.b3log.solo.model.Preference;
import org.b3log.solo.processor.util.Filler; import org.b3log.solo.processor.util.Filler;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.StatisticMgmtService;
import org.b3log.solo.util.Skins; import org.b3log.solo.util.Skins;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* User template processor. * User template processor.
* *
* <p> * <p>
* User can add a template (for example "links.ftl") then visits the page ("links.html"). * User can add a template (for example "links.ftl") then visits the page ("links.html").
* </p> * </p>
* *
* <p> * <p>
* See <a href="https://code.google.com/p/b3log-solo/issues/detail?id=409">issue 409</a> for more details. * See <a href="https://code.google.com/p/b3log-solo/issues/detail?id=409">issue 409</a> for more details.
* </p> * </p>
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.3, Jul 11, 2012 * @version 1.0.0.4, Nov 17, 2013
* @since 0.4.5 * @since 0.4.5
*/ */
@RequestProcessor @RequestProcessor
...@@ -83,13 +84,19 @@ public class UserTemplateProcessor { ...@@ -83,13 +84,19 @@ public class UserTemplateProcessor {
@Inject @Inject
private LangPropsService langPropsService; private LangPropsService langPropsService;
/**
* Statistic management service.
*/
@Inject
private StatisticMgmtService statisticMgmtService;
/** /**
* Shows the user template page. * Shows the user template page.
* *
* @param context the specified context * @param context the specified context
* @param request the specified HTTP servlet request * @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response * @param response the specified HTTP servlet response
* @throws IOException io exception * @throws IOException io exception
*/ */
@RequestProcessing(value = "/*.html", method = HTTPRequestMethod.GET) @RequestProcessing(value = "/*.html", method = HTTPRequestMethod.GET)
public void showPage(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) public void showPage(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response)
...@@ -129,6 +136,8 @@ public class UserTemplateProcessor { ...@@ -129,6 +136,8 @@ public class UserTemplateProcessor {
filler.fillUserTemplate(request, template, dataModel, preference); filler.fillUserTemplate(request, template, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference); filler.fillBlogFooter(request, dataModel, preference);
Skins.fillLangs(preference.optString(Preference.LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel); Skins.fillLangs(preference.optString(Preference.LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
......
...@@ -28,11 +28,11 @@ import org.b3log.solo.SoloServletListener; ...@@ -28,11 +28,11 @@ import org.b3log.solo.SoloServletListener;
/** /**
* <a href="http://freemarker.org">FreeMarker</a> HTTP response * <a href="http://freemarker.org">FreeMarker</a> HTTP response
* renderer for administrator console and initialization rendering. * renderer for administrator console and initialization rendering.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Mar 29, 2012 * @version 1.0.0.1, Nov 17, 2013
* @since 0.4.1 * @since 0.4.1
*/ */
public final class ConsoleRenderer extends AbstractFreeMarkerRenderer { public final class ConsoleRenderer extends AbstractFreeMarkerRenderer {
...@@ -60,9 +60,12 @@ public final class ConsoleRenderer extends AbstractFreeMarkerRenderer { ...@@ -60,9 +60,12 @@ public final class ConsoleRenderer extends AbstractFreeMarkerRenderer {
} }
@Override @Override
protected Template getTemplate(final String templateDirName, final String templateName) protected Template getTemplate(final String templateDirName, final String templateName) {
throws IOException { try {
return TEMPLATE_CFG.getTemplate(templateName); return TEMPLATE_CFG.getTemplate(templateName);
} catch (final IOException e) {
return null;
}
} }
@Override @Override
......
...@@ -39,9 +39,9 @@ import org.json.JSONException; ...@@ -39,9 +39,9 @@ import org.json.JSONException;
/** /**
* Statistic management service. * Statistic management service.
* *
* <p> * <p>
* <b>Note</b>: The {@link #onlineVisitorCount online visitor counting} is NOT cluster-safe. * <b>Note</b>: The {@link #onlineVisitorCount online visitor counting} is NOT cluster-safe.
* </p> * </p>
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
...@@ -76,7 +76,7 @@ public class StatisticMgmtService { ...@@ -76,7 +76,7 @@ public class StatisticMgmtService {
/** /**
* Online visitor cache. * Online visitor cache.
* *
* <p> * <p>
* &lt;ip, recentTime&gt; * &lt;ip, recentTime&gt;
* </p> * </p>
...@@ -90,23 +90,21 @@ public class StatisticMgmtService { ...@@ -90,23 +90,21 @@ public class StatisticMgmtService {
/** /**
* Blog statistic view count +1. * Blog statistic view count +1.
* *
* <p> * <p>
* If it is a search engine bot made the specified request, will NOT increment blog statistic view count. * If it is a search engine bot made the specified request, will NOT increment blog statistic view count.
* </p> * </p>
* *
* <p> * <p>
* There is a cron job (/console/stat/viewcnt) to flush the blog view count from cache to datastore. * There is a cron job (/console/stat/viewcnt) to flush the blog view count from cache to datastore.
* </p> * </p>
* *
* @param request the specified request * @param request the specified request
* @param response the specified response * @param response the specified response
* @throws RepositoryException repository exception * @throws ServiceException service exception
* @throws JSONException json exception * @see Requests#searchEngineBotRequest(javax.servlet.http.HttpServletRequest)
* @see Requests#searchEngineBotRequest(javax.servlet.http.HttpServletRequest)
*/ */
public void incBlogViewCount(final HttpServletRequest request, final HttpServletResponse response) public void incBlogViewCount(final HttpServletRequest request, final HttpServletResponse response) throws ServiceException {
throws RepositoryException, JSONException {
if (Requests.searchEngineBotRequest(request)) { if (Requests.searchEngineBotRequest(request)) {
return; return;
} }
...@@ -115,22 +113,22 @@ public class StatisticMgmtService { ...@@ -115,22 +113,22 @@ public class StatisticMgmtService {
return; return;
} }
final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC); final Transaction transaction = statisticRepository.beginTransaction();
JSONObject statistic = null;
if (null == statistic) {
return;
}
LOGGER.log(Level.TRACE, "Before inc blog view count[statistic={0}]", statistic); try {
statistic = statisticRepository.get(Statistic.STATISTIC);
if (null == statistic) {
return;
}
int blogViewCnt = statistic.getInt(Statistic.STATISTIC_BLOG_VIEW_COUNT); LOGGER.log(Level.TRACE, "Before inc blog view count[statistic={0}]", statistic);
++blogViewCnt; int blogViewCnt = statistic.optInt(Statistic.STATISTIC_BLOG_VIEW_COUNT);
statistic.put(Statistic.STATISTIC_BLOG_VIEW_COUNT, blogViewCnt);
final Transaction transaction = statisticRepository.beginTransaction(); ++blogViewCnt;
statistic.put(Statistic.STATISTIC_BLOG_VIEW_COUNT, blogViewCnt);
try {
statisticRepository.update(Statistic.STATISTIC, statistic); statisticRepository.update(Statistic.STATISTIC, statistic);
transaction.commit(); transaction.commit();
...@@ -140,9 +138,11 @@ public class StatisticMgmtService { ...@@ -140,9 +138,11 @@ public class StatisticMgmtService {
} }
LOGGER.log(Level.ERROR, "Updates blog view count failed", e); LOGGER.log(Level.ERROR, "Updates blog view count failed", e);
return;
} }
LOGGER.log(Level.DEBUG, "Inced blog view count[statistic={0}]", statistic); LOGGER.log(Level.TRACE, "Inced blog view count[statistic={0}]", statistic);
} }
/** /**
...@@ -317,7 +317,7 @@ public class StatisticMgmtService { ...@@ -317,7 +317,7 @@ public class StatisticMgmtService {
/** /**
* Refreshes online visitor count for the specified request. * Refreshes online visitor count for the specified request.
* *
* @param request the specified request * @param request the specified request
*/ */
public void onlineVisitorCount(final HttpServletRequest request) { public void onlineVisitorCount(final HttpServletRequest request) {
...@@ -373,7 +373,7 @@ public class StatisticMgmtService { ...@@ -373,7 +373,7 @@ public class StatisticMgmtService {
/** /**
* Sets the article repository with the specified article repository. * Sets the article repository with the specified article repository.
* *
* @param articleRepository the specified article repository * @param articleRepository the specified article repository
*/ */
public void setArticleRepository(final ArticleRepository articleRepository) { public void setArticleRepository(final ArticleRepository articleRepository) {
...@@ -382,7 +382,7 @@ public class StatisticMgmtService { ...@@ -382,7 +382,7 @@ public class StatisticMgmtService {
/** /**
* Sets the statistic repository with the specified statistic repository. * Sets the statistic repository with the specified statistic repository.
* *
* @param statisticRepository the specified statistic repository * @param statisticRepository the specified statistic repository
*/ */
public void setStatisticRepository(final StatisticRepository statisticRepository) { public void setStatisticRepository(final StatisticRepository statisticRepository) {
...@@ -391,7 +391,7 @@ public class StatisticMgmtService { ...@@ -391,7 +391,7 @@ public class StatisticMgmtService {
/** /**
* Sets the language service with the specified language service. * Sets the language service with the specified language service.
* *
* @param langPropsService the specified language service * @param langPropsService the specified language service
*/ */
public void setLangPropsService(final LangPropsService langPropsService) { public void setLangPropsService(final LangPropsService langPropsService) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment