Skip to content

Commit

Permalink
Merge branch 'fix-observation-sync' of https://github.com/ngageoint/m…
Browse files Browse the repository at this point in the history
…age-server into add-arcgis-docs
  • Loading branch information
ryanslatten committed Jan 21, 2025
2 parents 9e06fd2 + 62a608f commit 4824d05
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 187 deletions.
10 changes: 6 additions & 4 deletions plugins/arcgis/service/src/ArcGISConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MageEventId } from "@ngageoint/mage.service/lib/entities/events/entities.events"

/**
* Contains an arc feature service url and layers.
*/
Expand All @@ -9,8 +11,8 @@ export interface FeatureServiceConfig {
url: string

/**
* Serialized ArcGISIdentityManager
*/
* Serialized ArcGISIdentityManager
*/
identityManager: string

/**
Expand All @@ -35,9 +37,9 @@ export interface FeatureLayerConfig {
geometryType?: string

/**
* The event ids or names that sync to this arc feature layer.
* The event ids that sync to this arc feature layer.
*/
events?: (number|string)[]
eventIds?: MageEventId[]
}


Expand Down
6 changes: 5 additions & 1 deletion plugins/arcgis/service/src/EventDeletionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class EventDeletionHandler {
this._config = config;
}

public updateConfig(newConfig: ArcGISPluginConfig): void {
this._config = newConfig;
}

/**
*
* @param activeEvents The current set of active events.
Expand Down Expand Up @@ -83,7 +87,7 @@ export class EventDeletionHandler {
}

/**
* Called when the query is finished. It goes through the results and gathers all even Ids currently stored
* Called when the query is finished. It goes through the results and gathers all event Ids currently stored
* in the arc layer. It then will remove any events from the arc layer that do not exist.
* @param layerProcessor The feature layer processor.
* @param result The returned results.
Expand Down
2 changes: 1 addition & 1 deletion plugins/arcgis/service/src/EventLayerProcessorOrganizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EventLayerProcessorOrganizer {
for (const event of events) {
let syncProcessors = new Array<FeatureLayerProcessor>();
for (const layerProcessor of layerProcessors) {
if (layerProcessor.layerInfo.hasEvent(event.name)) {
if (layerProcessor.layerInfo.hasEvent(event.id)) {
syncProcessors.push(layerProcessor);
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/arcgis/service/src/FeatureLayerProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class FeatureLayerProcessor {
constructor(layerInfo: LayerInfo, config: ArcGISPluginConfig, identityManager: ArcGISIdentityManager, console: Console) {
this.layerInfo = layerInfo;
this.lastTimeStamp = 0;
this.featureQuerier = new FeatureQuerier(layerInfo, config, identityManager,console);
this.featureQuerier = new FeatureQuerier(layerInfo, config, identityManager, console);
this._binner = new ObservationBinner(layerInfo, this.featureQuerier, config);
this.sender = new ObservationsSender(layerInfo, config, identityManager, console);
}
Expand Down Expand Up @@ -85,7 +85,7 @@ export class FeatureLayerProcessor {

for (const arcObservation of observations.deletions) {
if (this.layerInfo.geometryType == arcObservation.esriGeometryType) {
this.sender.sendDelete(Number(arcObservation.id));
this.sender.sendDelete(arcObservation.id);
}
}
}
Expand Down
88 changes: 28 additions & 60 deletions plugins/arcgis/service/src/FeatureQuerier.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ArcGISPluginConfig } from "./ArcGISPluginConfig";
import { LayerInfo } from "./LayerInfo";
import { QueryObjectResult } from "./QueryObjectResult";
import { ArcGISIdentityManager, request } from "@esri/arcgis-rest-request";
import { ArcGISIdentityManager } from "@esri/arcgis-rest-request";
import { queryFeatures } from '@esri/arcgis-rest-feature-service';

/**
* Performs various queries on observations for a specific arc feature layer.
Expand Down Expand Up @@ -52,22 +53,17 @@ export class FeatureQuerier {
* @param geometry query the geometry, default is true
*/
async queryObservation(observationId: string, response: (result: QueryObjectResult) => void, fields?: string[], geometry?: boolean) {
const queryUrl = new URL(this._url)
if (this._config.eventIdField == null) {
queryUrl.searchParams.set('where', `${this._config.observationIdField} LIKE '${observationId}${this._config.idSeparator}%'`);
} else {
queryUrl.searchParams.set('where', `${this._config.observationIdField} = ${observationId}`);
}
queryUrl.searchParams.set('outFields', this.outFields(fields))
queryUrl.searchParams.set('returnGeometry', geometry === false ? 'false' : 'true')
this._console.info('ArcGIS query: ' + queryUrl)

const queryResponse = await request(queryUrl.toString(), {
const where = !this._config.eventIdField
? `${this._config.observationIdField} LIKE '${observationId}${this._config.idSeparator}%'`
: `${this._config.observationIdField} = '${observationId}'`;
this._console.info('ArcGIS query observation: ' + this._url.toString() + where);
await queryFeatures({
url: this._url.toString(),
authentication: this._identityManager,
params: { f: 'json' }
});

response(queryResponse as QueryObjectResult);
where,
returnGeometry: geometry,
outFields: fields?.length ? fields : '*'
}).then((queryResponse) => response(queryResponse as QueryObjectResult)).catch((error) => this._console.error('Error in FeatureQuerier.queryObservation :: ' + error));
}

