// Copyright 2015-2018 Kuzzle
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collection_test
import (
"fmt"
"github.com/kuzzleio/sdk-go/collection"
"testing"
"github.com/kuzzleio/sdk-go/internal"
"github.com/kuzzleio/sdk-go/kuzzle"
"github.com/kuzzleio/sdk-go/types"
"github.com/stretchr/testify/assert"
)
func TestTruncateIndexNull(t *testing.T) {
k, _ := kuzzle.NewKuzzle(&internal.MockedConnection{}, nil)
nc := collection.NewCollection(k)
err := nc.Truncate("", "collection", nil)
assert.NotNil(t, err)
}
func TestTruncateCollectionNull(t *testing.T) {
k, _ := kuzzle.NewKuzzle(&internal.MockedConnection{}, nil)
nc := collection.NewCollection(k)
err := nc.Truncate("index", "", nil)
assert.NotNil(t, err)
}
func TestTruncateError(t *testing.T) {
c := &internal.MockedConnection{
MockSend: func(query []byte, options types.QueryOptions) *types.KuzzleResponse {
return &types.KuzzleResponse{Error: types.KuzzleError{Message: "Unit test error"}}
},
}
k, _ := kuzzle.NewKuzzle(c, nil)
nc := collection.NewCollection(k)
err := nc.Truncate("index", "collection", nil)
assert.NotNil(t, err)
}
func TestTruncate(t *testing.T) {
c := &internal.MockedConnection{
MockSend: func(query []byte, options types.QueryOptions) *types.KuzzleResponse {
return &types.KuzzleResponse{Result: []byte(`{
"acknowledged": true
}`)}
},
}
k, _ := kuzzle.NewKuzzle(c, nil)
nc := collection.NewCollection(k)
err := nc.Truncate("index", "collection", nil)
assert.Nil(t, err)
}
func ExampleCollection_Truncate() {
c := &internal.MockedConnection{}
k, _ := kuzzle.NewKuzzle(c, nil)
nc := collection.NewCollection(k)
err := nc.Truncate("index", "collection", nil)
if err != nil {
fmt.Println(err.Error())
return
}
}
package services
import "stock/utils"
var StatPathMap = map[string]string{
"other":"å
¶å®",
"/pub/coin_price/:coin/:vs_coin/:timestamp": "è·åå¸ä»·æ¢ç®ï¼å¤èç¹ç¾åç",
"/pub/dex/ftx_chart_prices/:coin_type/:count/:interval/:timestamp": "è·åæ æbtc代å¸ä¸åæ¶é´åºé´çä»·æ ¼å¾è¡¨ä¿¡æ¯",
"/pub/dex/ftx_price/:coin_type/:data_type/:timestamp": "è·åftx tokenä»·æ ¼ä¿¡æ¯",
"/pub/dex/lp_price/:pair/:timestamp": "è·ålpä»·æ ¼ä¿¡æ¯",
"/pub/dex/stock_chart_prices/:coin_type/:count/:interval/:timestamp": "è·åè¡ç¥¨ä¸åæ¶é´åºé´çä»·æ ¼å¾è¡¨ä¿¡æ¯",
"/pub/dex/token_chain_price/:token/:data_type/:timestamp": "è·åtokené¾ä¸ä»·æ ¼ä¿¡æ¯",
"/pub/dex/token_chart_prices/:token/:count/:interval/:timestamp": "è·åtokenä¸åæ¶é´åºé´çä»·æ ¼å¾è¡¨ä¿¡æ¯",
"/pub/dex/token_day_datas/:token/:days/:timestamp": "è·åtokenç¸åºå¤©æ°çç»è®¡å¾è¡¨ä¿¡æ¯",
"/pub/dex/token_price/:token/:data_type/:timestamp": "è·åtokenä»·æ ¼ä¿¡æ¯",
"/pub/internal/coin_price/:coin/:vs_coin": "è·åå¸ä»·æ¢ç®ï¼å
é¨åèç¹",
"/pub/internal/dex/ftx_price/:coin_type/:timestamp": "è·åftx coinæè¿ä¸å°æ¶æé«æä½ä»·æ ¼ä¿¡æ¯,å
é¨åèç¹",
"/pub/internal/dex/lp_price/:pair/:timestamp": "è·ålpä»·æ ¼ä¿¡æ¯,å
é¨åèç¹:",
"/pub/internal/dex/token_chain_price/:token/:timestamp": "è·åtokenæè¿ä¸å°æ¶æé«æä½ä»·æ ¼ä¿¡æ¯,å
é¨åèç¹æ¨¡å¼",
"/pub/internal/dex/token_info/:token/:timestamp": "è·åtokenä¿¡æ¯,å
é¨åèç¹",
"/pub/internal/dex/token_price/:token/:timestamp": "è·åtokenæè¿ä¸å°æ¶æé«æä½ä»·æ ¼ä¿¡æ¯,å
é¨åèç¹",
"/pub/internal/stock_avgprice": "è·åè¡ç¥¨å¹³åä»·æ ¼å
±è¯:",
"/pub/internal/token_avgprice": "è·åtokenå¹³åä»·æ ¼å
±è¯:",
"/pub/stock/aggre_info/:code/:data_type/:timestamp": "è·åå
±è¯ç¾è¡ä»·æ ¼:",
"/pub/stock/any_api": "å½åèç¹any-api",
"/pub/stock/any_apis": "ææèç¹any-api",
"/pub/stock/info/:code/:data_type/:timestamp": "è·åç¾è¡ä»·æ ¼",
"/pub/stock/market_status/:timestamp": "è·åç¾è¡å¸åºå¼çç¶æ",
"/pub/stock/stat": "å½åèç¹ç¶æ:è®°å½æ°,é±å
å°å",
"/pub/stock/stats": "ææèç¹ç¶æ:è®°å½æ°,é±å
å°å",
"/pub/dex/pair/token_chart_prices/:pair/:token/:count/:interval/:timestamp": "ä»Pairè·åtokenä¸åæ¶é´åºé´çä»·æ ¼å¾è¡¨ä¿¡æ¯",
"/pub/dex/pair/token_price/:pair/:token/:data_type/:timestamp": "ä»Pairè·åtokenä»·æ ¼ä¿¡æ¯",
"/pub/internal/dex/pair/token_info/:pair/:token/:timestamp": "ä»pairè·åtokenä¿¡æ¯,å
é¨åèç¹",
"/pub/internal/dex/pair/token_price/:pair/:token/:timestamp": "ä»Pairè·åtokenæè¿ä¸å°æ¶æé«æä½ä»·æ ¼ä¿¡æ¯,å
é¨åèç¹",
"/pub/dex/token/token_chart_supply/:token/:amount/:timestamp": "è·åæä¸ªtokençtotalSupplyçååé",
}
var StatPath2IDMap = map[string]int{
"/pub/coin_price/:coin/:vs_coin/:timestamp": 1,
"/pub/dex/ftx_chart_prices/:coin_type/:count/:interval/:timestamp": 2,
"/pub/dex/ftx_price/:coin_type/:data_type/:timestamp": 3,
"/pub/dex/lp_price/:pair/:timestamp": 4,
"/pub/dex/stock_chart_prices/:coin_type/:count/:interval/:timestamp": 5,
"/pub/dex/token_chain_price/:token/:data_type/:timestamp": 6,
"/pub/dex/token_chart_prices/:token/:count/:interval/:timestamp": 7,
"/pub/dex/token_day_datas/:token/:days/:timestamp": 8,
"/pub/dex/token_price/:token/:data_type/:timestamp": 9,
"/pub/internal/coin_price/:coin/:vs_coin": 10,
"/pub/internal/dex/ftx_price/:coin_type/:timestamp": 11,
"/pub/internal/dex/lp_price/:pair/:timestamp": 12,
"/pub/internal/dex/token_chain_price/:token/:timestamp": 13,
"/pub/internal/dex/token_info/:token/:timestamp": 14,
"/pub/internal/dex/token_price/:token/:timestamp": 15,
"/pub/internal/stock_avgprice": 16,
"/pub/internal/token_avgprice": 17,
"/pub/stock/aggre_info/:code/:data_type/:timestamp": 18,
"/pub/stock/any_api": 19,
"/pub/stock/any_apis": 20,
"/pub/stock/info/:code/:data_type/:timestamp": 21,
"/pub/stock/market_status/:timestamp": 22,
"/pub/stock/stat": 23,
"/pub/stock/stats": 24,
"/pub/dex/pair/token_chart_prices/:pair/:token/:count/:interval/:timestamp": 25,
"/pub/dex/pair/token_price/:pair/:token/:data_type/:timestamp": 26,
"/pub/internal/dex/pair/token_info/:pair/:token/:timestamp": 27,
"/pub/internal/dex/pair/token_price/:pair/:token/:timestamp": 28,
"/pub/dex/token/token_chart_supply/:token/:amount/:timestamp": 29,
}
var StatID2PathMap = map[int]string{ }
var StatID2ResMap = map[int]string{ }
func init(){
StatPath2IDMap["other"]=100
for key, value := range StatPath2IDMap {
StatID2PathMap[value]=key
StatID2ResMap[value]=StatPathMap[key]
}
}
func ApiStat()(map[string]interface{},error) {
res := map[string]interface{}{}
allStat := map[string]interface{}{}
err := utils.Orm.Raw(
`select substr(from_unixtime(min(timestamp)),1,10) begin_date,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0`).First(&allStat).Error
res["allStat"] = allStat
type apiRankStat struct{
PathId int
Counter int
PathName string
}
apiRankStats := []*apiRankStat{}
err = utils.Orm.Raw(
`select stats.path_id,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal=0 and stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
group by stats.path_id order by counter desc;`).
Find(&apiRankStats).Error
if err == nil {
for _, item := range apiRankStats {
item.PathName=StatID2ResMap[item.PathId]
}
res["apiRankStats"] = apiRankStats
}
day30Stat := []map[string]interface{}{}
err = utils.Orm.Raw(
`select substr(from_unixtime(aa.timespan),1,10) daystr,
sum((case when is_internal = 1 then 0 else aa.counter end)) as oss,
sum((case when is_internal = 1 then aa.counter else 0 end)) as internal
from (
select stats.is_internal,
(truncate(timestamp / (3600 * 24), 0) * 3600 * 24) as timespan,
sum(stats.counter) counter
from api_stats stats
# where stats.is_internal = 0
where stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
group by stats.is_internal, timespan
) aa
group by daystr`).
Find(&day30Stat).Error
if err == nil {
res["day30Stat"] = day30Stat
}
day30HourRankStat := []map[string]interface{}{}
err = utils.Orm.Raw(
`select concat( hour(from_unixtime(truncate(timestamp / (3600), 0) * 3600)),'ç¹') as hour_name,
# stats.is_internal,
# hour(date_add(
# FROM_UNIXTIME(0), interval
# (truncate(timestamp / (3600), 0) * 3600) +
# TIMESTAMPDIFF(SECOND, NOW(), UTC_TIMESTAMP()) SECOND
# )) timespan,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0
and stats.timestamp > truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24 - (3600 * 24) * 30
group by hour_name
order by counter desc`).
Find(&day30HourRankStat).Error
if err == nil {
res["day30HourRankStat"] = day30HourRankStat
}
last48HourStat:= []map[string]interface{}{}
err = utils.Orm.Raw(
`select from_unixtime(truncate(timestamp / (3600), 0) * 3600) as datestr,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0
and stats.timestamp > truncate(unix_timestamp() / (3600), 0) * 3600 - 3600 * 48
group by datestr;`).
Find(&last48HourStat).Error
if err == nil {
res["last48HourStat"] = last48HourStat
}
last48Hour10MinStat:= []map[string]interface{}{}
err = utils.Orm.Raw(
`select from_unixtime(truncate(timestamp / (600), 0) * 600) as datestr,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0
and stats.timestamp > truncate(unix_timestamp() / (600), 0) * 600 - 3600 * 48
group by datestr`).
Find(&last48Hour10MinStat).Error
if err == nil {
res["last48Hour10MinStat"] = last48Hour10MinStat
}
return res, err
}
/*
-- æ»è®¿é®æ¬¡æ°
select substr(from_unixtime(min(timestamp)),1,10) begin_date,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0;
-- æè¿30å¤©è®¿é®æå¤çapi
select stats.path_id,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal=0 and stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
group by stats.path_id order by counter desc;
-- æè¿30天æ¯å¤©è®¿é®æ¬¡æ°ç»è®¡
select substr(from_unixtime(aa.timespan),1,10) daystr,
sum((case when is_internal = 1 then 0 else aa.counter end)) as oss,
sum((case when is_internal = 1 then aa.counter else 0 end)) as internal
from (
select stats.is_internal,
(truncate(timestamp / (3600 * 24), 0) * 3600 * 24) as timespan,
sum(stats.counter) counter
from api_stats stats
# where stats.is_internal = 0
where stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
group by stats.is_internal, timespan
) aa
group by daystr;
-- æè¿30天尿¶æ¬¡æ°æå.
select concat( hour(from_unixtime(truncate(timestamp / (3600), 0) * 3600)),'ç¹') as hour_name,
# stats.is_internal,
# hour(date_add(
# FROM_UNIXTIME(0), interval
# (truncate(timestamp / (3600), 0) * 3600) +
# TIMESTAMPDIFF(SECOND, NOW(), UTC_TIMESTAMP()) SECOND
# )) timespan,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0
and stats.timestamp > truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24 - (3600 * 24) * 30
group by hour_name
order by counter desc
-- æè¿48å°æ¶æ¯å°æ¶è®¿é®é
select from_unixtime(truncate(timestamp / (3600), 0) * 3600) as datestr,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0
and stats.timestamp > truncate(unix_timestamp() / (3600), 0) * 3600 - 3600 * 48
group by datestr;
# order by counter desc;
-- æè¿48å°æ¶æ¯10åé访é®é
select from_unixtime(truncate(timestamp / (600), 0) * 600) as datestr,
sum(stats.counter) counter
from api_stats stats
where stats.is_internal = 0
and stats.timestamp > truncate(unix_timestamp() / (600), 0) * 600 - 3600 * 48
group by datestr
*/