| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* user_stat
|
| 4 |
* - data model and basic operations of user related statistics
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "log.h"
|
| 14 |
#include "section_list.h"
|
| 15 |
#include "user_list.h"
|
| 16 |
#include "user_stat.h"
|
| 17 |
|
| 18 |
int user_stat_map_init(USER_STAT_MAP *p_map)
|
| 19 |
{
|
| 20 |
if (p_map == NULL)
|
| 21 |
{
|
| 22 |
log_error("NULL pointer error");
|
| 23 |
return -1;
|
| 24 |
}
|
| 25 |
|
| 26 |
p_map->user_count = 0;
|
| 27 |
p_map->last_uid = -1;
|
| 28 |
p_map->last_article_index = -1;
|
| 29 |
|
| 30 |
return 0;
|
| 31 |
}
|
| 32 |
|
| 33 |
int user_stat_map_update(USER_STAT_MAP *p_map)
|
| 34 |
{
|
| 35 |
int32_t uid_list[BBS_max_user_count];
|
| 36 |
int user_cnt = BBS_max_user_count;
|
| 37 |
int article_cnt;
|
| 38 |
int i;
|
| 39 |
ARTICLE *p_article;
|
| 40 |
|
| 41 |
if (p_map == NULL)
|
| 42 |
{
|
| 43 |
log_error("NULL pointer error");
|
| 44 |
return -1;
|
| 45 |
}
|
| 46 |
|
| 47 |
// Load uid_list
|
| 48 |
if (get_user_id_list(uid_list, &user_cnt, p_map->last_uid + 1) < 0)
|
| 49 |
{
|
| 50 |
log_error("get_user_id_list(%d, 0) error", user_cnt);
|
| 51 |
return -2;
|
| 52 |
}
|
| 53 |
|
| 54 |
for (i = 0; i < user_cnt && p_map->user_count + i < BBS_max_user_count; i++)
|
| 55 |
{
|
| 56 |
p_map->stat_list[p_map->user_count + i].uid = uid_list[i];
|
| 57 |
p_map->stat_list[p_map->user_count + i].article_count = 0;
|
| 58 |
}
|
| 59 |
|
| 60 |
p_map->user_count += i;
|
| 61 |
p_map->last_uid = uid_list[i - 1];
|
| 62 |
|
| 63 |
// Scan articles
|
| 64 |
article_cnt = article_block_article_count();
|
| 65 |
for (i = p_map->last_article_index + 1; i < article_cnt; i++)
|
| 66 |
{
|
| 67 |
p_article = article_block_find_by_index(i);
|
| 68 |
if (p_article == NULL)
|
| 69 |
{
|
| 70 |
log_error("article_block_find_by_index(index=%d) error", i);
|
| 71 |
break;
|
| 72 |
}
|
| 73 |
|
| 74 |
if (p_article->visible == 0)
|
| 75 |
{
|
| 76 |
continue;
|
| 77 |
}
|
| 78 |
|
| 79 |
if (p_article->uid > p_map->last_uid)
|
| 80 |
{
|
| 81 |
log_debug("uid=%d of article(aid=%d) is greater than last_uid=%d",
|
| 82 |
p_article->uid, p_article->aid, p_map->last_uid);
|
| 83 |
continue;
|
| 84 |
}
|
| 85 |
|
| 86 |
if (user_stat_article_cnt_inc(p_map, p_article->uid, 1) < 0)
|
| 87 |
{
|
| 88 |
log_error("user_stat_article_cnt_inc(uid=%d, 1) error", p_article->uid);
|
| 89 |
break;
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
| 93 |
p_map->last_article_index = i - 1;
|
| 94 |
|
| 95 |
return 0;
|
| 96 |
}
|
| 97 |
|
| 98 |
int user_stat_article_cnt_inc(USER_STAT_MAP *p_map, int32_t uid, int n)
|
| 99 |
{
|
| 100 |
int left;
|
| 101 |
int right;
|
| 102 |
int mid;
|
| 103 |
|
| 104 |
if (p_map == NULL)
|
| 105 |
{
|
| 106 |
log_error("NULL pointer error");
|
| 107 |
return -1;
|
| 108 |
}
|
| 109 |
|
| 110 |
left = 0;
|
| 111 |
right = p_map->user_count - 1;
|
| 112 |
|
| 113 |
while (left < right)
|
| 114 |
{
|
| 115 |
mid = (left + right) / 2;
|
| 116 |
if (uid < p_map->stat_list[mid].uid)
|
| 117 |
{
|
| 118 |
right = mid - 1;
|
| 119 |
}
|
| 120 |
else if (uid > p_map->stat_list[mid].uid)
|
| 121 |
{
|
| 122 |
left = mid + 1;
|
| 123 |
}
|
| 124 |
else // if (uid == p_map->stat_list[mid].uid)
|
| 125 |
{
|
| 126 |
left = mid;
|
| 127 |
break;
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
if (uid == p_map->stat_list[left].uid) // found
|
| 132 |
{
|
| 133 |
p_map->stat_list[left].article_count += n;
|
| 134 |
return 1;
|
| 135 |
}
|
| 136 |
|
| 137 |
return 0; // not found
|
| 138 |
}
|
| 139 |
|
| 140 |
int user_stat_get(USER_STAT_MAP *p_map, int32_t uid, const USER_STAT **pp_stat)
|
| 141 |
{
|
| 142 |
int left;
|
| 143 |
int right;
|
| 144 |
int mid;
|
| 145 |
|
| 146 |
if (p_map == NULL)
|
| 147 |
{
|
| 148 |
log_error("NULL pointer error");
|
| 149 |
return -1;
|
| 150 |
}
|
| 151 |
|
| 152 |
left = 0;
|
| 153 |
right = p_map->user_count - 1;
|
| 154 |
|
| 155 |
while (left < right)
|
| 156 |
{
|
| 157 |
mid = (left + right) / 2;
|
| 158 |
if (uid < p_map->stat_list[mid].uid)
|
| 159 |
{
|
| 160 |
right = mid - 1;
|
| 161 |
}
|
| 162 |
else if (uid > p_map->stat_list[mid].uid)
|
| 163 |
{
|
| 164 |
left = mid + 1;
|
| 165 |
}
|
| 166 |
else // if (uid == p_map->stat_list[mid].uid)
|
| 167 |
{
|
| 168 |
left = mid;
|
| 169 |
break;
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
| 173 |
if (uid == p_map->stat_list[left].uid) // found
|
| 174 |
{
|
| 175 |
*pp_stat = &(p_map->stat_list[left]);
|
| 176 |
return 1;
|
| 177 |
}
|
| 178 |
|
| 179 |
// not found
|
| 180 |
*pp_stat = NULL;
|
| 181 |
return 0;
|
| 182 |
}
|