Compare commits

...

3 Commits

Author SHA1 Message Date
Thea Schöbl
5f67c57c8b feat: backend index diffing 2025-08-26 10:04:04 +00:00
Rainer Killinger
6b06de4019 refactor: update audit relevant nodemailer dependency 2025-07-30 13:58:25 +02:00
56331fe4ff feat: backend index diffing 2024-07-22 11:21:05 +00:00
10 changed files with 158 additions and 50 deletions

View File

@@ -0,0 +1,6 @@
---
"@openstapps/backend": patch
"@openstapps/logger": patch
---
Updated nodemailer dependency

View File

@@ -69,7 +69,7 @@
"nock": "13.3.1",
"node-cache": "5.1.2",
"node-cron": "3.0.2",
"nodemailer": "6.9.1",
"nodemailer": "6.9.9",
"prom-client": "14.1.1",
"promise-queue": "2.2.5",
"uuid": "8.3.2"
@@ -106,6 +106,9 @@
"entry": [
"src/cli.ts"
],
"loader": {
".groovy": "text"
},
"sourcemap": true,
"clean": true,
"target": "es2022",

View File

@@ -0,0 +1,65 @@
void traverse(def a, def b, ArrayList path, HashMap result) {
if (a instanceof Map && b instanceof Map) {
for (key in a.keySet()) {
path.add(key);
traverse(a.get(key), b.get(key), path, result);
path.remove(path.size() - 1);
}
} else if (a instanceof List && b instanceof List) {
int la = a.size();
int lb = b.size();
int max = la > lb ? la : lb;
for (int i = 0; i < max; i++) {
path.add(i);
if (i < la && i < lb) {
traverse(a[i], b[i], path, result);
} else if (i >= la) {
result.added.add(path.toArray());
} else {
result.removed.add(path.toArray());
}
path.remove(path.size() - 1);
}
} else if (a == null && b != null) {
result.removed.add(path.toArray());
} else if (a != null && b == null) {
result.added.add(path.toArray());
} else if (!a.equals(b)) {
result.changed.add(path.toArray());
}
}
def to;
def from;
for (state in states) {
if (state.index.equals(params.newIndex)) {
to = state.doc;
} else {
from = state.doc;
}
}
HashMap result = [
'added': [],
'removed': [],
'changed': []
];
traverse(to, from, new ArrayList(), result);
if (to == null && from != null) {
result.status = 'removed';
} else if (to != null && from == null) {
result.status = 'added';
} else if (
result.added.size() == 0 &&
result.removed.size() == 0 &&
result.changed.size() == 0
) {
result.status = 'unchanged';
} else {
result.status = 'changed';
}
return result;

View File

