Skip to content

Commit

Permalink
fix(back): fix liane match algo
Browse files Browse the repository at this point in the history
  • Loading branch information
agjini committed Jun 6, 2024
1 parent 1cf90f4 commit 777432f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 55 deletions.
54 changes: 32 additions & 22 deletions app/src/screens/communities/ListGroupScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AppPressableIcon } from "@/components/base/AppPressable";
import { AppIcon } from "@/components/base/AppIcon";
import { AppColors, ContextualColors } from "@/theme/colors";
import { extractDays } from "@/util/hooks/days";
import { CoMatch, MatchGroup, RallyingPoint } from "@liane/common";
import { CoMatch, MatchGroup } from "@liane/common";
import { extractWaypointFromTo } from "@/util/hooks/lianeRequest";

export const ListGroupScreen = () => {
Expand All @@ -18,31 +18,12 @@ export const ListGroupScreen = () => {

const insets = useSafeAreaInsets();
const [error, setError] = useState<Error | undefined>(undefined);
const { to, from, steps } = useMemo(() => extractWaypointFromTo(lianeRequest?.wayPoints), [lianeRequest.wayPoints]);
const { to, from } = useMemo(() => extractWaypointFromTo(lianeRequest?.wayPoints), [lianeRequest.wayPoints]);
const daysReccurence = extractDays(lianeRequest.weekDays);
const localeTime = lianeRequest?.timeConstraints[0]
? `${lianeRequest?.timeConstraints[0]?.when?.start?.hour}h${lianeRequest?.timeConstraints[0]?.when?.start?.minute}`
: "";

const GroupItem = ({ group }: { group: CoMatch }) => (
<Pressable onPress={() => navigation.navigate("CommunitiesChat", { group: group, request: lianeRequest })}>
<View style={styles.memberContainer}>
<View style={styles.memberInfo}>
<View style={styles.textContainer}>
<AppText style={styles.nameText}>{`${group.pickup.label}${group.deposit.label}`}</AppText>
<AppText style={styles.locationText}>{`${extractDays(group.weekDays)}`}</AppText>
<AppText style={styles.timeText}>{`${(group as MatchGroup).matches?.length ?? 1} membre${
(group as MatchGroup).matches?.length > 1 ? "s" : ""
}`}</AppText>
</View>
</View>
<View style={{ paddingRight: 10 }}>
<AppIcon name={"arrow-right"} />
</View>
</View>
</Pressable>
);

return (
<View style={styles.mainContainer}>
{error && (
Expand Down Expand Up @@ -76,12 +57,41 @@ export const ListGroupScreen = () => {
</View>
</View>
<View style={styles.membersContainer}>
<FlatList data={groups} renderItem={({ item }) => <GroupItem group={item} />} />
<FlatList
data={groups}
renderItem={({ item }) => (
<GroupItem group={item} onPress={() => navigation.navigate("CommunitiesChat", { group: item, request: lianeRequest })} />
)}
/>
</View>
</View>
);
};

type GroupItemProps = {
group: CoMatch;
onPress: () => void;
};

const GroupItem = ({ group, onPress }: GroupItemProps) => (
<Pressable onPress={onPress}>
<View style={styles.memberContainer}>
<View style={styles.memberInfo}>
<View style={styles.textContainer}>
<AppText style={styles.nameText}>{`${group.pickup.label}${group.deposit.label}`}</AppText>
<AppText style={styles.locationText}>{`${extractDays(group.weekDays)}`}</AppText>
<AppText style={styles.timeText}>{`${(group as MatchGroup).matches?.length ?? 1} membre${
(group as MatchGroup).matches?.length > 1 ? "s" : ""
}`}</AppText>
</View>
</View>
<View style={{ paddingRight: 10 }}>
<AppIcon name={"arrow-right"} />
</View>
</View>
</Pressable>
);

const styles = StyleSheet.create({
mainContainer: {
backgroundColor: AppColors.lightGrayBackground,
Expand Down
7 changes: 6 additions & 1 deletion app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7713,11 +7713,16 @@ tslib@^1.8.1:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.4.0, tslib@^2.6.2:
tslib@^2.0.1, tslib@^2.0.3, tslib@^2.4.0:
version "2.6.2"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==

tslib@^2.1.0, tslib@^2.6.2:
version "2.6.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.3.tgz#0438f810ad7a9edcde7a241c3d80db693c8cbfe0"
integrity sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==

tsutils@^3.21.0:
version "3.21.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
Expand Down
62 changes: 30 additions & 32 deletions back/src/Liane/Liane.Service/Internal/Community/LianeMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,35 @@ public async Task<ImmutableDictionary<Guid, LianeMatcherResult>> FindMatches(
private static async Task<IEnumerable<LianeRawMatch>> FindRawMatches(IDbConnection connection, IEnumerable<Guid> lianeRequests, IDbTransaction? tx = null)
{
return await connection.QueryAsync<LianeRawMatch>("""
SELECT liane_request_a.id AS "from",
liane_request_b.id AS liane_request,
liane_request_b.name AS name,
liane_request_b.created_by AS "user",
liane_request_b.way_points AS way_points,
liane_request_b.is_enabled AS is_enabled,
st_length(intersection) / a_length AS score,
matching_weekdays(liane_request_a.week_days, liane_request_b.week_days) AS week_days,
st_startpoint(intersection) AS pickup,
st_endpoint(intersection) AS deposit,
nearest_rp(st_startpoint(intersection)) AS pickup_point,
nearest_rp(st_endpoint(intersection)) AS deposit_point,
(SELECT array_agg(liane_id) FROM liane_member WHERE liane_request_id = liane_request_b.id) AS lianes
FROM (
SELECT a.way_points AS a,
b.way_points AS b,
st_linemerge(st_intersection(a.geometry, b.geometry)) intersection,
st_length(a.geometry) AS a_length
FROM route a
INNER JOIN route b ON st_intersects(a.geometry, b.geometry)
) AS matches
INNER JOIN liane_request liane_request_a ON
liane_request_a.way_points = a
INNER JOIN liane_request liane_request_b ON
liane_request_b.way_points = b AND liane_request_b.created_by != liane_request_a.created_by
LEFT JOIN liane_member ON
liane_request_id = liane_request_b.id
WHERE
matching_weekdays(liane_request_a.week_days, liane_request_b.week_days)::integer != 0
AND st_length(intersection) / a_length > 0.3
AND liane_request_a.id = ANY(@liane_requests)
ORDER BY st_length(intersection) / a_length DESC, liane_request_a.id
SELECT liane_request_match.id AS "from",
liane_request_b.id AS liane_request,
liane_request_b.name AS name,
liane_request_b.created_by AS "user",
liane_request_b.way_points AS way_points,
liane_request_b.is_enabled AS is_enabled,
st_length(intersection) / a_length AS score,
matching_weekdays(liane_request_match.week_days, liane_request_b.week_days) AS week_days,
st_startpoint(intersection) AS pickup,
st_endpoint(intersection) AS deposit,
nearest_rp(st_startpoint(intersection)) AS pickup_point,
nearest_rp(st_endpoint(intersection)) AS deposit_point,
(SELECT array_agg(DISTINCT liane_id) FROM liane_member WHERE liane_request_id = liane_request_b.id) AS lianes
FROM (SELECT liane_request.id,
liane_request.created_by,
liane_request.week_days,
a.way_points AS a,
b.way_points AS b,
st_linemerge(st_intersection(a.geometry, b.geometry)) intersection,
st_length(a.geometry) AS a_length
FROM liane_request
INNER JOIN route a ON a.way_points = liane_request.way_points
INNER JOIN route b ON st_intersects(a.geometry, b.geometry)
WHERE liane_request.id = ANY(@liane_requests)) AS liane_request_match
INNER JOIN liane_request liane_request_b ON
liane_request_b.way_points = b AND liane_request_b.created_by != liane_request_match.created_by
WHERE matching_weekdays(liane_request_match.week_days, liane_request_b.week_days)::integer != 0
AND st_length(intersection) / a_length > 0.3
ORDER BY st_length(intersection) / a_length DESC, "from"
""",
new { liane_requests = lianeRequests.ToArray() },
tx
Expand Down Expand Up @@ -125,6 +122,7 @@ private async Task<ImmutableList<Match>> GroupMatchByLiane(IEnumerable<LianeRawM
first.Deposit,
first.Score));
}))
.Distinct()
.OrderByDescending(m => m.Score)
.ThenByDescending(m => m switch
{
Expand Down

0 comments on commit 777432f

Please sign in to comment.