File size: 12,218 Bytes
519b145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import argparse
import csv
import os
import sys
from datetime import datetime, date
from pathlib import Path
from typing import Dict, Optional

import django
from pydantic import BaseModel, ValidationError, field_validator


ROOT_DIR = Path(__file__).resolve().parents[2]
BACKEND_DIR = ROOT_DIR / "backend"
HUE_PORTAL_DIR = BACKEND_DIR / "hue_portal"
DEFAULT_DATA_DIR = ROOT_DIR / "tài nguyên"
DATA_DIR = Path(os.environ.get("ETL_DATA_DIR", DEFAULT_DATA_DIR))
LOG_DIR = ROOT_DIR / "backend" / "logs" / "data_quality"

for path in (HUE_PORTAL_DIR, BACKEND_DIR, ROOT_DIR):
    if str(path) not in sys.path:
        sys.path.insert(0, str(path))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hue_portal.hue_portal.settings")
django.setup()

from hue_portal.core.models import Fine, Office, Procedure, Advisory  # noqa: E402


LOG_DIR.mkdir(parents=True, exist_ok=True)


class OfficeRecord(BaseModel):
    unit_name: str
    address: Optional[str] = ""
    district: Optional[str] = ""
    working_hours: Optional[str] = ""
    phone: Optional[str] = ""
    email: Optional[str] = ""
    latitude: Optional[float]
    longitude: Optional[float]
    service_scope: Optional[str] = ""
    updated_at: Optional[datetime]

    @field_validator("unit_name")
    @classmethod
    def unit_name_not_blank(cls, value: str) -> str:
        if not value:
            raise ValueError("unit_name is required")
        return value


class FineRecord(BaseModel):
    violation_code: str
    violation_name: Optional[str] = ""
    article: Optional[str] = ""
    decree: Optional[str] = ""
    min_fine: Optional[float]
    max_fine: Optional[float]
    license_points: Optional[str] = ""
    remedial_measures: Optional[str] = ""
    source_url: Optional[str] = ""
    updated_at: Optional[datetime]

    @field_validator("violation_code")
    @classmethod
    def code_not_blank(cls, value: str) -> str:
        if not value:
            raise ValueError("violation_code is required")
        return value


class ProcedureRecord(BaseModel):
    title: str
    domain: Optional[str] = ""
    level: Optional[str] = ""
    conditions: Optional[str] = ""
    dossier: Optional[str] = ""
    fee: Optional[str] = ""
    duration: Optional[str] = ""
    authority: Optional[str] = ""
    source_url: Optional[str] = ""
    updated_at: Optional[datetime]

    @field_validator("title")
    @classmethod
    def title_not_blank(cls, value: str) -> str:
        if not value:
            raise ValueError("title is required")
        return value


class AdvisoryRecord(BaseModel):
    title: str
    summary: str
    source_url: Optional[str] = ""
    published_at: Optional[date]

    @field_validator("title")
    @classmethod
    def title_not_blank(cls, value: str) -> str:
        if not value:
            raise ValueError("title is required")
        return value

    @field_validator("summary")
    @classmethod
    def summary_not_blank(cls, value: str) -> str:
        if not value:
            raise ValueError("summary is required")
        return value


def parse_datetime(value: Optional[str]) -> Optional[datetime]:
    if not value:
        return None
    for fmt in ("%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%Y/%m/%d", "%d/%m/%Y"):
        try:
            return datetime.strptime(value, fmt)
        except ValueError:
            continue
    try:
        return datetime.fromisoformat(value)
    except ValueError:
        return None


def parse_date(value: Optional[str]) -> Optional[datetime]:
    """Parse date string to datetime.date object (for Advisory.published_at)"""
    if not value:
        return None
    for fmt in ("%Y-%m-%d", "%Y/%m/%d", "%d/%m/%Y"):
        try:
            dt = datetime.strptime(value, fmt)
            return dt.date()
        except ValueError:
            continue
    return None


def log_error(file_handle, dataset: str, row: Dict[str, str], error: str) -> None:
    file_handle.write(
        f"[{datetime.utcnow().isoformat()}Z] dataset={dataset} error={error} row={row}\n"
    )


def should_skip(updated_at: Optional[datetime], since: Optional[datetime]) -> bool:
    if not since or not updated_at:
        return False
    return updated_at < since


def load_offices(since: Optional[datetime], dry_run: bool, log_file) -> int:
    path = DATA_DIR / "danh_ba_diem_tiep_dan.csv"
    if not path.exists():
        log_error(log_file, "offices", {}, f"File không tồn tại: {path}")
        return 0

    processed = 0
    with path.open(encoding="utf-8") as handle:
        reader = csv.DictReader(handle)
        for row in reader:
            row = {k: (v or "").strip() for k, v in row.items()}
            for key in ["latitude", "longitude"]:
                if row.get(key) == "":
                    row[key] = None
            row["updated_at"] = parse_datetime(row.get("updated_at"))
            try:
                record = OfficeRecord(**row)
            except ValidationError as exc:
                log_error(log_file, "offices", row, str(exc))
                continue

            if should_skip(record.updated_at, since):
                continue

            processed += 1
            if dry_run:
                continue

            Office.objects.update_or_create(
                unit_name=record.unit_name,
                defaults={
                    "address": record.address or "",
                    "district": record.district or "",
                    "working_hours": record.working_hours or "",
                    "phone": record.phone or "",
                    "email": record.email or "",
                    "latitude": record.latitude,
                    "longitude": record.longitude,
                    "service_scope": record.service_scope or "",
                },
            )
    return processed


