Modifies sql queries to use CTEs (#773)

* fixes huge slowodwn on version item

* changes!

* fixes, touch ups, indices

* clippy prepare
This commit is contained in:
Wyatt Verchere
2023-11-30 11:10:56 -08:00
committed by GitHub
parent ed33dd2127
commit 58093a9438
19 changed files with 957 additions and 549 deletions

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use super::ids::{Base62Id, OrganizationId};
use super::teams::TeamId;
@@ -109,10 +109,49 @@ pub struct Project {
/// The monetization status of this project
pub monetization_status: MonetizationStatus,
/// Aggregated loader-fields across its myriad of versions
#[serde(flatten)]
pub fields: HashMap<String, Vec<serde_json::Value>>,
}
fn remove_duplicates(values: Vec<serde_json::Value>) -> Vec<serde_json::Value> {
let mut seen = HashSet::new();
values
.into_iter()
.filter(|value| {
// Convert the JSON value to a string for comparison
let as_string = value.to_string();
// Check if the string is already in the set
seen.insert(as_string)
})
.collect()
}
impl From<QueryProject> for Project {
fn from(data: QueryProject) -> Self {
let mut fields: HashMap<String, Vec<serde_json::Value>> = HashMap::new();
for vf in data.aggregate_version_fields {
// We use a string directly, so we can remove duplicates
let serialized = if let Some(inner_array) = vf.value.serialize_internal().as_array() {
inner_array.clone()
} else {
vec![vf.value.serialize_internal()]
};
// Create array if doesnt exist, otherwise push, or if json is an array, extend
if let Some(arr) = fields.get_mut(&vf.field_name) {
arr.extend(serialized);
} else {
fields.insert(vf.field_name, serialized);
}
}
// Remove duplicates by converting to string and back
for (_, v) in fields.iter_mut() {
*v = remove_duplicates(v.clone());
}
let m = data.inner;
Self {
id: m.id.into(),
@@ -196,6 +235,7 @@ impl From<QueryProject> for Project {
color: m.color,
thread_id: data.thread_id.into(),
monetization_status: m.monetization_status,
fields,
}
}
}
@@ -640,7 +680,7 @@ pub struct VersionFile {
/// A dendency which describes what versions are required, break support, or are optional to the
/// version's functionality
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Dependency {
/// The specific version id that the dependency uses
pub version_id: Option<VersionId>,
@@ -677,7 +717,7 @@ impl VersionType {
}
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum DependencyType {
Required,