Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: priority recompute restructure #279

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

pratikmishra356
Copy link
Collaborator

@pratikmishra356 pratikmishra356 commented Nov 7, 2024

Problem

User has to declare this exponential value, as value is exponential it has limitation on the total number of dimensions that can be defined

Solution

Postgresql's Numeric Type Approach

For dimension we can have another column named order which will store order of dimension's specificity ,
the higher order is means more specific
For context , we will have another column named weightage , where we will calculate the value just like we do now
it will summation of (2^ dimension's order value)

since context's weightage value can be too large , so we will use postgresql Numeric type

CREATE TABLE large_numbers (
id SERIAL PRIMARY KEY,
big_value NUMERIC
);
Rust/ Diesel supports BigDecimal type which is compatible with Numeric type

Environment variable changes

What ENVs need to be added or changed

Pre-deployment activity

Things needed to be done before deploying this change (if any)

Post-deployment activity

link for migration/backward compatibility: #277 (comment)

API changes

Endpoint Method Request body Response Body
API GET/POST, etc request response

Possible Issues in the future

Describe any possible issues that could occur because of this change

@pratikmishra356 pratikmishra356 requested a review from a team as a code owner November 7, 2024 10:15
@pratikmishra356 pratikmishra356 force-pushed the priority-value-restructure branch 9 times, most recently from 14c0833 to 40cd334 Compare November 18, 2024 06:57
ALTER TABLE public.dimensions
add column position integer DEFAULT 1 NOT NULL;

ALTER TABLE public.contexts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we write a query to copy priority to weightage

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Datron we can write , but we will anyway have to re populate dimension position and then recompute

migration approach: #277 (comment)

@@ -0,0 +1,8 @@
-- Your SQL goes here
ALTER TABLE public.dimensions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you backfill this based on priority? Can be computed from priority

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can but , serial order is being not followed in all tenants and their values does not need be of 2's power

@@ -11,6 +11,7 @@ actix-http = "3.3.1"
actix-web = { workspace = true }
anyhow = { workspace = true }
base64 = { workspace = true }
bigdecimal = { version = "0.3.1" , features= ["serde"]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need bigdecimal?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @Datron we need to calculate higher value

@@ -38,6 +42,12 @@ use crate::db::{
},
};

pub struct DimensionData {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derive debug and Clone

@@ -38,6 +42,12 @@ use crate::db::{
},
};

pub struct DimensionData {
pub schema: JSONSchema,
pub priority: i32,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub priority: i32,
pub weight: i32,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Datron it will be priority only,
I refactored a bit here , changed from tuples to struct

.get(dimension.as_str())
.map(|x| x.position)
.ok_or_else(|| {
let msg = String::from("Dimension not found in Dimension schema map");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also log the name of the dimension we couldn't parse?

add column position integer DEFAULT 1 NOT NULL;

ALTER TABLE public.contexts
add column weightage numeric(1000,0) DEFAULT 1 NOT NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call this weight?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can , lets check with team also

@@ -527,7 +529,8 @@ async fn reduce_config(
.and_then(|value| value.to_str().ok().and_then(|s| s.parse::<bool>().ok()))
.unwrap_or(false);

let dimensions_schema_map = get_all_dimension_schema_map(&mut conn)?;
let dimensions_vec = get_dimension_data(&mut conn)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should get_dimension_data return a map?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No , it does not need to be , i have separated it into 2 function and get_dimension_data_map this function returns only values which are needed

Comment on lines 90 to 91
pub old_weightage: BigDecimal,
pub new_weightage: BigDecimal,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub old_weightage: BigDecimal,
pub new_weightage: BigDecimal,
pub old_weight: BigDecimal,
pub new_weight: BigDecimal,

impl Position {
fn validate_data(position_val: Option<i32>) -> Result<Self, String> {
if let Some(val) = position_val {
if val < 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should allow 0 right? 2^0 is 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we are allowing 0

@pratikmishra356 pratikmishra356 force-pushed the priority-value-restructure branch 3 times, most recently from 286c0d0 to ec03437 Compare November 22, 2024 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants