Skip to content
Snippets Groups Projects
Commit e5c41888 authored by Sjoerd Vink's avatar Sjoerd Vink
Browse files

feat(reportOnNewData): use hashing instead of dict

parent 1762b548
No related tags found
No related merge requests found
Pipeline #143169 passed
......@@ -24,6 +24,7 @@ import { parseCypherQuery } from "../utils/cypher/queryParser";
import { formatTimeDifference } from "ts-common/src/logger/logger";
import { graphQueryBackend2graphQuery } from "../frontend/statistics";
import { Query2BackendQuery } from "../utils/reactflow/query2backend";
import { hashDictionary, hashIsEqual } from "../utils/hashing";
export const queryService = async (db: DbConnection, query: string): Promise<GraphQueryResultMetaFromBackend> => {
// TODO: only neo4j is supported for now
......@@ -287,16 +288,13 @@ export const queryServiceReaderDiffCheck = async (type: QueryExecutionTypes) =>
USER_MANAGEMENT_SERVICE_API
);
// const ssInsight = await getUserSaveStateInsight(
// headers.message.sessionData.userID,
// headers.message.sessionData.saveStateID,
// USER_MANAGEMENT_SERVICE_API
// );
const previousQueryResult = {
nodes: [],
edges: [],
};
const ssInsight = await getUserSaveStateInsight(
headers.message.sessionData.userID,
headers.message.sessionData.saveStateID,
USER_MANAGEMENT_SERVICE_API
);
// const previousQueryResult = ssInsight.previous_result_hash;
const previousQueryResult = "Get hash from db";
log.debug("Received query request:", ss);
......@@ -331,21 +329,14 @@ export const queryServiceReaderDiffCheck = async (type: QueryExecutionTypes) =>
log.debug("Query result!");
log.info(`Query executed in ${formatTimeDifference(Date.now() - startTime)}`);
const queryResult = {
const queryResult = hashDictionary({
nodes: result.nodes.map((node) => node._id),
edges: result.edges.map((edge) => edge._id),
};
// Compare query results
const nodesDifferent = queryResult.nodes.some((node, index) => node !== previousQueryResult.nodes[index]);
const edgesDifferent = queryResult.edges.some((edge, index) => edge !== previousQueryResult.edges[index]);
if (
queryResult.nodes.length !== previousQueryResult.nodes.length ||
queryResult.edges.length !== previousQueryResult.edges.length ||
nodesDifferent ||
edgesDifferent
) {
});
log.info("Comparing hash values from current and previous query");
if (previousQueryResult && !hashIsEqual(queryResult, previousQueryResult)) {
log.info("Different results, use Dennis code...");
} else {
log.info("No difference in result sets");
......@@ -356,7 +347,7 @@ export const queryServiceReaderDiffCheck = async (type: QueryExecutionTypes) =>
headers.message.sessionData.userID,
headers.message.sessionData.saveStateID,
USER_MANAGEMENT_SERVICE_API,
queryResult
{ previous_result_hash: queryResult }
);
log.info("Updated node and edge ids in SaveState");
......
const FNV_OFFSET_BASIS = 2166136261;
export const hashDictionary = (dictionary: Record<"nodes" | "edges", string[]>): string => {
// Convert the dictionary into a consistent string format
const jsonString = JSON.stringify(dictionary, Object.keys(dictionary).sort());
// FNV-1a hash implementation
let hash = FNV_OFFSET_BASIS; // FNV offset basis
for (let i = 0; i < jsonString.length; i++) {
hash ^= jsonString.charCodeAt(i);
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); // FNV prime
}
// Convert hash to a hexadecimal string
return (hash >>> 0).toString(16); // Ensure unsigned integer representation
};
export const hashIsEqual = (hash1: string, hash2: string): boolean => {
return hash1 === hash2;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment