Skip to content

Commit

Permalink
add dataStore for manual sync
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Aug 1, 2024
1 parent 9b59ced commit 06f7719
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/data/common/D2ApiDataStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from "lodash";
import { D2Api } from "../../types/d2-api";
import { DataSource, isDhisInstance } from "../../domain/instance/entities/DataSource";
import { getD2APiFromInstance } from "../../utils/d2-utils";
Expand All @@ -15,10 +16,17 @@ export class D2ApiDataStore {
this.api = getD2APiFromInstance(instance);
}

async getDataStore(): Promise<DataStore[]> {
async getDataStore(filter: { namespaces: string[] }): Promise<DataStore[]> {
const response = await this.api.request<string[]>({ method: "get", url: "/dataStore" }).getData();
const namespacesWithKeys = await this.getAllKeysFromNamespaces(response);
return namespacesWithKeys;
return filter.namespaces.length === 0
? namespacesWithKeys
: _(namespacesWithKeys)
.map(namespace => {
return filter.namespaces.includes(namespace.id) ? namespace : undefined;
})
.compact()
.value();
}

private async getAllKeysFromNamespaces(namespaces: string[]): Promise<DataStore[]> {
Expand Down
13 changes: 12 additions & 1 deletion src/data/metadata/MetadataD2ApiRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { paginate } from "../../utils/pagination";
import { metadataTransformations } from "../transformations/PackageTransformations";
import { D2MetadataUtils } from "./D2MetadataUtils";
import { D2ApiDataStore } from "../common/D2ApiDataStore";
import { DataStoreMetadata } from "../../domain/data-store/DataStoreMetadata";

export class MetadataD2ApiRepository implements MetadataRepository {
private api: D2Api;
Expand All @@ -64,6 +65,8 @@ export class MetadataD2ApiRepository implements MetadataRepository {
): Promise<MetadataPackage<T>> {
const { apiVersion } = this.instance;

const d2ApiDataStore = new D2ApiDataStore(this.instance);
const dataStoreIds = DataStoreMetadata.getDataStoreIds(ids);
const requestFields = typeof fields === "object" ? getFieldsAsString(fields) : fields;
const d2Metadata = await this.getMetadata<D2Model>(ids, requestFields, includeDefaults);

Expand All @@ -83,6 +86,10 @@ export class MetadataD2ApiRepository implements MetadataRepository {
metadataTransformations
);

if (dataStoreIds.length > 0) {
metadataPackage.dataStores = await d2ApiDataStore.getDataStore({ namespaces: ids });
}

return metadataPackage as T;
} else {
const metadataPackage = this.transformationRepository.mapPackageFrom(
Expand All @@ -91,6 +98,10 @@ export class MetadataD2ApiRepository implements MetadataRepository {
metadataTransformations
);

if (dataStoreIds.length > 0) {
metadataPackage.dataStores = await d2ApiDataStore.getDataStore({ namespaces: ids });
}

return metadataPackage as T;
}
}
Expand All @@ -104,7 +115,7 @@ export class MetadataD2ApiRepository implements MetadataRepository {
const options = { type, fields, filter, order, page, pageSize, rootJunction };
if (type === "dataStores") {
const d2ApiDataStore = new D2ApiDataStore(this.instance);
const response = await d2ApiDataStore.getDataStore();
const response = await d2ApiDataStore.getDataStore({ namespaces: [] });
// Hardcoded pagination since DHIS2 does not support pagination for namespaces
return { objects: response, pager: { page: 1, total: response.length, pageSize: 100 } };
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/domain/data-store/DataStoreMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class DataStoreMetadata {
}

static buildFromKeys(keysWithNamespaces: string[]): DataStoreMetadata[] {
const dataStoreIds = keysWithNamespaces.filter(id => id.includes(DataStoreMetadata.NS_SEPARATOR));
const dataStoreIds = this.getDataStoreIds(keysWithNamespaces);

const namespaceAndKey = dataStoreIds.map(dataStoreId => {
const match = dataStoreId.split(DataStoreMetadata.NS_SEPARATOR);
Expand All @@ -41,7 +41,6 @@ export class DataStoreMetadata {
.map((keys, namespace) => {
return new DataStoreMetadata({
namespace,

keys: _(keys)
.map(key => {
if (!key.key) return undefined;
Expand Down Expand Up @@ -95,4 +94,8 @@ export class DataStoreMetadata {
});
});
}

static getDataStoreIds(keys: string[]): string[] {
return keys.filter(id => id.includes(DataStoreMetadata.NS_SEPARATOR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import SyncSummary from "../../../../react/core/components/sync-summary/SyncSumm
import { TestWrapper } from "../../../../react/core/components/test-wrapper/TestWrapper";
import { useAppContext } from "../../../../react/core/contexts/AppContext";
import InstancesSelectors from "./InstancesSelectors";
import { DataStoreMetadata } from "../../../../../domain/data-store/DataStoreMetadata";

const config: Record<
SynchronizationType,
Expand All @@ -49,7 +50,7 @@ const config: Record<
metadata: {
title: i18n.t("Metadata Synchronization"),
models: metadataModels,
childrenKeys: undefined,
childrenKeys: ["keys"],
},
aggregated: {
title: i18n.t("Aggregated Data Synchronization"),
Expand Down Expand Up @@ -250,6 +251,8 @@ const ManualSyncPage: React.FC = () => {
text: i18n.t("Metadata type"),
hidden: config[type].childrenKeys === undefined,
getValue: (row: MetadataType) => {
if (row.id.includes(DataStoreMetadata.NS_SEPARATOR)) return "";

return row.model.getModelName();
},
},
Expand Down

0 comments on commit 06f7719

Please sign in to comment.