Right now I have a small application for calculating statistics with a parallel algorithms. Now I've got a problem with expanding some part of functionality. I'll try to explain shortly. App is build on revel framework. One of the actions of "stat" controller takes incoming POST json. Parses it. And generates two channels (goroutines) for tasks and results. All that works like a charm. But I have troubles with models. I wrote code to be able to expand models amount linearly, but for this moment there was only one in work.
And not all of methods are adopted for this expanding.
In some part of code I have this :
for t := range in {
for sourceName, charts := range t.Request.Charts {
var cacheData []byte
var deserializedData models.StatModel
//determine the model type
switch sourceName {
case "noagg":
deserializedData = new(models.NoaggModel)
case "acsi":
deserializedData = new(models.AcsiModel)
}
cache_err := cache.Get(string(string(sourceName) + "_" + string(t.Date)), &cacheData);
if cache_err != nil {
panic("the cache is empty")
}
marshal_error := json.Unmarshal([]byte(cacheData), &deserializedData)
if marshal_error == nil {
}
deserializedData.FilterData(t.Request.Filters)
deserializedData.ClusterData(t.Request.Filters)
w := Work{}
for _, chart := range charts {
countedData := ChartElements{}
if marshal_error == nil {
countedData = deserializedData.CountDataForChart(string(chart.Name))
}else {
panic("some is bad")
}
w.Name, w.Data = chart.Name, countedData
out <- w
}
}
}
Noagg model and Asci model are implementing the same interface of the "stat" model :
type StatModel interface {
FilterData(Filter)
ClusterData(Filter)
CountDataForChart(string)[]ChartElement
GroupByTreeGroups(Filter)[]OrgPack
}
But now I have to add some new models with the same interface, but there is code, that I can't expand . I can't remember how to do this..
func statCount(model NoaggRow, f func(NoaggRow) float64) float64 {
countedStat := f(model)
return countedStat
}
func Count(model NoaggRow, name string) float64{
m := map[string]func(NoaggRow) float64 {
"HOLD" : HOLD,
"INB" : INB,
"AHT" : AHT,
"RING" : RING,
"TALK" : TALK,
"ACW" : ACW,
"OCC" : OCC,
}
countedStat := statCount(model, m[name])
return countedStat
}
The same methods I need for ASCI model. For AcsiRow instead of NoaggRow. How to make this input params types dynamic or how to make methods common for all models. Only array and names of "map[string]func(......Row)" would differ in this place. Can anyone help me out with this ?