Skip to content

Commit

Permalink
Merge pull request #16 from carapai/main
Browse files Browse the repository at this point in the history
Prepare for next release
  • Loading branch information
sekiskylink authored Dec 18, 2023
2 parents b744ccb + 2b1966c commit 781e1df
Show file tree
Hide file tree
Showing 56 changed files with 6,533 additions and 3,340 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "data-import-wizard-v2",
"version": "2.0.0",
"version": "2.0.1",
"description": "",
"license": "BSD-3-Clause",
"private": true,
Expand Down Expand Up @@ -37,6 +37,7 @@
"@tanstack/react-table": "^8.7.9",
"@turf/turf": "^6.5.0",
"antd": "^5.11.0",
"array-to-tree": "^3.3.2",
"axios": "0.27.2",
"chakra-react-select": "3.1.2",
"chakra-ui-steps": "^1.8.0",
Expand All @@ -59,6 +60,7 @@
"react-papaparse": "^4.4.0",
"react-pdf": "^7.1.2",
"react-virtual": "^2.10.4",
"styled-components": "^6.1.1",
"xlsx": "^0.18.5",
"zod": "^3.21.4"
},
Expand Down
27 changes: 2 additions & 25 deletions src/Events.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { IDataSet, IMapping, IProgram } from "data-import-wizard-utils";
import { domain } from "./Domain";
import {
IMapping,
ICategoryOption,
ICategoryCombo,
ICategory,
} from "./Interfaces";

import { ISchedule } from "./pages/schedules/Interfaces";
import { IProgram } from "./pages/program/Interfaces";
import { IDataSet } from "./pages/aggregate/Interfaces";

export const loadDefaults = domain.createEvent<{
mappings: string[];
Expand Down Expand Up @@ -50,20 +44,3 @@ export const setMappings = domain.createEvent<IMapping[]>();
// export const setPaging = domain.createEvent<>();

export const changeElementPage = domain.createEvent<string>();

// Category Events
export const setCategoryProperty = domain.createEvent<{
attribute: keyof ICategory;
value: ICategory[keyof ICategory]; // this references all value types in Category
}>();
export const setCategoryMapping = domain.createEvent<IMapping>();
export const setCategoryOptions = domain.createEvent<ICategoryOption[]>();

// CategoryCombo Events
export const setCategoryComboProperty = domain.createEvent<{
attribute: keyof ICategoryCombo;
value: ICategoryCombo[keyof ICategoryCombo];
}>();
export const setCategoryComboOptions = domain.createEvent<ICategoryOption[]>();

// CategoryOptionCombo Events
34 changes: 34 additions & 0 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import { MakeGenerics } from "@tanstack/react-location";
import { Event } from "effector";
import type { DataNode as IDataNode } from "antd/es/tree";

export interface Threshold {
id: string;
value: number;
color: string;
}
export type Column = {
label: string;
value: string;
span: number;
actual: string;
position: number;
key: string;
};
export interface DataNode extends IDataNode {
id?: string;
value?: string;
pId: string;
children?: DataNode[];
type?: string;
nodeSource?: { [key: string]: any };
hasChildren?: boolean;
bg?: string;
actual?: string;
parent?: { [key: string]: any };
order?: string;
metadata?: Partial<{
rows: number;
columns: number;
rowsPerPage: number;
}>;
filter?: string;
}

export type LocationGenerics = MakeGenerics<{
LoaderData: {};
Expand Down
136 changes: 134 additions & 2 deletions src/Queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { fromPairs, groupBy, isEmpty, map, pick } from "lodash";
import { getOr } from "lodash/fp";
import { useQuery } from "react-query";
import { db } from "./db";
import { closeDialog, setDataSets, setTotalDataSets } from "./Events";
import { tokenApi } from "./pages/program";
import { versionApi } from "./Store";
Expand Down Expand Up @@ -54,10 +55,9 @@ export const useDHIS2Metadata = <TData>(
];
}

const stringParams = convertDataToURL(parameters);
const metadataQuery = {
data: {
resource: `${resource}.json?${stringParams}`,
resource: `${resource}.json`,
params,
},
};
Expand All @@ -84,13 +84,48 @@ export const useInitials = () => {
info: {
resource: "system/info",
},
organisationUnits: {
resource: "organisationUnits.json",
params: {
fields: "id,name,path,leaf",
level: 1,
},
},
levels: {
resource: "filledOrganisationUnitLevels.json",
params: {
fields: "id,level~rename(value),name~rename(label)",
},
},
groups: {
resource: "organisationUnitGroups.json",
params: {
fields: "id~rename(value),name~rename(label)",
},
},
};

return useQuery<boolean, Error>(["initials"], async () => {
const {
info: { version },
organisationUnits: { organisationUnits },
levels: organisationUnitLevels,
groups: { organisationUnitGroups },
}: any = await engine.query(query);
const versionNumbers = String(version).split(".");
const availableUnits = organisationUnits.map((unit: any) => {
return {
id: unit.id,
pId: unit.pId || "",
value: unit.id,
title: unit.name,
key: unit.id,
isLeaf: unit.leaf,
};
});
await db.organisations.bulkPut(availableUnits);
await db.levels.bulkPut(organisationUnitLevels);
await db.groups.bulkPut(organisationUnitGroups);
versionApi.set(Number(versionNumbers[1]));
return true;
});
Expand Down Expand Up @@ -840,3 +875,100 @@ export const useInfiniteDHIS2Query = <T>({
}
);
};