/**
Expand All @@ -77,19 +73,14 @@ export class FeatureQuerier {
* @param geometry query the geometry, default is true
*/
async queryObservations(response: (result: QueryObjectResult) => void, fields?: string[], geometry?: boolean) {
const queryUrl = new URL(this._url)
queryUrl.searchParams.set('where', `${this._config.observationIdField} IS NOT NULL`);
queryUrl.searchParams.set('outFields', this.outFields(fields));
queryUrl.searchParams.set('returnGeometry', geometry === false ? 'false' : 'true');

this._console.info('ArcGIS query: ' + queryUrl)

const queryResponse = await request(queryUrl.toString(), {
this._console.info('ArcGIS query observation: ' + this._url.toString());
await queryFeatures({
url: this._url.toString(),
authentication: this._identityManager,
params: { f: 'json' }
});

response(queryResponse as QueryObjectResult);
where: `${this._config.observationIdField} IS NOT NULL`,
returnGeometry: geometry,
outFields: fields?.length ? fields : '*'
}).then((queryResponse) => response(queryResponse as QueryObjectResult)).catch((error) => this._console.error('Error in FeatureQuerier.queryObservations :: ' + error));
}

/**
Expand All @@ -98,37 +89,14 @@ export class FeatureQuerier {
* @param field field to query
*/
async queryDistinct(response: (result: QueryObjectResult) => void, field: string) {
const queryUrl = new URL(this._url);
queryUrl.searchParams.set('where', `${field} IS NOT NULL`);
queryUrl.searchParams.set('returnDistinctValues', 'true');
queryUrl.searchParams.set('outFields', this.outFields([field]));
queryUrl.searchParams.set('returnGeometry', 'false');
this._console.info('ArcGIS query: ' + queryUrl)

try {
const queryResponse = await request(queryUrl.toString(), {
authentication: this._identityManager,
params: { f: 'json' }

});

response(queryResponse as QueryObjectResult);
} catch (err) {
console.error("could not query", err)
}
}

/**
* Build the out fields query parameter
* @param fields query fields
* @returns out fields
*/
private outFields(fields?: string[]): string {
if (fields != null && fields.length > 0) {
return fields.join(',');
} else {
return '*';
}
this._console.info('ArcGIS query observation: ' + this._url.toString());
await queryFeatures({
url: this._url.toString(),
authentication: this._identityManager,
where: `${field} IS NOT NULL`,
returnGeometry: false,
outFields: field ? [field] : '*',
returnDistinctValues: true
}).then((queryResponse) => response(queryResponse as QueryObjectResult)).catch((error) => this._console.error('Error in FeatureQuerier.queryDistinct :: ' + error));
}

}
18 changes: 9 additions & 9 deletions plugins/arcgis/service/src/FeatureServiceAdmin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArcGISPluginConfig } from "./ArcGISPluginConfig"
import { FeatureServiceConfig, FeatureLayerConfig } from "./ArcGISConfig"
import { MageEvent, MageEventRepository } from '@ngageoint/mage.service/lib/entities/events/entities.events'
import { MageEvent, MageEventId, MageEventRepository } from '@ngageoint/mage.service/lib/entities/events/entities.events'
import { Layer, Field } from "./AddLayersRequest"
import { Form, FormField, FormFieldType, FormId } from '@ngageoint/mage.service/lib/entities/events/entities.events.forms'
import { ObservationsTransformer } from "./ObservationsTransformer"
Expand Down Expand Up @@ -120,29 +120,29 @@ export class FeatureServiceAdmin {
}

