gmfe / Think

观麦前端团队的官方博客
68 stars 3 forks source link

数据平台的搭建 #20

Open liyatang opened 6 years ago

liyatang commented 6 years ago

背景:考察过这么多平台,需要花费非常多的心思去了解,且基于 node 的相对少。有东西要定制,就尴尬了。所以还是决定自己搭建,顺便全栈下。

环节

关键就几个环节

后面还有很多,定时任务,通知等等。

数据收集

跨域

看 http header。koa-cors。细节待了解

上报频率

及时上报?攒N次在上报? 及时上报。

全量上报?灰度上报?全量。btw sentry 10次才上报。

上报方式

post ? get ?

(new Image()).src = 'xxxx';

有可能变量回收导致上报丢失

server

查询语言

传统?REST?graphql?graphql。

http://graphql.cn http://graphql.cn/graphql-js/

mongo

http://mongoosejs.com/docs/guide.html

客户端工具 https://robomongo.org/

// 连接。连接池,没搞定,mongodb连接经常会断掉,说太久没用。
mongoose.connect("mongodb://dev.guanmai.cn:27017/gm_trace", {
    useMongoClient: true,
    autoReconnect: true,
    poolSize: 10
});

// Promise 让开发者实现
mongoose.Promise = global.Promise;

代码

router.all('/graphql/request/ma', request('ma'));
import {buildSchema} from 'graphql';
import mongoose from 'mongoose';
import graphqlHTTP from 'koa-graphql';
import moment from 'moment';
import {process} from './util';

function create(platform) {
    const schema = buildSchema(`
    type Request {
        url: String!,
        code: Int,
        isSuccess: Boolean,
        time: Int,
        msg: String,
        client: String,
        requestId: String,
        extension: String,
        createTime: String
    }

    type Query {
        request: [Request],    
        createRequest(url: String!, code: Int, isSuccess: Boolean, time: Int, msg: String, client: String, requestId: String, extension: String): Request
    }

`);

    const Request = mongoose.model(platform + 'Request', {
        url: String,
        code: Number,
        isSuccess: Boolean,
        time: Number,
        msg: String,
        client: String,
        requestId: String,
        extension: String,
        createTime: {type: Date, default: Date.now}
    });

    const rootValue = {
        // request
        request() {
            return Request.find({
                createTime: {
                    '$gte': moment().startOf('day').toDate(),
                    '$lte': moment().endOf('day').toDate()
                }
            });
        },
        // create
        createRequest(request) {
            const user = new Request(request);
            return user.save().then(() => user);
        }
    };

    return graphqlHTTP({
        schema,
        rootValue,
        graphiql: true
    });
}

export default create;

查看

有两个表 Request RequestReport