def load_fines(since: Optional[datetime], dry_run: bool, log_file) -> int:
    path = DATA_DIR / "muc_phat_theo_hanh_vi.csv"
    if not path.exists():
        log_error(log_file, "fines", {}, f"File không tồn tại: {path}")
        return 0

    processed = 0
    with path.open(encoding="utf-8") as handle:
        reader = csv.DictReader(handle)
        for row in reader:
            row = {k: (v or "").strip() for k, v in row.items()}
            for key in ["min_fine", "max_fine"]:
                if row.get(key) == "":
                    row[key] = None
            row["updated_at"] = parse_datetime(row.get("updated_at"))
            try:
                record = FineRecord(**row)
            except ValidationError as exc:
                log_error(log_file, "fines", row, str(exc))
                continue

            if should_skip(record.updated_at, since):
                continue

            processed += 1
            if dry_run:
                continue

            Fine.objects.update_or_create(
                code=record.violation_code,
                defaults={
                    "name": record.violation_name or "",
                    "article": record.article or "",
                    "decree": record.decree or "",
                    "min_fine": record.min_fine,
                    "max_fine": record.max_fine,
                    "license_points": record.license_points or "",
                    "remedial": record.remedial_measures or "",
                    "source_url": record.source_url or "",
                },
            )
    return processed


def load_procedures(since: Optional[datetime], dry_run: bool, log_file) -> int:
    path = DATA_DIR / "thu_tuc_hanh_chinh.csv"
    if not path.exists():
        log_error(log_file, "procedures", {}, f"File không tồn tại: {path}")
        return 0

    processed = 0
    with path.open(encoding="utf-8") as handle:
        reader = csv.DictReader(handle)
        for row in reader:
            # Clean row: ensure keys and values are strings
            clean_row = {}
            for k, v in row.items():
                key = str(k).strip() if k else ""
                value = (v.strip() if isinstance(v, str) else str(v or "")) if v else ""
                clean_row[key] = value
            clean_row["updated_at"] = parse_datetime(clean_row.get("updated_at"))
            try:
                record = ProcedureRecord(**clean_row)
            except ValidationError as exc:
                log_error(log_file, "procedures", clean_row, str(exc))
                continue

            if should_skip(record.updated_at, since):
                continue

            processed += 1
            if dry_run:
                continue

            Procedure.objects.update_or_create(
                title=record.title,
                domain=record.domain or "",
                defaults={
                    "level": record.level or "",
                    "conditions": record.conditions or "",
                    "dossier": record.dossier or "",
                    "fee": record.fee or "",
                    "duration": record.duration or "",
                    "authority": record.authority or "",
                    "source_url": record.source_url or "",
                },
            )
    return processed


def load_advisories(since: Optional[datetime], dry_run: bool, log_file) -> int:
    path = DATA_DIR / "canh_bao_lua_dao.csv"
    if not path.exists():
        log_error(log_file, "advisories", {}, f"File không tồn tại: {path}")
        return 0

    processed = 0
    with path.open(encoding="utf-8") as handle:
        reader = csv.DictReader(handle)
        for row in reader:
            # Clean row: ensure keys and values are strings
            clean_row = {}
            for k, v in row.items():
                key = str(k).strip() if k else ""
                value = (v.strip() if isinstance(v, str) else str(v or "")) if v else ""
                clean_row[key] = value
            clean_row["published_at"] = parse_date(clean_row.get("published_at"))
            try:
                record = AdvisoryRecord(**clean_row)
            except ValidationError as exc:
                log_error(log_file, "advisories", clean_row, str(exc))
                continue

            # Advisory không có updated_at, chỉ check published_at nếu since được set
            if since and record.published_at:
                if record.published_at < since.date():
                    continue

            processed += 1
            if dry_run:
                continue

            Advisory.objects.update_or_create(
                title=record.title,
                defaults={
                    "summary": record.summary or "",
                    "source_url": record.source_url or "",
                    "published_at": record.published_at,
                },
            )
    return processed


def parse_args():
    parser = argparse.ArgumentParser(description="ETL dữ liệu chatbot")
    parser.add_argument("--since", help="Chỉ xử lý bản ghi có updated_at >= giá trị này (ISO date)")
    parser.add_argument("--dry-run", action="store_true", help="Chỉ kiểm tra dữ liệu, không ghi vào DB")
    parser.add_argument("--datasets", nargs="*", default=["offices", "fines"], choices=["offices", "fines", "procedures", "advisories"], help="Chọn dataset cần nạp")
    return parser.parse_args()


def main():
    args = parse_args()
    since = parse_datetime(args.since) if args.since else None
    log_path = LOG_DIR / f"etl_{datetime.utcnow().strftime('%Y%m%d%H%M%S')}.log"

    with log_path.open("a", encoding="utf-8") as log_file:
        if "offices" in args.datasets:
            total = load_offices(since, args.dry_run, log_file)
            print(f"Offices processed: {total}")
        if "fines" in args.datasets:
            total = load_fines(since, args.dry_run, log_file)
            print(f"Fines processed: {total}")
        if "procedures" in args.datasets:
            total = load_procedures(since, args.dry_run, log_file)
            print(f"Procedures processed: {total}")
        if "advisories" in args.datasets:
            total = load_advisories(since, args.dry_run, log_file)
            print(f"Advisories processed: {total}")

    print(f"Log ghi tại {log_path}")


if __name__ == "__main__":
    main()