@@ -47,6 +47,7 @@ import {
import {noUndefined} from './util/no-undefined.js';
import {retryCatch, RetryOptions} from './util/retry.js';
import {Feature, Point, Polygon} from 'geojson';
import indexDiffScript from './diff-index.groovy';
/**
* A database interface for elasticsearch
@@ -239,6 +240,42 @@ export class Elasticsearch implements Database {
.then(it => Object.entries(it).map(([name]) => name))
.catch(() => [] as string[]);
if (activeIndices.length <= 1) {
const result = await this.client.transform.previewTransform({
source: {
index: [...activeIndices, index],
query: {match_all: {}},
},
dest: {index: 'compare'},
pivot: {
group_by: {
uid: {terms: {field: 'uid.raw'}},
},
aggregations: {
compare: {
scripted_metric: {
map_script: `
state.index = doc['_index'];
state.doc = params['_source'];`,
combine_script: `
state.index = state.index[0];
return state;
`,
reduce_script: {
source: indexDiffScript,
params: {
newIndex: index,
},
},
},
},
},
},
});
console.log(JSON.stringify(result.preview, null, 2))
}
await this.client.indices.updateAliases({
actions: [
{

View File

@@ -0,0 +1,26 @@
// initialize the sort value with the maximum
double price = Double.MAX_VALUE;
// if we have any offers
if (params._source.containsKey(params.field)) {
// iterate through all offers
for (offer in params._source[params.field]) {
// if this offer contains a role specific price
if (offer.containsKey('prices') && offer.prices.containsKey(params.universityRole)) {
// if the role specific price is smaller than the cheapest we found
if (offer.prices[params.universityRole] < price) {
// set the role specific price as cheapest for now
price = offer.prices[params.universityRole];
}
} else { // we have no role specific price for our role in this offer
// if the default price of this offer is lower than the cheapest we found
if (offer.price < price) {
// set this price as the cheapest
price = offer.price;
}
}
}
}
// return cheapest price for our role
return price;

View File

@@ -13,7 +13,8 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SortOptions} from '@elastic/elasticsearch/lib/api/types.js';
import {SCPriceSort, SCSportCoursePriceGroup, SCThingsField} from '@openstapps/core';
import {SCPriceSort} from '@openstapps/core';
import priceSortScript from './price-sort.groovy';
/**
* Converts a price sort to elasticsearch syntax
@@ -23,47 +24,11 @@ export function buildPriceSort(sort: SCPriceSort): SortOptions {
return {
_script: {
order: sort.order,
script: buildPriceSortScript(sort.arguments.universityRole, sort.arguments.field),
script: {
source: priceSortScript,
params: sort.arguments,
},
type: 'number' as const,
},
};
}
/**
* Provides a script for sorting search results by prices
* @param universityRole User group which consumes university services
* @param field Field in which wanted offers with prices are located
*/
export function buildPriceSortScript(
universityRole: keyof SCSportCoursePriceGroup,
field: SCThingsField,
): string {
return `
// initialize the sort value with the maximum
double price = Double.MAX_VALUE;
// if we have any offers
if (params._source.containsKey('${field}')) {
// iterate through all offers
for (offer in params._source.${field}) {
// if this offer contains a role specific price
if (offer.containsKey('prices') && offer.prices.containsKey('${universityRole}')) {
// if the role specific price is smaller than the cheapest we found
if (offer.prices.${universityRole} < price) {
// set the role specific price as cheapest for now
price = offer.prices.${universityRole};
}
} else { // we have no role specific price for our role in this offer
// if the default price of this offer is lower than the cheapest we found
if (offer.price < price) {
// set this price as the cheapest
price = offer.price;
}
}
}
}
// return cheapest price for our role
return price;
`;
}

6
backend/backend/src/types.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
declare module '*.groovy' {
const content: string;
export default content;
}
export {};

0
examples/minimal-connector/app.js Normal file → Executable file
View File

View File

@@ -31,7 +31,7 @@
"@types/nodemailer": "6.4.7",
"chalk": "5.2.0",
"flatted": "3.2.7",
"nodemailer": "6.9.1"
"nodemailer": "6.9.9"
},
"devDependencies": {
"@openstapps/eslint-config": "workspace:*",

14
pnpm-lock.yaml generated
View File

@@ -134,8 +134,8 @@ importers:
specifier: 3.0.2
version: 3.0.2
nodemailer:
specifier: 6.9.1
version: 6.9.1
specifier: 6.9.9
version: 6.9.9
prom-client:
specifier: 14.1.1
version: 14.1.1
@@ -1990,8 +1990,8 @@ importers:
specifier: 3.2.7
version: 3.2.7
nodemailer:
specifier: 6.9.1
version: 6.9.1
specifier: 6.9.9
version: 6.9.9
devDependencies:
'@openstapps/eslint-config':
specifier: workspace:*
@@ -8631,8 +8631,8 @@ packages:
node-releases@2.0.19:
resolution: {integrity: sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw==}
nodemailer@6.9.1:
resolution: {integrity: sha512-qHw7dOiU5UKNnQpXktdgQ1d3OFgRAekuvbJLcdG5dnEo/GtcTHRYM7+UfJARdOFU9WUQO8OiIamgWPmiSFHYAA==}
nodemailer@6.9.9:
resolution: {integrity: sha512-dexTll8zqQoVJEZPwQAKzxxtFn0qTnjdQTchoU6Re9BUUGBJiOy3YMn/0ShTW6J5M0dfQ1NeDeRTTl4oIWgQMA==}
engines: {node: '>=6.0.0'}
non-layered-tidy-tree-layout@2.0.2:
@@ -19780,7 +19780,7 @@ snapshots:
node-releases@2.0.19: {}
nodemailer@6.9.1: {}
nodemailer@6.9.9: {}
non-layered-tidy-tree-layout@2.0.2:
optional: true