|
1 | 1 | import os
|
2 | 2 | import requests
|
3 |
| -import json |
4 | 3 | import polars as pl
|
5 | 4 | from datetime import datetime
|
6 | 5 | import logging
|
|
12 | 11 | from utils.os_functions import ensure_data_dir
|
13 | 12 | from utils.paths import LOG_DIR, DATA_DIR
|
14 | 13 |
|
15 |
| -# Create a log filename with the current date (YYYY-MM-DD) |
| 14 | +# Create a log file with the current date (YYYY-MM-DD) |
16 | 15 | log_filename = os.path.join(LOG_DIR,
|
17 | 16 | f"{datetime.now().strftime('%Y-%m-%d')}.log")
|
18 | 17 |
|
19 | 18 | logging.basicConfig(
|
20 |
| - filename=log_filename, |
21 | 19 | level=logging.INFO,
|
22 | 20 | format=
|
23 |
| - "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(funcName)s - %(message)s" |
24 |
| -) |
| 21 | + "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(funcName)s - %(message)s", |
| 22 | + handlers=[ |
| 23 | + logging.FileHandler(log_filename), |
| 24 | + logging.StreamHandler( |
| 25 | + ) # This allows logs to be printed to the console as well |
| 26 | + ]) |
25 | 27 |
|
26 | 28 | logging.info("=========================================")
|
27 | 29 | logging.info("Starting data download from ThingsBoard")
|
|
44 | 46 | startTS, endTS = download_interval(jwt_token, device_name,
|
45 | 47 | device_id, session)
|
46 | 48 |
|
47 |
| - logging.info(f"Downloading data for device: {device_name}") |
48 |
| - print( |
| 49 | + logging.info( |
49 | 50 | f"Downloading data for device: {device_name} with starting timestamp: {datetime.fromtimestamp(startTS / 1000)}"
|
50 | 51 | )
|
51 | 52 |
|
|
80 | 81 | current_timestamp = df_key.select(
|
81 | 82 | pl.col("ts").max()).to_series()[0] + 1
|
82 | 83 |
|
83 |
| - print(f"Starting pivot for device: {device_name}") |
| 84 | + logging.info(f"Starting pivot for device: {device_name}") |
| 85 | + |
| 86 | + if len(df_chunk) == 0: |
| 87 | + logging.info(f"No data downloaded for device: {device_name}") |
| 88 | + continue |
84 | 89 |
|
85 | 90 | df_long = pl.concat(df_chunk)
|
86 | 91 |
|
|
90 | 95 | .pivot(index="ts", on="key", values="value") \
|
91 | 96 | .with_columns(pl.from_epoch("ts", time_unit="ms").alias("datetime"))
|
92 | 97 |
|
93 |
| - print(f"Finished pivot for device: {device_name}") |
| 98 | + logging.info(f"Finished pivot for device: {device_name}") |
94 | 99 |
|
95 | 100 | # Save the data to a local Parquet file split by year
|
96 | 101 | for year in df_wide["datetime"].dt.year().unique().to_list():
|
|
0 commit comments