/**
* Get the layer events
* Get the Mage layer events
* @param layer feature layer
* @param eventRepo event repository
* @returns layer events
* @returns Mage layer events
*/
private async layerEvents(layer: FeatureLayerConfig, eventRepo: MageEventRepository): Promise<MageEvent[]> {
const layerEvents: Set<number|string> = new Set()
if (layer.events != null) {
for (const layerEvent of layer.events) {
layerEvents.add(layerEvent)
const layerEventIds: Set<MageEventId> = new Set()
if (layer.eventIds != null) {
for (const layerEventId of layer.eventIds) {
layerEventIds.add(layerEventId)
}
}

let mageEvents
if (layerEvents.size > 0) {
if (layerEventIds.size > 0) {
mageEvents = await eventRepo.findAll()
} else {
mageEvents = await eventRepo.findActiveEvents()
}

const events: MageEvent[] = []
for (const mageEvent of mageEvents) {
if (layerEvents.size == 0 || layerEvents.has(mageEvent.name) || layerEvents.has(mageEvent.id)) {
if (layerEventIds.size == 0 || layerEventIds.has(mageEvent.id)) {
const event = await eventRepo.findById(mageEvent.id)
if (event != null) {
events.push(event)
Expand Down
17 changes: 8 additions & 9 deletions plugins/arcgis/service/src/LayerInfo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MageEventId } from "@ngageoint/mage.service/lib/entities/events/entities.events";
import { LayerInfoResult, LayerField } from "./LayerInfoResult";

/**
Expand Down Expand Up @@ -28,7 +29,7 @@ export class LayerInfo {
/**
* The events that are synching to this layer.
*/
events: Set<string> = new Set<string>()
events: Set<MageEventId> = new Set<MageEventId>()

/**
* Constructor.
Expand All @@ -37,12 +38,10 @@ export class LayerInfo {
* @param layerInfo The layer info.
* @param token The access token.
*/
constructor(url: string, events: string[], layerInfo: LayerInfoResult) {
constructor(url: string, events: MageEventId[], layerInfo: LayerInfoResult) {
this.url = url
if (events != undefined && events != null && events.length == 0) {
this.events.add('nothing to sync')
}
if (events != undefined || events != null) {

if (events && events.length > 0) {
for (const event of events) {
this.events.add(event);
}
Expand All @@ -69,11 +68,11 @@ export class LayerInfo {

/**
* Determine if the layer is enabled for the event.
* @param event The event.
* @param eventId The event.
* @return true if enabled
*/
hasEvent(event: string) {
return this.events.size == 0 || this.events.has(event)
hasEvent(eventId: MageEventId) {
return this.events.size == 0 || this.events.has(eventId)
}

}
3 changes: 2 additions & 1 deletion plugins/arcgis/service/src/ObservationBinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export class ObservationBinner {
const bins = new ObservationBins();

for (const arcObservation of observations.observations) {
if (arcObservation.lastModified != arcObservation.createdAt) {
// TODO: Would probably want a better way to determine which observations need to be updated in arcgis
if (observations.firstRun || arcObservation.lastModified != arcObservation.createdAt) {
bins.updates.add(arcObservation);
} else if (!this._addedObs.has(arcObservation.id)) {
bins.adds.add(arcObservation);
Expand Down
Loading

0 comments on commit 4824d05

Please sign in to comment.