Skip to content

Commit

Permalink
Merge pull request #39 from thkruz/minor-fixes-and-linter
Browse files Browse the repository at this point in the history
Minor fixes and linter
  • Loading branch information
ajmas authored Dec 21, 2023
2 parents 48eb5ca + bed9742 commit 063b917
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 94 deletions.
63 changes: 41 additions & 22 deletions src/hud/SearchBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import logger from '@/utils/logger';
import { Viewer } from '@satellite-viewer/index';
import SatelliteGroup from '@satellite-viewer/SatelliteGroup';
import HudWindowManager from './HudWindowManager';
import { SearchResults } from '../viewer/interfaces/SearchResults';

const SEARCH_LIMIT = 200;

Expand Down Expand Up @@ -73,7 +74,7 @@ function hideResults () {
}
}

function fillResultBox (results: any, searchStr: string) {
function fillResultBox (results: SearchResults[], searchStr: string) {
const resultBox = document.querySelector('#search-results') as HTMLElement;
const satelliteStore = viewer.getSatelliteStore();
if (!satelliteStore) {
Expand All @@ -82,34 +83,44 @@ function fillResultBox (results: any, searchStr: string) {

let html = '';

for (let i = 0; i < results.length; i++) {
if (results[i].satId === undefined) {
for (const result of results) {
if (result.satId === undefined) {
continue;
}

const satellite = satelliteStore.getSatellite(results[i].satId);
const satellite = satelliteStore.getSatellite(result.satId);
if (!satellite) {
logger.warn('satellite not found', results[i].satId);
logger.warn('satellite not found', result.satId);
continue;
}

html += `<div class="search-result" data-sat-id="${satellite.id}">`;
if (results[i].isIntlDes) {
if (result.type !== 'name') {
html += satellite.OBJECT_NAME;
} else {
html += satellite.OBJECT_NAME.substring(0, results[i].strIndex);
html += '<span class="search-hilight">';
html += satellite.OBJECT_NAME.substring(results[i].strIndex, results[i].strIndex + searchStr.length);
html += '</span>';
html += satellite.OBJECT_NAME.substring(results[i].strIndex + searchStr.length);
html += `
${satellite.OBJECT_NAME.substring(0, result.strIndex)}
<span class="search-hilight">
${satellite.OBJECT_NAME.substring(result.strIndex, result.strIndex + searchStr.length)}
</span>
${satellite.OBJECT_NAME.substring(result.strIndex + searchStr.length)}`;
}

html += '<div class="search-result-intldes">';
if (results[i].isIntlDes) {
html += satellite.intlDes.substring(0, results[i].strIndex);
html += '<span class="search-hilight">';
html += satellite.intlDes.substring(results[i].strIndex, results[i].strIndex + searchStr.length);
html += '</span>';
html += satellite.intlDes.substring(results[i].strIndex + searchStr.length);
if (result.type === 'intlDes') {
html += `
${satellite.intlDes.substring(0, result.strIndex)}
<span class="search-hilight">
${satellite.intlDes.substring(result.strIndex, result.strIndex + searchStr.length)}
</span>
${satellite.intlDes.substring(result.strIndex + searchStr.length)}`;
} else if (result.type === 'noradId') {
html += `
${satellite.NORAD_CAT_ID.substring(0, result.strIndex)}
<span class="search-hilight">
${satellite.NORAD_CAT_ID.substring(result.strIndex, result.strIndex + searchStr.length)}
</span>
${satellite.NORAD_CAT_ID.substring(result.strIndex + searchStr.length)}`;
} else {
html += satellite.intlDes;
}
Expand Down Expand Up @@ -145,19 +156,27 @@ function doSearch (str: string) {

str = str.toUpperCase();

const results = [];
const results: SearchResults[] = [];
for (let i = 0; i < satData.length; i++) {
if (satData[i]?.NORAD_CAT_ID?.indexOf(str) !== -1) {
results.push({
type: 'noradId',
strIndex: satData[i].NORAD_CAT_ID.indexOf(str),
satId: i
});
}

if (satData[i]?.OBJECT_NAME.indexOf(str) !== -1) {
results.push({
isIntlDes: false,
type: 'name',
strIndex: satData[i].OBJECT_NAME.indexOf(str),
satId: i
});
}

if (satData[i].intlDes && satData[i].intlDes.indexOf(str) !== -1) {
results.push({
isIntlDes: true,
type: 'intlDes',
strIndex: satData[i].intlDes.indexOf(str),
satId: i
});
Expand All @@ -170,8 +189,8 @@ function doSearch (str: string) {

// make a group to hilight results
const idList = [];
for (let i = 0; i < results.length; i++) {
idList.push(results[i].satId);
for (const result of results) {
idList.push(result.satId);
}

const dispGroup = new SatelliteGroup(
Expand Down
146 changes: 101 additions & 45 deletions src/viewer/SatelliteGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SatelliteGroup {
id: string;
name: string;
groupType: string;
data: any;
data: number[] | string[] | RegExp | string;
private satelliteStore: SatelliteStore;

constructor (groupId: string, name: string, groupType: string, data: any, satelliteStore: SatelliteStore) {
Expand All @@ -23,56 +23,112 @@ class SatelliteGroup {
reload () {
this.sats = [];

if (this.groupType === 'intlDes') {
for (let i = 0; i < this.data.length; i++) {
this.sats.push({
satId: this.satelliteStore.getIdFromIntlDes(this.data[i]) as number,
isIntlDes: true,
strIndex: 0
});
}
} else if (this.groupType === 'nameRegex') {
let regex = this.data;
if (typeof regex === 'string') {
regex = new RegExp(regex);
}
const satIdList = this.satelliteStore.searchNameRegex(regex);
for (let i = 0; i < satIdList.length; i++) {
this.sats.push({
satId: satIdList[i],
isIntlDes: false,
strIndex: 0
});
}
} else if (this.groupType === 'idList') {
for (let i = 0; i < this.data.length; i++) {
this.sats.push({
satId: this.data[i],
isIntlDes: false,
strIndex: 0
});
}
} else if (this.groupType === 'objectType') {
const field = 'OBJECT_TYPE';
const satIdList = this.satelliteStore.search({ [field]: this.data });
for (let i = 0; i < satIdList.length; i++) {
this.sats.push({
satId: satIdList[i].id,
isIntlDes: false,
strIndex: 0
});
}
switch (this.groupType) {
case 'intlDes':
this.searchIntlDes();
break;
case 'nameRegex':
this.searchNameRegex();
break;
case 'idList':
this.searchIdList();
break;
case 'objectType':
this.searchObjectType();
break;
default:
throw new Error('Invalid groupType');
}
}

/**
* Searches for satellite objects of a specific type.
* @throws {Error} If objectType is not a string.
*/
private searchObjectType () {
if (typeof this.data !== 'string') {
throw new Error('objectType must be a string');
}
const field = 'OBJECT_TYPE';
const satIdList = this.satelliteStore.search({ [field]: this.data });
for (const satId of satIdList) {
this.sats.push({
satId: satId.id,
isIntlDes: false,
strIndex: 0
});
}
}

/**
* Searches the id list and adds satellites to the 'sats' array.
* @throws {Error} If 'idList' is a string.
*/
private searchIdList () {
if (typeof this.data === 'string') {
throw new Error('idList must be an array');
}
for (const satId of this.data as number[]) {
this.sats.push({
satId: satId,
isIntlDes: false,
strIndex: 0
});
}
}

/**
* Searches for satellite names that match the specified regular expression.
*
* @throws {Error} If nameRegex is not a string.
*/
private searchNameRegex () {
if (typeof this.data !== 'string') {
throw new Error('nameRegex must be a string');
}
const regex = new RegExp(this.data);
const satIdList = this.satelliteStore.searchNameRegex(regex);
for (const satId of satIdList) {
this.sats.push({
satId: satId,
isIntlDes: false,
strIndex: 0
});
}
}

/**
* Searches for international designators in the data and adds them to the sats array.
* @throws {Error} Throws an error if intlDes is a string.
*/
private searchIntlDes () {
if (typeof this.data === 'string') {
throw new Error('intlDes must be an array');
}
for (const intlDes of this.data as string[]) {
this.sats.push({
satId: this.satelliteStore.getIdFromIntlDes(intlDes) as number,
isIntlDes: true,
strIndex: 0
});
}
}

getSat (satId: number): Record<string, any> | undefined {
/**
* Retrieves a satellite object based on the provided satellite ID.
* @param satId - The ID of the satellite to retrieve.
* @returns The satellite object matching the provided ID, or undefined if not found.
*
* @deprecated Use SatelliteStore.getSat instead.
* TODO: This is a duplicate of the method in SatelliteStore. It should be removed from here.
*/
getSat (satId: number) {
return this.satelliteStore.satData.find((satellite) => satellite.id === satId);
}

hasSat (satId: number): boolean {
const len = this.sats.length;
for (let i = 0; i < len; i++) {
if (this.sats[i].satId === satId) {
hasSat (satId: number) {
for (const sat of this.sats) {
if (sat.satId === satId) {
return true;
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/viewer/SatelliteStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import EventManager from '../utils/event-manager';
import logger from '../utils/logger';
import { SatelliteObject } from './interfaces/SatelliteObject';

const config = {
baseUrl: import.meta.env.BASE_URL
Expand All @@ -9,7 +10,7 @@ const config = {
class SatelliteStore {
tleUrl = `${config.baseUrl}/data/attributed-TLE.json`;
eventManager: EventManager;
satData: Record<string, any>[] = [];
satData: SatelliteObject[] = [];
attribution?: Record<string, any>;
updateDate?: Date;
satelliteVelocities: Float32Array = new Float32Array();
Expand Down Expand Up @@ -45,11 +46,11 @@ class SatelliteStore {

for (let i = 0; i < this.satData.length; i++) {
if (this.satData[i].INTLDES) {
let year = this.satData[i].INTLDES.substring(0, 2); // clean up intl des for display
const prefix = (year > 50) ? '19' : '20';
year = prefix + year;
const yearVal = Number(this.satData[i].INTLDES.substring(0, 2)); // convert year to number
const prefix = (yearVal > 50) ? '19' : '20';
const yearStr = prefix + yearVal.toString();
const rest = this.satData[i].INTLDES.substring(2);
this.satData[i].intlDes = `${year}-${rest}`;
this.satData[i].intlDes = `${yearStr}-${rest}`;
} else {
this.satData[i].intlDes = 'unknown';
}
Expand All @@ -72,7 +73,7 @@ class SatelliteStore {
return this.updateDate;
}

setSatelliteData (satData: Record<string, any>[], includesExtraData = false) {
setSatelliteData (satData: SatelliteObject[], includesExtraData = false) {
this.satData = satData;
this.gotExtraData = includesExtraData;

Expand Down Expand Up @@ -100,7 +101,7 @@ class SatelliteStore {
return undefined;
}

getSatData (): Record<string, any>[] {
getSatData (): SatelliteObject[] {
return this.satData || [];
}

Expand Down Expand Up @@ -130,12 +131,11 @@ class SatelliteStore {
return res;
}

search (query: Record<string, any>): any[] {
const keys = Object.keys(query);
let data = Object.assign([], this.satData);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
data = data.filter((entry: Record<string, any>) => entry[key] === query[key]);
search (query: Partial<SatelliteObject>): SatelliteObject[] {
const keys = Object.keys(query) as (keyof SatelliteObject)[];
let data = Object.assign([] as SatelliteObject[], this.satData);
for (const key of keys) {
data = data.filter((sat: SatelliteObject) => sat[key] === query[key]);
}
return data;
}
Expand All @@ -159,7 +159,7 @@ class SatelliteStore {
return null;
}

getSatellite (satelliteId: number): Record<string, any> | undefined {
getSatellite (satelliteId: number): SatelliteObject | undefined {
if (satelliteId === -1 || satelliteId === undefined || !this.satData) {
return undefined;
}
Expand Down
Loading

0 comments on commit 063b917

Please sign in to comment.