We record software builds from our build machine into a database and for practice purposes I'm building a little web dashboard for it.
The API is REST (WebApi) and provides access to query for Products, Branches, Profiles and specific Builds.
The controller queries all products (each product contains a list of branches and profiles it has been build for) as well as all branches and profiles anything was ever built for.
It then goes on to query the last 10 builds for each of the branches and profiles a product was built for.
This is my first attempt at anything related to web-programming so I thought I'll get someone to look at it. Here is the code:
(function (angular) {
var app = angular.module("app", ["ngResource"]);
app.factory("Product", ["$resource", function ($resource) {
return $resource("/api/Product");
}]);
app.factory("Build", ["$resource", function ($resource) {
return $resource("/api/Build");
}]);
app.factory("BasicResource", ["$q", "$http", function ($q, $http) {
var resourceFactory = {
async: function(type) {
var d = $q.defer();
var result = $http.get("/api/" + type).success(function() {
d.resolve(result);
});
return d.promise;
}
};
return resourceFactory;
}]);
app.controller("MainCtrl", ["$rootScope", "$scope", "$q", "$http", "BasicResource", "Product", "Build", function ($rootScope, $scope, $q, $http, BasicResource, Product, Build) {
$rootScope.title = "Build Dashboard";
$scope.Builds = [];
$q.all([
Product.query().$promise,
BasicResource.async("Branch"),
BasicResource.async("Profile")
]).then(function(data) {
$scope.Products = data[0];
$scope.Branches = data[1].data;
$scope.Profiles = data[2].data;
}).then(function() {
var allBuildQueries = [];
$scope.Products.forEach(function (prod) {
prod.branches.forEach(function(b) {
prod.profiles.forEach(function(prof) {
allBuildQueries.push(Build.query({ product: prod.name, branch: b, profile: prof, limit: 10 }).$promise);
});
});
});
$q.all(allBuildQueries).then(function (allBuilds) {
allBuilds.forEach(function(builds) {
builds.forEach(function(build) {
$scope.Builds.push(build);
});
});
});
});
}]);
})(angular);
There are a few things which I don't really like about it so far but don't know how to improve it:
The
/api/Branch
and/api/Profile
resources return a list of strings. This causes the issue described in the SO question. The two basic workarounds are to use$http
or transform the response. I have used$http
because it seemed the easiest and I have not figured out how to usetransformResponse
properly so far (documentation on that is somewhat light). However it results in the factories (Branch
andProfile
vsProduct
andBuild
) having slightly different interfaces which bugs me.The second issue is the async query of the builds. Currently it queries
Number of Branches
xNumber of Profiles
per product for all products. At the moment this results in about 90 queries and really is no problem. However I'm wondering how this should be done properly in case the number of queries can grow into the hundreds or thousands.The way how I combine all the build query results seems somewhat inelegant to me. In C# I'd use a LINQ query likeallBuilds.SelectMany(x => x.builds).ToList()
. Would strike me as weird if there wouldn't be a more elegant javascript way of doing it.
Since asking the question I have started using linqjs so that solves this issue.