use std::io::Cursor;
use actix_http::StatusCode;
use actix_web::web::Data;
use chrono::Utc;
use image::{ImageBuffer, Rgba};
use crate::{
config::data::AppDataInner,
error::ServiceError,
model::{
dto::{HeatMapQueryParams, RelationSearchParameters, RelationsDto},
entity::plant_layer,
r#enum::heatmap_color::HeatmapColor,
},
};
pub async fn heatmap(
map_id: i32,
query_params: HeatMapQueryParams,
app_data: &Data<AppDataInner>,
) -> Result<Vec<u8>, ServiceError> {
let mut conn = app_data.pool.get().await?;
let result = plant_layer::heatmap(
map_id,
query_params.plant_layer_id,
query_params.shade_layer_id,
query_params.plant_id,
query_params.date.unwrap_or_else(|| Utc::now().date_naive()),
&mut conn,
)
.await?;
let buffer = matrix_to_image(&result)?;
Ok(buffer)
}
#[allow(
clippy::cast_possible_truncation, clippy::indexing_slicing, clippy::cast_sign_loss )]
fn matrix_to_image(matrix: &Vec<Vec<(HeatmapColor, f32)>>) -> Result<Vec<u8>, ServiceError> {
let (width, height) = (matrix[0].len(), matrix.len());
let mut imgbuf = ImageBuffer::new(width as u32, height as u32);
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
let (color, relevance) = &matrix[y as usize][x as usize];
let alpha = (relevance * 255.0) as u8;
*pixel = match color {
HeatmapColor::Black => Rgba([0, 0, 0, alpha]),
HeatmapColor::Red => Rgba([255, 0, 0, alpha]),
HeatmapColor::Orange => Rgba([255, 136, 0, alpha]),
HeatmapColor::Green => Rgba([0, 255, 0, alpha]),
};
}
let mut buffer: Vec<u8> = Vec::new();
imgbuf
.write_to(&mut Cursor::new(&mut buffer), image::ImageOutputFormat::Png)
.map_err(|err| ServiceError::new(StatusCode::INTERNAL_SERVER_ERROR, &err.to_string()))?;
Ok(buffer)
}
pub async fn find_relations(
search_query: RelationSearchParameters,
app_data: &Data<AppDataInner>,
) -> Result<RelationsDto, ServiceError> {
let mut conn = app_data.pool.get().await?;
let result = plant_layer::find_relations(search_query, &mut conn).await?;
Ok(result)
}