-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnav_loader.py
More file actions
1266 lines (1052 loc) · 52.2 KB
/
Copy pathnav_loader.py
File metadata and controls
1266 lines (1052 loc) · 52.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
AMFI NAV Loader
Version: 1.0.0
Copyright 2025
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import argparse
import logging
import os
import time
import random
import psutil
import pandas as pd
import requests
import chardet
import sys
import mysql.connector
from datetime import datetime, timedelta, date
from typing import List, Optional, Tuple
import holidays
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class NAVLoader:
# Configuration constants
MIN_DELAY = 2 # Minimum delay between requests in seconds
MAX_DELAY = 5 # Maximum delay between requests in seconds
MAX_RETRIES = 3 # Maximum number of retries for failed requests
RETRY_DELAY = 10 # Delay between retries in seconds
NAVALL_BASE_URL = "https://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?frmdt={}&todt={}"
def __init__(self, db_config: dict = None, telegram_config: dict = None):
"""
Initialize the NAVLoader with database and notification configuration.
Args:
db_config (dict): Database configuration dictionary
telegram_config (dict): Telegram configuration dictionary
"""
self.db_config = db_config or {
'host': os.getenv('MYSQL_HOST', 'mysqldb'),
'user': os.getenv('MYSQL_USER', 'bob'),
'password': os.getenv('MYSQL_PASSWORD', 'marley'),
'database': os.getenv('MYSQL_DATABASE', 'dont_worry'),
'port': int(os.getenv('MYSQL_PORT', 3306))
}
self.telegram_config = telegram_config or {
'bot_token': os.getenv('TELEGRAM_BOT_TOKEN', ''),
'chat_id': os.getenv('TELEGRAM_CHAT_ID', '')
}
# Initialize Indian holidays
self.indian_holidays = holidays.India()
# Configure logging
self._setup_logging()
def _setup_logging(self):
"""Configure logging for the NAVLoader."""
# Create logs directory if it doesn't exist
os.makedirs('logs', exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('logs/nav_loader.log'),
logging.StreamHandler()
]
)
self.logger = logging.getLogger(__name__)
def get_connection(self):
"""Get a database connection."""
return mysql.connector.connect(**self.db_config)
def get_random_delay(self) -> float:
"""Generate a random delay between requests."""
return random.uniform(self.MIN_DELAY, self.MAX_DELAY)
def get_latest_business_day(self, date: datetime) -> datetime:
"""Get the latest business day before the given date."""
while date.weekday() >= 5: # 5 is Saturday, 6 is Sunday
date -= timedelta(days=1)
return date
def check_data_exists_in_db(self, date: datetime) -> bool:
"""
Check if data exists in the database for a specific date.
Args:
date (datetime): Date to check
Returns:
bool: True if data exists, False otherwise
"""
conn = self.get_connection()
cursor = conn.cursor()
try:
# Convert to date object if it's a datetime
date_obj = date.date() if isinstance(date, datetime) else date
query = "SELECT COUNT(*) FROM nav_data WHERE nav_date = %s"
cursor.execute(query, (date_obj,))
count = cursor.fetchone()[0]
return count > 0
finally:
cursor.close()
conn.close()
def verify_data_completeness(self, date: datetime) -> bool:
"""
Verify if data for a specific date is complete in the database.
Checks if the number of records matches expected count.
Args:
date (datetime): Date to verify
Returns:
bool: True if data is complete, False otherwise
"""
conn = self.get_connection()
cursor = conn.cursor()
try:
# Convert to date object if it's a datetime
date_obj = date.date() if isinstance(date, datetime) else date
# Get count of records for the date
query = "SELECT COUNT(*) FROM nav_data WHERE nav_date = %s"
cursor.execute(query, (date_obj,))
db_count = cursor.fetchone()[0]
# If no records, data is incomplete
if db_count == 0:
return False
# Get count from the local file if it exists
file_path = f"data/navall_{date_obj.strftime('%Y-%m-%d')}.txt"
if os.path.exists(file_path):
df = self.parse_nav_file(file_path)
file_count = len(df) if df is not None else 0
# If file count is significantly different from db count, data might be incomplete
if abs(file_count - db_count) > 5: # Allow small differences due to data cleaning
self.logger.warning(f"Data completeness mismatch for {date_obj}: "
f"DB records: {db_count}, File records: {file_count}")
return False
return True
finally:
cursor.close()
conn.close()
def download_nav_file_for_date(self, date: datetime) -> str:
"""
Download NAV file for a specific date with rate limiting and retries.
Checks both local file and database existence before downloading.
Args:
date (datetime): The date for which to download NAV data
Returns:
str: Path to the downloaded file
Raises:
Exception: If download fails after all retries
"""
# Convert to date if it's datetime
nav_date = date.date() if isinstance(date, datetime) else date
file_path = f"data/navall_{nav_date.strftime('%Y-%m-%d')}.txt"
# Check if data already exists in database
if self.check_data_exists_in_db(nav_date):
self.logger.info(f"Data for {nav_date} already exists in database")
if os.path.exists(file_path):
return file_path
# If data exists in DB but file is missing, download to maintain local copy
self.logger.info(f"Downloading missing local file for {nav_date}")
return self._fetch_nav_file(nav_date, file_path)
def _fetch_nav_file(self, nav_date, file_path: str) -> str:
"""
Download the navall.txt file for a date directly from AMFI, with rate
limiting and retries. Does not touch the database.
Args:
nav_date (date): The date to fetch data for
file_path (str): Where to save the downloaded file
Returns:
str: Path to the downloaded file
Raises:
Exception: If download fails after all retries
"""
url = self.NAVALL_BASE_URL.format(nav_date.strftime('%d-%b-%Y'), nav_date.strftime('%d-%b-%Y'))
for attempt in range(self.MAX_RETRIES):
try:
time.sleep(self.get_random_delay())
response = requests.get(url)
if response.status_code == 200 and any(char.isdigit() for char in response.text):
os.makedirs("data", exist_ok=True)
with open(file_path, "wb") as f:
f.write(response.content)
return file_path
else:
error_msg = f"Download failed for {nav_date}. Status code: {response.status_code}"
if attempt < self.MAX_RETRIES - 1:
self.logger.warning(f"{error_msg}. Retrying in {self.RETRY_DELAY} seconds...")
time.sleep(self.RETRY_DELAY)
else:
self.logger.error(f"{error_msg}. Response content: {response.text[:200]}...")
raise Exception(error_msg)
except requests.exceptions.RequestException as e:
error_msg = f"Network error for {nav_date}: {str(e)}"
if attempt < self.MAX_RETRIES - 1:
self.logger.warning(f"{error_msg}. Retrying in {self.RETRY_DELAY} seconds...")
time.sleep(self.RETRY_DELAY)
else:
self.logger.error(error_msg)
raise Exception(error_msg)
except Exception as e:
error_msg = f"Unexpected error for {nav_date}: {str(e)}"
if attempt < self.MAX_RETRIES - 1:
self.logger.warning(f"{error_msg}. Retrying in {self.RETRY_DELAY} seconds...")
time.sleep(self.RETRY_DELAY)
else:
self.logger.error(error_msg)
raise Exception(error_msg)
# Maps our internal field names to the AMFI header labels that identify
# them. AMFI has changed navall.txt's column layout before (e.g. inserting
# "Plan"/"Option" columns) without changing the total column count, so
# fields are resolved by header name rather than trusted fixed positions.
SOURCE_COLUMN_ALIASES = {
'scheme_code': ['Scheme Code'],
'scheme_name': ['NAV Name', 'Scheme Name'],
'isin_growth': ['ISIN Div Payout/ISIN Growth', 'ISIN Div Payout / ISIN Growth'],
'isin_reinv': ['ISIN Div Reinvestment'],
'nav': ['Net Asset Value'],
'nav_date': ['Date'],
}
# Fixed column order used only when a file has no header row at all
# (older archived files). Positions 5 and 6 (Repurchase/Sale Price) are
# unused and left unmapped.
LEGACY_COLUMN_ORDER = [
'scheme_code', 'scheme_name', 'isin_growth', 'isin_reinv',
'nav', None, None, 'nav_date'
]
def _resolve_header_indices(self, header_line: str) -> dict:
"""
Build a field-name -> column-index map from a navall.txt header row.
Raises:
ValueError: if a required field's column can't be found in the header.
"""
header_cells = [c.strip() for c in header_line.split(';')]
indices = {}
for field, aliases in self.SOURCE_COLUMN_ALIASES.items():
for alias in aliases:
if alias in header_cells:
indices[field] = header_cells.index(alias)
break
else:
raise ValueError(
f"Could not find a column for '{field}' (looked for {aliases}) "
f"in header: {header_line}"
)
indices['_width'] = len(header_cells)
return indices
def parse_nav_file(self, file_path: str) -> pd.DataFrame:
"""
Parse NAV data from a file.
Args:
file_path (str): Path to the NAV file
Returns:
pd.DataFrame: Parsed NAV data
"""
with open(file_path, 'rb') as raw:
raw_data = raw.read()
result = chardet.detect(raw_data)
encoding = result['encoding'] or 'latin1'
lines = raw_data.decode(encoding, errors='replace').splitlines()
# Check if first line contains header information
header_indices = None
if lines and ';' in lines[0] and any(col in lines[0] for col in ['Scheme', 'ISIN', 'NAV']):
header_indices = self._resolve_header_indices(lines[0])
lines = lines[1:] # Skip the header line
self.logger.info("Skipped header row in input file")
data = []
scheme_type = scheme_category = scheme_sub_category = fund_structure = ""
fund_house = ""
for line in lines:
line = line.strip()
if not line:
continue
if ';' not in line:
if line.startswith("Open Ended") or line.startswith("Close Ended"):
scheme_type = line
scheme_category = scheme_sub_category = ""
elif "Fund" in line:
fund_house = line
continue
parts = line.split(';')
if header_indices is not None:
if len(parts) != header_indices['_width']:
self.logger.warning(
f"Skipping malformed line (expected {header_indices['_width']} "
f"fields, got {len(parts)}): {line}"
)
continue
scheme_code = parts[header_indices['scheme_code']]
scheme_name = parts[header_indices['scheme_name']]
isin_growth = parts[header_indices['isin_growth']]
isin_reinv = parts[header_indices['isin_reinv']]
nav = parts[header_indices['nav']]
nav_date = parts[header_indices['nav_date']]
else:
if len(parts) != 8:
continue
mapped = dict(zip(self.LEGACY_COLUMN_ORDER, parts))
scheme_code = mapped['scheme_code']
scheme_name = mapped['scheme_name']
isin_growth = mapped['isin_growth']
isin_reinv = mapped['isin_reinv']
nav = mapped['nav']
nav_date = mapped['nav_date']
data.append([
scheme_type,
scheme_category,
scheme_sub_category,
scheme_code,
isin_growth,
isin_reinv,
scheme_name,
nav,
nav_date,
fund_house
])
df = pd.DataFrame.from_records(data, columns=[
"Scheme Type",
"Scheme Category",
"Scheme Sub-Category",
"Scheme Code",
"ISIN Div Payout/ISIN Growth",
"ISIN Div Reinvestment",
"Scheme Name",
"Net Asset Value",
"Date",
"Fund Structure"
])
self.logger.info(f"Parsed {len(df)} records from file: {file_path}")
return df
def validate_data(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Validate and clean the NAV data before insertion.
Args:
df (pd.DataFrame): Input DataFrame containing NAV data
Returns:
pd.DataFrame: Cleaned and validated DataFrame
"""
if df.empty:
raise ValueError("Empty DataFrame received")
# Create a copy of the DataFrame to avoid SettingWithCopyWarning
df = df.copy()
# Check if first row contains header values
if df.iloc[0, 0] == 'Scheme Code':
df = df.iloc[1:].reset_index(drop=True)
self.logger.warning("Header row detected and removed")
# Validate required columns
required_columns = ['Scheme Code', 'Scheme Name', 'Net Asset Value', 'Date']
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
raise ValueError(f"Missing required columns: {missing_columns}")
# Clean and validate data
try:
# Convert NAV values to float, handling any invalid values.
# Plain column assignment (not .loc) so pandas can change the
# column's dtype from str to float64 -- pandas 3.x raises on a
# .loc assignment that would change an existing column's dtype.
df['Net Asset Value'] = pd.to_numeric(df['Net Asset Value'], errors='coerce')
# Remove rows with invalid NAV values
invalid_nav_rows = df['Net Asset Value'].isna()
if invalid_nav_rows.any():
self.logger.warning(f"Removing {invalid_nav_rows.sum()} rows with invalid NAV values")
df = df[~invalid_nav_rows]
# Convert dates to datetime
df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%Y', errors='coerce')
# Remove rows with invalid dates
invalid_date_rows = df['Date'].isna()
if invalid_date_rows.any():
self.logger.warning(f"Removing {invalid_date_rows.sum()} rows with invalid dates")
df = df[~invalid_date_rows]
# Clean scheme codes and names
df['Scheme Code'] = df['Scheme Code'].str.strip()
df['Scheme Name'] = df['Scheme Name'].str.strip()
# Remove any empty rows
df = df.dropna(how='all')
if df.empty:
raise ValueError("No valid data remaining after cleaning")
return df
except Exception as e:
self.logger.error(f"Error during data validation: {str(e)}")
raise ValueError(f"Data validation failed: {str(e)}")
def dry_run(self, date_str: Optional[str] = None) -> dict:
"""
Download (if needed) and parse a single day's NAV file, then print a
summary of what would be inserted -- without touching MySQL or
sending a Telegram notification.
Args:
date_str (str): Optional date in YYYY-MM-DD format (matches
--date elsewhere in the CLI). Defaults to the latest
business day.
Returns:
dict: Summary of the parsed/validated data (date, file_path,
raw_row_count, valid_row_count, invalid_row_count, error,
preview).
"""
if date_str:
nav_date = datetime.strptime(date_str, '%Y-%m-%d').date()
else:
nav_date = self.get_latest_business_day(datetime.now()).date()
file_path = f"data/navall_{nav_date.strftime('%Y-%m-%d')}.txt"
if os.path.exists(file_path):
self.logger.info(f"[dry-run] Using existing local file: {file_path}")
else:
self.logger.info(f"[dry-run] Downloading NAV file for {nav_date}")
self._fetch_nav_file(nav_date, file_path)
df = self.parse_nav_file(file_path)
raw_row_count = len(df)
summary = {
'date': nav_date.strftime('%Y-%m-%d'),
'file_path': file_path,
'raw_row_count': raw_row_count,
'valid_row_count': 0,
'invalid_row_count': raw_row_count,
'error': None,
'preview': [],
}
if raw_row_count == 0:
summary['error'] = "No rows parsed from file"
else:
try:
validated_df = self.validate_data(df)
summary['valid_row_count'] = len(validated_df)
summary['invalid_row_count'] = raw_row_count - len(validated_df)
summary['preview'] = validated_df.head(10).astype(str).to_dict(orient='records')
except ValueError as e:
summary['error'] = str(e)
self._print_dry_run_summary(summary)
return summary
def _print_dry_run_summary(self, summary: dict) -> None:
"""Print a dry-run summary to stdout (not the log file)."""
print(f"\n--- DRY RUN: {summary['date']} ---")
print(f"File: {summary['file_path']}")
print(f"Rows parsed: {summary['raw_row_count']}")
if summary['error']:
print(f"Validation FAILED: {summary['error']}")
else:
print(f"Rows valid for insert: {summary['valid_row_count']}")
print(f"Rows dropped by validation: {summary['invalid_row_count']}")
if summary['preview']:
print("\nPreview (first 10 valid rows):")
print(pd.DataFrame(summary['preview']).to_string(index=False))
print("--- No database or Telegram calls were made ---\n")
def insert_nav(self, df: pd.DataFrame) -> None:
"""
Insert NAV data into the database.
Args:
df (pd.DataFrame): DataFrame containing NAV data to insert
"""
df = self.validate_data(df)
# Process data in chunks to manage memory
chunk_size = 1000
total_rows = len(df)
processed_rows = 0
failed_rows = 0
failed_schemes = set()
conn = self.get_connection()
cursor = conn.cursor()
try:
for i in range(0, total_rows, chunk_size):
chunk = df.iloc[i:i + chunk_size]
# Prepare data for insertion
values = []
for _, row in chunk.iterrows():
try:
values.append((
row['Scheme Type'],
row['Scheme Category'],
row['Scheme Sub-Category'],
row['Scheme Code'],
row['ISIN Div Payout/ISIN Growth'],
row['ISIN Div Reinvestment'],
row['Scheme Name'],
float(row['Net Asset Value']),
row['Date'].date(),
row['Fund Structure']
))
except Exception as e:
failed_rows += 1
failed_schemes.add(row['Scheme Code'])
self.logger.warning(f"Skipping row due to invalid data: {row.to_dict()}. Error: {str(e)}")
continue
if not values:
self.logger.warning("No valid rows to insert in this chunk")
continue
try:
# Insert data
query = """
INSERT INTO nav_data
(scheme_type, scheme_category, scheme_sub_category, scheme_code,
isin_growth, isin_reinv, scheme_name, nav, nav_date, fund_structure)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
nav = VALUES(nav)
"""
cursor.executemany(query, values)
conn.commit()
processed_rows += len(values)
self.logger.info(f"Processed {processed_rows}/{total_rows} rows")
except mysql.connector.Error as e:
conn.rollback()
error_msg = f"Database error: {e.msg} (Error code: {e.errno})"
self.logger.error(error_msg)
# Log the first few failed values for debugging
for v in values[:5]:
self.logger.error(f"Failed value example: {v}")
raise Exception(error_msg)
except Exception as e:
conn.rollback()
error_msg = f"Error inserting data: {str(e)}"
self.logger.error(error_msg)
raise
finally:
cursor.close()
conn.close()
# Log summary of failed operations
if failed_rows > 0:
self.logger.error(f"Failed to process {failed_rows} rows")
self.logger.error(f"Affected schemes: {', '.join(sorted(failed_schemes))}")
if processed_rows < total_rows:
self.logger.warning(f"Only processed {processed_rows} out of {total_rows} rows")
# Log final record count comparison
self.logger.info(f"Database insertion summary: {processed_rows} records inserted, {failed_rows} records failed")
def get_latest_nav_date(self) -> Optional[datetime]:
"""Get the latest NAV date from the database."""
conn = self.get_connection()
cursor = conn.cursor()
try:
cursor.execute("SELECT MAX(nav_date) FROM nav_data")
result = cursor.fetchone()
return result[0] if result[0] else None
finally:
cursor.close()
conn.close()
def get_earliest_nav_date(self) -> Optional[datetime]:
"""Get the earliest NAV date from the database."""
conn = self.get_connection()
cursor = conn.cursor()
try:
cursor.execute("SELECT MIN(nav_date) FROM nav_data")
result = cursor.fetchone()
return result[0] if result[0] else None
finally:
cursor.close()
conn.close()
def is_business_day(self, date: datetime) -> bool:
"""
Check if a date is a business day (not weekend or holiday).
Args:
date (datetime): Date to check
Returns:
bool: True if it's a business day, False otherwise
"""
# Convert to date object if it's a datetime
if isinstance(date, datetime):
date = date.date()
# Check if it's a weekend
if date.weekday() >= 5: # 5 is Saturday, 6 is Sunday
return False
# Check if it's a holiday
if date in self.indian_holidays:
self.logger.info(f"{date} is a holiday: {self.indian_holidays.get(date)}")
return False
return True
def get_incomplete_dates(self, start_date: datetime, end_date: datetime) -> set:
"""
Get all dates with incomplete data in a single database query.
Args:
start_date (datetime): Start date of the period
end_date (datetime): End date of the period
Returns:
set: Set of dates that need processing
"""
conn = self.get_connection()
cursor = conn.cursor()
try:
# Convert to date objects if they're datetime
start_date = start_date.date() if isinstance(start_date, datetime) else start_date
end_date = end_date.date() if isinstance(end_date, datetime) else end_date
# Get the oldest date in the database
cursor.execute("SELECT MIN(nav_date) FROM nav_data")
oldest_db_date = cursor.fetchone()[0]
if oldest_db_date is None:
# If no data in database, all dates need processing
self.logger.info("No data in database. All dates need processing.")
return {d.date() for d in pd.date_range(start_date, end_date)}
# Get dates with no data at all (before oldest date)
missing_dates = set()
if start_date < oldest_db_date:
# All dates before oldest_db_date need processing
missing_dates = {d.date() for d in pd.date_range(start_date, oldest_db_date - timedelta(days=1))}
self.logger.info(f"Found {len(missing_dates)} dates before oldest database date {oldest_db_date}")
# Get average record count per date for dates after oldest_db_date
avg_query = """
SELECT AVG(count)
FROM (
SELECT COUNT(*) as count
FROM nav_data
WHERE nav_date BETWEEN %s AND %s
GROUP BY nav_date
) as counts
"""
cursor.execute(avg_query, (oldest_db_date, end_date))
avg_count = cursor.fetchone()[0] or 0
# Get dates with significantly fewer records than average
incomplete_query = """
SELECT nav_date
FROM nav_data
WHERE nav_date BETWEEN %s AND %s
GROUP BY nav_date
HAVING COUNT(*) < %s - 5
"""
cursor.execute(incomplete_query, (oldest_db_date, end_date, avg_count))
incomplete_dates = {row[0] for row in cursor.fetchall()}
# Combine missing and incomplete dates
all_dates_to_process = missing_dates.union(incomplete_dates)
# Filter out weekends and holidays
business_dates_to_process = {
d for d in all_dates_to_process
if self.is_business_day(d)
}
self.logger.info(f"Found {len(business_dates_to_process)} business dates that need processing")
return business_dates_to_process
finally:
cursor.close()
conn.close()
def get_date_range_for_period(self, period_days: int, reference_date: datetime = None) -> Tuple[datetime, datetime]:
"""
Get the date range for a specified period, excluding weekends and holidays.
Args:
period_days (int): Number of days in the period
reference_date (datetime): Reference date to start from (default: latest date in database)
Returns:
Tuple[datetime, datetime]: Start and end dates for the period
"""
if reference_date is None:
reference_date = self.get_earliest_nav_date()
if reference_date is None:
self.logger.info("No existing data in database. Using today as reference.")
reference_date = datetime.now()
else:
self.logger.info(f"Found existing data in database. Oldest date available: {reference_date}")
# Convert to date if it's datetime
reference_date = reference_date.date() if isinstance(reference_date, datetime) else reference_date
# Calculate end date (one day before reference)
end_date = reference_date - timedelta(days=1)
# Calculate start date
start_date = end_date - timedelta(days=period_days)
# Adjust start date to skip weekends and holidays
while not self.is_business_day(start_date):
start_date += timedelta(days=1)
self.logger.info(f"Calculated date range: {start_date} to {end_date} (period: {period_days} days)")
return start_date, end_date
def bulk_download_past_years(self, years: int = 15) -> List[str]:
"""
Download NAV data for the specified number of past years.
Starts from the earliest date in the database and goes back.
Uses optimized database queries for better performance.
Args:
years (int): Number of past years to download data for
Returns:
List[str]: List of downloaded file paths
"""
start_time = datetime.now()
self.logger.info(f"Starting yearly job for {years} years")
# Get date range based on existing data
start_date, end_date = self.get_date_range_for_period(years * 365)
# Get all dates that need processing in one query
dates_to_process = self.get_incomplete_dates(start_date, end_date)
self.logger.info(f"Processing data from {start_date.strftime('%Y-%m-%d')} to {end_date.strftime('%Y-%m-%d')}")
self.logger.info(f"Found {len(dates_to_process)} dates that need processing")
self.logger.info(f"Dates to process: {sorted(dates_to_process)}")
downloaded_files = []
current_date = end_date
while current_date >= start_date:
# Convert to date if it's datetime
current_date_date = current_date.date() if isinstance(current_date, datetime) else current_date
if self.is_business_day(current_date_date):
file_path = f"data/navall_{current_date_date.strftime('%Y-%m-%d')}.txt"
if current_date_date in dates_to_process:
if not os.path.exists(file_path):
try:
self.logger.info(f"Downloading for {current_date_date.strftime('%Y-%m-%d')} (data incomplete in database)...")
self.download_nav_file_for_date(current_date_date)
downloaded_files.append(file_path)
except Exception as e:
self.logger.error(f"Failed for {current_date_date.strftime('%Y-%m-%d')}: {e}")
else:
self.logger.info(f"File exists but data incomplete for {current_date_date.strftime('%Y-%m-%d')}")
downloaded_files.append(file_path)
else:
self.logger.info(f"Data already complete in database for {current_date_date.strftime('%Y-%m-%d')}")
else:
self.logger.info(f"Skipping {current_date_date.strftime('%Y-%m-%d')} (not a business day)")
current_date -= timedelta(days=1)
if not downloaded_files:
self.logger.warning("No files were downloaded")
self.send_telegram_notification(f"<b>Yearly NAV Update</b>\n\nNo files were downloaded for the specified period.")
return []
self.logger.info(f"Downloaded {len(downloaded_files)} files")
success_count = 0
failed_count = 0
failed_files = []
total_records_processed = 0
total_records_failed = 0
for file_path in downloaded_files:
try:
self.logger.info(f"Processing file: {file_path}")
df = self.parse_nav_file(file_path)
if df is None or df.empty:
self.logger.warning(f"No data found in file: {file_path}")
continue
csv_path = file_path.replace('.txt', '.csv')
df.to_csv(csv_path, index=False)
self.logger.info(f"Saved {len(df)} records to CSV: {csv_path}")
self.insert_nav(df)
success_count += 1
total_records_processed += len(df)
self.logger.info(f"Successfully processed: {file_path} with {len(df)} records")
except Exception as e:
failed_count += 1
failed_files.append(file_path)
total_records_failed += len(df) if df is not None and not df.empty else 0
self.logger.error(f"Error processing {file_path}: {str(e)}")
duration = datetime.now() - start_time
summary = f"""
<b>Yearly NAV Update Summary</b>
-----------------------
Total files processed: {len(downloaded_files)}
Successfully processed: {success_count}
Failed to process: {failed_count}
Total records processed: {total_records_processed}
Total records failed: {total_records_failed}
Duration: {duration}
"""
if failed_files:
summary += "\nFailed files:\n"
summary += "\n".join(f" - {file}" for file in failed_files)
self.logger.info(summary)
self.send_telegram_notification(summary)
# Clean up temporary CSV files
try:
for file_path in downloaded_files:
csv_path = file_path.replace('.txt', '.csv')
if os.path.exists(csv_path):
os.remove(csv_path)
self.logger.info(f"Cleaned up temporary file: {csv_path}")
except Exception as e:
self.logger.error(f"Error cleaning up temporary files: {str(e)}")
return downloaded_files
def bulk_download_past_months(self, months: int = 3, start_date: datetime = None, end_date: datetime = None) -> List[str]:
"""
Download NAV data for the specified number of past months.
Uses optimized database queries for better performance.
Args:
months (int): Number of past months to download data for
start_date (datetime): Optional start date
end_date (datetime): Optional end date
Returns:
List[str]: List of downloaded file paths
"""
if not end_date:
end_date = self.get_latest_business_day(datetime.now())
if not start_date:
start_date = end_date - timedelta(days=months*30)
# Get all dates that need processing in one query
dates_to_process = self.get_incomplete_dates(start_date, end_date)
self.logger.info(f"Processing data from {start_date.strftime('%Y-%m-%d')} to {end_date.strftime('%Y-%m-%d')}")
self.logger.info(f"Found {len(dates_to_process)} dates that need processing")
self.logger.info(f"Dates to process: {[d.strftime('%Y-%m-%d') for d in sorted(dates_to_process)]}")
downloaded_files = []
os.makedirs("data", exist_ok=True)
current_date = end_date
while current_date >= start_date:
# Convert to date if it's datetime
current_date_date = current_date.date() if isinstance(current_date, datetime) else current_date
if self.is_business_day(current_date_date):
file_path = f"data/navall_{current_date_date.strftime('%Y-%m-%d')}.txt"
if current_date_date in dates_to_process:
if not os.path.exists(file_path):
try:
self.logger.info(f"Downloading NAV data for {current_date_date.strftime('%Y-%m-%d')} (data incomplete in database)...")
self.download_nav_file_for_date(current_date_date)
downloaded_files.append(file_path)
self.logger.info(f"Successfully downloaded: {file_path}")
except Exception as e:
self.logger.error(f"Failed to download for {current_date_date.strftime('%Y-%m-%d')}: {e}")
else:
self.logger.info(f"File exists but data incomplete for {current_date_date.strftime('%Y-%m-%d')}")
downloaded_files.append(file_path)
else:
self.logger.info(f"Data already complete in database for {current_date_date.strftime('%Y-%m-%d')}")
else:
self.logger.info(f"Skipping {current_date_date.strftime('%Y-%m-%d')} (not a business day)")
current_date -= timedelta(days=1)
return downloaded_files
def send_telegram_notification(self, message: str) -> bool:
"""
Send Telegram notification.
Args:
message (str): Message to send
Returns:
bool: True if message was sent successfully, False otherwise
"""
# Check if Telegram config is valid (not empty and not placeholder values)
bot_token = self.telegram_config.get('bot_token', '')
chat_id = self.telegram_config.get('chat_id', '')
if not bot_token or not chat_id or bot_token == 'your_bot_token' or chat_id == 'your_chat_id':
self.logger.debug("Telegram configuration not set, skipping notification")
return False
try:
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
data = {
"chat_id": chat_id,
"text": message,
"parse_mode": "HTML"
}
response = requests.post(url, data=data)
response.raise_for_status()
self.logger.info("Telegram notification sent successfully")
return True
except Exception as e:
self.logger.debug(f"Telegram notification not sent: {e}")
return False
def run_daily_job(self) -> Tuple[int, int, List[str]]:
"""
Run the daily job to download and process the latest NAV data.
Improved to handle both missing files and incomplete database data.
Returns:
Tuple[int, int, List[str]]: Success count, failure count, and failed dates
"""
start_time = datetime.now()
self.logger.info("Starting daily job")
try: