Skip to content

Commit b216afb

Browse files
committed
CSV parsing with multiple symbols
1 parent 4d20d4b commit b216afb

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

console.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
66
-- We start by creating a regular SQL table
77

88
CREATE TABLE nse_data_daily (
9-
timestamp TIMESTAMP PRIMARY KEY NOT NULL,
9+
timestamp TIMESTAMP NOT NULL,
1010
open DOUBLE PRECISION NOT NULL,
1111
close DOUBLE PRECISION NOT NULL,
1212
high DOUBLE PRECISION NOT NULL,
1313
low DOUBLE PRECISION NOT NULL,
1414
volume DOUBLE PRECISION NOT NULL,
1515
symbol VARCHAR(20) NOT NULL,
1616
created_time TIMESTAMP NOT NULL,
17-
updated_time TIMESTAMP NOT NULL
17+
updated_time TIMESTAMP NOT NULL,
18+
CONSTRAINT timestamp_symbol PRIMARY KEY(timestamp, symbol)
1819
);
1920

2021
-- This creates a hypertable that is partitioned by time
21-
-- using the values in the `time` column.
22+
-- using the values in the `time` column.
2223

2324
SELECT create_hypertable('nse_data_daily', 'timestamp');
2425

src/repository/nse_repository.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ def upsert_one(data):
2626
if not isinstance(data, NseDailyDataModel):
2727
raise RuntimeError("Model error")
2828

29-
nse_data = session.query(NseDailyDataModel).get(data.timestamp)
29+
nse_data = (
30+
session.query(NseDailyDataModel)
31+
.filter(
32+
NseDailyDataModel.timestamp == data.timestamp,
33+
NseDailyDataModel.symbol == data.symbol,
34+
)
35+
.first()
36+
)
3037
if nse_data is not None:
3138
update_params = {
3239
"timestamp": data.timestamp,
@@ -39,10 +46,18 @@ def upsert_one(data):
3946
"updated_time": datetime.utcnow(),
4047
}
4148
session.query(NseDailyDataModel).filter(
42-
NseDailyDataModel.timestamp == data.timestamp
49+
NseDailyDataModel.timestamp == data.timestamp,
50+
NseDailyDataModel.symbol == data.symbol,
4351
).update(update_params)
4452
session.commit()
45-
updated_nse_data = session.query(NseDailyDataModel).get(data.timestamp)
53+
updated_nse_data = (
54+
session.query(NseDailyDataModel)
55+
.filter(
56+
NseDailyDataModel.timestamp == data.timestamp,
57+
NseDailyDataModel.symbol == data.symbol,
58+
)
59+
.first()
60+
)
4661
log.info(f"nse_repository : update_one : updated_nse_data = {updated_nse_data}")
4762
return generate_response(200, "update", True, repr(updated_nse_data))
4863
else:

src/routes/nse/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class NsePriceVolumeDeliverableData(Schema):
2727
no_of_trades = fields.Int(required=True)
2828
deliverable_qty = fields.Int(required=True)
2929
percent_daily_qty_to_traded_qty = fields.Float(required=True)
30+
is_valid_data = fields.Bool(missing=True)
3031

3132
@post_dump
3233
def date_to_string(self, output, **kwargs):

src/services/nse_service.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def parse_one_row_of_nse_data_csv(index, row):
6565
"no_of_trades": row["No. of Trades"],
6666
"deliverable_qty": row["Deliverable Qty"],
6767
"percent_daily_qty_to_traded_qty": row["% Dly Qt to Traded Qty"],
68+
"is_valid_data": True,
6869
}
70+
71+
# manual fixes on csv data
72+
if nse_data_dict["deliverable_qty"] == "-":
73+
nse_data_dict["deliverable_qty"] = 0
74+
nse_data_dict["is_valid_data"] = False
75+
if nse_data_dict["percent_daily_qty_to_traded_qty"] == "-":
76+
nse_data_dict["percent_daily_qty_to_traded_qty"] = 0
77+
nse_data_dict["is_valid_data"] = False
78+
6979
nse_data = nse_pvd_schema.dump(nse_data_dict)
7080
upsert_nse_data_from_nse_pvd_data(nse_data)

0 commit comments

Comments
 (0)