Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add presearch filter #389

Merged
merged 2 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions packages/playground/src/components/select_farm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const props = defineProps({
modelValue: { type: Object as PropType<Farm> },
country: String,
filters: { default: () => ({} as Filters), type: Object as PropType<Filters> },
exclusiveFor: String,
});
const emits = defineEmits<{ (event: "update:modelValue", value?: Farm): void }>();

Expand All @@ -61,15 +62,19 @@ async function loadFarms() {

const grid = await getGrid(profileManager.profile!);
const filters = props.filters;
farms.value = await getFarms(grid!, {
country: country.value,
cru: filters.cpu,
mru: filters.memory ? Math.round(filters.memory / 1024) : undefined,
hru: filters.disk,
sru: filters.ssd,
publicIPs: filters.publicIp,
availableFor: grid!.twinId,
});
farms.value = await getFarms(
grid!,
{
country: country.value,
cru: filters.cpu,
mru: filters.memory ? Math.round(filters.memory / 1024) : undefined,
hru: filters.disk,
sru: filters.ssd,
publicIPs: filters.publicIp,
availableFor: grid!.twinId,
},
{ exclusiveFor: props.exclusiveFor },
);

if (oldFarm) {
farm.value = farms.value.find(f => f.name === oldFarm.name);
Expand Down
37 changes: 34 additions & 3 deletions packages/playground/src/utils/get_farms.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { FilterOptions, GridClient, NodeInfo } from "@threefold/grid_client";

import { gqlClient } from "../clients";
import { gqlClient, gridProxyClient } from "../clients";

export async function getFarms(grid: GridClient, filters: FilterOptions) {
export interface GetFarmsOptions {
exclusiveFor?: string;
}
export async function getFarms(grid: GridClient, filters: FilterOptions, options: GetFarmsOptions = {}) {
const nodes: NodeInfo[][] = [];

let page = 1;
Expand All @@ -12,10 +15,38 @@ export async function getFarms(grid: GridClient, filters: FilterOptions) {
page = nodes[page - 1].length ? ++page : 0;
}

const farmIds = Array.from(new Set(nodes.flat(1).map(node => node.farmId)));
let farmIds = Array.from(new Set(nodes.flat(1).map(node => node.farmId)));

if (options.exclusiveFor && !filters.publicIPs) {
const blockedFarms = await getBlockedFarmSet(options.exclusiveFor);
farmIds = farmIds.filter(id => !blockedFarms.has(id));
}

return gqlClient.farms(
{ farmID: true, name: true },
{ orderBy: ["farmID_ASC"], where: { farmID_in: farmIds }, limit: farmIds.length },
);
}

export async function getBlockedFarmSet(exclusiveFor: string): Promise<Set<number>> {
const { totalCount } = await gqlClient.nodeContractsConnection(
{ totalCount: true },
{
orderBy: ["id_ASC"],
where: { deploymentData_contains: exclusiveFor, state_eq: "Created", numberOfPublicIPs_eq: 0 },
},
);

const nodes = await gqlClient.nodeContracts(
{ nodeID: true },
{
limit: totalCount,
where: { deploymentData_contains: exclusiveFor, state_eq: "Created" },
},
);

const farms = await Promise.all(
Array.from(new Set(nodes.map(node => node.nodeID))).map(id => gridProxyClient.nodes.byId(id)),
);
return new Set(farms.map(farm => farm.farmId));
}
1 change: 1 addition & 0 deletions packages/playground/src/weblets/tf_presearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
ssd: rootFsSize,
publicIp: ipv4,
}"
exclusive-for="research"
v-model="farm"
/>
</template>
Expand Down