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