Skip to content

Commit

Permalink
spi-stats:fix record.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Dec 4, 2024
1 parent def2140 commit a5c5db7
Showing 1 changed file with 69 additions and 75 deletions.
144 changes: 69 additions & 75 deletions backend/spi/spi-stats/src/serv/pg/stats_pg_record_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,84 +237,78 @@ pub(crate) async fn fact_record_load(
// TODO check data type
}
info!("fact_col_conf_set={0},req_data={1}", fact_col_conf_set.len(), req_data.len());
if fact_col_conf_set.len() != req_data.len() {
if let Some(latest_data) = latest_data_resp {
for fact_col_conf in fact_col_conf_set {
if !req_data.contains_key(&fact_col_conf.key) {
if exist_fields.contains(&fact_col_conf.key) {
continue;
}
exist_fields.insert(fact_col_conf.key.clone());
fields.push(fact_col_conf.key.to_string());
if fact_col_conf.kind == StatsFactColKind::Dimension {
let Some(dim_rel_conf_dim_key) = &fact_col_conf.dim_rel_conf_dim_key else {
return Err(funs.err().internal_error("fact_record", "load", "dim_rel_conf_dim_key unexpectedly being empty", "500-spi-stats-internal-error"));
};
let Some(dim_conf) = stats_pg_conf_dim_serv::get(dim_rel_conf_dim_key, None, None, &conn, ctx, inst).await? else {
return Err(funs.err().internal_error(
"fact_record",
"load",
&format!("key [{dim_rel_conf_dim_key}] missing corresponding config "),
"500-spi-stats-internal-error",
));
};
if fact_col_conf.dim_multi_values.unwrap_or(false) {
values.push(dim_conf.data_type.result_to_sea_orm_value_array(&latest_data, &fact_col_conf.key)?);
} else {
values.push(dim_conf.data_type.result_to_sea_orm_value(&latest_data, &fact_col_conf.key)?);
}
} else if fact_col_conf.kind == StatsFactColKind::Measure {
let Some(mes_data_type) = fact_col_conf.mes_data_type.as_ref() else {
return Err(funs.err().bad_request(
"fact_record",
"load",
"Col_conf.mes_data_type shouldn't be empty while fact_col_conf.kind is Measure",
"400-spi-stats-invalid-request",
));
};
values.push(mes_data_type.result_to_sea_orm_value(&latest_data, &fact_col_conf.key)?);
} else {
values.push(Value::from(latest_data.try_get::<String>("", &fact_col_conf.key)?));
}
if let Some(latest_data) = latest_data_resp {
for fact_col_conf in fact_col_conf_set {
if exist_fields.contains(&fact_col_conf.key) {
continue;
}
exist_fields.insert(fact_col_conf.key.clone());
fields.push(fact_col_conf.key.to_string());
if fact_col_conf.kind == StatsFactColKind::Dimension {
let Some(dim_rel_conf_dim_key) = &fact_col_conf.dim_rel_conf_dim_key else {
return Err(funs.err().internal_error("fact_record", "load", "dim_rel_conf_dim_key unexpectedly being empty", "500-spi-stats-internal-error"));
};
let Some(dim_conf) = stats_pg_conf_dim_serv::get(dim_rel_conf_dim_key, None, None, &conn, ctx, inst).await? else {
return Err(funs.err().internal_error(
"fact_record",
"load",
&format!("key [{dim_rel_conf_dim_key}] missing corresponding config "),
"500-spi-stats-internal-error",
));
};
if fact_col_conf.dim_multi_values.unwrap_or(false) {
values.push(dim_conf.data_type.result_to_sea_orm_value_array(&latest_data, &fact_col_conf.key)?);
} else {
values.push(dim_conf.data_type.result_to_sea_orm_value(&latest_data, &fact_col_conf.key)?);
}
} else if fact_col_conf.kind == StatsFactColKind::Measure {
let Some(mes_data_type) = fact_col_conf.mes_data_type.as_ref() else {
return Err(funs.err().bad_request(
"fact_record",
"load",
"Col_conf.mes_data_type shouldn't be empty while fact_col_conf.kind is Measure",
"400-spi-stats-invalid-request",
));
};
values.push(mes_data_type.result_to_sea_orm_value(&latest_data, &fact_col_conf.key)?);
} else {
values.push(Value::from(latest_data.try_get::<String>("", &fact_col_conf.key)?));
}
} else {
for fact_col_conf in fact_col_conf_set {
if !req_data.contains_key(&fact_col_conf.key) {
if exist_fields.contains(&fact_col_conf.key) {
continue;
}
exist_fields.insert(fact_col_conf.key.clone());
fields.push(fact_col_conf.key.to_string());
if fact_col_conf.kind == StatsFactColKind::Dimension {
let Some(dim_rel_conf_dim_key) = &fact_col_conf.dim_rel_conf_dim_key else {
return Err(funs.err().internal_error("fact_record", "load", "dim_rel_conf_dim_key unexpectedly being empty", "500-spi-stats-internal-error"));
};
let Some(dim_conf) = stats_pg_conf_dim_serv::get(dim_rel_conf_dim_key, None, None, &conn, ctx, inst).await? else {
return Err(funs.err().internal_error(
"fact_record",
"load",
&format!("key [{dim_rel_conf_dim_key}] missing corresponding config "),
"500-spi-stats-internal-error",
));
};
if fact_col_conf.dim_multi_values.unwrap_or(false) {
values.push(dim_conf.data_type.result_to_sea_orm_value_array_default()?);
} else {
values.push(dim_conf.data_type.result_to_sea_orm_value_default()?);
}
} else if fact_col_conf.kind == StatsFactColKind::Measure {
let Some(mes_data_type) = fact_col_conf.mes_data_type.as_ref() else {
return Err(funs.err().bad_request(
"fact_record",
"load",
"Col_conf.mes_data_type shouldn't be empty while fact_col_conf.kind is Measure",
"400-spi-stats-invalid-request",
));
};
values.push(mes_data_type.result_to_sea_orm_value_default()?);
}
}
} else {
for fact_col_conf in fact_col_conf_set {
if exist_fields.contains(&fact_col_conf.key) {
continue;
}
exist_fields.insert(fact_col_conf.key.clone());
fields.push(fact_col_conf.key.to_string());
if fact_col_conf.kind == StatsFactColKind::Dimension {
let Some(dim_rel_conf_dim_key) = &fact_col_conf.dim_rel_conf_dim_key else {
return Err(funs.err().internal_error("fact_record", "load", "dim_rel_conf_dim_key unexpectedly being empty", "500-spi-stats-internal-error"));
};
let Some(dim_conf) = stats_pg_conf_dim_serv::get(dim_rel_conf_dim_key, None, None, &conn, ctx, inst).await? else {
return Err(funs.err().internal_error(
"fact_record",
"load",
&format!("key [{dim_rel_conf_dim_key}] missing corresponding config "),
"500-spi-stats-internal-error",
));
};
if fact_col_conf.dim_multi_values.unwrap_or(false) {
values.push(dim_conf.data_type.result_to_sea_orm_value_array_default()?);
} else {
values.push(dim_conf.data_type.result_to_sea_orm_value_default()?);
}
} else if fact_col_conf.kind == StatsFactColKind::Measure {
let Some(mes_data_type) = fact_col_conf.mes_data_type.as_ref() else {
return Err(funs.err().bad_request(
"fact_record",
"load",
"Col_conf.mes_data_type shouldn't be empty while fact_col_conf.kind is Measure",
"400-spi-stats-invalid-request",
));
};
values.push(mes_data_type.result_to_sea_orm_value_default()?);
}
}
}
Expand Down

0 comments on commit a5c5db7

Please sign in to comment.