# # # profile_validation.cfv # # # validate_profile_item_label() # # get_validated_database_fields() # # # include "templates.util.base_util"; # # # # # validate_profile_item_label() # # # # subroutine(validate_profile_item_label( node label_checksums, node profile_item), ( # This checks for label existance and for a unique label in the profile_item (i.e. log fields, database fields, etc.). # If the label doesn't exist or already exists in label_checksums then a new label is created and set. # It returns is_modified # debug_message("\n#### validate_profile_item_label() \n"); # debug_message("#### label_checksums 1:\n" . node_as_string(label_checksums) . "\n"); string label; string expanded_label; string expanded_label_checksum; bool is_modified = false; if (profile_item?{"label"} and @profile_item{"label"} ne "") then ( label = @profile_item{"label"}; expanded_label = get_expanded_label(label); ) else ( # No label exists, use the node name label = node_name(profile_item); expanded_label = label; # Finally set the lable now @profile_item{"label"} = label; is_modified = true; ); expanded_label_checksum = md5_digest(expanded_label); if (label_checksums?{expanded_label_checksum}) then ( # The label already exists, modifiy the label so that # it becomes unqiue. bool is_unique = false; int count = 2; string suffix; string draft_label; while (!is_unique) ( suffix = " (" . count . ")"; draft_label = expanded_label . suffix; expanded_label_checksum = md5_digest(draft_label); is_unique = (!label_checksums?{expanded_label_checksum}); count++; ); # # Set modified label in profile_item # # Note, we concat the label with the suffix, not the expanded_label # so that language variables are resolved. # Replace existing label in source node @profile_item{"label"} = label . suffix; is_modified = true; ); # Add checksum to label_checksums @label_checksums{expanded_label_checksum} = true; # debug_message("#### label_checksums 2:\n" . node_as_string(label_checksums) . "\n"); # debug_message("#### is_modified: " . is_modified . "\n"); # Return is_modified is_modified; )); # # # # # get_validated_database_fields() # # # # subroutine(get_validated_database_fields( node profile), ( # This returns a validated database fields node by making sure that # - all relevant nodes exist (most missing nodes are added automatically) # - all labels are unique (fixes duplicate labels) # - all given log fields exist (throws an error otherwise) # Note, we save the profile if missing nodes or duplicate labels are fixed. debug_message("\n#### get_validated_database_fields() \n"); node db_fields; if (?(profile . ".database.fields") and (num_subnodes(profile . ".database.fields") > 0)) then ( db_fields = profile . ".database.fields"; node db_field; bool is_modified = false; node log_fields = if (?(profile . ".log.fields")) then (profile . ".log.fields") else (new_node()); # Track label checksum to check for duplicate labels node label_checksums = new_node(); string db_field_name; string derivation_method; string log_field_name; string db_field_type; string error_message; foreach db_field db_fields ( db_field_name = node_name(db_field); # # Handle the label # is_modified = validate_profile_item_label(label_checksums, db_field); # # Check for derivation method (it may be missed due an import profiles bug) # if (db_field?{"derivation_method"} and (@db_field{"derivation_method"} eq "log_field")) then ( derivation_method = "log_field"; ) else if (db_field?{"derivation_method"} and (@db_field{"derivation_method"} eq "session_field")) then ( # ToDo, revise in nunchaku derivation_method = "session_field"; ) else ( # derivation_method is missing, auto fix it if (db_field?{"log_field"} and (@db_field{"log_field"} ne "")) then ( derivation_method = "log_field"; ) else if (db_field?{"session_field"} and (@db_field{"session_field"} ne "")) then ( derivation_method = "session_field"; ) else if (matches_regular_expression(db_field_name, "^ssession(s|_id|_events|_user|_users|_date_time|_begin|_end|_duration|_page|_entrances|_exits)$")) then ( # This must be based on a session field derivation_method = "session_field"; # Set session field @db_field{"session_field"} = replace_first(db_field_name, "ss", "s"); ) else ( # We assume this db_field is based on a log field derivation_method = "log_field"; # Set log_field @db_field{"log_field"} = db_field_name; ); # Set derivation_method @db_field{"derivation_method"} = derivation_method; is_modified = true; ); # # Check if the log field exists # if (derivation_method eq "log_field") then ( log_field_name = @db_field{"log_field"}; if (!log_fields?{log_field_name}) then ( # Log field is missing, throw an error volatile.param1 = db_field_name; volatile.param2 = log_field_name; error_message = expand(lang_admin.profile_validation.database_fields.missing_log_field); error(error_message); ); ); # # Check db_field_type # if (db_field?{"type"}) then ( db_field_type = @db_field{"type"}; ) else ( # Missing type, set string as defauly db_field_type = "string"; @db_field{"type"} = db_field_type; is_modified = true; ); # # Check type related parameters # if (db_field_type ne "float") then ( # # Check integer_bits # if (!db_field?{"integer_bits"} or (@db_field{"integer_bits"} eq "")) then ( @db_field{"integer_bits"} = 0; is_modified = true; ); # # Check suppress_top, suppress_bottom, sql_field_length # if (db_field_type eq "string") then ( if (!db_field?{"suppress_top"} or (@db_field{"suppress_top"} eq "")) then ( @db_field{"suppress_top"} = 0; is_modified = true; ); if (!db_field?{"suppress_bottom"} or (@db_field{"suppress_bottom"} eq "")) then ( @db_field{"suppress_bottom"} = 2; is_modified = true; ); if (!db_field?{"sql_field_length"} or (@db_field{"sql_field_length"} eq "")) then ( @db_field{"sql_field_length"} = 200; is_modified = true; ); ); ); # # Check aggregation_method # if (!db_field?{"aggregation_method"} or (@db_field{"aggregation_method"} eq "")) then ( @db_field{"aggregation_method"} = if (db_field_type eq "string") then ("none") else ("sum"); is_modified = true; ); # # Check index # if (!db_field?{"index"}) then ( @db_field{"index"} = true; is_modified = true; ); # # Check category # if (!db_field?{"category"}) then ( @db_field{"category"} = ""; is_modified = true; ); ); # # Save profile if is_modified # if (is_modified) then ( save_node(profile); ); ) else ( # No database fields, return empty node db_fields = new_node(); ); debug_message("#### Validated database fields node:\n" . node_as_string(db_fields) . "\n"); # Return validated db_fields node db_fields; ));