/[LeafOK_CVS]/lbbs/src/user_stat.c
ViewVC logotype

Contents of /lbbs/src/user_stat.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations)
Tue Nov 11 00:28:05 2025 UTC (4 months ago) by sysadm
Branch: MAIN
Changes since 1.5: +4 -0 lines
Content type: text/x-csrc
Use config.h

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 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\n");
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\n");
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\n", 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\n", 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 #ifdef _DEBUG
82 log_error("uid=%d of article(aid=%d) is greater than last_uid=%d\n",
83 p_article->uid, p_article->aid, p_map->last_uid);
84 #endif
85 continue;
86 }
87
88 if (user_stat_article_cnt_inc(p_map, p_article->uid, 1) < 0)
89 {
90 log_error("user_stat_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
91 break;
92 }
93 }
94
95 p_map->last_article_index = i - 1;
96
97 return 0;
98 }
99
100 int user_stat_article_cnt_inc(USER_STAT_MAP *p_map, int32_t uid, int n)
101 {
102 int left;
103 int right;
104 int mid;
105
106 if (p_map == NULL)
107 {
108 log_error("NULL pointer error\n");
109 return -1;
110 }
111
112 left = 0;
113 right = p_map->user_count - 1;
114
115 while (left < right)
116 {
117 mid = (left + right) / 2;
118 if (uid < p_map->stat_list[mid].uid)
119 {
120 right = mid - 1;
121 }
122 else if (uid > p_map->stat_list[mid].uid)
123 {
124 left = mid + 1;
125 }
126 else // if (uid == p_map->stat_list[mid].uid)
127 {
128 left = mid;
129 break;
130 }
131 }
132
133 if (uid == p_map->stat_list[left].uid) // found
134 {
135 p_map->stat_list[left].article_count += n;
136 return 1;
137 }
138
139 return 0; // not found
140 }
141
142 int user_stat_get(USER_STAT_MAP *p_map, int32_t uid, const USER_STAT **pp_stat)
143 {
144 int left;
145 int right;
146 int mid;
147
148 if (p_map == NULL)
149 {
150 log_error("NULL pointer error\n");
151 return -1;
152 }
153
154 left = 0;
155 right = p_map->user_count - 1;
156
157 while (left < right)
158 {
159 mid = (left + right) / 2;
160 if (uid < p_map->stat_list[mid].uid)
161 {
162 right = mid - 1;
163 }
164 else if (uid > p_map->stat_list[mid].uid)
165 {
166 left = mid + 1;
167 }
168 else // if (uid == p_map->stat_list[mid].uid)
169 {
170 left = mid;
171 break;
172 }
173 }
174
175 if (uid == p_map->stat_list[left].uid) // found
176 {
177 *pp_stat = &(p_map->stat_list[left]);
178 return 1;
179 }
180
181 // not found
182 *pp_stat = NULL;
183 return 0;
184 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1