Skip to content

Commit

Permalink
update (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
DLHTX authored Nov 6, 2023
1 parent a1509dc commit f70c6ed
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 203 deletions.
4 changes: 4 additions & 0 deletions public/images/templates/depin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect, useState } from 'react';
import { WidgetProps } from '@rjsf/utils';
import { Text, Flex, Image, chakra, Box, FlexProps } from '@chakra-ui/react';
import initTemplates from '@/constants/initTemplates.json';
import { rootStore } from '@/store/index';
// import initTemplates from '@/constants/initTemplates.json';

type Options = {
flexProps?: FlexProps;
Expand All @@ -19,6 +20,7 @@ export interface InitializationTemplateWidgetUIOptions {
function InitializationTemplateWidget({ id, options, value, required, label, onChange }: InitializationTemplateWidgetProps) {
const [templateName, setTemplateName] = useState('');
const { flexProps = {} } = options;
const initTemplates = rootStore.w3s.initTemplateJSON
useEffect(() => {
setTemplateName(value);
}, [value]);
Expand All @@ -41,7 +43,7 @@ function InitializationTemplateWidget({ id, options, value, required, label, onC
}
}}
>
{initTemplates.templates.map((template) => (
{initTemplates?.templates?.map((template) => (
<Flex
key={template.name}
flexDir="column"
Expand Down
35 changes: 33 additions & 2 deletions src/constants/initTemplates.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,42 @@
]
},
"envs": {
"env": [["LOG_LEVEL", "debug"]]
"env": [
[
"LOG_LEVEL",
"debug"
]
]
},
"datas": []
}
]
},
{
"name": "DePIN Intergation",
"image": "/images/templates/depin.svg",
"autoCollectionMetrics": true,
"project": [
{
"name": "hello_world",
"description": "",
"applets": [
{
"wasmURL": "https://raw.githubusercontent.com/machinefi/w3bstream-wasm-assemblyscript-sdk/main/examples/wasms/log.wasm",
"appletName": "applet_01"
}
],
"envs": {
"env": [
[
"LOG_LEVEL",
"debug"
]
]
},
"datas": []
}
]
}
]
}
}
4 changes: 3 additions & 1 deletion src/lib/lodash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import isEqual from 'lodash/isEqual';
import uniqWith from 'lodash/uniqWith';
import findIndex from 'lodash/findIndex';
import remove from 'lodash/remove';
import isEmpty from 'lodash/isEmpty';

