A method for real-time statistics of user operation behavior data
By using a dictionary-type data structure and a scheduled task in memory to store user operation behavior data, the real-time and performance bottlenecks in existing technologies are solved, enabling fast and accurate user operation behavior statistics and improving system performance and user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NANJING ZHUOSHENGYUN INFORMATION TECH CO LTD
- Filing Date
- 2022-11-24
- Publication Date
- 2026-04-17
AI Technical Summary
Existing technologies using databases and log files have performance bottlenecks when real-time statistics of user behavior are required, making it difficult to achieve real-time performance and accuracy while ensuring system performance.
User actions are recorded using an in-memory data structure (dictionary type). Scheduled tasks update and save the data in memory to a log file. Data merging and writing back are performed using a caching and high-speed storage system to ensure data accuracy and real-time performance.
It improves the efficiency of caching and logging, enables real-time statistics of user behavior, and enhances system performance and user experience.
Smart Images

Figure CN115757074B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of data statistics, and in particular relates to a method for real-time statistical analysis of user operation behavior data. Background Technology
[0002] With the development of the internet, various applications are paying increasing attention to user behavior analysis. Based on user behavior analysis, better product recommendations and user interest discovery can be achieved.
[0003] To track user actions, it's necessary to record the number of times a user clicks on a specific item, API interface, or button. Typically, this data isn't directly saved to a database, as storing it for every click would overload the database. A common practice is to save each click to a log file, and then perform big data analysis on these log files to generate statistical results.
[0004] This approach is sufficient for general applications and data analysis. However, in some applications, obtaining records of user actions requires a certain degree of real-time capability while maintaining system performance. In such cases, both database recording and log storage methods have significant limitations. Summary of the Invention
[0005] In view of this, the present invention aims to propose a method for real-time statistical analysis of user operation behavior data, so as to improve the efficiency of caching and logging. A large number of operations are performed in memory or cache, which is fast and accurate, and ultimately realizes the real-time statistical analysis of the number of operations of a certain type of user behavior.
[0006] To achieve the above objectives, the technical solution of the present invention is implemented as follows:
[0007] In a first aspect, the present invention provides a method for real-time statistical analysis of user operation behavior data, comprising the following steps:
[0008] S1. Establish a dictionary-type memory data structure in memory. The key of the memory data structure is the username, and the content value is an array. The first element of the array records the number of times a specific operation is performed, and its initial value is 0, denoted as MemOpCount. The second element of the array records the time when MemOpCount changes from 0 to 1, denoted as MemFirstTime. Each username corresponds to one key. The memory data structure is initialized when the system starts.
[0009] S2. When the user performs the specific operation, update MemOpCount and assign a value to MemFirstTime based on MemOpCount. Specifically, the user's username is used as the key to retrieve the corresponding content value from the memory data structure. This content value includes MemOpCount and MemFirstTime. MemOpCount is incremented by 1. If the current MemOpCount is equal to 1, then MemFirstTime is assigned the current time. If the current MemOpCount is greater than 1, then MemFirstTime remains unchanged.
[0010] S3. Execute a scheduled task, which is executed once in a loop within each task time period, and the task time period is less than or equal to 24 hours; the scheduled task includes saving the current username, MemOpCount, and MemFirstTime to a log file; and after saving, setting the MemOpCount of all users in the memory data structure to zero and assigning the MemFirstTime of all users to null.
[0011] S4. Perform statistics on the specific operations, including the following steps:
[0012] S401. Obtain query parameters, including username, statistical time period, start time of statistics, and end time of statistics; the statistical results during the statistics are used to obtain a specific operation count sequence at intervals according to the statistical time period, which can be days, weeks, months, or years; S402. Check in the cache whether there is a statistical result corresponding to the query parameters. If the statistical result does not exist, calculate the statistical result based on the query parameters in the log file and save the statistical result to the cache; S403. Merge the obtained statistical result with the MemOpCount value of the user in memory to form the final statistical result.
[0013] Furthermore, in step S3, the task time period is less than or equal to the statistical time period, and the scheduled task is executed at least once a day at midnight.
[0014] Furthermore, in step S3, when saving the current username, MemOpCount, and MemFirstTime to the log file, it can be saved as a text file or as a database. Each saved record contains the username, MemOpCount, and MemFirstTime. At the same time, all data containing the relevant user and MemFirstTime time point in the cache is cleared.
[0015] Furthermore, it also includes the following steps:
[0016] S5. When the system exits or terminates, the memory data structure is traversed in advance, and the MemOpCount and MemFirstTime of all users are written back to the log file. At the same time, all data containing the relevant users and MemFirstTime timestamps in the cache are cleared.
[0017] Further, in step S402, the step of calculating the statistical result based on the query parameters in the log file includes: S410, obtaining a specified time period based on the start time and end time of the statistics, and acquiring records for the specified time period; S411, based on the statistical time period, extracting multiple records in the log file whose MemFirstTime belongs to a certain statistical time period, summing the MemOpCount in the records, and obtaining the total number of operations for the relevant statistical time period; S412, based on the specified time period, dividing according to the statistical time period, and using the obtained sequence of the total number of operations as the statistical result.
[0018] Furthermore, in step S402, saving the statistical results to the cache means saving the statistical results from the log file to the high-speed storage system, so that the high-speed storage system can directly obtain the statistical results according to specified parameters.
[0019] Furthermore, in step S403, the step of merging the obtained statistical results and the MemOpCount value of the user in memory includes: S413, if the statistical cutoff time is less than or equal to MemFirstTime, then no merging is required, and the current statistical result is directly used as the final statistical result; S414, if the statistical cutoff time is greater than MemFirstTime, then the number of operations in the last period in the current statistical result is added to the MemOpCount in the current memory to obtain the statistical data for the last period, and the whole is used as the final statistical result.
[0020] In a second aspect, the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor executes the program to implement the method as described above.
[0021] Thirdly, the present invention provides a computer-readable storage medium storing a computer program, characterized in that the computer program is executed by a processor to perform the steps of the method.
[0022] Compared with existing technologies, the method for real-time statistical analysis of user operation behavior data described in this invention has the following advantages:
[0023] The method described in this invention does not require immediate recording of each user action in a database or log file; instead, it is first stored in memory. A timed save is performed after a certain period, while also considering the write-back of memory data upon system exit. This significantly improves the efficiency of caching and logging, allowing numerous operations to be performed in memory or cache quickly and accurately. Ultimately, it enables real-time statistics of the number of user actions of a certain type, resulting in a better user experience when displaying and analyzing data. Attached Figure Description
[0024] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an undue limitation of the invention. In the drawings:
[0025] Figure 1 This is a flowchart of the method described in an embodiment of the present invention;
[0026] Figure 2 This is a flowchart illustrating the statistical analysis of the specific operation as described in an embodiment of the present invention. Detailed Implementation
[0027] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other.
[0028] The present invention will now be described in detail with reference to the accompanying drawings and embodiments.
[0029] like Figure 1 As shown, a method for real-time statistical analysis of user behavior data includes the following steps:
[0030] S1. Establish a dictionary-type memory data structure named UserMemData in memory. The key of the memory data structure is the username, and the content value is an array. The first element of the array records the number of times a specific operation is performed, with an initial value of 0, denoted as MemOpCount. The second element of the array records the time when MemOpCount changes from 0 to 1, denoted as MemFirstTime. Each username corresponds to one key. The memory data structure is initialized when the system starts.
[0031] It should be further explained that in the aforementioned memory data structure, the Key is the username, which is a string type, and the Value is an array. The first element of the array is an integer, and the second element is data in date / time format. At startup, the first element is 0, and the second element is null. If the number of users is uncertain, UserMemData is initialized as an empty dictionary-type data structure.
[0032] S2. When the user performs the specific operation, update MemOpCount and assign a value to MemFirstTime based on MemOpCount. Specifically, the user's username is used as the key to retrieve the corresponding content value from the memory data structure. This content value includes MemOpCount and MemFirstTime. MemOpCount is incremented by 1. If the current MemOpCount is equal to 1, then MemFirstTime is assigned the current time. If the current MemOpCount is greater than 1, then MemFirstTime remains unchanged.
[0033] It should be further explained that when reading data from UserMemData, if the key does not exist, a key can be added to UserMemData with the username as the key, and the first element of the value being 1, and the second element being the current time. This method avoids storing all user-related data in memory at once; data can be created only when new user actions are encountered.
[0034] S3. Execute a scheduled task, which is executed once in a loop within each task time period, and the task time period is less than or equal to 24 hours; the scheduled task includes saving the current username, MemOpCount, and MemFirstTime to a log file; and after saving, setting the MemOpCount of all users in the memory data structure to zero and assigning the MemFirstTime of all users to null.
[0035] In step S3, the task time period is less than or equal to the statistical time period, and the scheduled task is executed at least once a day at midnight. The statistical time period refers to the interval between daily, weekly, monthly, and yearly user operations used to analyze user behavior.
[0036] It should be further explained that each record in the log file contains the username, MemOpCount, and MemFirstTime. When MemOpCount is 1, MemFirstTime represents the execution time of the operation; when MemOpCount is greater than 1, MemFirstTime represents the start execution time of multiple operations. Since the task time period is less than or equal to the statistical time period, and it is guaranteed to be executed at least once at midnight every day, this ensures that the multiple operations represented by MemOpCount in a log record do not span across days, nor do they exceed the range of the minimum statistical time period. This ensures the accuracy of subsequent statistical results.
[0037] In step S3, when saving the current username, MemOpCount, and MemFirstTime to the log file, it can be saved as a text file or as a database. Each saved record contains the username, MemOpCount, and MemFirstTime. At the same time, all data containing the relevant user and MemFirstTime time point in the cache is cleared.
[0038] It should be further explained that: through a scheduled task, at each fixed task time period, all keys in UserMemData are traversed, and the value of the number of user operations is saved to the log file, as follows:
[0039] a) The log file can be saved in any way, such as in a database or a simple text-based log file. b) Each record must include at least: username, number of operations, and the initial time corresponding to each operation. c) The task time period must be less than or equal to the statistical time period. The scheduled task should also execute once daily at midnight. d) After iteration, all operation counts (MemOpCount) in UserMemData should be reset to zero, and MemFirstTime should be set to null.
[0040] S4. Perform statistics on the specific operations, such as... Figure 2 As shown, the execution steps include:
[0041] S401. Obtain query parameters, including username, statistical time period, start time of statistics, and end time of statistics; the statistical results during the statistics are used to obtain a specific operation count sequence at intervals according to the statistical time period, which can be days, weeks, months, or years; S402. Check in the cache whether there is a statistical result corresponding to the query parameters. If the statistical result does not exist, calculate the statistical result based on the query parameters in the log file and save the statistical result to the cache; S403. Merge the obtained statistical result with the MemOpCount value of the user in memory to form the final statistical result.
[0042] In step S402, the step of calculating the statistical result based on the query parameters in the log file includes: S410, obtaining a specified time period based on the start time and end time of the statistics, and acquiring records for the specified time period; S411, extracting multiple records in the log file whose MemFirstTime belongs to a certain statistical time period based on the statistical time period, summing the MemOpCount in the records, and obtaining the total number of operations for the relevant statistical time period; S412, using the total number of operations sequence obtained by dividing according to the statistical time period based on the specified time period as the statistical result.
[0043] In step S402, saving the statistical results to the cache means saving the statistical results from the log file to the high-speed storage system. Subsequently, the high-speed storage system can directly obtain the statistical results according to specified parameters.
[0044] It should be further explained that the caching module is designed for a specific user. Based on the specified query parameters, it saves the actual statistical results to a high-speed storage system. When the same query parameters are used again in the future, there is no need to perform statistical analysis in the log file. Instead, the results can be obtained directly from the cache, which improves the performance and efficiency of the system.
[0045] In step S402, when calculating the statistical results based on the query parameters in the log file, it is necessary to further explain that: Generally, statistical analysis often uses time periods such as days, weeks, months, and years, meaning it's necessary to obtain the number of operations per day, week, month, or year within a certain time range. The time period for scheduled tasks can be days or less, resulting in multiple log records per day. However, during statistical analysis, by summing the number of operations in log records where MemFirstTime belongs to the same day, we can obtain the statistical data of user operations for that day. Similarly, by summing the number of operations in log records where MemFirstTime belongs to a week, month, or year, we can obtain relevant statistical data for weeks, months, and years. For example, if we need to obtain statistical data for January in daily units, the result is generally a sequence showing the total number of operations for each day from January 1st to 31st. Ultimately, this invention can obtain a sequence of user operation counts for any given period of time, whether daily, weekly, monthly, or yearly.
[0046] In step S403, the step of merging the obtained statistical results and the MemOpCount value of the user in memory includes: S413, if the statistical cutoff time is less than or equal to MemFirstTime, then no merging is required, and the current statistical result is directly used as the final statistical result; S414, if the statistical cutoff time is greater than MemFirstTime, then the number of operations in the last period in the current statistical result is added to the MemOpCount in the current memory to obtain the statistical data for the last period, and the whole is used as the final statistical result.
[0047] It should be further explained that if the statistics are based on a historical time and do not cover the MemFirstTime time in memory, then the records in the cache or log file are valid and can be used directly. If they cover MemFirstTime, it means that some operation counts are stored in memory, and the data in memory is the latest data. Therefore, it is sufficient to add these data to the statistics of the last period.
[0048] The following steps are also included: S5. When the system exits or terminates, the memory data structure is traversed in advance, and the MemOpCount and MemFirstTime of all users are written back to the log file. At the same time, all data containing the relevant user and MemFirstTime time point in the cache is cleared.
[0049] To further illustrate the operational flow of the method of the present invention for performing statistics on the specific operation, the following example is provided:
[0050] The first step is to specify the query parameters to be analyzed, including the username, the statistical time period, the start time of the statistical analysis, and the end time of the statistical analysis.
[0051] The second step is to obtain the user's content value, Value, through UserMemData, including MemOpCount and MemFirstTime.
[0052] The third step involves checking the cache for corresponding statistical results based on the query parameters. The caching module saves the statistical results from the log files to a high-speed storage system. The statistical results can then be quickly retrieved again based on the query parameters. The method for retrieving statistical results from the log files will be discussed in the next step.
[0053] Fourth step: If the statistical results are not found in the cache, firstly, obtain all log records within the specified time range from the log file; secondly, based on the statistical time period, merge the number of operations in the log records within each time period to form the total number of operations within the current time period; thirdly, form the number of operations for each time period within a certain time range as the statistical result; finally, save the query parameters and the corresponding statistical results to the cache.
[0054] Step 5: Compare the cutoff time of this statistic with the last modification time in memory.
[0055] a) If the deadline is less than or equal to MemFirstTime, the statistical results from the previous step are valid and are the final result. b) If the deadline is greater than MemFirstTime, the number of operations in the last period of the statistical results from the previous step is added to MemOpCount to form the statistical data for that period. The data for other periods remain unchanged, and the whole is taken as the final result.
[0056] Using the method of this invention, each user operation does not need to be immediately recorded in the database or log file, but is first saved in memory. A timed save is performed after a certain period, also taking into account the write-back of memory data when the system exits. MemOpCount may be greater than 1. In this case, the time when MemOpCount is 1 corresponding to MemFirstTime is not recorded. Subsequent operations when MemOpCount is greater than 1 are not recorded, but because the period of the scheduled task is shorter than the statistical time period, this issue does not affect the accuracy of the statistical results.
[0057] By statistically analyzing the specific operations, the validity of cached or log entries can be determined using the MemFirstTime time. When the recorded content is invalid, it simply needs to be merged with the data in memory. Therefore, this invention significantly improves the efficiency of caching and logging, allowing numerous operations to be performed in memory or cache quickly and accurately. Ultimately, it enables real-time statistics of the number of user actions for a specific type, providing a better user experience for display and analysis. The data structure stored in memory only includes the username, MemOpCount, and MemFirstTime values, resulting in low memory consumption. Furthermore, in a local area network application with a limited user base, a typical server can easily support storing this type of data in memory.
[0058] Those skilled in the art will understand that all or part of the steps in the methods of the above embodiments can be implemented by hardware related to program instructions. The program can be stored in a computer-readable storage medium. When the program is executed, it includes the following steps: the storage medium, such as ROM / RAM, magnetic disk, optical disk, etc.
[0059] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method of statistically collecting user operational behavior data in real time, the method comprising: Includes the following steps: S1. Establish a dictionary-type memory data structure in memory. The key of the memory data structure is the username, and the content value is an array. The first element of the array records the number of times a specific operation is performed, and its initial value is 0, denoted as MemOpCount. The second element of the array records the time when MemOpCount changes from 0 to 1, denoted as MemFirstTime. Each username corresponds to one key. The memory data structure is initialized when the system starts. S2. When the user performs the specific operation, update MemOpCount and assign a value to MemFirstTime based on MemOpCount. Specifically, the user's username is used as the key to retrieve the corresponding content value from the memory data structure. This content value includes MemOpCount and MemFirstTime. MemOpCount is incremented by 1. If the current MemOpCount is equal to 1, then MemFirstTime is assigned the current time. If the current MemOpCount is greater than 1, then MemFirstTime remains unchanged. S3. Execute a scheduled task, which is executed once in a loop within each task time period, wherein the task time period is less than or equal to 24 hours; the scheduled task includes: saving the current username, MemOpCount, and MemFirstTime to a log file; and after saving, resetting the MemOpCount of all users in the memory data structure to zero and assigning the MemFirstTime of all users to null. S4. Perform statistics on the specific operations, including the following steps: S401. Obtain query parameters, including username, statistical time period, start time of statistics, and end time of statistics; the statistical results during the statistics are used to obtain a specific operation count sequence at intervals according to the statistical time period, which is a day, week, month, or year; S402. Check if there is a statistical result corresponding to the query parameter in the cache. If the statistical result does not exist, calculate the statistical result based on the query parameter in the log file and save the statistical result to the cache. S403. Combine the obtained statistical results with the MemOpCount value of the user in memory to form the final statistical results; In step S403, the step of merging the obtained statistical results with the MemOpCount value of the user in memory includes: S413. If the statistical cutoff time is less than or equal to MemFirstTime, then there is no need to merge, and the current statistical result is used directly as the final statistical result. S414. If the statistical cutoff time is greater than MemFirstTime, then add the number of operations in the last period in the current statistical result to the MemOpCount in the current memory as the statistical data for the last period, and take the whole as the final statistical result.
2. The method of claim 1, wherein: In step S3, the task time period is less than or equal to the statistical time period, and the scheduled task is executed at least once a day at midnight.
3. The method of claim 1, wherein: In step S3, when saving the current username, MemOpCount, and MemFirstTime to the log file, it can be saved as a text file or as a database. Each saved record contains the username, MemOpCount, and MemFirstTime. At the same time, all data containing the relevant user and MemFirstTime time point in the cache is cleared.
4. The method of claim 1, wherein: It also includes the following steps: S5. When the system exits or terminates, the memory data structure is traversed in advance, and the MemOpCount and MemFirstTime of all users are written back to the log file. At the same time, the cache containing data of relevant users and MemFirstTime points is cleared.
5. The method of claim 1, wherein: In step S402, the step of calculating statistical results based on query parameters in the log file includes: S410. Based on the start time and end time of the statistics shown, a specified time period is obtained, and the records of the specified time period are acquired. S411. Based on the statistical time period, extract multiple records in the log file that belong to a certain statistical time period for MemFirstTime, sum up the MemOpCount in the records, and obtain the total number of operations in the relevant statistical time period. S412. Based on the specified time period, and divided according to the statistical time period, the obtained sequence of the total number of operations is used as the statistical result.
6. The method of claim 1, wherein: In step S402, saving the statistical results to the cache means saving the statistical results from the log file to the high-speed storage system. Subsequently, the high-speed storage system can directly obtain the statistical results according to specified parameters.
7. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the method as described in any one of claims 1 to 6.
8. A computer-readable storage medium having stored thereon a computer program, characterized in that When a computer program is run by a processor, it performs the steps of the method described in any one of claims 1 to 6.
Citation Information
Patent Citations
User behavior data acquisition method and device, computer equipment and storage medium
CN114138605A