diff --git a/app/assets/javascripts/admin/models/report.js.es6 b/app/assets/javascripts/admin/models/report.js.es6 index 03e3888f97b..af078528b75 100644 --- a/app/assets/javascripts/admin/models/report.js.es6 +++ b/app/assets/javascripts/admin/models/report.js.es6 @@ -1,6 +1,7 @@ import { ajax } from 'discourse/lib/ajax'; import round from "discourse/lib/round"; import { fmt } from 'discourse/lib/computed'; +import { fillMissingDates } from 'discourse/lib/utilities'; const Report = Discourse.Model.extend({ reportUrl: fmt("type", "/admin/reports/%@"), @@ -142,14 +143,13 @@ Report.reopenClass({ group_id: groupId } }).then(json => { - // Add a percent field to each tuple - let maxY = 0; - json.report.data.forEach(row => { - if (row.y > maxY) maxY = row.y; - }); - if (maxY > 0) { - json.report.data.forEach(row => row.percentage = Math.round((row.y / maxY) * 100)); + // Add zero values for missing dates + if (json.report.data.length > 0) { + const startDateFormatted = moment(json.report.start_date).format('YYYY-MM-DD'); + const endDateFormatted = moment(json.report.end_date).format('YYYY-MM-DD'); + json.report.data = fillMissingDates(json.report.data, startDateFormatted, endDateFormatted); } + const model = Report.create({ type: type }); model.setProperties(json.report); return model; diff --git a/app/assets/javascripts/admin/routes/admin-search-logs-term.js.es6 b/app/assets/javascripts/admin/routes/admin-search-logs-term.js.es6 index 775fec62c5e..65c5c92eeb8 100644 --- a/app/assets/javascripts/admin/routes/admin-search-logs-term.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-search-logs-term.js.es6 @@ -1,4 +1,5 @@ import { ajax } from 'discourse/lib/ajax'; +import { fillMissingDates } from 'discourse/lib/utilities'; export default Discourse.Route.extend({ queryParams: { @@ -15,6 +16,13 @@ export default Discourse.Route.extend({ search_type: params.searchType } }).then(json => { + // Add zero values for missing dates + if (json.term.data.length > 0) { + const startDate = (json.term.period === "all") ? moment(json.term.data[0].x).format('YYYY-MM-DD') : moment(json.term.start_date).format('YYYY-MM-DD'); + const endDate = moment(json.term.end_date).format('YYYY-MM-DD'); + json.term.data = fillMissingDates(json.term.data, startDate, endDate); + } + const model = Ember.Object.create({ type: "search_log_term" }); model.setProperties(json.term); return model; diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index 33558befacb..ceeb8c81835 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -450,5 +450,25 @@ export function clipboardData(e, canUpload) { return { clipboard, types, canUpload, canPasteHtml }; } +export function fillMissingDates(data, startDate, endDate) { + const startMoment = moment(startDate, "YYYY-MM-DD"); + const endMoment = moment(endDate, "YYYY-MM-DD"); + const countDays = endMoment.diff(startMoment, 'days'); + let currentMoment = startMoment; + + for (let i = 0; i <= countDays; i++) { + let date = (data[i]) ? moment(data[i].x, "YYYY-MM-DD") : null; + if (i === 0 && date.isAfter(startMoment)) { + data.splice(i, 0, { "x" : startMoment.format("YYYY-MM-DD"), 'y': 0 }); + } else { + if (!date || date.isAfter(moment(currentMoment))) { + data.splice(i, 0, { "x" : currentMoment, 'y': 0 }); + } + } + currentMoment = moment(currentMoment).add(1, "day").format("YYYY-MM-DD"); + } + return data; +} + // This prevents a mini racer crash export default {}; diff --git a/app/models/search_log.rb b/app/models/search_log.rb index d91b1ae7a1a..729dedcffbc 100644 --- a/app/models/search_log.rb +++ b/app/models/search_log.rb @@ -77,7 +77,10 @@ class SearchLog < ActiveRecord::Base return { type: "search_log_term", title: I18n.t("search_logs.graph_title"), - data: details + start_date: start_of(period), + end_date: Time.zone.now, + data: details, + period: period.to_s } end diff --git a/spec/models/search_log_spec.rb b/spec/models/search_log_spec.rb index 478fc8d0334..0e244eabb16 100644 --- a/spec/models/search_log_spec.rb +++ b/spec/models/search_log_spec.rb @@ -172,6 +172,7 @@ RSpec.describe SearchLog, type: :model do SearchLog.where(term: 'ruby', ip_address: '127.0.0.2').update_all(search_result_id: 24) term_click_through_details = SearchLog.term_details("ruby", :all, :click_through_only) + expect(term_click_through_details[:period]).to eq("all") expect(term_click_through_details[:data][0][:y]).to eq(1) end end diff --git a/test/javascripts/lib/utilities-test.js.es6 b/test/javascripts/lib/utilities-test.js.es6 index 00e00f4c198..4a11b4bca9a 100644 --- a/test/javascripts/lib/utilities-test.js.es6 +++ b/test/javascripts/lib/utilities-test.js.es6 @@ -14,7 +14,8 @@ import { validateUploadedFiles, getUploadMarkdown, caretRowCol, - setCaretPosition + setCaretPosition, + fillMissingDates } from 'discourse/lib/utilities'; import * as Utilities from 'discourse/lib/utilities'; @@ -253,3 +254,12 @@ QUnit.test("caretRowCol", assert => { document.body.removeChild(textarea); }); + +QUnit.test("fillMissingDates", assert => { + const startDate = "2017-11-12"; // YYYY-MM-DD + const endDate = "2017-12-12"; // YYYY-MM-DD + const data = '[{"x":"2017-11-12","y":3},{"x":"2017-11-27","y":2},{"x":"2017-12-06","y":9},{"x":"2017-12-11","y":2}]'; + + assert.equal(fillMissingDates(JSON.parse(data), startDate, endDate).length, 31, + "it returns a JSON array with 31 dates"); +});