0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-07 00:50:23 -05:00

fix(server): Improve reverse geocoded location metadata (#9051)

* fix: improve reverse geocoding

* fix: update tests referencing states

* fix: expect state suggestion in any order

---------

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
Hermes Espínola González 2024-04-28 13:29:08 -07:00 committed by GitHub
parent a2cf8c7fc7
commit 4bb7d2df49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 8 deletions

View file

@ -1006,7 +1006,7 @@ describe('/asset', () => {
id: expect.any(String),
lat: expect.closeTo(39.115),
lon: expect.closeTo(-108.400_968),
state: 'Mesa County, Colorado',
state: 'Colorado',
},
{
city: 'Ralston',
@ -1014,7 +1014,7 @@ describe('/asset', () => {
id: expect.any(String),
lat: expect.closeTo(41.2203),
lon: expect.closeTo(-96.071_625),
state: 'Douglas County, Nebraska',
state: 'Nebraska',
},
]);
});
@ -1033,7 +1033,7 @@ describe('/asset', () => {
id: expect.any(String),
lat: expect.closeTo(39.115),
lon: expect.closeTo(-108.400_968),
state: 'Mesa County, Colorado',
state: 'Colorado',
},
{
city: 'Ralston',
@ -1041,7 +1041,7 @@ describe('/asset', () => {
id: expect.any(String),
lat: expect.closeTo(41.2203),
lon: expect.closeTo(-96.071_625),
state: 'Douglas County, Nebraska',
state: 'Nebraska',
},
]);
});

View file

@ -469,7 +469,8 @@ describe('/search', () => {
const { status, body } = await request(app)
.get('/search/suggestions?type=state')
.set('Authorization', `Bearer ${admin.accessToken}`);
expect(body).toEqual(states);
expect(body).toHaveLength(states.length);
expect(body).toEqual(expect.arrayContaining(states));
expect(status).toBe(200);
});

View file

@ -79,7 +79,9 @@ export class MetadataRepository implements IMetadataRepository {
queryRunner: QueryRunner,
lineToEntityMapper: (lineSplit: string[]) => GeodataPlacesEntity,
filePath: string,
options?: { entityFilter?: (linesplit: string[]) => boolean },
) {
const _entityFilter = options?.entityFilter ?? (() => true);
if (!existsSync(filePath)) {
this.logger.error(`Geodata file ${filePath} not found`);
throw new Error(`Geodata file ${filePath} not found`);
@ -91,6 +93,9 @@ export class MetadataRepository implements IMetadataRepository {
for await (const line of lineReader) {
const lineSplit = line.split('\t');
if (!_entityFilter(lineSplit)) {
continue;
}
const geoData = lineToEntityMapper(lineSplit);
bufferGeodata.push(geoData);
if (bufferGeodata.length > 1000) {
@ -123,6 +128,7 @@ export class MetadataRepository implements IMetadataRepository {
admin2Name: admin2Map.get(`${lineSplit[8]}.${lineSplit[10]}.${lineSplit[11]}`),
}),
geodataCities500Path,
{ entityFilter: (lineSplit) => lineSplit[7] != 'PPLX' },
);
}
@ -167,10 +173,9 @@ export class MetadataRepository implements IMetadataRepository {
this.logger.verbose(`Raw: ${JSON.stringify(response, null, 2)}`);
const { countryCode, name: city, admin1Name, admin2Name } = response;
const { countryCode, name: city, admin1Name } = response;
const country = getName(countryCode, 'en') ?? null;
const stateParts = [admin2Name, admin1Name].filter((name) => !!name);
const state = stateParts.length > 0 ? stateParts.join(', ') : null;
const state = admin1Name;
return { country, state, city };
}