-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Post tags endpoint: Get data from cache or graph * Integration tests created
- Loading branch information
Showing
22 changed files
with
408 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use utoipa::ToSchema; | ||
|
||
type TagFieldsTuple<'a> = (Vec<(f64, &'a str)>, (Vec<&'a str>, Vec<Vec<&'a str>>)); | ||
|
||
#[derive(Serialize, Deserialize, ToSchema, Debug, Clone, Default)] | ||
pub struct TagDetails { | ||
pub label: String, | ||
taggers: Vec<String>, | ||
taggers_count: usize, | ||
} | ||
|
||
impl TagDetails { | ||
/// Creates a list of `TagDetails` from cached tag scores and taggers. | ||
/// # Arguments | ||
/// * `tag_scores` - A `Vec` of tuples where each tuple contains a tag label as a `String` and a score as an `f64`. | ||
/// * `taggers_list` - A `Vec` of `Option` tuples where each tuple contains a `Vec` of tagger identifiers as `String` and a `usize`. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A `Vec` of `TagDetails` instances, filtered to include only those where `taggers_list` contains `Some` data. | ||
pub fn from_index( | ||
tag_scores: Vec<(String, f64)>, | ||
taggers_list: Vec<Option<(Vec<String>, usize)>>, | ||
) -> Vec<TagDetails> { | ||
tag_scores | ||
.into_iter() | ||
.zip(taggers_list) | ||
.filter_map(|((label, _), taggers)| { | ||
// TIP: MAP will not process None types and it will be automatically passed through unchanged | ||
taggers.map(|(taggers, taggers_count)| TagDetails { | ||
label, | ||
taggers, | ||
taggers_count, | ||
}) | ||
}) | ||
.collect() | ||
} | ||
|
||
/// Splits fields of `TagDetails` and calculates scores based on the number of taggers. | ||
/// # Arguments | ||
/// * `tag_details` - A reference to a slice of `TagDetails` instances. | ||
/// # Returns | ||
/// - A list of tag scores paired with their corresponding labels. | ||
/// - A list of labels and a corresponding list of lists containing tagger identifiers. | ||
/// | ||
pub fn process_tag_details<'a>(tag_details: &'a [TagDetails]) -> TagFieldsTuple { | ||
let mut tag_scores: Vec<(f64, &str)> = Vec::with_capacity(tag_details.len()); | ||
let mut labels = Vec::with_capacity(tag_details.len()); | ||
let mut taggers_id = Vec::with_capacity(tag_details.len()); | ||
for tag in tag_details { | ||
let label: &str = &tag.label; | ||
let taggers = tag | ||
.taggers | ||
.iter() | ||
.map(|s| s.as_str()) | ||
.collect::<Vec<&'a str>>(); | ||
tag_scores.push((tag.taggers.len() as f64, label)); | ||
labels.push(tag.label.as_str()); | ||
taggers_id.push(taggers); | ||
} | ||
(tag_scores, (labels, taggers_id)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,8 @@ | ||
use chrono::Utc; | ||
use serde::{Deserialize, Serialize}; | ||
use std::ops::Deref; | ||
use utoipa::ToSchema; | ||
|
||
pub mod details; | ||
pub mod global; | ||
pub mod post; | ||
pub mod stream; | ||
pub mod user; | ||
|
||
// Atomic struct to save in the cache | ||
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)] | ||
pub struct Tag { | ||
tag_id: String, // TODO: Crobfordbase32 type | ||
indexed_at: i64, | ||
tagger_id: String, | ||
} | ||
|
||
impl Default for Tag { | ||
fn default() -> Self { | ||
Self { | ||
tag_id: String::new(), | ||
indexed_at: Utc::now().timestamp(), | ||
tagger_id: String::new(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema, Default)] | ||
pub struct Tags(Vec<Tag>); | ||
|
||
// Implement Deref so TagList can be used like Vec<String> | ||
impl Deref for Tags { | ||
type Target = Vec<Tag>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
// TODO: Use all the structs in that away | ||
pub use details::TagDetails; |
Oops, something went wrong.