export const makeSQLQuery = async (
engine: any,
id: string,
query: string,
name: string
) => {
const sqlQuery = {
description: name,
type: "QUERY",
id,
sqlQuery: query,
sharing: {
public: "rwrw----",
},
name,
cacheStrategy: "NO_CACHE",
};

const mutation: any = {
type: "create",
resource: `metadata`,
data: { sqlViews: [sqlQuery] },
};
await engine.mutate(mutation);
};

export const useSQLViewMetadata = (program: string, mapping: string) => {
const engine = useDataEngine();
return useQuery<any, Error>(
["sql-view-metadata", program, mapping],
async () => {
await makeSQLQuery(
engine,
mapping,
`select * from analytics_event_${program.toLowerCase()}`,
mapping
);

const metadataQuery = {
data: {
resource: `sqlViews/${mapping}/data.json`,
},
};

const { data }: any = await engine.query(metadataQuery);

let {
listGrid: { headers, rows },
} = data;

const withIds = headers.flatMap((h: any) => {
if (
h.name.length === 11 &&
["lastupdated", "teigeometry"].indexOf(h.name) === -1
) {
return h.name;
}
return [];
});

if (withIds.length > 0) {
const query = {
data2: {
resource: "metadata",
params: {
filter: `id:in:[${withIds.join(",")}]`,
},
},
};
const {
data2: { system, ...rest },
}: any = await engine.query(query);

const allObjects = fromPairs(
Object.values(rest).flatMap((a: any) =>
a.map(({ id, name }: any) => [id, name])
)
);

headers = headers.map((h: any) => {
if (withIds.indexOf(h.name) !== -1) {
return {
...h,
name: allObjects[h.name] ?? h.name,
column: allObjects[h.name] ?? h.name,
};
}
return h;
});
}
return rows.map((row: string[]) =>
fromPairs(row.map((r, index) => [headers[index].name, r]))
);
}
);
};
17 changes: 16 additions & 1 deletion src/Store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Option } from "data-import-wizard-utils";
import { Option, Period } from "data-import-wizard-utils";
import { createApi } from "effector";
import { WorkBook } from "xlsx";
import { domain } from "./Domain";

export const $periods = domain.createStore<Period[]>([]);
export const $ous = domain.createStore<string[]>([]);

export const $steps = domain.createStore<number>(0);
export const $version = domain.createStore<number>(0);
export const $hasError = domain.createStore<boolean>(false);

export const stepper = createApi($steps, {
next: (state) => state + 1,
Expand Down Expand Up @@ -36,3 +40,14 @@ export const $sheets = $workbook.map<Option[]>((workbook) => {
}
return [];
});

export const periodsApi = createApi($periods, {
set: (_, { periods, remove }: { periods: Period[]; remove: boolean }) =>
periods,
});
export const ousApi = createApi($ous, {
set: (_, ous: string[]) => ous,
});
export const hasErrorApi = createApi($hasError, {
set: (_, hasError: boolean) => hasError,
});
25 changes: 0 additions & 25 deletions src/components/APICredentials.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -383,31 +383,6 @@ export default function APICredentials<U extends IMapping>({
/>
</Stack>
)}
{/* <Modal isOpen={isOpen} onClose={onClose} size="2xl">
<ModalOverlay />
<ModalContent>
<ModalHeader>
Select{" "}
{mapping.dataSource === "dhis2"
? "Program"
: mapping.dataSource === "godata"
? "Outbreak"
: ""}
</ModalHeader>
<ModalCloseButton />
<ModalBody
display="flex"
alignItems="center"
justifyContent="center"
>
{mapping.dataSource === "dhis2" ? (
<RemotePrograms onClose={onClose} />
) : mapping.dataSource === "godata" ? (
<RemoteOutbreaks onClose={onClose} />
) : null}
</ModalBody>
</ModalContent>
</Modal> */}
</Stack>
)}
{/* <MetadataOptions /> */}
Expand Down
Loading

0 comments on commit 781e1df

Please sign in to comment.