export const _ = {
throttle,
Expand All @@ -31,5 +32,6 @@ export const _ = {
isEqual,
uniqWith,
findIndex,
remove
remove,
isEmpty
};
29 changes: 26 additions & 3 deletions src/pages/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fetch, { FormData, File } from 'node-fetch';
import type { NextApiRequest, NextApiResponse } from 'next';
import Blob from 'cross-blob';
import { _ } from '@/lib/lodash';
import axios from 'axios';

export interface InitProject {
name: string;
Expand All @@ -10,6 +11,7 @@ export interface InitProject {
envs?: {
env: [string, string][];
};
autoCollectionMetrics?: boolean;
datas?: {
cronJob?: {
eventType: string;
Expand Down Expand Up @@ -75,7 +77,11 @@ const createProject = async (
}
};

const createApplet = async ({ projectName, appletName, wasmURL, wasmRaw }: Applet & { projectName: string }, token: string): Promise<string> => {
const createApplet = async (
{ projectName, appletName, wasmURL, wasmRaw }: Applet & { projectName: string },
token: string,
projectID: string,
autoCollectionMetrics): Promise<string> => {
try {
const formData = new FormData();
let wasmName = '';
Expand Down Expand Up @@ -112,6 +118,23 @@ const createApplet = async ({ projectName, appletName, wasmURL, wasmRaw }: Apple
}
const data: any = await res.json();
if (data.appletID) {
if (autoCollectionMetrics) {
try {
const strategy = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/srv-applet-mgr/v0/strategy/x/${projectName}/datalist`, {
headers: { Authorization: token }
})
await axios.put(`${process.env.NEXT_PUBLIC_API_URL}/srv-applet-mgr/v0/strategy/${strategy.data.data[0].strategyID}`,
{
"appletID": data.appletID, "eventType": "DEFAULT", "handler": "start", "autoCollectMetric": true
}, {
headers: {
Authorization: token
}
})
} catch (e) {
console.log(e.message)
}
}
return data.appletID;
}
throw data;
Expand Down Expand Up @@ -186,7 +209,7 @@ const createCronJob = async (
body: JSON.stringify(data),
headers: { Authorization: token }
});
} catch (error) {}
} catch (error) { }
};

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
Expand All @@ -205,7 +228,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { projectID, projectName } = await createProject(p, token);
createdProjectIds.push(projectID);
for (const a of p.applets) {
await createApplet({ ...a, projectName }, token);
await createApplet({ ...a, projectName }, token, projectID, req.body.autoCollectionMetrics);
}
for (const d of p.datas) {
if (d.monitor) {
Expand Down
19 changes: 16 additions & 3 deletions src/store/lib/w3bstream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { hooks } from '@/lib/hooks';
import { ProjectManager } from './project';
import W3bstreamConfigModule from './schema/config';
import UserModule from './schema/user';
import ProjectModule from './schema/project';
import ProjectModule, { initializationTemplateSchema } from './schema/project';
import PublisherModule from './schema/publisher';
import StrategyModule from './schema/strategy';
import AppletModule from './schema/applet';
Expand Down Expand Up @@ -56,6 +56,8 @@ export class W3bStream {

currentHeaderTab: 'PROJECTS' | 'LABS' | 'SUPPORT' | 'TOOLS' | 'FLOW' | 'SETTING' = 'PROJECTS';

initTemplateJSON = null;

isReady = false;

actions = {
Expand All @@ -73,6 +75,16 @@ export class W3bStream {
this.initWatch();
}

getInitTemplateJSON = async () => {
if (!this.initTemplateJSON) {
const res = await fetch('https://raw.githubusercontent.com/machinefi/w3bstream-studio-template-json/main/index.json');
this.initTemplateJSON = await res.json();
console.log('initTemplateJSON', this.initTemplateJSON);
//@ts-ignore
initializationTemplateSchema.properties.template.enum = this.initTemplateJSON.templates.map((i) => i.name);
}
}

initWatch() {
reaction(
() => this.currentHeaderTab,
Expand All @@ -93,7 +105,7 @@ export class W3bStream {
.on('user.login', async () => {
NextRouter.push('/');
})
.on('user.update-pwd', () => {})
.on('user.update-pwd', () => { })
.on('project.create', async () => {
await this.project.allProjects.call();
this.projectManager.sync();
Expand All @@ -120,7 +132,7 @@ export class W3bStream {
await this.project.projectDetail.call();
this.projectManager.sync();
})
.on('applet.publish-event', () => {})
.on('applet.publish-event', () => { })
.on('instance.deploy', async () => {
await this.project.allProjects.call();
await this.project.projectDetail.call();
Expand Down Expand Up @@ -186,6 +198,7 @@ export class W3bStream {
}

async init() {
this.getInitTemplateJSON()
hooks.waitLogin().then(async () => {
await this.project.allProjects.call();
this.projectManager.sync();
Expand Down
6 changes: 4 additions & 2 deletions src/store/lib/w3bstream/schema/applet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { dataURItoBlob, UiSchema } from '@rjsf/utils';
import { FromSchema } from 'json-schema-to-ts';
import { definitions } from './definitions';
import InitializationTemplateWidget from '@/components/JSONFormWidgets/InitializationTemplateWidget';
import initTemplates from '@/constants/initTemplates.json';
// import initTemplates from '@/constants/initTemplates.json';
import toast from 'react-hot-toast';
import { rootStore } from '@/store/index';

export const schema = {
definitions: {
Expand Down Expand Up @@ -492,7 +493,8 @@ export default class AppletModule {
const appletId = this.curApplet?.f_applet_id;

if (formData.template) {
const templateData = initTemplates.templates.find((i) => i.name === formData.template);
const initTemplates = rootStore.w3s.initTemplateJSON
const templateData = initTemplates?.templates?.find((i) => i.name === formData.template);
const wasmURL = templateData?.project[0]?.applets[0]?.wasmURL;
if (!wasmURL) {
toast.error('This template does not exist.');
Expand Down
Loading

0 comments on commit f70c6ed

Please sign in to comment.