Skip to content

Commit

Permalink
Merge branch '2.5-dashboard' into 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
baozhoutao committed Nov 6, 2023
2 parents 95a8e90 + 3844a8b commit f90fd13
Show file tree
Hide file tree
Showing 33 changed files with 792 additions and 54 deletions.
17 changes: 11 additions & 6 deletions packages/metadata-api/src/metadata/collection/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export class MetadataBaseCollection{
collectionName: string;
relatedProperties: Array<string>;

formatDataOnRetrieve(metadata){
getIdKey(){
return 'name';
}

formatDataOnRetrieve(metadata, dbManager){
return metadata;
}

formatDataOnDeploy(metadata){
formatDataOnDeploy(metadata, dbManager){
return metadata;
}

Expand All @@ -30,7 +34,7 @@ export class MetadataBaseCollection{
for(var i=0; i<metadataList.length; i++){
var metadataItem = metadataList[i]
var metadataFullName = getFullName(this.metadataName, metadataItem)
packageMetadata[metadataFullName] = this.formatDataOnRetrieve(metadataItem)
packageMetadata[metadataFullName] = await this.formatDataOnRetrieve(metadataItem, dbManager)
}
}else{

Expand All @@ -40,14 +44,14 @@ export class MetadataBaseCollection{

var metadataItem = await this.get(dbManager, metadataApiName);
var metadataFullName = getFullName(this.metadataName, metadataItem)
packageMetadata[metadataFullName] = this.formatDataOnRetrieve(metadataItem);
packageMetadata[metadataFullName] = await this.formatDataOnRetrieve(metadataItem, dbManager);
}
}
}

async deploy(dbManager, metadataList){
for(const metadataName in metadataList){
var metadata = this.formatDataOnDeploy(metadataList[metadataName]);
var metadata = await this.formatDataOnDeploy(metadataList[metadataName], dbManager);
// await checkComponentsExist(dbManager, layout);
delete metadata.__filename
await this.save(dbManager, metadata);
Expand Down Expand Up @@ -82,7 +86,8 @@ export class MetadataBaseCollection{
}

protected async save(dbManager, data){
var filter = {name: data.name};
var idKey = this.getIdKey();
var filter = {[idKey]: data[idKey]};
var record = await dbManager.findOne(this.collectionName, filter);

if(record == null){
Expand Down
75 changes: 75 additions & 0 deletions packages/metadata-api/src/metadata/collection/dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* @Author: [email protected]
* @Date: 2023-07-28 11:37:13
* @LastEditors: [email protected]
* @LastEditTime: 2023-07-28 18:06:44
* @Description:
*/
import { deleteCommonAttribute, sortAttribute } from '../../util/attributeUtil'
import { SteedosMetadataTypeInfoKeys as TypeInfoKeys } from '@steedos/metadata-core';
import _ from 'underscore';
import { MetadataBaseCollection } from './_base'

const metadataName = TypeInfoKeys.Dashboard;

export class DashboardCollection extends MetadataBaseCollection{
constructor(){
super(metadataName);
}

getIdKey(){
return "id";
}

async formatDataOnRetrieve(metadata, dbManager){
metadata.cards = await dbManager.find("analytics_dashboard_card", {
dashboard_id: metadata._id
});

delete metadata.created_at;
delete metadata.updated_at;
delete metadata.creator_id;

_.each(metadata.cards, (card)=>{
delete card._id;
delete card.dashboard_id;
delete card.created_at;
delete card.updated_at;
delete card.creator_id;
delete card.company_id;
delete card.company_ids;
deleteCommonAttribute(card);
sortAttribute(card);
})

return metadata;
}

async formatDataOnDeploy(metadata, dbManager){
metadata._id = metadata.id;
if(metadata && metadata.cards){
for(const card of metadata.cards){
card._id = card.id;
card.dashboard_id = metadata._id;
await this.saveDashboardCard(dbManager, card);
}
}
}

async saveDashboardCard(dbManager, data){
var idKey = this.getIdKey();
var filter = {[idKey]: data[idKey]};
var collectionName = "analytics_dashboard_card";
var record = await dbManager.findOne(collectionName, filter);

if(record == null){
record = await dbManager.findOne(collectionName, filter, false)
if(record) {
throw new Error(`process api_name already exists: ${data.name}`);
}
return await dbManager.insert(collectionName, data);
}else{
return await dbManager.update(collectionName, filter, data);
}
}
}
8 changes: 8 additions & 0 deletions packages/metadata-api/src/metadata/collection/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* @Author: [email protected]
* @Date: 2022-03-28 09:35:34
* @LastEditors: [email protected]
* @LastEditTime: 2023-07-28 13:57:22
* @Description:
*/
import { getCollectionNameByMetadata, SteedosMetadataTypeInfoKeys} from '@steedos/metadata-core';
import { workflowsFromDb} from './workflow';
import _ from 'underscore';
Expand Down Expand Up @@ -30,6 +37,7 @@ export async function getMetadataSources(dbManager, metadataName){
filters.type = {$ne: 'profile'};
}
records = await dbManager.find(collectionName, filters);
console.log(`records====>`, records)
return records;
}
}
15 changes: 15 additions & 0 deletions packages/metadata-api/src/metadata/collection/question.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SteedosMetadataTypeInfoKeys as TypeInfoKeys } from '@steedos/metadata-core';
import _ from 'underscore';
import { MetadataBaseCollection } from './_base'

const metadataName = TypeInfoKeys.Question;

export class QuestionCollection extends MetadataBaseCollection{
constructor(){
super(metadataName);
}

getIdKey(){
return "id";
}
}
16 changes: 15 additions & 1 deletion packages/metadata-api/src/metadata/deploy/fileToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
LoadRestrictionRules,
LoadProcessFile,
LoadTriggerFile,
LoadImportFile
LoadImportFile,
LoadQuestionFile,
LoadDashboardFile
} from '@steedos/metadata-core';
import { checkNameEquals } from '../../util/check_name_equals'

Expand All @@ -29,6 +31,8 @@ const loadRestrictionRules = new LoadRestrictionRules();
const loadProcessFile = new LoadProcessFile();
const loadTriggersFile = new LoadTriggerFile();
const loadImportFile = new LoadImportFile();
const loadQuestionFile = new LoadQuestionFile();
const loadDashboardFile = new LoadDashboardFile();

//扫描Permissionsets并输出为json
async function loadPermissionsets(filePath){
Expand Down Expand Up @@ -692,6 +696,8 @@ export async function loadFileToJson(packagePath:string, packageYml?){
let processes = {};
let triggers = {};
let imports = {};
let questions = {};
let dashboards = {};
let mark:boolean = false;

for(const metadataname in packageYml){
Expand Down Expand Up @@ -798,6 +804,12 @@ export async function loadFileToJson(packagePath:string, packageYml?){
}else if (metadataname === TypeInfoKeys.Import) {
imports = loadImportFile.load(packagePath);
mark = true;
}else if (metadataname === TypeInfoKeys.Question) {
questions = loadQuestionFile.load(packagePath);
mark = true;
}else if (metadataname === TypeInfoKeys.Dashboard) {
dashboards = loadDashboardFile.load(packagePath);
mark = true;
}

}
Expand Down Expand Up @@ -836,6 +848,8 @@ export async function loadFileToJson(packagePath:string, packageYml?){
steedosPackage[TypeInfoKeys.Process] = processes;
steedosPackage[TypeInfoKeys.Trigger] = triggers;
steedosPackage[TypeInfoKeys.Import] = imports;
steedosPackage[TypeInfoKeys.Question] = questions;
steedosPackage[TypeInfoKeys.Dashboard] = dashboards;

//用于测试查看本地生成的steedosPackage结构和属性是否完整
// let targetFolderName = './data';
Expand Down
69 changes: 39 additions & 30 deletions packages/metadata-api/src/metadata/deploy/jsonToDb.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as _ from 'underscore'

import {objectsToDb} from '../collection/object'
import {fieldsToDb} from '../collection/field'
import {actionsToDb} from '../collection/action'
import {listviewsToDb} from '../collection/listview'
import {validationsToDb} from '../collection/objectValidation'
import {objectPermissionsToDb} from '../collection/objectPermission'
import {permissionSetsToDb} from '../collection/permissionset'
import {applicationsToDb} from '../collection/application'
import {reportsToDb} from '../collection/report'
import { objectsToDb } from '../collection/object'
import { fieldsToDb } from '../collection/field'
import { actionsToDb } from '../collection/action'
import { listviewsToDb } from '../collection/listview'
import { validationsToDb } from '../collection/objectValidation'
import { objectPermissionsToDb } from '../collection/objectPermission'
import { permissionSetsToDb } from '../collection/permissionset'
import { applicationsToDb } from '../collection/application'
import { reportsToDb } from '../collection/report'
import { workflowsToDb } from '../collection/workflow'
import { flowsToDb } from '../collection/flow'
import { approvalProcessesToDb } from '../collection/approvalProcess'
Expand All @@ -25,7 +25,8 @@ import { RestrictionRuleCollection } from '../collection/restrictionRule'
import { ProcessCollection } from '../collection/process'
import { TriggerCollection } from '../collection/trigger'
import { ImportCollection } from '../collection/import'

import { QuestionCollection } from '../collection/question'
import { DashboardCollection } from '../collection/dashboard'

const queryCollection = new QueryCollection();
const chartCollection = new ChartCollection();
Expand All @@ -36,12 +37,14 @@ const restrictionRuleCollection = new RestrictionRuleCollection();
const processCollection = new ProcessCollection();
const triggerCollection = new TriggerCollection();
const importCollection = new ImportCollection();
const questionCollection = new QuestionCollection();
const dashboardCollection = new DashboardCollection();

import { SteedosMetadataTypeInfoKeys as TypeInfoKeys, getMetadataTypeInfo, hasChild, getChilds } from '@steedos/metadata-core';

async function metatdataRecordsToDb(dbManager, metadataName, metatdataRecords, parentName?, assistRecords?){
async function metatdataRecordsToDb(dbManager, metadataName, metatdataRecords, parentName?, assistRecords?) {

switch(metadataName){
switch (metadataName) {
case TypeInfoKeys.Object:
await objectsToDb(dbManager, metatdataRecords);
break;
Expand Down Expand Up @@ -74,7 +77,7 @@ async function metatdataRecordsToDb(dbManager, metadataName, metatdataRecords, p
await permissionSetsToDb(dbManager, metatdataRecords, true);
break;

case TypeInfoKeys.Process:
case TypeInfoKeys.Process:
await processCollection.deploy(dbManager, metatdataRecords);
break;

Expand All @@ -93,11 +96,11 @@ async function metatdataRecordsToDb(dbManager, metadataName, metatdataRecords, p
case TypeInfoKeys.Flow:
await flowsToDb(dbManager, metatdataRecords);
break;

case TypeInfoKeys.ApprovalProcess:
await approvalProcessesToDb(dbManager, metatdataRecords);
break;

case TypeInfoKeys.Role:
await rolesToDb(dbManager, metatdataRecords);
break;
Expand All @@ -113,7 +116,7 @@ async function metatdataRecordsToDb(dbManager, metadataName, metatdataRecords, p
case TypeInfoKeys.Chart:
await chartCollection.deploy(dbManager, metatdataRecords);
break;

case TypeInfoKeys.Query:
await queryCollection.deploy(dbManager, metatdataRecords);
break;
Expand All @@ -136,13 +139,19 @@ async function metatdataRecordsToDb(dbManager, metadataName, metatdataRecords, p
case TypeInfoKeys.Import:
await importCollection.deploy(dbManager, metatdataRecords);
break;
case TypeInfoKeys.Question:
await questionCollection.deploy(dbManager, metatdataRecords);
break;
case TypeInfoKeys.Dashboard:
await dashboardCollection.deploy(dbManager, metatdataRecords);
break;
default:
break;
}
}
export async function jsonToDb(steedosPackage, dbManager, session){
export async function jsonToDb(steedosPackage, dbManager, session) {

const transactionOptions:any = {
const transactionOptions: any = {
readPreference: 'primary',
readConcern: { level: 'majority' },
writeConcern: { w: 'majority' }
Expand All @@ -152,25 +161,25 @@ export async function jsonToDb(steedosPackage, dbManager, session){
await session.withTransaction(async () => {

const topKeys = [TypeInfoKeys.Profile, TypeInfoKeys.Permissionset, TypeInfoKeys.Role, TypeInfoKeys.FlowRole, TypeInfoKeys.Object]
var keys = _.sortBy(_.keys(steedosPackage), function(key){
return _.include(topKeys, key) ? -1: 1
var keys = _.sortBy(_.keys(steedosPackage), function (key) {
return _.include(topKeys, key) ? -1 : 1
})
for(const metadataName of keys){

for (const metadataName of keys) {
var metatdataRecords = steedosPackage[metadataName];
if(hasChild(metadataName)){
if (hasChild(metadataName)) {

const childs = getChilds(metadataName)
for(const metatdataRecordName in metatdataRecords){ // accounts,agreement

for (const metatdataRecordName in metatdataRecords) { // accounts,agreement
var metatdataRecord = metatdataRecords[metatdataRecordName];
for(const childMetadataName of childs){
for (const childMetadataName of childs) {
var childMetadataRecords = metatdataRecord[childMetadataName];

if(childMetadataName == TypeInfoKeys.Permission){
if (childMetadataName == TypeInfoKeys.Permission) {
var assistRecords = steedosPackage[TypeInfoKeys.Permissionset]
await metatdataRecordsToDb(dbManager, childMetadataName, childMetadataRecords, metatdataRecordName, assistRecords);
}else{
} else {

await metatdataRecordsToDb(dbManager, childMetadataName, childMetadataRecords, metatdataRecordName);
}
Expand All @@ -180,10 +189,10 @@ export async function jsonToDb(steedosPackage, dbManager, session){
}
await metatdataRecordsToDb(dbManager, metadataName, metatdataRecords)
}


}, transactionOptions);
} catch(err) {
} catch (err) {
console.log(err)
throw err
}
Expand Down
Loading

0 comments on commit f90fd13

Please sign in to comment.