|
| 1 | +/* |
| 2 | + * Parseable Server (C) 2022 - 2024 Parseable, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +use actix_web::{web, HttpRequest, HttpResponse, Responder}; |
| 20 | +use anyhow::anyhow; |
| 21 | +use bytes::Bytes; |
| 22 | +use http::StatusCode; |
| 23 | +use serde_json::json; |
| 24 | + |
| 25 | +use crate::{ |
| 26 | + option::CONFIG, |
| 27 | + querycache::{CacheMetadata, QueryCacheManager}, |
| 28 | +}; |
| 29 | + |
| 30 | +use super::ingest::PostError; |
| 31 | + |
| 32 | +pub async fn list(req: HttpRequest) -> Result<impl Responder, PostError> { |
| 33 | + let stream = req |
| 34 | + .match_info() |
| 35 | + .get("stream") |
| 36 | + .ok_or_else(|| PostError::Invalid(anyhow!("Invalid Stream Name in resource path")))?; |
| 37 | + |
| 38 | + let user_id = req |
| 39 | + .match_info() |
| 40 | + .get("user_id") |
| 41 | + .ok_or_else(|| PostError::Invalid(anyhow!("Invalid User ID not in Resource path")))?; |
| 42 | + |
| 43 | + let query_cache_manager = QueryCacheManager::global(CONFIG.parseable.query_cache_size) |
| 44 | + .await |
| 45 | + .unwrap_or(None); |
| 46 | + |
| 47 | + if let Some(query_cache_manager) = query_cache_manager { |
| 48 | + let cache = query_cache_manager |
| 49 | + .get_cache(stream, user_id) |
| 50 | + .await |
| 51 | + .map_err(PostError::CacheError)?; |
| 52 | + |
| 53 | + let size = cache.current_size(); |
| 54 | + let queries = cache.queries(); |
| 55 | + |
| 56 | + let out = json!({ |
| 57 | + "current_cache_size": size, |
| 58 | + "cache": queries |
| 59 | + }); |
| 60 | + |
| 61 | + Ok((web::Json(out), StatusCode::OK)) |
| 62 | + } else { |
| 63 | + Err(PostError::Invalid(anyhow!( |
| 64 | + "Query Caching is not active on server " |
| 65 | + ))) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +pub async fn remove(req: HttpRequest, body: Bytes) -> Result<impl Responder, PostError> { |
| 70 | + let stream = req |
| 71 | + .match_info() |
| 72 | + .get("stream") |
| 73 | + .ok_or_else(|| PostError::Invalid(anyhow!("Invalid Stream Name in resource path")))?; |
| 74 | + |
| 75 | + let user_id = req |
| 76 | + .match_info() |
| 77 | + .get("user_id") |
| 78 | + .ok_or_else(|| PostError::Invalid(anyhow!("Invalid User ID not in Resource path")))?; |
| 79 | + |
| 80 | + let query = serde_json::from_slice::<CacheMetadata>(&body)?; |
| 81 | + |
| 82 | + let query_cache_manager = QueryCacheManager::global(CONFIG.parseable.query_cache_size) |
| 83 | + .await |
| 84 | + .unwrap_or(None); |
| 85 | + |
| 86 | + if let Some(query_cache_manager) = query_cache_manager { |
| 87 | + query_cache_manager |
| 88 | + .remove_from_cache(query, stream, user_id) |
| 89 | + .await?; |
| 90 | + |
| 91 | + Ok(HttpResponse::Ok().finish()) |
| 92 | + } else { |
| 93 | + Err(PostError::Invalid(anyhow!("Query Caching is not enabled"))) |
| 94 | + } |
| 95 | +} |
0 commit comments