1
+ <?php
2
+ use Swoole \Coroutine \Lock ;
3
+ class Database {
4
+ private $ dirPath ;
5
+
6
+ public function __construct () {
7
+ if (!$ this ->dirPath ) {
8
+ $ base_dir = api::getconfig ();
9
+ $ this ->dirPath = $ base_dir ['file ' ]['database_dir ' ];
10
+ }
11
+ }
12
+
13
+ public function initializeDatabase () {
14
+ $ filename = $ this ->dirPath . '/ ' . date ('Ymd ' );
15
+ if (!is_dir ($ this ->dirPath )) {
16
+ mkdir ($ this ->dirPath , 0777 , true );
17
+ }
18
+ if (!file_exists ($ filename )) {
19
+ $ initialData = [];
20
+ for ($ hour = 0 ; $ hour < 24 ; ++$ hour ) {
21
+ $ timeKey = date ('Ymd ' ) . str_pad ($ hour , 2 , '0 ' , STR_PAD_LEFT );
22
+ $ initialData [$ timeKey ] = ['hits ' => 0 , 'bytes ' => 0 ];
23
+ }
24
+ file_put_contents ($ filename , json_encode ($ initialData , JSON_PRETTY_PRINT ));
25
+ }
26
+ }
27
+
28
+ public function writeDatabase ($ hits , $ bytes ) {
29
+ $ filename = $ this ->dirPath . '/ ' . date ('Ymd ' );
30
+ if (!file_exists ($ filename )) {
31
+ $ this ->initializeDatabase ();
32
+ }
33
+ $ data = json_decode (Swoole \Coroutine \System::readFile ($ filename ), true );
34
+ $ timeKey = date ('Ymd ' ) . str_pad (date ('G ' ), 2 , '0 ' , STR_PAD_LEFT );
35
+ if (!isset ($ data [$ timeKey ])) {
36
+ $ data [$ timeKey ] = ['hits ' => 0 , 'bytes ' => 0 ];
37
+ }
38
+ $ data [$ timeKey ]['hits ' ] += $ hits ;
39
+ $ data [$ timeKey ]['bytes ' ] += $ bytes ;
40
+ $ w = Swoole \Coroutine \System::writeFile ($ filename , json_encode ($ data , JSON_PRETTY_PRINT ));
41
+ }
42
+
43
+ public function getDaysData (): array {
44
+ $ dailyTraffic = [];
45
+ $ endDate = date ('Ymd ' );
46
+ $ startDate = date ('Ymd ' , strtotime ('-14 days ' ));
47
+
48
+ for ($ i = 0 ; $ i <= 14 ; ++$ i ) {
49
+ $ date = date ('Ymd ' , strtotime ("- $ i days " , strtotime ($ endDate )));
50
+ $ filename = $ this ->dirPath . '/ ' . $ date ;
51
+ $ dailySummary = ['hits ' => 0 , 'bytes ' => 0 ];
52
+ if (file_exists ($ filename )) {
53
+ $ data = json_decode (Swoole \Coroutine \System::readFile ($ filename ), true );
54
+ foreach ($ data as $ hourlyRecord ) {
55
+ if (isset ($ hourlyRecord ['hits ' ]) && isset ($ hourlyRecord ['bytes ' ])) {
56
+ $ dailySummary ['hits ' ] += $ hourlyRecord ['hits ' ];
57
+ $ dailySummary ['bytes ' ] += $ hourlyRecord ['bytes ' ];
58
+ }
59
+ }
60
+ }
61
+ $ dailyTraffic [$ date ] = $ dailySummary ;
62
+ }
63
+ ksort ($ dailyTraffic );
64
+ return $ dailyTraffic ;
65
+ }
66
+
67
+ public function getMonthsData (): array {
68
+ $ monthlyTraffic = [];
69
+ $ endDate = new DateTime (); // 获取当前日期
70
+ $ startDate = clone $ endDate ;
71
+ $ startDate ->modify ('-11 months ' ); // 回溯11个月,以包含完整的12个月数据
72
+
73
+ while ($ startDate <= $ endDate ) {
74
+ $ monthStr = $ startDate ->format ('Ym ' ); // 格式化为YYYYMM格式
75
+ $ monthlySummary = ['hits ' => 0 , 'bytes ' => 0 ];
76
+
77
+ for ($ day = 1 ; $ day <= $ startDate ->format ('t ' ); ++$ day ) { // 遍历当月的所有天
78
+ $ dateStr = $ startDate ->format ('Ymd ' );
79
+ $ filename = $ this ->dirPath . '/ ' . $ dateStr ;
80
+
81
+ if (file_exists ($ filename )) {
82
+ $ data = json_decode (file_get_contents ($ filename ), true ); // 使用file_get_contents以兼容更多环境
83
+
84
+ foreach ($ data as $ hourlyRecord ) {
85
+ if (isset ($ hourlyRecord ['hits ' ]) && isset ($ hourlyRecord ['bytes ' ])) {
86
+ $ monthlySummary ['hits ' ] += $ hourlyRecord ['hits ' ];
87
+ $ monthlySummary ['bytes ' ] += $ hourlyRecord ['bytes ' ];
88
+ }
89
+ }
90
+ }
91
+ $ startDate ->modify ('+1 day ' ); // 移动到下一天
92
+ }
93
+
94
+ // 累加完一个月的数据后存入结果数组
95
+ $ monthlyTraffic [$ monthStr ] = $ monthlySummary ;
96
+ }
97
+
98
+ return $ monthlyTraffic ;
99
+ }
100
+
101
+ public function getYearsData (): array {
102
+ $ annualTraffic = [];
103
+ $ currentYear = (int )date ('Y ' );
104
+ $ startYear = $ currentYear - 5 ;
105
+
106
+ for ($ year = $ startYear ; $ year <= $ currentYear ; ++$ year ) {
107
+ $ yearHits = 0 ;
108
+ $ yearBytes = 0 ;
109
+
110
+ for ($ month = 1 ; $ month <= 12 ; ++$ month ) {
111
+ for ($ day = 1 ; $ day <= 31 ; ++$ day ) { // 假定每月最多31天,实际应用需按月份调整
112
+ $ date = sprintf ('%04d%02d%02d ' , $ year , $ month , $ day );
113
+ $ filename = $ this ->dirPath . '/ ' . $ date ;
114
+
115
+ if (file_exists ($ filename )) {
116
+ $ data = json_decode (Swoole \Coroutine \System::readFile ($ filename ), true );
117
+ foreach ($ data as $ hourlyRecord ) {
118
+ if (isset ($ hourlyRecord ['hits ' ]) && isset ($ hourlyRecord ['bytes ' ])) {
119
+ $ yearHits += $ hourlyRecord ['hits ' ];
120
+ $ yearBytes += $ hourlyRecord ['bytes ' ];
121
+ }
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ // 累计完一年的数据后存入结果数组
128
+ $ annualTraffic [$ year ] = [
129
+ 'hits ' => $ yearHits ,
130
+ 'bytes ' => $ yearBytes
131
+ ];
132
+ }
133
+
134
+ return $ annualTraffic ;
135
+ }
136
+ }
0 commit comments