CREATE TABLE paliot.V0601A650016470_ingestion_data (
    timestamp TIMESTAMPTZ NOT NULL,
    machine_sensors_id INT REFERENCES paliot.machine_sensors(machine_sensors_id),
    acc_x FLOAT, acc_y FLOAT, acc_z FLOAT, -- Questi dati andrebbero messi nel campo "external_sensor_data", così da gestire tutto come json 
    gyr_x FLOAT, gyr_y FLOAT, gyr_z FLOAT, -- idem come sopra
    external_sensor_data JSONB,
    PRIMARY KEY (timestamp, machine_sensors_id)
);

SELECT create_hypertable('paliot.V0601A650016470_ingestion_data', 'timestamp');
SELECT set_chunk_time_interval('paliot.V0601A650016470_ingestion_data', INTERVAL '24 hours');

-- Indexes
CREATE INDEX ON paliot.V0601A650016470_ingestion_data (machine_sensors_id);
CREATE INDEX ON paliot.V0601A650016470_ingestion_data (timestamp DESC);

-- Add compression policy (compress data older than 7 days)
ALTER TABLE paliot.V0601A650016470_ingestion_data SET (
    timescaledb.compress,
    timescaledb.compress_segmentby = 'machine_sensors_id',
    timescaledb.compress_orderby = 'timestamp'
);

SELECT add_compression_policy('paliot.V0601A650016470_ingestion_data', INTERVAL '7 days');

-- Retention policy
SELECT add_retention_policy('paliot.V0601A650016470_ingestion_data', INTERVAL '1 month');

COMMENT ON TABLE paliot.V0601A650016470_ingestion_data IS 'Ingested raw sensor data from V0601A650016470';

--

CREATE TABLE paliot.L0601B300083712_ingestion_data (
    timestamp TIMESTAMPTZ NOT NULL,
    machine_sensors_id INT REFERENCES paliot.machine_sensors(machine_sensors_id),
    acc_x FLOAT, acc_y FLOAT, acc_z FLOAT, -- Questi dati andrebbero messi nel campo "external_sensor_data", così da gestire tutto come json 
    gyr_x FLOAT, gyr_y FLOAT, gyr_z FLOAT, -- idem come sopra
    external_sensor_data JSONB,
    PRIMARY KEY (timestamp, machine_sensors_id)
);

SELECT create_hypertable('paliot.L0601B300083712_ingestion_data', 'timestamp');
SELECT set_chunk_time_interval('paliot.L0601B300083712_ingestion_data', INTERVAL '24 hours');

-- Indexes
CREATE INDEX ON paliot.L0601B300083712_ingestion_data (machine_sensors_id);
CREATE INDEX ON paliot.L0601B300083712_ingestion_data (timestamp DESC);

-- Add compression policy (compress data older than 7 days)
ALTER TABLE paliot.L0601B300083712_ingestion_data SET (
    timescaledb.compress,
    timescaledb.compress_segmentby = 'machine_sensors_id',
    timescaledb.compress_orderby = 'timestamp'
);

SELECT add_compression_policy('paliot.L0601B300083712_ingestion_data', INTERVAL '7 days');

-- Retention policy
SELECT add_retention_policy('paliot.L0601B300083712_ingestion_data', INTERVAL '1 month');

COMMENT ON TABLE paliot.L0601B300083712_ingestion_data IS 'Ingested raw sensor data from L0601B300083712';

--

CREATE TABLE paliot.L0601B300029398_ingestion_data (
    timestamp TIMESTAMPTZ NOT NULL,
    machine_sensors_id INT REFERENCES paliot.machine_sensors(machine_sensors_id),
    acc_x FLOAT, acc_y FLOAT, acc_z FLOAT, -- Questi dati andrebbero messi nel campo "external_sensor_data", così da gestire tutto come json 
    gyr_x FLOAT, gyr_y FLOAT, gyr_z FLOAT, -- idem come sopra
    external_sensor_data JSONB,
    PRIMARY KEY (timestamp, machine_sensors_id)
);

SELECT create_hypertable('paliot.L0601B300029398_ingestion_data', 'timestamp');
SELECT set_chunk_time_interval('paliot.L0601B300029398_ingestion_data', INTERVAL '24 hours');

-- Indexes
CREATE INDEX ON paliot.L0601B300029398_ingestion_data (machine_sensors_id);
CREATE INDEX ON paliot.L0601B300029398_ingestion_data (timestamp DESC);

-- Add compression policy (compress data older than 7 days)
ALTER TABLE paliot.L0601B300029398_ingestion_data SET (
    timescaledb.compress,
    timescaledb.compress_segmentby = 'machine_sensors_id',
    timescaledb.compress_orderby = 'timestamp'
);

SELECT add_compression_policy('paliot.L0601B300029398_ingestion_data', INTERVAL '7 days');

-- Retention policy
SELECT add_retention_policy('paliot.L0601B300029398_ingestion_data', INTERVAL '1 month');

COMMENT ON TABLE paliot.L0601B300029398_ingestion_data IS 'Ingested raw sensor data from L0601B300029398';