mengxaingshuo commited on
Commit
d49c7d9
·
verified ·
1 Parent(s): 02156e8

Upload logbook_availability_query.py

Browse files
Files changed (1) hide show
  1. logbook_availability_query.py +401 -0
logbook_availability_query.py ADDED
@@ -0,0 +1,401 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import os
3
+ from datetime import datetime
4
+
5
+
6
+ # ============================
7
+ # 自动定位项目路径
8
+ # ============================
9
+
10
+ BASE_DIR = os.path.dirname(
11
+ os.path.dirname(
12
+ os.path.abspath(__file__)
13
+ )
14
+ )
15
+
16
+
17
+ DATA_PATH = os.path.join(
18
+ BASE_DIR,
19
+ "data",
20
+ "logbook_availability.csv"
21
+ )
22
+
23
+
24
+ OUTPUT_DIR = os.path.join(
25
+ BASE_DIR,
26
+ "output"
27
+ )
28
+
29
+
30
+
31
+ # ============================
32
+ # markdown生成函数
33
+ # 替代 pandas.to_markdown()
34
+ # ============================
35
+
36
+ def dataframe_to_markdown(df, max_rows=10):
37
+
38
+ if df.empty:
39
+ return "暂无数据"
40
+
41
+
42
+ df=df.head(max_rows)
43
+
44
+
45
+ columns=df.columns.tolist()
46
+
47
+
48
+ md="| " + " | ".join(columns)+" |\n"
49
+
50
+ md+="| "+" | ".join(
51
+ ["---"]*len(columns)
52
+ )+" |\n"
53
+
54
+
55
+ for _,row in df.iterrows():
56
+
57
+ md+="| "+" | ".join(
58
+ str(x)
59
+ for x in row.tolist()
60
+ )+" |\n"
61
+
62
+
63
+ return md
64
+
65
+
66
+
67
+ # ============================
68
+ # 参数检查
69
+ # ============================
70
+
71
+ def check_year(value,name):
72
+
73
+ if value is None:
74
+ return None
75
+
76
+
77
+ try:
78
+ return int(value)
79
+
80
+ except:
81
+
82
+ raise ValueError(
83
+ f"{name}必须是年份数字,例如2020"
84
+ )
85
+
86
+
87
+
88
+
89
+ # ============================
90
+ # 主查询函数
91
+ # ============================
92
+
93
+ def query_logbook_availability(
94
+ region=None,
95
+ year_start=None,
96
+ year_end=None,
97
+ species=None,
98
+ data_type=None,
99
+ output_format="markdown"
100
+ ):
101
+
102
+ """
103
+ 查询logbook数据可用性
104
+
105
+ 返回:
106
+ summary
107
+ records
108
+ preview_markdown
109
+ csv_path
110
+ excel_path
111
+ source_files
112
+
113
+ """
114
+
115
+
116
+ try:
117
+
118
+ # ------------------
119
+ # 参数校验
120
+ # ------------------
121
+
122
+ year_start = check_year(
123
+ year_start,
124
+ "year_start"
125
+ )
126
+
127
+ year_end = check_year(
128
+ year_end,
129
+ "year_end"
130
+ )
131
+
132
+
133
+
134
+ if year_start and year_end:
135
+
136
+ if year_start > year_end:
137
+
138
+ return {
139
+
140
+ "summary":
141
+ "错误:开始年份不能大于结束年份",
142
+
143
+ "records":[]
144
+
145
+ }
146
+
147
+
148
+
149
+ # ------------------
150
+ # 文件检查
151
+ # ------------------
152
+
153
+ if not os.path.exists(DATA_PATH):
154
+
155
+ return {
156
+
157
+ "summary":
158
+ f"找不到数据文件:{DATA_PATH}",
159
+
160
+ "records":[]
161
+
162
+ }
163
+
164
+
165
+
166
+ # ------------------
167
+ # 读取数据
168
+ # ------------------
169
+
170
+ df=pd.read_csv(
171
+ DATA_PATH
172
+ )
173
+
174
+
175
+ # year强制转换
176
+
177
+ if "year" in df.columns:
178
+
179
+ df["year"]=pd.to_numeric(
180
+ df["year"],
181
+ errors="coerce"
182
+ )
183
+
184
+
185
+ result=df.copy()
186
+
187
+
188
+
189
+ # ------------------
190
+ # 条件过滤
191
+ # ------------------
192
+
193
+ if region:
194
+
195
+ result=result[
196
+ result["region"]
197
+ .astype(str)
198
+ .str.contains(
199
+ region,
200
+ na=False
201
+ )
202
+ ]
203
+
204
+
205
+
206
+ if species:
207
+
208
+ result=result[
209
+ result["species"]
210
+ .astype(str)
211
+ .str.contains(
212
+ species,
213
+ na=False
214
+ )
215
+ ]
216
+
217
+
218
+
219
+ if data_type:
220
+
221
+ result=result[
222
+ result["data_type"]
223
+ .astype(str)
224
+ .str.contains(
225
+ data_type,
226
+ na=False
227
+ )
228
+ ]
229
+
230
+
231
+
232
+ if year_start:
233
+
234
+ result=result[
235
+ result.year>=year_start
236
+ ]
237
+
238
+
239
+
240
+ if year_end:
241
+
242
+ result=result[
243
+ result.year<=year_end
244
+ ]
245
+
246
+
247
+
248
+ # ------------------
249
+ # 无结果
250
+ # ------------------
251
+
252
+ if result.empty:
253
+
254
+ return {
255
+
256
+ "summary":
257
+ "没有找到符合条件的logbook数据",
258
+
259
+ "records":[],
260
+
261
+ "preview_markdown":
262
+ "暂无数据",
263
+
264
+ "source_files":
265
+ [
266
+ DATA_PATH
267
+ ]
268
+
269
+ }
270
+
271
+
272
+
273
+ # ------------------
274
+ # 统计信息
275
+ # ------------------
276
+
277
+ summary={
278
+
279
+ "records_count":
280
+ len(result),
281
+
282
+ "year_range":
283
+ [
284
+ int(result.year.min()),
285
+ int(result.year.max())
286
+ ],
287
+
288
+ "regions":
289
+ result.region.unique().tolist(),
290
+
291
+ "species":
292
+ result.species.unique().tolist()
293
+
294
+ }
295
+
296
+
297
+
298
+ # ------------------
299
+ # 输出文件
300
+ # ------------------
301
+
302
+ os.makedirs(
303
+ OUTPUT_DIR,
304
+ exist_ok=True
305
+ )
306
+
307
+
308
+ timestamp=datetime.now()\
309
+ .strftime("%Y%m%d_%H%M%S")
310
+
311
+
312
+ csv_path=None
313
+ excel_path=None
314
+
315
+
316
+
317
+ if output_format in [
318
+ "csv",
319
+ "excel"
320
+ ]:
321
+
322
+ csv_path=os.path.join(
323
+ OUTPUT_DIR,
324
+ f"logbook_{timestamp}.csv"
325
+ )
326
+
327
+
328
+ result.to_csv(
329
+ csv_path,
330
+ index=False,
331
+ encoding="utf-8-sig"
332
+ )
333
+
334
+
335
+
336
+ if output_format=="excel":
337
+
338
+ excel_path=os.path.join(
339
+ OUTPUT_DIR,
340
+ f"logbook_{timestamp}.xlsx"
341
+ )
342
+
343
+
344
+ result.to_excel(
345
+ excel_path,
346
+ index=False
347
+ )
348
+
349
+
350
+
351
+ # ------------------
352
+ # Agent标准返回
353
+ # ------------------
354
+
355
+ return {
356
+
357
+
358
+ "summary":
359
+ summary,
360
+
361
+
362
+ "records":
363
+ result.to_dict(
364
+ orient="records"
365
+ ),
366
+
367
+
368
+ "preview_markdown":
369
+ dataframe_to_markdown(
370
+ result
371
+ ),
372
+
373
+
374
+ "csv_path":
375
+ csv_path,
376
+
377
+
378
+ "excel_path":
379
+ excel_path,
380
+
381
+
382
+ "source_files":
383
+ [
384
+ DATA_PATH
385
+ ]
386
+
387
+ }
388
+
389
+
390
+
391
+ except Exception as e:
392
+
393
+
394
+ return {
395
+
396
+ "summary":
397
+ f"查询失败:{str(e)}",
398
+
399
+ "records":[]
400
+
401
+ }