group TimelineSqlDao;

CHECK_TENANT() ::= "tenant_record_id = :tenantRecordId"
AND_CHECK_TENANT() ::= "AND <CHECK_TENANT()>"

getSourceRecordId() ::= <<
select
  record_id
from sources
where source = :source
<AND_CHECK_TENANT()>
;
>>

getSourceName() ::= <<
select
  source
from sources
where record_id = :recordId
<AND_CHECK_TENANT()>
;
>>

getSources() ::= <<
select
  record_id
, source
from sources
where <CHECK_TENANT()>
;
>>

addSource() ::= <<
insert into sources (
  source
, created_date
, created_by
, updated_date
, updated_by
, account_record_id
, tenant_record_id
) values (
  :source
, :createdDate
, :userName
, :updatedDate
, :userName
, :accountRecordId
, :tenantRecordId
);
>>


getCategoryRecordId() ::= <<
select
  record_id
from categories
where category = :category
<AND_CHECK_TENANT()>
;
>>

getCategory() ::= <<
select
  category
from categories
where record_id = :recordId
<AND_CHECK_TENANT()>
;
>>

getCategories() ::= <<
select
  record_id
, category
from categories
where <CHECK_TENANT()>
;
>>

addCategory() ::= <<
insert into categories (
  category
, created_date
, created_by
, updated_date
, updated_by
, tenant_record_id
) values (
  :category
, :createdDate
, :userName
, :updatedDate
, :userName
, :tenantRecordId
);
>>


getMetricRecordId() ::= <<
select
  record_id
from metrics
where metric = :metric
and category_record_id = :categoryRecordId
<AND_CHECK_TENANT()>
;
>>

getCategoryRecordIdAndMetric() ::= <<
select
  category_record_id
, metric
from metrics
where record_id = :recordId
<AND_CHECK_TENANT()>
;
>>

getMetric() ::= <<
select
  metric
from metrics
where record_id = :recordId
<AND_CHECK_TENANT()>
;
>>

getMetrics() ::= <<
select
  record_id
, category_record_id
, metric
from metrics
where <CHECK_TENANT()>
;
>>

addMetric() ::= <<
insert into metrics (
  category_record_id
, metric
, created_date
, created_by
, updated_date
, updated_by
, tenant_record_id
) values (
  :categoryRecordId
, :metric
, :createdDate
, :userName
, :updatedDate
, :userName
, :tenantRecordId
);
>>


getLastInsertedRecordId() ::= <<
select last_insert_id();
>>

insertTimelineChunk() ::= <<
insert into timeline_chunks (
  record_id
, source_record_id
, metric_record_id
, sample_count
, start_time
, end_time
, in_row_samples
, blob_samples
, aggregation_level
, not_valid
, dont_aggregate
, account_record_id
, tenant_record_id
) values (
  :chunkId
, :sourceRecordId
, :metricRecordId
, :sampleCount
, :startTime
, :endTime
, :inRowSamples
, :blobSamples
, :aggregationLevel
, :notValid
, :dontAggregate
, :accountRecordId
, :tenantRecordId
);
>>
bulkInsertTimelineChunks() ::= "<insertTimelineChunk()>"

getSamplesBySourceRecordIdsAndMetricRecordIds(sourceIds, metricIds) ::= <<
select
  record_id
, metric_record_id
, source_record_id
, sample_count
, in_row_samples
, blob_samples
, start_time
, end_time
, aggregation_level
, not_valid
, dont_aggregate
from timeline_chunks
where end_time >= :startTime
and start_time \<= :endTime
and source_record_id in (<sourceIds>)
<if(metricIds)>
  and metric_record_id in (<metricIds>)
<endif>
and not_valid = 0
<AND_CHECK_TENANT()>
order by source_record_id, metric_record_id, start_time asc
;
>>

insertLastStartTimes() ::= <<
insert into last_start_times (
  time_inserted
, start_times
, account_record_id
, tenant_record_id
) values (
  :timeInserted
, :startTimes
, :accountRecordId
, :tenantRecordId
);
>>

getLastStartTimes() ::= <<
select
  time_inserted
, start_times
from last_start_times
where <CHECK_TENANT()>
order by time_inserted desc
limit 1
>>

deleteLastStartTimes() ::= <<
delete from last_start_times where <CHECK_TENANT()>;
>>

test() ::= <<
select 1 from timeline_chunks where <CHECK_TENANT()> limit 1;
>>
